From b8d27ab9ca9b6dda82e7f3f0be1cc81fbce606df Mon Sep 17 00:00:00 2001 From: Astro Date: Mon, 5 Jun 2023 01:25:04 +0200 Subject: [PATCH] nixos-module/container/lxc-config: simplify --- nix/nixos-module/container/lxc-config.nix | 47 ++++++++++++----------- 1 file changed, 25 insertions(+), 22 deletions(-) diff --git a/nix/nixos-module/container/lxc-config.nix b/nix/nixos-module/container/lxc-config.nix index 63bb2a3..b20e8b8 100644 --- a/nix/nixos-module/container/lxc-config.nix +++ b/nix/nixos-module/container/lxc-config.nix @@ -4,6 +4,8 @@ let inherit (config.networking) hostName; + interfaces = config.site.hosts.${hostName}.physicalInterfaces; + # linux iface name max length = 15 shortenNetName = name: if builtins.match "priv(.*)" name != null @@ -21,27 +23,8 @@ let else ifname; # `lxc.net.*` formatter for lxc.container.conf files - netConfig = ctName: interfaces: + netConfig = let - config = map (netName: - let - ifData = interfaces.${netName}; - in { - type = ifData.type; - name = checkIfname netName; - flags = "up"; - hwaddr = if ifData ? hwaddr && ifData.hwaddr != null - then ifData.hwaddr - else "0A:14:48:xx:xx:xx"; - } // (lib.optionalAttrs (ifData.type == "veth") { - veth.pair = checkIfname "${shortenNetName ctName}-${shortenNetName netName}"; - veth.mode = checkIfname "bridge"; - link = checkIfname netName; - }) // (lib.optionalAttrs (ifData.type == "phys") { - link = checkIfname "ext-${netName}"; - }) - ) (builtins.attrNames interfaces); - attrNamesOrdered = attrs: if attrs ? type then [ "type" ] ++ lib.remove "type" (builtins.attrNames attrs) @@ -69,8 +52,28 @@ let map ({ e, i }: serialize "${name}.${toString i}" e) (enumerate x 0) ) else throw "Invalid data in lxc net config for ${name}: ${lib.generators.toPretty {} x}"; + in - serialize "lxc.net" config; + serialize "lxc.net" ( + map (netName: + let + ifData = interfaces.${netName}; + in { + type = ifData.type; + name = checkIfname netName; + flags = "up"; + hwaddr = if ifData ? hwaddr && ifData.hwaddr != null + then ifData.hwaddr + else "0A:14:48:xx:xx:xx"; + } // (lib.optionalAttrs (ifData.type == "veth") { + veth.pair = checkIfname "${shortenNetName hostName}-${shortenNetName netName}"; + veth.mode = checkIfname "bridge"; + link = checkIfname netName; + }) // (lib.optionalAttrs (ifData.type == "phys") { + link = checkIfname "ext-${netName}"; + }) + ) (builtins.attrNames interfaces) + ); in { @@ -106,6 +109,6 @@ in lxc.cgroup.devices.allow = c 108:0 rwm lxc.cgroup2.devices.allow = c 108:0 rwm - ${netConfig hostName config.site.hosts.${hostName}.physicalInterfaces} + ${netConfig} ''; }