One upgrade for nixos-router and the packages

Check, preview and apply look at the repository's release tags and the
channel together; the new nixos-router is a highlighted row in the
preview table. The update server section and the separate git update
are gone; bundles stay.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-25 11:49:57 +01:00
co-authored by Claude Sonnet 5.5
parent 3e6eee0b9c
commit 2b7cd86e98
15 changed files with 372 additions and 695 deletions
+162 -40
View File
@@ -30,23 +30,29 @@ let
up = cfg.upgrade;
snapshot = "${cfg.stateDirectory}/config.pre-update";
dir = "${cfg.stateDirectory}/upgrade";
migrate = pkgs.callPackage ../../pkgs/router-migrate.nix { };
repository = toString config.routerUpdate.repository;
upgrade = pkgs.writeShellApplication {
name = "router-upgrade";
runtimeInputs = [
config.nix.package
config.systemd.package
migrate
pkgs.coreutils
pkgs.curl
pkgs.gawk
pkgs.git
pkgs.jq
pkgs.util-linux
];
text = ''
usage() {
echo "usage: router-upgrade check | releases | preview [--force] [--downgrade] | download | apply [timeout] | --request" >&2
echo " check the newest revision of router.update.channel, against the running one" >&2
echo " releases which stable NixOS releases the channel server has (a timer runs it daily)" >&2
echo " preview the packages that change and the size of the download (nothing is fetched)" >&2
echo "usage: router-upgrade check | daily | releases | preview [--force] [--downgrade] | download | apply [timeout] | --request" >&2
echo " check the newest nixpkgs revision of router.update.channel and the newest release tag of nixos-router, against the running ones" >&2
echo " daily check, quietly (a timer runs it)" >&2
echo " releases which stable NixOS releases the channel server has" >&2
echo " preview the new nixos-router and the packages that change, and the size of the download (nothing is fetched)" >&2
echo " download fetch and build what was previewed, without activating it" >&2
echo " apply activate what was previewed (fetching it if need be), with commit-confirm" >&2
exit 2
@@ -78,12 +84,15 @@ let
config=${cfg.directory}
mkdir -p ${dir}
# nixos-router comes from this repository (its tags vX.Y.Z); the packages
# (nixpkgs) from the channel, if one is followed. Both are inputs of the
# configuration flake, and one lock moves them together.
repository=${lib.escapeShellArg repository}
channel=""
branch=""
if [ "$cmd" != releases ]; then
channel=$(jq -r '.router.update.channel // empty' "$config/settings.json")
[ -n "$channel" ] || die "no channel: set router.update.channel (unstable, or a release like 26.05)"
if [ "$channel" = unstable ]; then branch=nixos-unstable; else branch="nixos-$channel"; fi
if [ "$channel" = unstable ]; then branch=nixos-unstable; elif [ -n "$channel" ]; then branch="nixos-$channel"; fi
fi
running_rev=$(/run/current-system/sw/bin/nixos-version --revision || true)
@@ -121,20 +130,64 @@ let
echo "releases on the channel server: ''${found[*]:-none}"
}
# The release tags of the repository, "TAG REVISION" per line, oldest first (an
# annotated tag counts with the commit it points to).
git_tags() {
git ls-remote --tags "$1" 'refs/tags/v*' | awk '
{ n = $2; sub("^refs/tags/", "", n); peeled = (n ~ /\^\{\}$/); sub(/\^\{\}$/, "", n)
if (n !~ /^v[0-9]+\.[0-9]+\.[0-9]+$/) next
if (peeled) p[n] = $1; else d[n] = $1
seen[n] = 1 }
END { for (n in seen) print n, (n in p ? p[n] : d[n]) }' | sort -V
}
installed_router=$(jq -r '.version // "unknown"' /etc/router-release 2>/dev/null || echo unknown)
r_tag="" r_rev="" r_version="" r_newer=false
check() {
releases || true
newest=$(newest_rev)
if [ "$newest" = "$running_rev" ]; then same=true; else same=false; fi
local packages_same=true router_json=null tags
newest=""
if [ -n "$channel" ]; then
newest=$(newest_rev)
if [ "$newest" != "$running_rev" ]; then packages_same=false; fi
fi
if [ -n "$repository" ]; then
# (Not fatal: the packages can still be looked at.)
if tags=$(git_tags "$repository" 2>/dev/null) && [ -n "$tags" ]; then
read -r r_tag r_rev <<<"$(tail -n 1 <<<"$tags")"
r_version=''${r_tag#v}
if [ "$r_version" != "$installed_router" ] \
&& [ "$(printf '%s\n%s\n' "$installed_router" "$r_version" | sort -V | tail -n 1)" = "$r_version" ]; then
r_newer=true
fi
router_json=$(jq -n --arg repo "$repository" --arg installed "$installed_router" --arg tag "$r_tag" \
--arg version "$r_version" --arg rev "$r_rev" --argjson newer "$r_newer" \
'{repository: $repo, installed: $installed, newest: {tag: $tag, version: $version, rev: $rev}, newer: $newer}')
else
echo "router-upgrade: cannot read the release tags of $repository" >&2
fi
fi
[ -n "$channel" ] || [ "$router_json" != null ] \
|| die "nothing to look at: no channel is set (router.update.channel: unstable, or a release like 26.05) and the repository cannot be read"
if [ "$packages_same" = true ] && [ "$r_newer" = false ]; then same=true; else same=false; fi
jq -n --arg channel "$channel" --arg branch "$branch" --arg time "$(date -u +%FT%TZ)" \
--arg running "$running_rev" --arg version "$running_version" --arg newest "$newest" \
--argjson upToDate "$same" \
--argjson packagesUpToDate "$packages_same" --argjson upToDate "$same" --argjson router "$router_json" \
'{channel: $channel, branch: $branch, time: $time, running: {rev: $running, version: $version},
newest: {rev: $newest}, upToDate: $upToDate}' > ${dir}/check.json.new
newest: {rev: $newest}, packagesUpToDate: $packagesUpToDate, upToDate: $upToDate, router: $router}' > ${dir}/check.json.new
mv ${dir}/check.json.new ${dir}/check.json
echo "channel $channel ($branch)"
echo " running $running_version"
echo " newest $(short "$newest")"
if [ "$same" = true ]; then echo "up to date"; else echo "a newer revision is available"; fi
if [ "$router_json" != null ]; then
echo "nixos-router ($repository)"
echo " installed $installed_router"
echo " newest $r_version"
fi
if [ -n "$channel" ]; then
echo "channel $channel ($branch)"
echo " running $running_version"
echo " newest $(short "$newest")"
fi
if [ "$same" = true ]; then echo "up to date"; else echo "a newer version is available"; fi
}
# The names (with versions) of store paths, one per line.
@@ -171,24 +224,71 @@ let
esac
done
check
rm -rf ${dir}/preview.json ${dir}/flake ${dir}/result
rm -rf ${dir}/preview.json ${dir}/flake ${dir}/base ${dir}/git-release ${dir}/result
if [ "$same" = true ] && [ "$force" -eq 0 ]; then return 0; fi
mkdir ${dir}/flake
mkdir ${dir}/flake ${dir}/base
for f in flake.nix settings.json local.nix flake.lock; do
if [ -e "$config/$f" ]; then cp "$config/$f" ${dir}/flake/; fi
done
ref="${up.nixpkgsRef}"
ref=''${ref//%rev%/$newest}
echo "router-upgrade: locking nixpkgs to $(short "$newest")"
nix flake lock --override-input nixos-router/nixpkgs "$ref" "path:${dir}/flake"
node=$(jq -r '.nodes[.nodes[.root].inputs["nixos-router"]].inputs.nixpkgs' ${dir}/flake/flake.lock)
hash=$(jq -er --arg n "$node" '.nodes[$n].locked.narHash' ${dir}/flake/flake.lock)
case $ref in
*narHash=*) pin=$ref ;;
*\?*) pin="$ref&narHash=$hash" ;;
*) pin="$ref?narHash=$hash" ;;
esac
# What the directory was like: apply refuses to go on when it changed.
cp "$config/flake.nix" "$config/settings.json" ${dir}/base/
# A newer nixos-router is one more input to lock: its source, from the tag, and
# the flake it brings, which names that source (config-flake.nix, with the
# address filled in), and the settings migrated to its schema.
router_new=false
if [ "$r_newer" = true ]; then
src="git+$repository?ref=refs/tags/$r_tag&rev=$r_rev"
echo "router-upgrade: fetching nixos-router $r_version ($(short "$r_rev"))"
# (No registry: the address is complete, and the global one is a download.)
meta=$(nix --option flake-registry "" flake prefetch --json "$src") || die "cannot fetch $src"
tree=$(jq -er .storePath <<<"$meta")
if [ ! -r "$tree/config-flake.nix" ] || [ ! -r "$tree/VERSION" ]; then
die "$r_tag is not a release this updater can build (no config-flake.nix or VERSION)"
fi
# What a release carries, made from the source: what the migration reads.
rel=${dir}/git-release
mkdir -p "$rel/migrations"
tr -d '\n' < "$tree/VERSION" > "$rel/version"
jq -er .locked.lastModified <<<"$meta" | tr -d '\n' > "$rel/serial"
printf %s "$r_rev" > "$rel/rev"
find "$tree/migrations" -name '*.jq' -exec cp {} "$rel/migrations/" \;
find "$rel/migrations" -name '*.jq' | wc -l | tr -d '\n' > "$rel/schema"
# (jq, not a shell substitution: the address has a & in it.)
jq -rRs --arg source "$src" 'gsub("@SOURCE@"; $source)' "$tree/config-flake.nix" > ${dir}/flake/flake.nix
rm -f ${dir}/flake/flake.lock
router-migrate-settings "$rel" ${dir}/flake/settings.json || die "migrating the settings to nixos-router $r_version failed"
router_new=true
fi
# The packages: the channel's revision; without a channel the one an earlier
# upgrade chose stays, else the nixos-router's own.
ref=""
pin=""
if [ -n "$channel" ]; then
ref="${up.nixpkgsRef}"
ref=''${ref//%rev%/$newest}
elif [ -r "$config/nixpkgs-pin.json" ]; then
ref=$(jq -r .ref "$config/nixpkgs-pin.json")
fi
lock=(nix flake lock)
if [ -n "$ref" ]; then lock+=(--override-input nixos-router/nixpkgs "$ref"); fi
if [ "$router_new" = true ]; then
echo "router-upgrade: locking nixos-router $r_version and the packages"
else
echo "router-upgrade: locking the packages''${newest:+ to $(short "$newest")}"
fi
"''${lock[@]}" "path:${dir}/flake"
if [ -n "$channel" ]; then
node=$(jq -r '.nodes[.nodes[.root].inputs["nixos-router"]].inputs.nixpkgs' ${dir}/flake/flake.lock)
hash=$(jq -er --arg n "$node" '.nodes[$n].locked.narHash' ${dir}/flake/flake.lock)
case $ref in
*narHash=*) pin=$ref ;;
*\?*) pin="$ref&narHash=$hash" ;;
*) pin="$ref?narHash=$hash" ;;
esac
fi
attr="path:${dir}/flake#nixosConfigurations.router.config.system"
echo "router-upgrade: evaluating the new system"
@@ -224,13 +324,19 @@ let
sed 's/^/ /' ${dir}/builds.txt | head -n 60
if [ "$(wc -l < ${dir}/builds.txt)" -gt 60 ]; then echo " ... and $(( $(wc -l < ${dir}/builds.txt) - 60 )) more"; fi
release_version=$(jq -r '.version // ""' /etc/router-release 2>/dev/null || true)
jq -n --arg channel "$channel" --arg release "$release_version" --arg rev "$newest" --arg ref "$pin" --arg version "$new_version" \
--arg time "$(date -u +%FT%TZ)" --arg download "$download" --argjson builds "$builds" \
# What changes in nixos-router itself, if it does: shown above the packages.
router_change=null
if [ "$router_new" = true ]; then
router_change=$(jq -n --arg from "$installed_router" --arg to "$r_version" --arg tag "$r_tag" --arg rev "$r_rev" \
'{from: $from, to: $to, tag: $tag, rev: $rev}')
echo " nixos-router: $installed_router -> $r_version"
fi
jq -n --arg channel "$channel" --arg release "$installed_router" --arg rev "$newest" --arg ref "$pin" --arg version "$new_version" \
--arg time "$(date -u +%FT%TZ)" --arg download "$download" --argjson builds "$builds" --argjson router "$router_change" \
--argjson reboot "$reboot" --slurpfile changes ${dir}/changes.json --rawfile buildNames ${dir}/builds.txt \
'{channel: $channel, release: $release, rev: $rev, ref: $ref, version: $version, time: $time, download: $download,
builds: $builds, buildNames: ($buildNames | split("\n") | map(select(. != "")) | .[:300]),
rebootNeeded: $reboot, changes: $changes[0]}' > ${dir}/preview.json
rebootNeeded: $reboot, router: $router, changes: $changes[0]}' > ${dir}/preview.json
rm -f ${dir}/builds.txt
echo "$new_version$([ "$reboot" = false ] || echo ' (a new kernel runs after a reboot)'): router-upgrade apply activates it"
}
@@ -251,18 +357,31 @@ let
exec 9>/run/router-update.lock
flock -n 9 || die "an update or upgrade is running"
[ ! -d ${snapshot} ] || die "an earlier update or upgrade is not confirmed yet"
cmp -s "$config/flake.nix" ${dir}/flake/flake.nix \
cmp -s "$config/flake.nix" ${dir}/base/flake.nix \
|| die "the configuration flake changed since it was previewed: preview again"
# A new nixos-router brings migrated settings: they are taken over whole.
router_to=$(jq -r '.router.to // ""' ${dir}/preview.json)
if [ -n "$router_to" ]; then
cmp -s "$config/settings.json" ${dir}/base/settings.json \
|| die "the settings changed since the upgrade was previewed: preview again"
fi
cp -a "$config" ${snapshot}
fail() {
echo "router-upgrade: $*" >&2
/run/current-system/sw/bin/router-update-restore
exit 1
}
if [ -n "$router_to" ]; then
cp ${dir}/flake/flake.nix "$config/flake.nix"
cp ${dir}/flake/settings.json "$config/settings.json"
fi
cp ${dir}/flake/flake.lock "$config/flake.lock"
jq '{channel, rev, ref}' ${dir}/preview.json > "$config/nixpkgs-pin.json"
# The packages that were chosen stay chosen when nixos-router is updated later.
if [ -n "$(jq -r '.ref // ""' ${dir}/preview.json)" ]; then
jq '{channel, rev, ref}' ${dir}/preview.json > "$config/nixpkgs-pin.json"
fi
echo "router-upgrade: building and activating $(jq -r .version ${dir}/preview.json)"
echo "router-upgrade: building and activating ''${router_to:+nixos-router $router_to on }$(jq -r .version ${dir}/preview.json)"
if ! router-apply ''${timeout:+"$timeout"}; then
if systemctl is-active --quiet router-rollback.timer; then
# Activation failed half way: the rollback restores the configuration.
@@ -271,7 +390,7 @@ let
fi
fail "the upgrade failed; the previous system is back"
fi
rm -rf ${dir}/preview.json ${dir}/flake ${dir}/result
rm -rf ${dir}/preview.json ${dir}/flake ${dir}/base ${dir}/git-release ${dir}/result
if systemctl is-active --quiet router-rollback.timer; then
echo "router-upgrade: active; confirm it (router-confirm, or in the web UI) or the previous system comes back"
else
@@ -281,6 +400,8 @@ let
case $cmd in
check) check ;;
# For the timer: a router with nothing to look at, or no internet, is no failure.
daily) check || echo "router-upgrade: nothing was looked at" >&2 ;;
releases) releases ;;
preview) preview "$@" ;;
download) download ;;
@@ -310,15 +431,16 @@ in
# The web UI starts this rather than running anything itself, so a switch
# that restarts the UI does not cut an upgrade short; a switch must not
# restart or stop it either (router-apply is what runs the switch).
# Which stable releases exist, once a day: the web UI says when a newer one than
# the followed channel appears. It only asks the channel server and writes a small
# file, so it is a unit of its own that never holds up an upgrade.
# Is there something newer (a nixos-router release, a nixpkgs revision of the
# channel, a NixOS release), once a day: the web UI says so on the Overview. It
# only asks the servers and writes small files, so it is a unit of its own that
# never holds up an upgrade.
systemd.services.router-upgrade-notice = {
description = "Look for new NixOS releases";
description = "Look for newer versions of nixos-router and NixOS";
path = [ "/run/current-system/sw" ];
serviceConfig = {
Type = "oneshot";
ExecStart = "${lib.getExe upgrade} releases";
ExecStart = "${lib.getExe upgrade} daily";
TimeoutStartSec = "5min";
};
};