Files
nixos/modules/pxe-infra/dhcp.nix
T
oqyude 54a93f1e48 feat(pxe-infra): add TFTP (via dnsmasq) and HTTP (via nginx) — L3
Wires the L3 PXE services from docs/ARCHITECTURE.md into the
homelab flake. The DHCP module already running on the bridge gets
TFTP bolted on (same dnsmasq daemon, same interface binding); a
new nginx vhost serves kernel, initramfs, and rootfs over HTTP
from the same private bridge.

- modules/options.nix: new options.pxe-infra.{artifacts, http, tftp}
  - artifacts.root defaults to /var/lib/pxe-infra; both servers
    read from subdirs of that path
  - http.{enable, port}: nginx listener (default port 80, bound
    to the bridge IP only)
  - tftp.enable: opt-out for the dnsmasq TFTP module

- modules/pxe-infra/dhcp.nix: when pxe-infra.tftp.enable, the
  dnsmasq settings now include `enable-tftp` and `tftp-root=`.
  No new daemon; the same bind-interfaces + interface= already
  restricts the DHCP server to the bridge, and that carries
  through to the TFTP server.

- modules/pxe-infra/http.nix: new file. services.nginx.enable
  and a single vhost listening on the bridge IP, root at
  artifacts.root/http, autoindex on. No default-server catch-all,
  so requests on any other interface are refused at the listen
  layer, not silently answered.

- modules/pxe-infra/default.nix: imports the new http.nix and
  adds systemd-tmpfiles rules that create artifacts.root plus
  its http/ and tftpboot/ subdirs at boot. The module does not
  populate them — that is L5 (image build)'s job.

nginx does not speak TFTP (UDP/69, RFC 1350); TFTP stays with
dnsmasq where it shares the daemon with DHCP. ARCHITECTURE.md
L3 calls out TFTP+HTTP conflation as the mistake; DHCP+TFTP
sharing a daemon is a natural fit and avoids a second service
on the Chuwi MiniBook.

Refs: docs/ARCHITECTURE.md §L3, docs/OPEN-QUESTIONS.md
"Resolved (2026-10-13)" B.9/B.10.
2026-10-13 02:35:59 +03:00

86 lines
2.7 KiB
Nix

# 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;
};
# Optional TFTP module on the same dnsmasq instance. Used to
# hand out the iPXE bootloader (a few hundred KB) before the
# client switches to HTTP for the kernel/initramfs/rootfs.
tftpSettings = lib.optionalAttrs config.pxe-infra.tftp.enable {
enable-tftp = true;
tftp-root = "${config.pxe-infra.artifacts.root}/tftpboot";
};
filtered = base // tftpSettings // 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;
};
};
}