Finally
This commit is contained in:
parent
62899898fe
commit
8b2042e2bd
2 changed files with 43 additions and 18 deletions
|
|
@ -5,16 +5,24 @@ let
|
||||||
|
|
||||||
metanix = config.metanix or { };
|
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 { };
|
network = metanix.network or { };
|
||||||
host = hosts.${thisHostName} or null;
|
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 =
|
subnet =
|
||||||
if host != null && host ? subnetKey
|
if host == null || !(host ? subnetKey) || !builtins.hasAttr host.subnetKey subnets
|
||||||
then subnets.${host.subnetKey} or null
|
then null
|
||||||
else null;
|
else subnets.${host.subnetKey};
|
||||||
|
|
||||||
cidrPrefix =
|
cidrPrefix =
|
||||||
cidr:
|
cidr:
|
||||||
|
|
@ -27,8 +35,10 @@ let
|
||||||
if subnet == null then 32 else cidrPrefix subnet.cidr;
|
if subnet == null then 32 else cidrPrefix subnet.cidr;
|
||||||
|
|
||||||
unitName =
|
unitName =
|
||||||
if host == null then "metanix"
|
if host == null || !(host ? interface)
|
||||||
|
then null
|
||||||
else "10-${host.location}-${host.subnetType}-${host.interface}";
|
else "10-${host.location}-${host.subnetType}-${host.interface}";
|
||||||
|
|
||||||
in
|
in
|
||||||
{
|
{
|
||||||
options.metanix.thisHost = mkOption {
|
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.enable = true;
|
||||||
|
|
||||||
systemd.network.networks.${unitName} = {
|
systemd.network.networks.${unitName} = {
|
||||||
|
|
@ -53,6 +63,8 @@ in
|
||||||
linkConfig = mkIf (host ? "hw-address") {
|
linkConfig = mkIf (host ? "hw-address") {
|
||||||
MACAddress = host."hw-address";
|
MACAddress = host."hw-address";
|
||||||
};
|
};
|
||||||
|
|
||||||
|
# Later: DNS, gateways, routes from policy
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,17 +2,28 @@
|
||||||
let
|
let
|
||||||
system = "x86_64-linux";
|
system = "x86_64-linux";
|
||||||
|
|
||||||
# Grab nixpkgs lib for convenience
|
# Real nixpkgs for addressing lib
|
||||||
pkgs = import <nixpkgs> { inherit system; };
|
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;
|
meta = import ../meta.nix;
|
||||||
addressing = import ../lib/addressing { inherit lib; };
|
addressing = import ../lib/addressing { lib = realLib; };
|
||||||
|
|
||||||
network = addressing.mkNetworkFromSpec meta;
|
network = addressing.mkNetworkFromSpec meta;
|
||||||
|
|
||||||
# Fake NixOS config that your module expects
|
# Fake NixOS config for this test
|
||||||
config = {
|
config = {
|
||||||
networking.hostName = "phobos";
|
networking.hostName = "phobos";
|
||||||
metanix = {
|
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 {
|
networkdModule = import ../modules/metanix/networkd.nix {
|
||||||
inherit lib config;
|
lib = testLib;
|
||||||
|
inherit config;
|
||||||
};
|
};
|
||||||
|
|
||||||
in
|
in
|
||||||
{
|
{
|
||||||
# Raw Metanix view
|
# Raw Metanix view for sanity
|
||||||
metanixHost = network.hosts.phobos;
|
metanixHost = network.hosts.phobos;
|
||||||
metanixSubnet = network.subnets."home-main";
|
metanixSubnet = network.subnets."home-main";
|
||||||
|
|
||||||
# What the module actually emits
|
# What the module actually emits once mkIf is forced
|
||||||
systemdNetwork = networkdModule.config.systemd.network;
|
systemdNetwork = networkdModule.config.systemd.network;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue