This commit is contained in:
Yaro Kasear 2025-11-29 14:14:08 -06:00
parent 62899898fe
commit 8b2042e2bd
2 changed files with 43 additions and 18 deletions

View file

@ -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
};
};
}

View file

@ -2,17 +2,28 @@
let
system = "x86_64-linux";
# Grab nixpkgs lib for convenience
# Real nixpkgs for addressing lib
pkgs = import <nixpkgs> { 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;
}