Update from a git repository

router.update.repository names a public repository whose tags vX.Y.Z are
releases. router-update check (daily, and from the Updates page) looks for a
newer one and the Overview says so; router-update --git builds the router
from a tag like a bundle update: the settings are migrated, the new version
must be confirmed, the nixpkgs pin stays. The flake of such a router comes
from the release's config-flake.nix. The global flake registry is off.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-25 08:57:00 +01:00
co-authored by Claude Sonnet 5.5
parent a2dbeb5dea
commit 3fc54e678e
13 changed files with 589 additions and 26 deletions
+131 -13
View File
@@ -55,12 +55,16 @@ let
migrate
pkgs.coreutils
pkgs.curl
pkgs.git
pkgs.gawk
pkgs.jq
pkgs.util-linux
];
text = ''
usage() {
echo "usage: router-update [--force] (--request | --bundle FILE | --url URL) [timeout]" >&2
echo "usage: router-update check | [--force] (--request | --bundle FILE | --url URL | --git TAG|latest) [timeout]" >&2
echo " check the newest release tag of router.update.repository, against the installed version" >&2
echo " --git TAG build the release from a tag (vX.Y.Z, or latest) of router.update.repository" >&2
echo " --bundle FILE a bundle made by router-release" >&2
echo " --url URL a server with the release's latest and cache/ (HTTP or HTTPS)" >&2
echo " --request what the web UI asked for (${updateDir}/request.json)" >&2
@@ -72,9 +76,11 @@ let
force=0 mode="" arg="" timeout=""
while [ $# -gt 0 ]; do
case $1 in
check) mode=check ;;
--force) force=1 ;;
--bundle) shift; [ $# -gt 0 ] || usage; mode=bundle arg=$1 ;;
--url) shift; [ $# -gt 0 ] || usage; mode=url arg=$1 ;;
--git) shift; [ $# -gt 0 ] || usage; mode=git arg=$1 ;;
--request)
req=${updateDir}/request.json
[ -r "$req" ] || die "no request"
@@ -88,11 +94,60 @@ let
done
[ -n "$mode" ] || usage
[ "$(id -u)" -eq 0 ] || die "must run as root"
config=${cfg.directory}
# The release tags of the repository, "TAG REVISION" per line (an
# annotated tag counts with the commit it points to).
repository() { jq -r '.router.update.repository // empty' "$config/settings.json"; }
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
}
if [ "$mode" = check ]; then
# Only looks: no lock, so it runs beside an update.
repo=$(repository)
if [ -z "$repo" ]; then
echo "router-update: no repository (router.update.repository): nothing to look at"
exit 0
fi
tags=$(git_tags "$repo") || die "cannot read the tags of $repo"
[ -n "$tags" ] || die "$repo has no release tags (vX.Y.Z)"
read -r tag rev <<<"$(tail -n 1 <<<"$tags")"
version=''${tag#v}
installed=$(jq -r '.version // "unknown"' /etc/router-release)
newer=false
if [ "$version" != "$installed" ] && [ "$(printf '%s\n%s\n' "$installed" "$version" | sort -V | tail -n 1)" = "$version" ]; then
newer=true
fi
mkdir -p ${updateDir}
jq -n --arg repo "$repo" --arg time "$(date -u +%FT%TZ)" --arg installed "$installed" \
--arg tag "$tag" --arg version "$version" --arg rev "$rev" --argjson newer "$newer" \
'{repository: $repo, time: $time, installed: $installed, newest: {tag: $tag, version: $version, rev: $rev}, newer: $newer}' \
> ${updateDir}/check.json.new
chmod 0644 ${updateDir}/check.json.new
mv ${updateDir}/check.json.new ${updateDir}/check.json
if [ "$newer" = true ]; then
echo "router-update: $version is available (installed: $installed)"
else
echo "router-update: up to date ($installed; the repository's newest is $version)"
fi
exit 0
fi
exec 9>/run/router-update.lock
flock -n 9 || die "another update is running"
trusted=$(tr '\n' ' ' < /etc/router-update-keys)
[ -n "''${trusted// /}" ] || die "this router has no update key"
# Bundles and servers are signed; a git repository is trusted as it is.
trusted=""
if [ "$mode" != git ]; then
trusted=$(tr '\n' ' ' < /etc/router-update-keys)
[ -n "''${trusted// /}" ] || die "this router has no update key"
fi
staged=${updateDir}/staged
case $mode in
@@ -109,20 +164,56 @@ let
base=''${arg%/}
store="$base/cache"
latest=$(curl -fsS --max-time 30 "$base/latest") ;;
git) ;;
*) usage ;;
esac
release=$(jq -er .release <<<"$latest") || die "the update has no release"
[[ $release =~ ^/nix/store/[0-9a-z]{32}-[A-Za-z0-9+._-]+$ ]] || die "not a store path: $release"
if [ "$mode" = git ]; then
repo=$(repository)
[ -n "$repo" ] || die "no repository: set router.update.repository"
tags=$(git_tags "$repo") || die "cannot read the tags of $repo"
if [ -z "$arg" ] || [ "$arg" = latest ]; then
found=$(tail -n 1 <<<"$tags")
else
[[ $arg =~ ^v[0-9]+\.[0-9]+\.[0-9]+$ ]] || die "not a release tag: $arg"
found=$(awk -v t="$arg" '$1 == t' <<<"$tags")
fi
[ -n "$found" ] || die "$repo has no such release tag"
read -r tag rev <<<"$found"
src="git+$repo?ref=refs/tags/$tag&rev=$rev"
echo "router-update: fetching $tag ($rev) from $repo"
# (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 "$tag is not a release this updater can build (no config-flake.nix or VERSION)"
fi
# What a release carries, made from the source: the files that the rest
# of the update reads (the flake that points at the source, its version,
# its commit time, its settings migrations).
release=${updateDir}/git-release
rm -rf "$release"
mkdir -p "$release/migrations"
tr -d '\n' < "$tree/VERSION" > "$release/version"
jq -er .locked.lastModified <<<"$meta" | tr -d '\n' > "$release/serial"
printf %s "$rev" > "$release/rev"
find "$tree/migrations" -name '*.jq' -exec cp {} "$release/migrations/" \;
find "$release/migrations" -name '*.jq' | wc -l | tr -d '\n' > "$release/schema"
# (jq, not a shell substitution: the address has a & in it.)
jq -rRs --arg source "$src" 'gsub("@SOURCE@"; $source)' "$tree/config-flake.nix" > "$release/flake.nix"
else
release=$(jq -er .release <<<"$latest") || die "the update has no release"
[[ $release =~ ^/nix/store/[0-9a-z]{32}-[A-Za-z0-9+._-]+$ ]] || die "not a store path: $release"
# Nothing is trusted until every path of the closure is signed by our key.
echo "router-update: verifying $release"
nix --option trusted-public-keys "$trusted" store verify \
--store "$store" --no-contents --sigs-needed 1 -r "$release" \
|| die "the update is not signed with this router's update key"
echo "router-update: importing it"
nix --option trusted-public-keys "$trusted" copy --from "$store" "$release"
rm -rf "$staged"
# Nothing is trusted until every path of the closure is signed by our key.
echo "router-update: verifying $release"
nix --option trusted-public-keys "$trusted" store verify \
--store "$store" --no-contents --sigs-needed 1 -r "$release" \
|| die "the update is not signed with this router's update key"
echo "router-update: importing it"
nix --option trusted-public-keys "$trusted" copy --from "$store" "$release"
rm -rf "$staged"
fi
new_version=$(cat "$release/version")
new_serial=$(cat "$release/serial")
@@ -214,6 +305,28 @@ in
router.rollback.onRollback = "${restore}/bin/router-update-restore";
router.rollback.onConfirm = "rm -rf ${snapshot}";
# Is there a newer release in router.update.repository? Once a day, and when
# the web UI asks. It only reads the repository's tag list.
systemd.services.router-update-check = {
description = "Look for a newer release of nixos-router";
restartIfChanged = false;
stopIfChanged = false;
path = [ "/run/current-system/sw" ];
serviceConfig = {
Type = "oneshot";
ExecStart = "${lib.getExe update} check";
TimeoutStartSec = "5min";
};
};
systemd.timers.router-update-check = {
wantedBy = [ "timers.target" ];
timerConfig = {
OnCalendar = "daily";
RandomizedDelaySec = "1h";
Persistent = true;
};
};
systemd.services.router-update = {
description = "Install a router update";
restartIfChanged = false;
@@ -226,6 +339,11 @@ in
};
};
# The router's flakes name their inputs in full; nix would otherwise try to
# download the global registry (channels.nixos.org) for a git input and
# fail, slowly, without the internet.
nix.settings.flake-registry = lib.mkDefault "";
# Old generations (and the updates' leftovers) would fill the disk.
nix.gc = {
automatic = true;