diff --git a/nix/lib/config/legacy.nix b/nix/lib/config/legacy.nix index ce93a9f..249aa08 100644 --- a/nix/lib/config/legacy.nix +++ b/nix/lib/config/legacy.nix @@ -3,6 +3,13 @@ let mainServer = "server1"; pillar = self.lib.saltPillarFor mainServer; + + renameAttr = from: to: attrset: + builtins.foldl' (result: name: + if name == from + then result // { "${to}" = attrset.${name}; } + else result // { "${name}" = attrset.${name}; } + ) {} (builtins.attrNames attrset); in { options.salt-pillar = lib.mkOption {}; @@ -10,11 +17,16 @@ in config.site.net = lib.mkMerge ([ (builtins.mapAttrs (_: vlan: { vlan = vlan; }) pillar.vlans) - (builtins.mapAttrs (_: subnet: { subnet4 = subnet; }) pillar.subnets-inet) + (builtins.mapAttrs (_: subnet4: { inherit subnet4; }) pillar.subnets-inet) + (builtins.mapAttrs (_: hosts4: { inherit hosts4; }) pillar.hosts-inet) ] ++ ( map (ctx: builtins.mapAttrs (_: subnet: { subnets6.${ctx} = subnet; }) pillar.subnets-inet6.${ctx} ) (builtins.attrNames pillar.subnets-inet6) + ) ++ ( + map (ctx: + builtins.mapAttrs (_: subnet: { hosts6.${ctx} = subnet; }) pillar.hosts-inet6.${ctx} + ) (builtins.attrNames pillar.hosts-inet6) )); config.site.hosts = lib.mkMerge ( @@ -36,7 +48,10 @@ in (builtins.mapAttrs (_: container: { role = "container"; location = mainServer; - inherit (container) interfaces; + interfaces = + builtins.mapAttrs (_: + renameAttr "gw" "gw6" + ) container.interfaces; }) pillar.containers) ] ++ diff --git a/nix/lib/config/options.nix b/nix/lib/config/options.nix index 049f33c..7ef935e 100644 --- a/nix/lib/config/options.nix +++ b/nix/lib/config/options.nix @@ -18,6 +18,16 @@ let type = with types; attrsOf str; default = {}; }; + hosts4 = mkOption { + description = "Attribute set of hostnames to IPv4 addresses"; + type = with types; attrsOf str; + default = {}; + }; + hosts6 = mkOption { + description = "Attribute set of contexts to attribute sets of hostnames to IPv4 addresses"; + type = with types; attrsOf (attrsOf str); + default = {}; + }; }; }; interfaceOpts = { name, ... }: { @@ -29,7 +39,7 @@ let type = mkOption { type = types.enum [ "veth" "phys" ]; }; - gw = mkOption { + gw4 = mkOption { type = with types; nullOr str; default = null; }; diff --git a/nix/nixos-module/network.nix b/nix/nixos-module/network.nix index 3a21502..bf903ac 100644 --- a/nix/nixos-module/network.nix +++ b/nix/nixos-module/network.nix @@ -13,14 +13,14 @@ enable = true; networks = - builtins.mapAttrs (ifName: { gw, gw6, ... }: { + builtins.mapAttrs (ifName: { gw4, gw6, ... }: { matchConfig.Name = ifName; # addresses = [ { # addressConfig.Address = "127.0.0.1/8"; # } ]; # TODO: lookup hostname gateway = with lib; - optional (gw != null) gw ++ + optional (gw4 != null) gw4 ++ optional (gw6 != null) gw6; }) config.site.hosts.${hostName}.interfaces; };