summaryrefslogtreecommitdiff
path: root/lib/hosts.nix
blob: 91b6b40e4c640e4773ffdebb525f0c2401b49cd0 (plain)
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
{
  pkgs,
  hostsDir ? ../hosts,
  commonImports ? [],
  pkgsPath ? ../pkgs,
  modules ? {},
  overlays ? {},
  profiles ? {},
  users ? {},
  sources ? {}
}:

with pkgs.lib;

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

  hostConfig = hostName: { config, ... }: {
    _module.args = {
      inherit hosts groups;
    };
    imports = [
      (import (hostsDir + "/${hostName}/configuration.nix"))
      # urgh, yes, we still need to manually import the deploy module for now
      # at least if i want to keep my thing reusable.
      ../modules/deploy
    ] ++ commonImports;
    networking = {
      inherit hostName;
    };
    nixpkgs.pkgs =
      let pkgs = import pkgsPath {
        inherit (config.nixpkgs) config system;
      };
      in if pkgs ? nixpkgs then pkgs.nixpkgs else pkgs;
  };

  hosts = listToAttrs (
    map (
      hostName: nameValuePair hostName (
        import (pkgs.path + "/nixos/lib/eval-config.nix") {
          modules = [
            (hostConfig hostName)
            (if sources ? home-manager then sources.home-manager + "/nixos" else {})
          ];
          specialArgs = { inherit modules overlays profiles users sources; };
        }
      )
    ) hostNames
  );

  groupNames = unique (
    concatLists (
      mapAttrsToList (
        name: host: host.config.hexchen.deploy.groups
      ) hosts
    )
  );

  groups = listToAttrs (
    map (
      groupName: nameValuePair groupName (
        filter (
          host: elem groupName host.config.hexchen.deploy.groups
        ) (
          attrValues (
            filterAttrs (_: h: h.config.hexchen.deploy.enable) hosts
          )
        )
      )
    ) groupNames
  );
}