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:
+20
-131
@@ -55,16 +55,14 @@ let
|
||||
migrate
|
||||
pkgs.coreutils
|
||||
pkgs.curl
|
||||
pkgs.git
|
||||
pkgs.gawk
|
||||
pkgs.jq
|
||||
pkgs.util-linux
|
||||
];
|
||||
text = ''
|
||||
# (A release from the git repository is not installed here but by
|
||||
# router-upgrade, with the packages: this is for signed bundles.)
|
||||
usage() {
|
||||
echo "usage: router-update check | [--force] (--request | --bundle FILE | --url URL | --git TAG|latest) [timeout]" >&2
|
||||
echo " check the newest release tag of the repository, against the installed version" >&2
|
||||
echo " --git TAG build the release from a tag (vX.Y.Z, or latest) of the repository" >&2
|
||||
echo "usage: router-update [--force] (--request | --bundle FILE | --url URL) [timeout]" >&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
|
||||
@@ -76,11 +74,9 @@ 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"
|
||||
@@ -94,60 +90,12 @@ 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() { echo ${lib.escapeShellArg (toString config.routerUpdate.repository)}; }
|
||||
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: this router has no 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"
|
||||
|
||||
# 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
|
||||
trusted=$(tr '\n' ' ' < /etc/router-update-keys)
|
||||
[ -n "''${trusted// /}" ] || die "this router has no update key"
|
||||
|
||||
staged=${updateDir}/staged
|
||||
case $mode in
|
||||
@@ -164,56 +112,20 @@ let
|
||||
base=''${arg%/}
|
||||
store="$base/cache"
|
||||
latest=$(curl -fsS --max-time 30 "$base/latest") ;;
|
||||
git) ;;
|
||||
*) usage ;;
|
||||
esac
|
||||
|
||||
if [ "$mode" = git ]; then
|
||||
repo=$(repository)
|
||||
[ -n "$repo" ] || die "this router has no repository to update from"
|
||||
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"
|
||||
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"
|
||||
fi
|
||||
# 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"
|
||||
|
||||
new_version=$(cat "$release/version")
|
||||
new_serial=$(cat "$release/serial")
|
||||
@@ -274,12 +186,11 @@ in
|
||||
example = "https://git.example.org/me/nixos-router";
|
||||
description = ''
|
||||
The public git repository of nixos-router whose tags `vX.Y.Z` are
|
||||
releases. `router-update check` (daily, and in the web UI) looks for a
|
||||
newer tag, and `router-update --git` builds the router from it, like an
|
||||
update from a bundle. The repository is trusted (TLS and the tag),
|
||||
nothing is signed on this path. It belongs to the installed version, not
|
||||
to the settings: a fork or a test sets it in a module. null: updates
|
||||
come from bundles and update servers only.
|
||||
releases. `router-upgrade` (daily, and in the web UI) looks for a newer
|
||||
tag and, with the packages, locks the configuration flake to it. The
|
||||
repository is trusted (TLS and the tag), nothing is signed on this path.
|
||||
It belongs to the installed version, not to the settings: a fork or a
|
||||
test sets it in a module. null: updates come from bundles only.
|
||||
'';
|
||||
};
|
||||
routerUpdate.publicKeys = lib.mkOption {
|
||||
@@ -321,28 +232,6 @@ in
|
||||
router.rollback.onRollback = "${restore}/bin/router-update-restore";
|
||||
router.rollback.onConfirm = "rm -rf ${snapshot}";
|
||||
|
||||
# Is there a newer release in the 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;
|
||||
|
||||
Reference in New Issue
Block a user