diff --git a/SPECS.md b/SPECS.md index c1e0178..d101187 100644 --- a/SPECS.md +++ b/SPECS.md @@ -224,7 +224,9 @@ verity. The image module and its test (`checks.image`) stay as they are for anyo ## Package upgrades (`router-upgrade`, 2026-11-08) -Upgrading nixpkgs (the kernel and every package) on the router, without a new release of nixos-router. Step 1 (CLI and setting) and step 2 (web UI page) are done; the notice of a new NixOS release follows. +Upgrading nixpkgs (the kernel and every package) on the router, without a new release of nixos-router. Step 1 (CLI and setting) and step 2 (web UI page) and step 3 (the notice of a new NixOS release) are done. + +- **Releases** (`router-upgrade releases`, daily timer `router-upgrade-notice`, and every `check`): asks the channel server whether `nixos-YY.05` and `nixos-YY.11` exist (last year to next year; 200 = exists, 404 = not yet) and writes `/var/lib/router/upgrade/releases.json`. With no answer at all (no internet) the file is left as it was. The web UI offers the newest two as channels (the built-in list until the file exists), says on the Overview and the Packages page when a stable release newer than the followed one is out (only when a stable release is followed: unstable has no newer one), and has a button that follows it (saving the channel changes nothing on the router). Not done: a note that a release is not tested with this version of nixos-router. - **Setting** `router.update.channel`: `unstable` or a release like `26.05` (branch `nixos-26.05`); null: the packages that came with the installed release. Read from the saved `settings.json` when upgrading; applying settings does not change nixpkgs. - **`router-upgrade check`**: the branch's newest revision from `//git-revision` (a redirect to releases.nixos.org, so curl follows it; the last revision Hydra's tests passed), against the running one (`nixos-version --revision`). Writes `/var/lib/router/upgrade/check.json`. diff --git a/modules/settings/upgrade.nix b/modules/settings/upgrade.nix index eca174c..1a511da 100644 --- a/modules/settings/upgrade.nix +++ b/modules/settings/upgrade.nix @@ -6,6 +6,8 @@ # flake.lock, so an upgrade is a new lock: # # router-upgrade check what the channel's newest revision is, against the running one +# router-upgrade releases which stable releases exist (nixos-26.05, nixos-26.11, ...), so the +# web UI can say when one newer than the followed channel appears # router-upgrade preview lock a copy of the configuration flake to it and evaluate the # system: which packages change version, how much would be # downloaded and built (nothing is downloaded or activated) @@ -41,8 +43,9 @@ let ]; text = '' usage() { - echo "usage: router-upgrade check | preview [--force] [--downgrade] | download | apply [timeout] | --request" >&2 + 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 " 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 @@ -75,9 +78,13 @@ let config=${cfg.directory} mkdir -p ${dir} - 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 + 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 + fi running_rev=$(/run/current-system/sw/bin/nixos-version --revision || true) running_version=$(cat /run/current-system/nixos-version) @@ -92,7 +99,30 @@ let echo "$rev" } + # The stable releases the channel server has (nixos-YY.05 and nixos-YY.11, from last + # year to next year), written to ${dir}/releases.json. A release that is not there yet + # answers 404. If nothing answers (no internet) the list is left as it was. + releases() { + local yy m r code answered=0 + local found=() + for yy in $(( 10#$(date +%y) - 1 )) $(( 10#$(date +%y) )) $(( 10#$(date +%y) + 1 )); do + for m in 05 11; do + r="$yy.$m" + code=$(curl -sL --max-time 20 -o /dev/null -w '%{http_code}' "${up.channelsUrl}/nixos-$r/git-revision" || true) + case $code in + 200) found+=("$r"); answered=1 ;; + 404) answered=1 ;; + esac + done + done + [ "$answered" -eq 1 ] || return 0 + jq -n --arg time "$(date -u +%FT%TZ)" '{time: $time, releases: $ARGS.positional}' --args "''${found[@]}" > ${dir}/releases.json.new + mv ${dir}/releases.json.new ${dir}/releases.json + echo "releases on the channel server: ''${found[*]:-none}" + } + check() { + releases || true newest=$(newest_rev) if [ "$newest" = "$running_rev" ]; then same=true; else same=false; fi jq -n --arg channel "$channel" --arg branch "$branch" --arg time "$(date -u +%FT%TZ)" \ @@ -251,6 +281,7 @@ let case $cmd in check) check ;; + releases) releases ;; preview) preview "$@" ;; download) download ;; apply) apply "$@" ;; @@ -279,6 +310,27 @@ 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. + systemd.services.router-upgrade-notice = { + description = "Look for new NixOS releases"; + path = [ "/run/current-system/sw" ]; + serviceConfig = { + Type = "oneshot"; + ExecStart = "${lib.getExe upgrade} releases"; + TimeoutStartSec = "5min"; + }; + }; + systemd.timers.router-upgrade-notice = { + wantedBy = [ "timers.target" ]; + timerConfig = { + OnCalendar = "daily"; + RandomizedDelaySec = "1h"; + Persistent = true; + }; + }; + systemd.services.router-upgrade = { description = "Look for, prepare or apply a nixpkgs upgrade"; restartIfChanged = false; diff --git a/pkgs/router-webui/main.go b/pkgs/router-webui/main.go index aa8ce06..00abf3c 100644 --- a/pkgs/router-webui/main.go +++ b/pkgs/router-webui/main.go @@ -488,14 +488,15 @@ func (a *app) logout(w http.ResponseWriter, r *http.Request, _ session) { } type overviewData struct { - Status Status - Dashboard dashboard + Status Status + Dashboard dashboard + NewRelease string // a stable NixOS release newer than the followed channel exists } func (a *app) overview(w http.ResponseWriter, r *http.Request, sess session) { st := a.sys.Status() p := page{Title: "Overview", CSRF: sess.csrf, LoggedIn: true, Flash: r.URL.Query().Get("msg"), - Data: overviewData{Status: st, Dashboard: a.dashboardData(r.URL.Query().Get("range"))}} + Data: overviewData{Status: st, Dashboard: a.dashboardData(r.URL.Query().Get("range")), NewRelease: a.releaseNotice()}} switch { case st.Applying || st.ConfirmRunning: p.Live = 2 diff --git a/pkgs/router-webui/static/style.css b/pkgs/router-webui/static/style.css index fedc7c3..d8644d5 100644 --- a/pkgs/router-webui/static/style.css +++ b/pkgs/router-webui/static/style.css @@ -51,7 +51,7 @@ main { padding: 1.5rem clamp(1rem, 3vw, 3rem) 3rem; } /* Pages of independent panels flow into up to three columns on wide screens */ .cols { columns: 38rem 3; column-gap: 1.5rem; } .cols > section, .cols > fieldset, .cols > form > fieldset { break-inside: avoid; } -.cols > section.busy, .cols > section.pending { column-span: all; } +.cols > section.busy, .cols > section.pending, .cols > section.notice { column-span: all; } /* A fold of its own (a log) sits below the columns: opening it must not make them balance again. */ .cols > details { column-span: all; break-inside: avoid; margin-top: 1rem; } .menu-toggle { position: absolute; opacity: 0; pointer-events: none; } @@ -60,6 +60,9 @@ h1 { font-size: 1.45rem; font-weight: 600; letter-spacing: -.01em; margin: .25re h2 { font-size: .95rem; font-weight: 600; margin: 0 0 .6rem; } section { margin: 0 0 1.5rem; } section.busy, section.pending { padding: .75rem 1rem; border-radius: var(--radius); background: var(--warn); } +section.notice { padding: .75rem 1rem; border-radius: var(--radius); border-left: 4px solid var(--accent); background: color-mix(in srgb, var(--accent) 10%, var(--surface)); } +section.notice h2 { margin-top: 0; } +section.notice p { margin: .4rem 0; } .upload-overlay { position: fixed; inset: 0; z-index: 100; display: flex; align-items: center; justify-content: center; background: rgba(0, 0, 0, .55); padding: 1rem; } .upload-card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: 0 8px 32px rgba(0, 0, 0, .35); padding: 1.25rem 1.5rem; width: min(30rem, 100%); } /* A heading that explains itself when the mouse rests on it. */ diff --git a/pkgs/router-webui/templates/overview.html b/pkgs/router-webui/templates/overview.html index 0f794ca..0f30955 100644 --- a/pkgs/router-webui/templates/overview.html +++ b/pkgs/router-webui/templates/overview.html @@ -22,6 +22,9 @@ {{end}} +{{with .Data.NewRelease}} +

