Allow previewing an older NixOS release with --downgrade

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01AeFqV9dZrK6nipHkPfyM4F
This commit is contained in:
2026-11-09 08:25:36 +01:00
co-authored by Claude Sonnet 5.5
parent 1efa068443
commit b37d8a0961
5 changed files with 26 additions and 14 deletions
+16 -6
View File
@@ -41,7 +41,7 @@ let
];
text = ''
usage() {
echo "usage: router-upgrade check | preview [--force] | download | apply [timeout] | --request" >&2
echo "usage: router-upgrade check | preview [--force] [--downgrade] | download | apply [timeout] | --request" >&2
echo " check the newest revision of router.update.channel, against the running one" >&2
echo " preview 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
@@ -56,12 +56,13 @@ let
[ -r "$req" ] || die "no request"
action=$(jq -er .action "$req")
force=$(jq -r 'if .force then "--force" else "" end' "$req")
downgrade=$(jq -r 'if .downgrade then "--downgrade" else "" end' "$req")
timeout=$(jq -r '.timeout // ""' "$req")
rm -f "$req"
# The web UI shows the last run (a finished unit forgets its journal).
exec > >(tee ${dir}/last.log) 2>&1
case $action in
preview) set -- preview ''${force:+"$force"} ;;
preview) set -- preview ''${force:+"$force"} ''${downgrade:+"$downgrade"} ;;
apply) set -- apply ''${timeout:+"$timeout"} ;;
check | download) set -- "$action" ;;
*) die "unknown request: $action" ;;
@@ -127,8 +128,14 @@ let
}
preview() {
local force=0
[ "''${1:-}" != --force ] || force=1
local force=0 downgrade=0 arg
for arg in "$@"; do
case $arg in
--force) force=1 ;;
--downgrade) downgrade=1 ;;
*) usage ;;
esac
done
check
rm -rf ${dir}/preview.json ${dir}/flake ${dir}/result
if [ "$same" = true ] && [ "$force" -eq 0 ]; then return 0; fi
@@ -155,8 +162,11 @@ let
# The release (26.05) of a version like 26.05.20260101.abcdef0 or 26.11pre-git.
release() { sed -E 's/^([0-9]+\.[0-9]+).*/\1/' <<<"$1"; }
newer=$(printf '%s\n%s\n' "$(release "$new_version")" "$(release "$running_version")" | sort -V | tail -1)
[ "$newer" = "$(release "$new_version")" ] \
|| die "$new_version is older than the running $running_version: NixOS cannot be downgraded"
if [ "$newer" != "$(release "$new_version")" ]; then
[ "$downgrade" -eq 1 ] \
|| die "$new_version is older than the running $running_version: NixOS cannot be downgraded safely (use --downgrade to try anyway)"
echo "router-upgrade: warning: $new_version is older than the running $running_version; state written by the newer release may not work with it"
fi
drv=$(nix eval --raw "$attr.build.toplevel.drvPath")
nix path-info -r /run/current-system | names > ${dir}/old.names