From 881f98545095d5017b93d7c770fa73e8c73b9acc Mon Sep 17 00:00:00 2001 From: Astro Date: Wed, 31 Mar 2021 02:46:21 +0200 Subject: [PATCH] nix/lib/config: add upstream, forwardedPorts, fix fixed-hosts --- nix/lib/config/legacy.nix | 55 +++++++++++++++++++++++++------------- nix/lib/config/options.nix | 23 ++++++++++++++++ 2 files changed, 60 insertions(+), 18 deletions(-) diff --git a/nix/lib/config/legacy.nix b/nix/lib/config/legacy.nix index 78fadee..15e3308 100644 --- a/nix/lib/config/legacy.nix +++ b/nix/lib/config/legacy.nix @@ -32,11 +32,15 @@ in (builtins.mapAttrs (_: hosts4: { inherit hosts4; }) pillar.hosts-inet) (builtins.mapAttrs (net: dhcpData: { dhcp = { - inherit (dhcpData) start end time max-time fixed-hosts; + inherit (dhcpData) start end time max-time; server = if netHasDHCP net then "${net}-gw" else null; + fixed-hosts = + if dhcpData ? fixed-hosts + then dhcpData.fixed-hosts + else {}; router = dhcpData.host-opts.routers; domainName = dhcpData.string-opts.domain-name; }; @@ -68,24 +72,39 @@ in role = "ap"; }) pillar.cpe) - (builtins.mapAttrs (name: container: { - role = "container"; - location = mainServer; - interfaces = - builtins.mapAttrs (_: interface: - renameAttr "gw" "gw4" - (forceVeth interface) - ) container.interfaces; - ospf = - let - hostPillar = self.lib.saltPillarFor name; - ospfConf = hostPillar.ospf; - in lib.optionalAttrs (hostPillar ? ospf && ospfConf ? stubnets-inet) { - stubNets4 = ospfConf.stubnets-inet; - } // lib.optionalAttrs (hostPillar ? ospf && ospfConf ? stubnets-inet6) { + (builtins.mapAttrs (name: container: + let + ctPillar = self.lib.saltPillarFor name; + in { + role = "container"; + location = mainServer; + interfaces = + builtins.mapAttrs (net: interface: + renameAttr "gw" "gw4" + (forceVeth interface) // { + upstream = + if ctPillar ? upstream && + ctPillar.upstream.interface == net + then { + upBandwidth = ctPillar.upstream.up-bandwidth; + } + else null; + } + ) container.interfaces; + ospf = + let + hostPillar = self.lib.saltPillarFor name; + ospfConf = hostPillar.ospf; + in lib.optionalAttrs (hostPillar ? ospf && ospfConf ? stubnets-inet) { + stubNets4 = ospfConf.stubnets-inet; + } // lib.optionalAttrs (hostPillar ? ospf && ospfConf ? stubnets-inet6) { stubNets6 = ospfConf.stubnets-inet6; - }; - }) pillar.containers) + }; + forwardedPorts = + if ctPillar ? port-forwarding + then ctPillar.port-forwarding + else []; + }) pillar.containers) ] ++ (map (net: diff --git a/nix/lib/config/options.nix b/nix/lib/config/options.nix index 3f8e97f..a6a655a 100644 --- a/nix/lib/config/options.nix +++ b/nix/lib/config/options.nix @@ -96,6 +96,11 @@ let }; }; }; + upstreamOpts = { + upBandwidth = mkOption { + type = with types; nullOr int; + }; + }; interfaceOpts = { name, ... }: { options = { hwaddr = mkOption { @@ -113,6 +118,10 @@ let type = with types; nullOr str; default = null; }; + upstream = mkOption { + type = with types; nullOr (submodule { options = upstreamOpts; }); + default = null; + }; }; }; hostOpts = { name, ... }: { @@ -147,6 +156,20 @@ let type = types.bool; default = config.site.hosts.${name}.interfaces ? core; }; + forwardedPorts = mkOption { + type = with types; listOf (submodule { options = { + proto = mkOption { + type = types.enum [ "tcp" "udp" ]; + }; + port = mkOption { + type = types.int; + }; + to = mkOption { + type = types.str; + }; + }; }); + default = []; + }; ospf.stubNets4 = mkOption { type = with types; listOf str; default = [];