Files
nixos/modules/pxe-infra/dhcp.nix
T
oqyude d7e46a4e89 feat(pxe-infra): stage iPXE EFI + boot.ipxe — completes the boot chain
Wires the missing piece between the L3 services and the L4 VM:
the firmware does DHCP+TFTP-fetch of an iPXE binary, then iPXE
fetches a static boot.ipxe script over HTTP, which sanboots the
alpine-worker raw image. Without this, the VM has a DHCP lease
but no filename to TFTP-fetch, so the chain stops at the
firmware.

- modules/options.nix: new options.pxe-infra.bootloader
  - enable: master switch (default true)
  - ipxeBinary: filename under tftpboot/ (default "ipxe.efi",
    the universal UEFI binary from pkgs.ipxe; "snp.efi" is a
    smaller virtio-net-only alternative)

- modules/pxe-infra/ipxe.nix: new file.
  - environment.etc drops pkgs.ipxe's binary under
    /etc/static/pxe-infra/tftpboot/<ipxeBinary>, plus a static
    boot.ipxe script under /etc/static/pxe-infra/http/.
  - The script uses iPXE's own ${boot-url} variable substitution
    (escaped with Nix's ''${...}'' so it survives into the
    output verbatim) to sanboot http://<bridge.address>/latest/
    alpine-worker.raw, with a 5s retry on failure.

- modules/pxe-infra/dhcp.nix: when both pxe-infra.tftp.enable
  and pxe-infra.bootloader.enable are on, dnsmasq settings now
  include `dhcp-boot=<ipxeBinary>`. The MAC filter (tag:!known)
  still drops requests from unknown MACs before they can use the
  filename.

- modules/pxe-infra/default.nix: imports ipxe.nix and adds two
  `L+` tmpfiles rules that symlink the iPXE binary and the
  boot.ipxe script from /etc/static/pxe-infra/... into the
  served TFTP/HTTP roots. `L+` (instead of plain `L`) means
  systemd-tmpfiles will update the symlink target on every
  boot, so pkgs.ipxe upgrades via nixos-rebuild take effect
  without redeploying anything.

Refs: docs/ARCHITECTURE.md §L3, §L4.
2026-10-13 02:46:07 +03:00

94 lines
3.1 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";
};
# Boot filename: when the iPXE binary is staged, hand it to
# UEFI firmware in the DHCP offer so the firmware knows what
# to TFTP-fetch. Sent to all clients; the `tag:!known` filter
# below still drops requests from unknown MACs.
bootSettings = lib.optionalAttrs (config.pxe-infra.tftp.enable && config.pxe-infra.bootloader.enable) {
dhcp-boot = config.pxe-infra.bootloader.ipxeBinary;
};
filtered = base // tftpSettings // bootSettings // 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;
};
};
}