hosts.nix 1.79 KB
Newer Older
Matthias Adamczyk's avatar
Matthias Adamczyk committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
{ pkgs ? import ../pkgs { /* TODO system */} }:

with pkgs.lib;

rec {
  hostsDir = ../hosts;

  hostNames = attrNames (
    filterAttrs (
      name: type: type == "directory"
    ) (
      builtins.readDir hostsDir
    )
  );

  # HACK: We want to choose a nixpkgs version depending on the host architecture, but
  # the host architecture is set in the NixOS config. We accept the limitation that
  # nixpkgs.system must be set in the main configuration.nix file to prevent reading
  # the host configuration twice.
  hostArch = hostName:
    (
      (
        import (hostsDir + "/${hostName}/configuration.nix") {
          inherit (pkgs) pkgs lib;
          config = {};
          modulesPath = "";
        }
      ).nixpkgs or {}
    ).system or "x86_64-linux"
  ;

  nixpkgsFor = hostName: (import ../pkgs {
    system = hostArch hostName;
  }).path;

  hostConfig = hostName: { config, ... }: {
    _module.args = {
      inherit hosts groups;
    };
    imports = [
      (import (hostsDir + "/${hostName}/configuration.nix"))
      ../modules
    ];
    networking = {
      inherit hostName;
    };
    nixpkgs.pkgs = import ../pkgs {
      inherit (config.nixpkgs) config system;
    };
  };

  hosts = listToAttrs (
    map (
      hostName: nameValuePair hostName (
        import ((nixpkgsFor hostName) + "/nixos") {
          configuration = hostConfig hostName;
          system = hostArch hostName;
        }
      )
    ) hostNames
  );

  groupNames = unique (
    concatLists (
      mapAttrsToList (
Matthias Adamczyk's avatar
Fix CI    
Matthias Adamczyk committed
66
        name: host: host.config.finf.deploy.groups
Matthias Adamczyk's avatar
Matthias Adamczyk committed
67
68
69
70
71
72
73
74
      ) hosts
    )
  );

  groups = listToAttrs (
    map (
      groupName: nameValuePair groupName (
        filter (
Matthias Adamczyk's avatar
Fix CI    
Matthias Adamczyk committed
75
          host: elem groupName host.config.finf.deploy.groups
Matthias Adamczyk's avatar
Matthias Adamczyk committed
76
77
78
79
80
81
82
        ) (
          attrValues hosts
        )
      )
    ) groupNames
  );
}