From 8b2042e2bdd8b31de64bf15aa2b23da16740dc7c Mon Sep 17 00:00:00 2001 From: Yaro Kasear Date: Sat, 29 Nov 2025 14:14:08 -0600 Subject: [PATCH] Finally --- modules/metanix/networkd.nix | 30 +++++++++++++++++++++--------- tests/networkd-phobos.nix | 31 ++++++++++++++++++++++--------- 2 files changed, 43 insertions(+), 18 deletions(-) diff --git a/modules/metanix/networkd.nix b/modules/metanix/networkd.nix index 66ee174..a64558d 100644 --- a/modules/metanix/networkd.nix +++ b/modules/metanix/networkd.nix @@ -5,16 +5,24 @@ let metanix = config.metanix or { }; - thisHostName = config.metanix.thisHost or config.networking.hostName; + # Prefer explicit metanix.thisHost, fall back to networking.hostName + thisHostName = + if metanix ? thisHost && metanix.thisHost != null then metanix.thisHost + else config.networking.hostName or null; - hosts = (metanix.network or { }).hosts or { }; - host = hosts.${thisHostName} or null; + network = metanix.network or { }; + hosts = network.hosts or { }; + subnets = network.subnets or { }; + + host = + if thisHostName == null || !builtins.hasAttr thisHostName hosts + then null + else hosts.${thisHostName}; - subnets = (metanix.network or { }).subnets or { }; subnet = - if host != null && host ? subnetKey - then subnets.${host.subnetKey} or null - else null; + if host == null || !(host ? subnetKey) || !builtins.hasAttr host.subnetKey subnets + then null + else subnets.${host.subnetKey}; cidrPrefix = cidr: @@ -27,8 +35,10 @@ let if subnet == null then 32 else cidrPrefix subnet.cidr; unitName = - if host == null then "metanix" + if host == null || !(host ? interface) + then null else "10-${host.location}-${host.subnetType}-${host.interface}"; + in { options.metanix.thisHost = mkOption { @@ -40,7 +50,7 @@ in ''; }; - config = mkIf (host != null && host ? interface) { + config = mkIf (host != null && host ? interface && unitName != null) { systemd.network.enable = true; systemd.network.networks.${unitName} = { @@ -53,6 +63,8 @@ in linkConfig = mkIf (host ? "hw-address") { MACAddress = host."hw-address"; }; + + # Later: DNS, gateways, routes from policy }; }; } diff --git a/tests/networkd-phobos.nix b/tests/networkd-phobos.nix index 7548a5d..72c47c7 100644 --- a/tests/networkd-phobos.nix +++ b/tests/networkd-phobos.nix @@ -2,17 +2,28 @@ let system = "x86_64-linux"; - # Grab nixpkgs lib for convenience + # Real nixpkgs for addressing lib pkgs = import { inherit system; }; - lib = pkgs.lib; + realLib = pkgs.lib; - # Metanix world + addressing + # Minimal "testing lib" that makes mkIf actually behave like a real if + testLib = realLib // { + mkOption = x: x; + mkIf = cond: body: if cond then body else { }; + + types = { + str = "str"; + nullOr = t: "nullOr " + builtins.toString t; + }; + }; + + # Your world + addressing lib meta = import ../meta.nix; - addressing = import ../lib/addressing { inherit lib; }; + addressing = import ../lib/addressing { lib = realLib; }; network = addressing.mkNetworkFromSpec meta; - # Fake NixOS config that your module expects + # Fake NixOS config for this test config = { networking.hostName = "phobos"; metanix = { @@ -21,16 +32,18 @@ let }; }; - # Call the networkd module directly as a function + # Call the Metanix networkd module with the fake lib + config networkdModule = import ../modules/metanix/networkd.nix { - inherit lib config; + lib = testLib; + inherit config; }; + in { - # Raw Metanix view + # Raw Metanix view for sanity metanixHost = network.hosts.phobos; metanixSubnet = network.subnets."home-main"; - # What the module actually emits + # What the module actually emits once mkIf is forced systemdNetwork = networkdModule.config.systemd.network; }