nixos-config/lib/hosts.nix

36 lines
977 B
Nix
Raw Normal View History

{ path, nixosModule }:
2021-06-05 06:22:38 +00:00
let
2021-10-28 19:01:50 +00:00
inherit (builtins) mapAttrs attrValues attrNames readDir foldl' listToAttrs;
2021-06-05 06:22:38 +00:00
2021-10-28 19:01:50 +00:00
# grab a list of systems, with associated hostnames
# { x86_64-linux = [ "host1" "host2" ]; }
sysToHosts = mapAttrs
2021-10-28 20:24:50 +00:00
(system: _: attrNames (readDir (path + "/${system}")))
2021-10-28 19:01:50 +00:00
(readDir path);
# invert the attrset, from {sys=[name]} to {name=sys}
# { host1 = "x86_64-linux"; host2 = "x86_64-linux"; }
hostToSys = foldl' (a: b: a // b) { }
(attrValues
(mapAttrs
(system: hosts: listToAttrs (map (name: { inherit name; value = system; }) hosts))
sysToHosts));
2021-06-05 06:22:38 +00:00
in
# produce stub configurations
# {
# host1 = { system = "x86_64-linux"; modules = [ ... ]; };
# host2 = { system = "x86_64-linux"; modules = [ ... ]; };
# }
mapAttrs
(hostName: system: {
inherit system;
modules = [
(nixosModule)
(path + "/${system}/${hostName}")
{ networking = { inherit hostName; }; }
];
})
hostToSys