Add local DNS records, DHCP reservations and an installer ISO
DNS and DHCP (needed to replace a dnsmasq-based router): - router.dns.domain/hosts/overrides/forwardZones: authoritative local domain, split-horizon host records with reverse entries, wildcard overrides and per-zone forwarders (private zones skip DNSSEC). - lans.<n>.dhcp.reservations: Kea host reservations, published in DNS. - Tunnels with dns = true admit what their peers route, so a site-to-site peer without a transfer address can use the resolver. Installer: - router-install wipes a disk, creates an ESP and an ext4 root and installs a prebuilt system with its secrets; nothing is built on the box. installed-layout is the shared disk layout contract. - lib.mkInstaller builds an ISO for a given nixosSystem; packages.installer-example is the example's ISO. Covered by the dns and installer VM tests (the latter installs onto an empty disk and boots it under UEFI), wireguard DNS and assertion cases. PLAN.md records the remaining gaps for replacing the hand-written router. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,118 @@
|
||||
# `router-install`: write a prebuilt router system to a disk. Nothing is built
|
||||
# on the box; the system closure travels with the installer.
|
||||
{
|
||||
config,
|
||||
lib,
|
||||
pkgs,
|
||||
...
|
||||
}:
|
||||
let
|
||||
cfg = config.router.installer;
|
||||
|
||||
installScript = pkgs.writeShellApplication {
|
||||
name = "router-install";
|
||||
runtimeInputs = [
|
||||
config.system.build.nixos-install
|
||||
pkgs.coreutils
|
||||
pkgs.dosfstools
|
||||
pkgs.e2fsprogs
|
||||
pkgs.gptfdisk
|
||||
pkgs.util-linux
|
||||
];
|
||||
text = ''
|
||||
usage() {
|
||||
echo "usage: router-install [--yes] [--secrets DIR] <disk>" >&2
|
||||
echo " Wipes <disk> (e.g. /dev/nvme0n1) and installs ${cfg.system}." >&2
|
||||
echo " --secrets DIR copied to /var/lib/router (PPPoE password, WireGuard keys, ...)" >&2
|
||||
exit 2
|
||||
}
|
||||
|
||||
yes=0
|
||||
secrets=
|
||||
disk=
|
||||
while [ $# -gt 0 ]; do
|
||||
case $1 in
|
||||
--yes) yes=1 ;;
|
||||
--secrets) shift; [ $# -gt 0 ] || usage; secrets=$1 ;;
|
||||
-*) usage ;;
|
||||
*) [ -z "$disk" ] || usage; disk=$1 ;;
|
||||
esac
|
||||
shift
|
||||
done
|
||||
[ -n "$disk" ] || usage
|
||||
|
||||
[ "$(id -u)" -eq 0 ] || { echo "router-install: must run as root" >&2; exit 1; }
|
||||
[ -b "$disk" ] || { echo "router-install: $disk is not a block device" >&2; exit 1; }
|
||||
if [ -n "$secrets" ] && [ ! -d "$secrets" ]; then
|
||||
echo "router-install: $secrets is not a directory" >&2
|
||||
exit 1
|
||||
fi
|
||||
if lsblk -nr -o MOUNTPOINTS "$disk" | grep -q .; then
|
||||
echo "router-install: $disk or one of its partitions is mounted; refusing" >&2
|
||||
exit 1
|
||||
fi
|
||||
[ -d /sys/firmware/efi ] || echo "warning: not booted in UEFI mode; the box itself must boot UEFI" >&2
|
||||
|
||||
echo "About to ERASE $disk:"
|
||||
lsblk -o NAME,SIZE,MODEL,FSTYPE,LABEL "$disk"
|
||||
if [ "$yes" -ne 1 ]; then
|
||||
printf 'Type the device name (%s) to continue: ' "$disk"
|
||||
read -r answer
|
||||
[ "$answer" = "$disk" ] || { echo "aborted" >&2; exit 1; }
|
||||
fi
|
||||
|
||||
part() {
|
||||
case $disk in
|
||||
*[0-9]) echo "''${disk}p$1" ;;
|
||||
*) echo "$disk$1" ;;
|
||||
esac
|
||||
}
|
||||
|
||||
echo ">> partitioning"
|
||||
sgdisk --zap-all "$disk" >/dev/null
|
||||
sgdisk -n 1:0:+512M -t 1:ef00 -c 1:boot -n 2:0:0 -t 2:8300 -c 2:nixos "$disk" >/dev/null
|
||||
partprobe "$disk" || true
|
||||
udevadm settle
|
||||
|
||||
echo ">> formatting"
|
||||
mkfs.fat -F 32 -n boot "$(part 1)" >/dev/null
|
||||
mkfs.ext4 -q -F -L nixos "$(part 2)"
|
||||
|
||||
echo ">> mounting"
|
||||
mkdir -p /mnt
|
||||
mount "$(part 2)" /mnt
|
||||
mkdir -p /mnt/boot
|
||||
mount -o umask=0077 "$(part 1)" /mnt/boot
|
||||
|
||||
if [ -n "$secrets" ]; then
|
||||
echo ">> copying secrets"
|
||||
install -d -m 0700 /mnt/var/lib/router
|
||||
cp -rT "$secrets" /mnt/var/lib/router
|
||||
chown -R root:root /mnt/var/lib/router
|
||||
chmod -R go-rwx /mnt/var/lib/router
|
||||
else
|
||||
echo "note: no --secrets given; put the PPPoE password etc. in /var/lib/router later" >&2
|
||||
fi
|
||||
|
||||
echo ">> installing ${cfg.system}"
|
||||
nixos-install --system ${cfg.system} --root /mnt --no-root-passwd --no-channel-copy
|
||||
|
||||
echo ">> unmounting"
|
||||
umount -R /mnt
|
||||
sync
|
||||
echo "Done. Remove the installer and reboot."
|
||||
'';
|
||||
};
|
||||
in
|
||||
{
|
||||
options.router.installer.system = lib.mkOption {
|
||||
type = lib.types.package;
|
||||
description = "The system (a `system.build.toplevel`) `router-install` writes to disk.";
|
||||
};
|
||||
|
||||
config = {
|
||||
environment.systemPackages = [ installScript ];
|
||||
# The closure must be on the installer itself.
|
||||
system.extraDependencies = [ cfg.system ];
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
# Bootable installer ISO: the live system, `router-install` and the target
|
||||
# system's closure. SSH is open to the target's root keys so secrets can be
|
||||
# copied over (`scp -r secrets root@<installer>:`) before installing.
|
||||
{
|
||||
config,
|
||||
lib,
|
||||
modulesPath,
|
||||
...
|
||||
}:
|
||||
{
|
||||
imports = [
|
||||
(modulesPath + "/installer/cd-dvd/installation-cd-minimal.nix")
|
||||
./install-script.nix
|
||||
];
|
||||
|
||||
isoImage.volumeID = "ROUTER_INSTALL";
|
||||
isoImage.squashfsCompression = "zstd -Xcompression-level 6";
|
||||
|
||||
services.openssh = {
|
||||
enable = true;
|
||||
settings.PermitRootLogin = "prohibit-password";
|
||||
};
|
||||
|
||||
users.motd = ''
|
||||
|
||||
nixos-router installer
|
||||
1. (optional) scp -r secrets root@<this-host>:secrets
|
||||
2. router-install --secrets /root/secrets /dev/<disk>
|
||||
The disk is erased after confirmation.
|
||||
'';
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
# The disk layout `router-install` creates, as a NixOS module: an ESP labelled
|
||||
# `boot` and an ext4 root labelled `nixos`. Target configurations import this so
|
||||
# the installed system finds its disks on any hardware.
|
||||
{ lib, ... }:
|
||||
{
|
||||
fileSystems."/" = {
|
||||
device = "/dev/disk/by-label/nixos";
|
||||
fsType = "ext4";
|
||||
};
|
||||
fileSystems."/boot" = {
|
||||
device = "/dev/disk/by-label/boot";
|
||||
fsType = "vfat";
|
||||
options = [ "umask=0077" ];
|
||||
};
|
||||
|
||||
boot.loader.systemd-boot.enable = true;
|
||||
# The installer may run anywhere (even a BIOS-booted stick) and the firmware
|
||||
# may be reset: boot through the removable fallback path, not NVRAM entries.
|
||||
boot.loader.efi.canTouchEfiVariables = lib.mkDefault false;
|
||||
|
||||
# Storage and USB controllers of common hardware, so the root is found on
|
||||
# an arbitrary box (and in VMs).
|
||||
boot.initrd.availableKernelModules = [
|
||||
"ahci"
|
||||
"nvme"
|
||||
"sd_mod"
|
||||
"sdhci_pci"
|
||||
"usb_storage"
|
||||
"xhci_pci"
|
||||
"virtio_blk"
|
||||
"virtio_pci"
|
||||
];
|
||||
hardware.enableRedistributableFirmware = lib.mkDefault true;
|
||||
}
|
||||
Reference in New Issue
Block a user