NixOS {{.}} is out. See what it would change.

+{{end}}
{{template "dashboard" .}} diff --git a/pkgs/router-webui/templates/upgrade.html b/pkgs/router-webui/templates/upgrade.html index c73e2cb..16f2df7 100644 --- a/pkgs/router-webui/templates/upgrade.html +++ b/pkgs/router-webui/templates/upgrade.html @@ -18,11 +18,25 @@ + {{if $d.ReleasesSeen}}

Stable releases as the channel server listed them on {{$d.ReleasesSeen}}; Check for upgrades asks again.

{{end}}

The channel only chooses what an upgrade offers; saving it changes nothing on the router. Going back to an older release (unstable to 26.05) is refused unless you tick "downgrade anyway" when previewing; state written by the newer release may not work with the older one.

+{{with $d.NewerRelease}} +
+

NixOS {{.}} is out

+

You follow {{$d.Channel}}. {{.}} is the newer stable release: switch the channel to it, then look for upgrades and + preview what would change. Nothing is installed until you upgrade, and NixOS cannot go back to the older release afterwards.

+
+ + + +
+
+{{end}} + {{if $d.Updating}}

Updating…

The router is installing a new version of nixos-router.

{{else if $d.Run.Running}} diff --git a/pkgs/router-webui/upgrade.go b/pkgs/router-webui/upgrade.go index 483344d..1d0f116 100644 --- a/pkgs/router-webui/upgrade.go +++ b/pkgs/router-webui/upgrade.go @@ -9,6 +9,7 @@ import ( "path/filepath" "regexp" "slices" + "strconv" "strings" ) @@ -150,29 +151,106 @@ var upgradeTitles = map[string]string{ type channelChoice struct{ Value, Label string } type upgradeData struct { - Version string // the running NixOS version - Channel string - Choices []channelChoice - Check *UpgradeCheck - Preview *UpgradePreview - Run UpgradeRun - Title string // what the running step does - Updating bool // router-update is running - Pending bool // a system waits for confirmation + Version string // the running NixOS version + Channel string + Choices []channelChoice + Check *UpgradeCheck + Preview *UpgradePreview + Run UpgradeRun + Title string // what the running step does + + NewerRelease string // a stable release newer than the followed channel exists + ReleasesSeen string // when the channel server was last asked which exist + Updating bool // router-update is running + Pending bool // a system waits for confirmation } // stableChannels are the stable NixOS releases the page offers (the branch -// nixos-26.05 and so on). They are listed here until the router can ask the -// channel server which exist; the release a router runs says nothing about it: -// nixos-unstable already calls itself the release that comes next. +// nixos-26.05 and so on) until the router has asked the channel server which +// exist (router-upgrade releases, daily); the release a router runs says +// nothing about it: nixos-unstable already calls itself the release that comes +// next. var stableChannels = []string{"26.05"} +// ReleaseList is releases.json: the stable releases the channel server had. +type ReleaseList struct { + Time string `json:"time"` + Releases []string `json:"releases"` +} + +var releaseNameRe = regexp.MustCompile(`^[0-9]{2}\.[0-9]{2}$`) + +// releaseCmp orders releases like 25.11 and 26.05 (and 100.05) by number. +func releaseCmp(a, b string) int { + pa, pb := strings.SplitN(a, ".", 2), strings.SplitN(b, ".", 2) + for i := range pa { + if i >= len(pb) { + return 1 + } + x, _ := strconv.Atoi(pa[i]) + y, _ := strconv.Atoi(pb[i]) + if x != y { + return x - y + } + } + if len(pb) > len(pa) { + return -1 + } + return 0 +} + +// seenReleases are the stable releases the channel server last answered with, +// oldest first; none if it has not been asked yet. +func (s System) seenReleases() ReleaseList { + var l ReleaseList + readUpgradeFile(filepath.Join(s.upgradeDir(), "releases.json"), &l) + var ok []string + for _, r := range l.Releases { + if releaseNameRe.MatchString(r) { + ok = append(ok, r) + } + } + slices.SortFunc(ok, releaseCmp) + l.Releases = slices.Compact(ok) + return l +} + +// supportedReleases are the stable releases worth following: NixOS supports the +// newest two. Before the router has asked, the ones built in. +func supportedReleases(seen []string) []string { + if len(seen) == 0 { + return stableChannels + } + return seen[max(len(seen)-2, 0):] +} + +// newerRelease is the newest stable release that is newer than the one the +// router follows, "" if it follows unstable (or nothing) or is up to date. +func newerRelease(current string, seen []string) string { + if !releaseNameRe.MatchString(current) || len(seen) == 0 { + return "" + } + if newest := seen[len(seen)-1]; releaseCmp(newest, current) > 0 { + return newest + } + return "" +} + +// releaseNotice says which newer release exists than the channel being followed. +func (a *app) releaseNotice() string { + s, err := loadSettings(a.sys.settingsPath()) + if err != nil { + return "" + } + return newerRelease(s.channel(), a.sys.seenReleases().Releases) +} + // channelChoices are the branches worth offering: none (the packages that came // with the release), unstable and the stable releases. A configured channel // that is none of these stays listed. -func channelChoices(current string) []channelChoice { +func channelChoices(current string, stable []string) []channelChoice { choices := []channelChoice{{"", "None: the packages that came with this release of nixos-router"}, {"unstable", "unstable (newest, may break)"}} - for _, r := range stableChannels { + for _, r := range stable { choices = append(choices, channelChoice{r, r + " (stable)"}) } if current != "" && !slices.ContainsFunc(choices, func(c channelChoice) bool { return c.Value == current }) { @@ -188,7 +266,10 @@ func (a *app) upgradeData() upgradeData { if s, err := loadSettings(a.sys.settingsPath()); err == nil { d.Channel = s.channel() } - d.Choices = channelChoices(d.Channel) + seen := a.sys.seenReleases() + d.Choices = channelChoices(d.Channel, supportedReleases(seen.Releases)) + d.NewerRelease = newerRelease(d.Channel, seen.Releases) + d.ReleasesSeen = seen.Time if d.Run.Running { data, _ := os.ReadFile(filepath.Join(a.sys.upgradeDir(), "action")) if d.Title = upgradeTitles[strings.TrimSpace(string(data))]; d.Title == "" { diff --git a/pkgs/router-webui/upgrade_test.go b/pkgs/router-webui/upgrade_test.go index 18ced29..2d7ec50 100644 --- a/pkgs/router-webui/upgrade_test.go +++ b/pkgs/router-webui/upgrade_test.go @@ -24,18 +24,65 @@ func TestReleaseOfAndChannelChoices(t *testing.T) { } return strings.Join(v, ",") } - if got := values(channelChoices("")); got != ",unstable,26.05" { + stable := supportedReleases(nil) + if got := values(channelChoices("", stable)); got != ",unstable,26.05" { t.Fatalf("choices: %q", got) } // A configured channel that is not offered (a release that is gone) is not lost. - if got := values(channelChoices("25.11")); got != ",unstable,26.05,25.11" { + if got := values(channelChoices("25.11", stable)); got != ",unstable,26.05,25.11" { t.Fatalf("choices: %q", got) } - if got := values(channelChoices("unstable")); got != ",unstable,26.05" { + if got := values(channelChoices("unstable", stable)); got != ",unstable,26.05" { t.Fatalf("choices: %q", got) } } +func TestReleaseNotice(t *testing.T) { + seen := []string{"25.05", "25.11", "26.05", "26.11"} + if got := supportedReleases(seen); strings.Join(got, ",") != "26.05,26.11" { + t.Fatalf("supported: %v", got) + } + for _, c := range []struct{ current, want string }{ + {"26.05", "26.11"}, {"25.11", "26.11"}, {"26.11", ""}, {"unstable", ""}, {"", ""}, + } { + if got := newerRelease(c.current, seen); got != c.want { + t.Errorf("following %q: %q, want %q", c.current, got, c.want) + } + } + if got := newerRelease("26.05", nil); got != "" { + t.Errorf("nothing seen: %q", got) + } + if releaseCmp("9.05", "10.05") >= 0 || releaseCmp("26.11", "26.05") <= 0 || releaseCmp("26.05", "26.05") != 0 { + t.Error("releaseCmp") + } +} + +func TestReleaseNoticeShows(t *testing.T) { + a, sys := newTestApp(t) + s, _ := loadSettings(sys.settingsPath()) + obj(obj(s, "router"), "update")["channel"] = "26.05" + if err := s.save(sys.settingsPath()); err != nil { + t.Fatal(err) + } + if err := os.MkdirAll(sys.upgradeDir(), 0o755); err != nil { + t.Fatal(err) + } + h := a.routes() + cookie := do(h, "POST", "/login", url.Values{"password": {"hunter2hunter2"}}, nil).Result().Cookies()[0] + if body := do(h, "GET", "/upgrade", nil, cookie).Body.String(); strings.Contains(body, "is out") { + t.Fatalf("a notice before the channel server was asked:\n%s", body) + } + os.WriteFile(filepath.Join(sys.upgradeDir(), "releases.json"), []byte(`{"time":"2026-11-19T00:00:00Z","releases":["26.05","26.11"]}`), 0o644) + for _, path := range []string{"/", "/upgrade"} { + if body := do(h, "GET", path, nil, cookie).Body.String(); !strings.Contains(body, "NixOS 26.11 is out") { + t.Errorf("%s has no notice:\n%s", path, body) + } + } + if body := do(h, "GET", "/upgrade", nil, cookie).Body.String(); !strings.Contains(body, `