Files
nixos-router/modules/router/rollback.nix
T
nilsandClaude Opus 5.5 753459f5d7 Add a web UI for settings-configured routers
router-webui (Go, standard library only) edits settings.json: host
name, WAN (DHCP or PPPoE), LANs with DHCP, and the raw JSON for the
rest; keys the forms do not know are kept. It applies and confirms by
starting router-apply.service and router-confirm.service, which a
switch never restarts, and shows the build log and the rollback
countdown. HTTPS with a self-signed certificate, a PBKDF2 password
hash, CSRF tokens and an Origin check; reachable from management LANs
only (new router.managementPorts).

router-apply records the settings it built and router-confirm keeps
them as settings.confirmed.json (new rollback.onConfirm hook), so the
UI can tell unapplied changes and discard them.

checks.apply now also logs in from a LAN client, discards the change
left by the rollback, edits the DHCP pool, applies and confirms.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-28 22:43:58 +01:00

90 lines
2.6 KiB
Nix

# Commit-confirm for deployments. `router-arm` starts a timer; unless
# `router-confirm` stops it in time, `router-rollback` re-activates the last
# confirmed generation. Pair it with `nixos-rebuild test` (activate without
# changing the boot default): a reboot also comes back to the confirmed system.
{
config,
lib,
pkgs,
...
}:
let
cfg = config.router.rollback;
profile = "/nix/var/nix/profiles/system";
tools = [
pkgs.coreutils
config.systemd.package
];
# The confirmed system: the profile, or what booted if there is none.
confirmed = ''
target=${profile}
[ -e "$target/bin/switch-to-configuration" ] || target=/run/booted-system
'';
rollback = pkgs.writeShellApplication {
name = "router-rollback";
runtimeInputs = tools;
text = ''
${confirmed}
echo "router: rolling back to $(readlink -f "$target")"
exec "$target/bin/switch-to-configuration" test
'';
};
arm = pkgs.writeShellApplication {
name = "router-arm";
runtimeInputs = tools;
text = ''
timeout=''${1:-${cfg.timeout}}
systemctl stop router-rollback.timer router-rollback.service 2>/dev/null || true
systemd-run --quiet --unit=router-rollback --on-active="$timeout" \
--timer-property=AccuracySec=1s ${rollback}/bin/router-rollback
echo "router: rollback in $timeout unless confirmed (router-confirm)"
'';
};
confirm = pkgs.writeShellApplication {
name = "router-confirm";
runtimeInputs = tools;
text = ''
systemctl stop router-rollback.timer
current=$(readlink -f /run/current-system)
${config.nix.package}/bin/nix-env -p ${profile} --set "$current"
"$current/bin/switch-to-configuration" boot
${cfg.onConfirm}
echo "router: confirmed $current"
'';
};
in
{
options.router.rollback = {
enable = lib.mkOption {
type = lib.types.bool;
default = true;
description = "Install the commit-confirm helpers `router-arm`, `router-confirm` and `router-rollback`.";
};
onConfirm = lib.mkOption {
type = lib.types.lines;
default = "";
internal = true;
description = "Shell commands `router-confirm` runs once the current system is confirmed.";
};
timeout = lib.mkOption {
type = lib.types.str;
default = "5min";
example = "90s";
description = "How long a deployment may stay unconfirmed before it is rolled back (systemd time span).";
};
};
config = lib.mkIf (config.router.enable && cfg.enable) {
environment.systemPackages = [
arm
confirm
rollback
];
};
}