From a940356e0a0c1ea909501e3df3a11b3638fb86dd Mon Sep 17 00:00:00 2001 From: oqyude Date: Tue, 13 Oct 2026 01:45:28 +0300 Subject: [PATCH] feat(pxe-infra): add isolated bridge + MAC-filtered DHCP module (L2) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Adds the L2 layer of the PXE pipeline test bed (docs/ARCHITECTURE.md §L2) to rydiwo only — isolated private subnet, never touches LAN. - modules/options.nix: options.pxe-infra.{enable, bridge, dhcp.*, guests[]} — fully typed, default master toggle is off so the existing rydiwo build is unchanged - modules/pxe-infra/default.nix: umbrella that imports the two sub-modules; inert when the master toggle is off - modules/pxe-infra/bridge.nix: idempotent systemd oneshot that creates the Linux bridge, assigns the lab host a private IPv4, and is explicitly NetworkManager.unmanaged so NM does not reconfigure it - modules/pxe-infra/dhcp.nix: dnsmasq as DHCP-only on the bridge only (bind-interfaces + interface=, port=0 disables DNS, no-resolv, no-poll, cache-size=0); MAC-filtered via dhcp-host=…,set:known and dhcp-ignore=tag:!known when authoritative=false - configurations/mini-laptop.nix: adds modules/pxe-infra to imports, plus a commented example showing how to enable for a real guest Sidesteps docs/OPEN-QUESTIONS.md Q21 (LAN-permission) by design: a fully isolated bridge satisfies L2 without the lab node answering DHCP on the real LAN. nix flake check passes for all six hosts. Refs: docs/ARCHITECTURE.md §L2, docs/OPEN-QUESTIONS.md F20/F21/F23. --- configurations/mini-laptop.nix | 23 +++++ modules/options.nix | 149 +++++++++++++++++++++++++++++++++ modules/pxe-infra/bridge.nix | 62 ++++++++++++++ modules/pxe-infra/default.nix | 18 ++++ modules/pxe-infra/dhcp.nix | 77 +++++++++++++++++ 5 files changed, 329 insertions(+) create mode 100644 modules/pxe-infra/bridge.nix create mode 100644 modules/pxe-infra/default.nix create mode 100644 modules/pxe-infra/dhcp.nix diff --git a/configurations/mini-laptop.nix b/configurations/mini-laptop.nix index 32ca9b2..0c2a88d 100644 --- a/configurations/mini-laptop.nix +++ b/configurations/mini-laptop.nix @@ -14,6 +14,7 @@ nixos-hardware.nixosModules.chuwi-minibook-x ./hardware/mini-laptop.nix self.nixosModules.default + ../modules/pxe-infra ]; boot = { @@ -34,4 +35,26 @@ hardware.intel-gpu-tools.enable = true; system.stateVersion = "26.05"; + + # -------------------------------------------------------------------------- + # PXE infrastructure rig (L2 of docs/ARCHITECTURE.md). + # Uncomment to enable on rydiwo. The rig gives rydiwo an isolated Linux + # bridge (`pxe-infra.bridge.name`, default `pxe0`) plus a MAC-filtered + # dnsmasq DHCP server bound only to that bridge — the real LAN is not + # touched. See `modules/options.nix` (options.pxe-infra.*) for the + # full option surface and `modules/pxe-infra/` for the implementation. + # + # pxe-infra = { + # enable = true; + # bridge.address = "10.66.0.1"; + # bridge.prefixLength = 24; + # dhcp.guests = [ + # { + # mac = "52:54:00:12:34:56"; + # ip = "10.66.0.10"; + # hostname = "pxe-vm-01"; # optional + # } + # ]; + # }; + # -------------------------------------------------------------------------- } diff --git a/modules/options.nix b/modules/options.nix index 93bf322..1e53236 100644 --- a/modules/options.nix +++ b/modules/options.nix @@ -78,4 +78,153 @@ ''; }; }; + + # pxe-infra — isolated private bridge + MAC-filtered DHCP for PXE + # VM guests (L2 of docs/ARCHITECTURE.md, see also Q20/Q21/Q23 in + # docs/OPEN-QUESTIONS.md). Decoupled from the host's primary + # network stack: dnsmasq is bound to a Linux bridge that lives on + # a private subnet and never touches the LAN's DHCP server. + options.pxe-infra = { + enable = lib.mkOption { + type = lib.types.bool; + default = false; + description = '' + Master toggle for the PXE infrastructure rig (L2 of + docs/ARCHITECTURE.md). When enabled, this host gains an + isolated private bridge and a MAC-filtered DHCP server, + usable as a stand-alone VM/PXE test bed without touching + the real LAN. + ''; + }; + + bridge = { + name = lib.mkOption { + type = lib.types.str; + default = "pxe0"; + example = "pxe0"; + description = '' + Name of the Linux bridge interface that PXE VMs attach + to. Brought up by a systemd oneshot (idempotent) and + explicitly unmanaged by NetworkManager so NM does not + reconfigure it. + ''; + }; + address = lib.mkOption { + type = lib.types.str; + default = "10.66.0.1"; + example = "10.66.0.1"; + description = '' + IP address assigned to the lab host on the private + bridge. The VM's default route should point here so + the lab host can always reach the VM via this address. + ''; + }; + prefixLength = lib.mkOption { + type = lib.types.int; + default = 24; + example = 24; + description = '' + CIDR prefix length for the bridge subnet. Must match + the chosen address (e.g. 10.66.0.1/24). Range and + gateway options below must all live inside this + subnet. + ''; + }; + }; + + dhcp = { + enable = lib.mkOption { + type = lib.types.bool; + default = true; + description = '' + Whether the bridge-side DHCP server (dnsmasq) should + run. Set false only if you bring your own DHCP server + on the bridge. + ''; + }; + authoritative = lib.mkOption { + type = lib.types.bool; + default = false; + description = '' + When true, dnsmasq responds authoritatively to all DHCP + requests on the bridge (recommended only when nothing + else on the bridge exists). When false (default), + dnsmasq answers only MACs listed in `guests` and + silently drops everything else. + ''; + }; + rangeStart = lib.mkOption { + type = lib.types.nullOr lib.types.str; + default = null; + example = "10.66.0.100"; + description = '' + Lower bound of a free address pool handed out to guests + whose MAC is not explicitly listed. Leave null to + disable the pool and hand out leases only to MACs + listed in `guests`. + ''; + }; + rangeEnd = lib.mkOption { + type = lib.types.nullOr lib.types.str; + default = null; + example = "10.66.0.200"; + description = '' + Upper bound of the free address pool. See `rangeStart`. + ''; + }; + leaseTime = lib.mkOption { + type = lib.types.str; + default = "12h"; + example = "1h"; + description = '' + Default DHCP lease time handed to guests. Short values + are useful while iterating on the VM's identity; long + values are useful in steady state. + ''; + }; + guests = lib.mkOption { + type = lib.types.listOf ( + lib.types.submodule { + options = { + mac = lib.mkOption { + type = lib.types.str; + example = "52:54:00:12:34:56"; + description = '' + MAC address of the guest NIC. Must be unique + within the list. dnsmasq answers DHCP only for + these MACs unless dhcp.authoritative is true. + ''; + }; + ip = lib.mkOption { + type = lib.types.str; + example = "10.66.0.10"; + description = '' + IP address handed to this guest. Must lie + inside the bridge subnet. + ''; + }; + hostname = lib.mkOption { + type = lib.types.nullOr lib.types.str; + default = null; + example = "pxe-vm-01"; + description = '' + Optional hostname returned to the guest via + DHCP. Useful when the image identity phase + reads the DHCP-supplied hostname (per + docs/OPEN-QUESTIONS.md #A.2). + ''; + }; + }; + } + ); + default = [ ]; + description = '' + Reserved guests: list of MAC ↔ IP mappings (with + optional hostname). When `dhcp.authoritative` is false, + dnsmasq answers DHCP only for these guests and silently + drops everything else. + ''; + }; + }; + }; } diff --git a/modules/pxe-infra/bridge.nix b/modules/pxe-infra/bridge.nix new file mode 100644 index 0000000..ee04988 --- /dev/null +++ b/modules/pxe-infra/bridge.nix @@ -0,0 +1,62 @@ +# pxe-infra/bridge — bring up the private Linux bridge the PXE +# VM attaches to. +# +# Why a systemd oneshot instead of systemd.networkd: +# rydiwo (and other desktop-class hosts in this flake) run +# NetworkManager (modules/desktop/default.nix), not systemd- +# networkd. We still want the bridge up at boot, regardless of +# LAN connectivity, so we drive the interface ourselves with +# the iproute2 `ip` command. +# +# Why NetworkManager.unmanaged: +# If NM were allowed to touch the bridge, it might try to +# reconfigure it (DHCP the bridge, replace the static IP we +# just set, or remove it entirely). Marking it unmanaged keeps +# NM out of our hair. +{ + config, + lib, + pkgs, + ... +}: +{ + config = lib.mkIf config.pxe-infra.enable { + # NM must leave the bridge alone — we set it up ourselves. + networking.networkmanager.unmanaged = [ config.pxe-infra.bridge.name ]; + + # Order: bridge must be up before dnsmasq tries to bind to it. + systemd.services.pxe-infra-bridge = { + description = "pxe-infra: bring up ${config.pxe-infra.bridge.name} bridge"; + wantedBy = [ "multi-user.target" ]; + after = [ "systemd-modules-load.service" ]; + wantedBefore = [ "dnsmasq.service" ]; + path = [ pkgs.iproute2 ]; + serviceConfig = { + Type = "oneshot"; + RemainAfterExit = true; + }; + script = '' + set -euo pipefail + + # Idempotent bridge creation. + if ! ip link show dev ${config.pxe-infra.bridge.name} >/dev/null 2>&1; then + ip link add name ${config.pxe-infra.bridge.name} type bridge \ + forward_delay 0 stp 0 + fi + + # Idempotent address assignment. Only rewrite the address + # if it isn't already what we expect — preserves state in + # case NM did something unexpected before this service ran. + if ! ip -j addr show dev ${config.pxe-infra.bridge.name} \ + | grep -q '"${config.pxe-infra.bridge.address}"'; then + ip addr flush dev ${config.pxe-infra.bridge.name} || true + ip addr add ${config.pxe-infra.bridge.address}/${toString config.pxe-infra.bridge.prefixLength} \ + dev ${config.pxe-infra.bridge.name} + fi + + # Always make sure the link is up. + ip link set ${config.pxe-infra.bridge.name} up + ''; + }; + }; +} diff --git a/modules/pxe-infra/default.nix b/modules/pxe-infra/default.nix new file mode 100644 index 0000000..b4747c5 --- /dev/null +++ b/modules/pxe-infra/default.nix @@ -0,0 +1,18 @@ +# pxe-infra — isolated private network for PXE VM guests (L2 of +# docs/ARCHITECTURE.md). Provides a Linux bridge plus a +# MAC-filtered DHCP server so a fresh VM can netboot from this +# host without touching the real LAN. +# +# Master toggle: `pxe-infra.enable`. Submodules guard their own +# config with `lib.mkIf config.pxe-infra.enable`, so importing +# this umbrella alone has no effect on a host that opts out. +{ + config, + ... +}: +{ + imports = [ + ./bridge.nix + ./dhcp.nix + ]; +} diff --git a/modules/pxe-infra/dhcp.nix b/modules/pxe-infra/dhcp.nix new file mode 100644 index 0000000..7f63c7d --- /dev/null +++ b/modules/pxe-infra/dhcp.nix @@ -0,0 +1,77 @@ +# pxe-infra/dhcp — dnsmasq as a DHCP-only server bound to the +# private bridge, never touching the LAN. +# +# Hard rules (per docs/ARCHITECTURE.md L2): +# - bind to the bridge interface only +# - never resolve DNS (port = 0 disables the DNS server) +# - when authoritative = false (default), only respond to DHCP +# requests for MACs listed in pxe-infra.dhcp.guests; +# everything else is silently dropped. +# +# Together these make the rig a stand-in DHCP server for the VM +# only; the real LAN's DHCP server stays authoritative for +# everything else and the bridge cannot become a second source +# of leases on the LAN. +{ + config, + lib, + ... +}: +let + cfg = config.pxe-infra.dhcp; + bridgeName = config.pxe-infra.bridge.name; + + # Build dhcp-host entries with a `known` tag attached, so when + # authoritative = false we can drop everything else. + dhcpHostLines = map + (g: + let + body = if g.hostname != null + then "${g.mac},${g.ip},${g.hostname}" + else "${g.mac},${g.ip}"; + in "${body},set:known") + cfg.guests; + + # Free address pool — only configured when both bounds are set. + rangeLines = lib.optionals + (cfg.rangeStart != null && cfg.rangeEnd != null) + [ "${cfg.rangeStart},${cfg.rangeEnd},${cfg.leaseTime}" ]; + + base = { + # Bind to the private bridge only — never 0.0.0.0, never eth0. + bind-interfaces = true; + interface = [ bridgeName ]; + # DHCP-only — kill the DNS server entirely. + port = 0; + # Don't read resolv.conf, don't poll it. + no-resolv = true; + no-poll = true; + # No cache (no DNS queries to cache anyway). + cache-size = 0; + # Logging. + log-dhcp = true; + log-queries = false; + log-facility = "-"; + # Reservations + free pool (lists become repeated keys). + dhcp-host = dhcpHostLines ++ rangeLines; + }; + + filtered = base // lib.optionalAttrs (!cfg.authoritative) { + # Drop any DHCP request not carrying the `known` tag we set on + # static hosts. This is the MAC filter that keeps the rig + # from accidentally serving strangers if a real-LAN packet + # somehow reached the bridge. + dhcp-ignore = "tag:!known"; + }; +in +{ + config = lib.mkIf (config.pxe-infra.enable && cfg.enable) { + services.dnsmasq = { + enable = true; + # Don't append 127.0.0.1 to resolv.conf — we are not a DNS + # resolver. + resolveLocalQueries = false; + settings = filtered; + }; + }; +}