Files
hermenix/tests/network-modes-check.nix
T
2026-09-23 13:24:07 -04:00

216 lines
8.3 KiB
Nix

# Static evaluation check for the host module's two networking modes (NAT
# and bridged) and the guest's mode-agnostic NIC configuration.
#
# Nothing here is built or booted: the check evaluates services.hermenix
# (modules/host.nix) in both modes plus guest/common.nix, and asserts on the
# networking configuration the modules produce - the nftables rulesets, the
# ip-family input/forward rules, the systemd-networkd units, and the guest's
# DHCP / route-metric setup. Any failing assertion fails the check listing
# what broke.
#
# Run by the operator/CI, never by an agent:
# nix build .#checks.x86_64-linux.network-modes
#
# The host evaluations use minimal stub options for the NixOS modules the
# host module's config feeds (networking.nftables, networking.firewall,
# systemd.network) because this check only reads those outputs back; every
# other option host.nix writes is out of scope and passed through with
# _module.check = false. The guest evaluation is different: guest/common.nix
# runs against the full NixOS option set (module-list.nix) because its real
# imports (qemu-guest profile and friends) need it.
{ pkgs }:
let
inherit (pkgs) lib;
nixpkgsPath = pkgs.path;
has = lib.strings.hasInfix;
# ------------------------------------------------------------------ #
# Host module, evaluated with stub options for what this check reads. #
# ------------------------------------------------------------------ #
hostOptions = { lib, ... }: {
config._module.check = false;
options = {
networking = {
useNetworkd = lib.mkOption {
type = lib.types.bool;
default = false;
};
nftables = {
enable = lib.mkOption {
type = lib.types.bool;
default = false;
};
tables = lib.mkOption {
type = lib.types.attrsOf lib.types.anything;
default = { };
};
};
firewall = {
enable = lib.mkOption {
type = lib.types.bool;
default = true;
};
filterForward = lib.mkOption {
type = lib.types.bool;
default = false;
};
extraInputRules = lib.mkOption {
type = lib.types.str;
default = "";
};
extraForwardRules = lib.mkOption {
type = lib.types.str;
default = "";
};
};
};
systemd.network = {
networks = lib.mkOption {
type = lib.types.attrsOf lib.types.anything;
default = { };
};
netdevs = lib.mkOption {
type = lib.types.attrsOf lib.types.anything;
default = { };
};
};
};
};
# The one scenario every assertion is made against.
evalHost =
bridged:
lib.evalModules {
modules = [
hostOptions
../modules/host.nix
{
services.hermenix = {
enable = true;
network = {
externalInterface = "eno1";
inherit bridged;
};
allowLan = false;
publish = [
{
hostPort = 8080;
guestPort = 8080;
}
];
lanAccess = [
{
address = "192.168.1.50";
port = 11434;
}
];
hostAccess = [ { port = 8188; } ];
};
}
];
specialArgs = { inherit pkgs; };
};
nat = evalHost false;
br = evalHost true;
natNft = nat.config.networking.nftables.tables;
brNft = br.config.networking.nftables.tables;
natNat = natNft."hermenix-nat" or { };
brNat = brNft."hermenix-nat" or { };
brBr = brNft."hermenix-bridge" or { };
natNetworks = nat.config.systemd.network.networks;
brNetworks = br.config.systemd.network.networks;
natInput = nat.config.networking.firewall.extraInputRules;
natForward = nat.config.networking.firewall.extraForwardRules;
brInput = br.config.networking.firewall.extraInputRules;
brForward = br.config.networking.firewall.extraForwardRules;
hostResults = {
# --- NAT mode: the original behaviour has to be unchanged ----------
"nat: no bridged-mode bridge table" = !(natNft ? "hermenix-bridge");
"nat: publish is DNATed to the guest" = has "dnat to 10.77.0.2:8080" natNat.content;
"nat: outbound masquerades on the uplink" = has "masquerade" natNat.content;
"nat: input rules match the fixed service-net address" =
has "ip saddr 10.77.0.2 tcp dport 5000 accept" natInput && !(has "meta mark" natInput);
"nat: published port has a forward accept" =
has "ip daddr 10.77.0.2 tcp dport 8080 accept" natForward;
"nat: bridge keeps the static service-net address, no LAN DHCP" =
natNetworks."10-hermes-br".address == [ "10.77.0.1/24" ]
&& (natNetworks."10-hermes-br".networkConfig.DHCP or null) == null;
"nat: uplink is not a bridge port" = !(natNetworks ? "12-eno1");
"nat: tap is a bridge port" = natNetworks."11-vm-hermes".networkConfig.Bridge == "hermes-br";
"nat: bridge netdev exists" =
nat.config.systemd.network.netdevs."10-hermes-br".netdevConfig.Kind == "bridge";
# --- bridged mode: the guest is directly on the LAN ----------------
"bridged: bridge-family table exists" = brNft ? "hermenix-bridge" && brBr.family == "bridge";
"bridged: guest traffic is marked as it enters the tap" = has "meta mark set 18501" brBr.content;
"bridged: inbound from the LAN is open on every port" =
has "oifname \"vm-hermes\" accept" brBr.content;
"bridged: lanAccess still gates guest egress" =
has "ip daddr 192.168.1.50 tcp dport 11434 accept" brBr.content;
"bridged: private-space egress is dropped unless allowlisted" =
has "192.168.0.0/16" brBr.content && has "fc00::/7" brBr.content;
"bridged: no DNAT for publish" =
!(has "dnat to" brNat.content) && !(has "chain prerouting" brNat.content);
"bridged: scoped fallback masquerade remains" = has "masquerade" brNat.content;
"bridged: input rules match the mark, not the unknown DHCP address" =
has "meta mark 18501 tcp dport 5000 accept" brInput
&& has "meta mark 18501 tcp dport 8188 accept" brInput
&& !(has "ip saddr 10.77.0.2" brInput);
"bridged: publish forward rules are dropped" =
!(has "ip daddr 10.77.0.2 tcp dport 8080 accept" brForward);
"bridged: bridge keeps the service net and takes LAN DHCP" =
brNetworks."10-hermes-br".address == [ "10.77.0.1/24" ]
&& brNetworks."10-hermes-br".networkConfig.DHCP == "ipv4";
"bridged: uplink is a bridge port" = brNetworks."12-eno1".networkConfig.Bridge == "hermes-br";
"bridged: tap is still a bridge port" =
brNetworks."11-vm-hermes".networkConfig.Bridge == "hermes-br";
};
# ------------------------------------------------------------------- #
# Guest module: must produce one NIC config that works in both modes. #
# Evaluated against the full NixOS option set. #
# ------------------------------------------------------------------- #
guest = lib.evalModules {
modules = (import "${nixpkgsPath}/nixos/modules/module-list.nix") ++ [
{ config._module.check = false; }
../guest/common.nix
];
specialArgs = {
inherit pkgs;
modulesPath = "${nixpkgsPath}/nixos/modules";
};
};
gEth = guest.config.systemd.network.networks."10-eth";
gRoute = builtins.elemAt gEth.routes 0;
guestResults = {
"guest: NIC asks for a DHCP lease (bridged mode)" = gEth.networkConfig.DHCP == "ipv4";
"guest: service-net address stays static (both modes)" = gEth.address == [ "10.77.0.2/24" ];
"guest: static default route via the host is the NAT fallback" =
gRoute.Gateway == "10.77.0.1" && gRoute.Metric == 200;
"guest: DHCP default route wins (metric 100)" = gEth.dhcpV4Config.RouteMetric == 100;
"guest: resolvers stay QUAD9, DHCP DNS is ignored" =
gEth.dhcpV4Config.UseDNS == false
&&
gEth.networkConfig.DNS == [
"9.9.9.9"
"149.112.112.112"
];
};
results = hostResults // guestResults;
failing = builtins.filter (n: !results.${n}) (builtins.attrNames results);
in
assert lib.assertMsg (failing == [ ]) ''
hermenix network-modes check FAILED:
${lib.concatMapStringsSep "\n" (n: " - " + n) failing}
'';
pkgs.runCommand "hermenix-network-modes-check" { } "touch $out"