modules/monitoring/grafana: restart grafana when the dashboards change (nix-config#361) #1383

Merged
hermes merged 1 commit from kb/NCFG-361-grafana-dashboard-restart into main 2026-10-01 10:13:43 -06:00
Collaborator

Kanban card nix-config#361.

Symptom (measured, not inferred)

While landing nix-config#350 the deploy swapped /etc/grafana-dashboards to the new store path and the RUNNING grafana (13.1.4 on mail) kept serving the version it loaded at its last start:

  • /etc/grafana-dashboards/command-tracking.json -> /nix/store/4k6wmynfx19m3dzp697jiq2m527f9p3d-grafana-dashboards/command-tracking.json, sha256 equal to origin/main's blob (the file on disk WAS the new one);
  • GET /api/dashboards/uid/command-tracking (Grafana admin on mail) still answered panels: 12, time.from: now-1h, meta.updated: 2026-09-22T23:57:24 for the whole 7-minute polling window;
  • only systemctl restart grafana made it live (then 18 panels, now-24h).

Consequence: every dashboards edit merged since grafana last started (2026-09-23 on mail) was silently invisible to the operator.

Change

modules/monitoring/grafana.nix:

  1. the built dashboards derivation is named once in the let block (dashboardsDrv) instead of living inline in the systemd.tmpfiles.rules string;
  2. the tmpfiles rule becomes "L+ ${cfg.dashboardsDir} - - - - ${dashboardsDrv}/" (both were hardcoded before);
  3. systemd.services.grafana.restartTriggers = [ dashboardsDrv ].

nixpkgs hashes trigger paths into the unit's X-Restart-Triggers, so a dashboard edit changes the derivation's store path, the hash changes, and switch-to-configuration restarts grafana as part of the deploy. This is the mechanism hosts/ already uses for the other generated inputs (uptime-kuma static monitors + status page, adguard policy, unbound), so it is consistent rather than novel.

Evidence on this tree (eval, before merge)

$ nix eval --json .#nixosConfigurations.mail.config.systemd.services.grafana.restartTriggers
["/nix/store/4k6wmynfx19m3dzp697jiq2m527f9p3d-grafana-dashboards"]

$ nix eval --json --apply ... .#nixosConfigurations.mail.config.systemd.tmpfiles.rules
["L+ /etc/grafana-dashboards - - - - /nix/store/4k6wmynfx19m3dzp697jiq2m527f9p3d-grafana-dashboards/"]

The trigger references the very store path the deployed symlink already points at, and the tmpfiles rule is byte-identical to the pre-change one — so this PR adds the restart and does not churn mail's symlink.

Pin

.forgejo/tests/test-grafana-dashboards-restart.sh (new, offline, ~0.1s, wired into nix-build-check.yml's offline-tests step): the real tree must carry all three pieces, and each of four mutations (trigger line deleted / pointed at a literal / emptied / tmpfiles symlink no longer pointing into the derivation) must turn the test red. Deleting the trigger is exactly what a future refactor does by accident, and eval cannot catch it — the host builds green and merely never restarts, which is how this bug shipped.

Deliberately out of scope

No dashboard JSON content changes; no change to cfg.dashboardsDir's default or any other host's grafana wiring.

<!-- base-guard: see PR body note --> Kanban card nix-config#361. ## Symptom (measured, not inferred) While landing nix-config#350 the deploy swapped `/etc/grafana-dashboards` to the new store path and the RUNNING grafana (13.1.4 on mail) kept serving the version it loaded at its last start: - `/etc/grafana-dashboards/command-tracking.json` -> `/nix/store/4k6wmynfx19m3dzp697jiq2m527f9p3d-grafana-dashboards/command-tracking.json`, sha256 equal to origin/main's blob (the file on disk WAS the new one); - `GET /api/dashboards/uid/command-tracking` (Grafana admin on mail) still answered `panels: 12`, `time.from: now-1h`, `meta.updated: 2026-09-22T23:57:24` for the whole 7-minute polling window; - only `systemctl restart grafana` made it live (then 18 panels, now-24h). Consequence: every dashboards edit merged since grafana last started (2026-09-23 on mail) was silently invisible to the operator. ## Change `modules/monitoring/grafana.nix`: 1. the built dashboards derivation is named once in the `let` block (`dashboardsDrv`) instead of living inline in the `systemd.tmpfiles.rules` string; 2. the tmpfiles rule becomes `"L+ ${cfg.dashboardsDir} - - - - ${dashboardsDrv}/"` (both were hardcoded before); 3. `systemd.services.grafana.restartTriggers = [ dashboardsDrv ]`. nixpkgs hashes trigger paths into the unit's `X-Restart-Triggers`, so a dashboard edit changes the derivation's store path, the hash changes, and `switch-to-configuration` restarts grafana as part of the deploy. This is the mechanism `hosts/` already uses for the other generated inputs (uptime-kuma static monitors + status page, adguard policy, unbound), so it is consistent rather than novel. ## Evidence on this tree (eval, before merge) ``` $ nix eval --json .#nixosConfigurations.mail.config.systemd.services.grafana.restartTriggers ["/nix/store/4k6wmynfx19m3dzp697jiq2m527f9p3d-grafana-dashboards"] $ nix eval --json --apply ... .#nixosConfigurations.mail.config.systemd.tmpfiles.rules ["L+ /etc/grafana-dashboards - - - - /nix/store/4k6wmynfx19m3dzp697jiq2m527f9p3d-grafana-dashboards/"] ``` The trigger references the very store path the deployed symlink already points at, and the tmpfiles rule is byte-identical to the pre-change one — so this PR adds the restart and does not churn mail's symlink. ## Pin `.forgejo/tests/test-grafana-dashboards-restart.sh` (new, offline, ~0.1s, wired into nix-build-check.yml's offline-tests step): the real tree must carry all three pieces, and each of four mutations (trigger line deleted / pointed at a literal / emptied / tmpfiles symlink no longer pointing into the derivation) must turn the test red. Deleting the trigger is exactly what a future refactor does by accident, and eval cannot catch it — the host builds green and merely never restarts, which is how this bug shipped. ## Deliberately out of scope No dashboard JSON content changes; no change to `cfg.dashboardsDir`'s default or any other host's grafana wiring.
modules/monitoring/grafana: restart grafana when the dashboards change (nix-config#361)
All checks were successful
PR base guard / Base drift / conflict check (pull_request) Successful in 4s
NixOS Build Check / Worker-scope reaper worker signature (mutation-tested) (pull_request) Successful in 4s
NixOS Build Check / Flake input regression guard (pull_request) Successful in 7s
Secret Scan / gitleaks scan (pull_request) Successful in 8s
NixOS Build Check / .forgejo/tests python suites (hermetic) (pull_request) Successful in 16s
NixOS Build Check / Scope changed deployable hosts (pull_request) Successful in 49s
NixOS Build Check / Hermes dispatcher worktree base (pull_request) Successful in 19s
NixOS Build Check / Hermes terminal timeout tree kill (pull_request) Successful in 34s
NixOS Build Check / foremerge accept-order contract (real binary) (pull_request) Successful in 6s
foremerge intent (advisory) all 3 changed path(s) covered by a live foremerge intent
NixOS Build Check / Build changed host closures (native, one eval) (pull_request) Successful in 3m43s
4ed6ad54ab
The deploy swapped /etc/grafana-dashboards to the new store path and the
RUNNING grafana kept serving the version it loaded at its last start: measured
2026-10-01 while landing nix-config#350, `/etc/grafana-dashboards/
command-tracking.json` already had sha256 equal to origin/main's blob while
`GET /api/dashboards/uid/command-tracking` still answered the pre-change
dashboard (12 panels, now-1h, meta.updated 2026-09-22) for the whole polling
window, and a manual `systemctl restart grafana` was the only thing that made
it live. Every dashboards edit merged since grafana last started was therefore
invisible to the operator.

Fix: name the built dashboards derivation once (`dashboardsDrv`), point the
tmpfiles `L+` rule into it through `cfg.dashboardsDir`, and give the unit
`systemd.services.grafana.restartTriggers = [ dashboardsDrv ]`. nixpkgs hashes
the trigger paths into the unit's X-Restart-Triggers, so a dashboard edit
changes that hash and switch-to-configuration restarts grafana as part of the
deploy — no manual step. Same mechanism hosts/ already use for the other
generated inputs (uptime-kuma monitors/status page, adguard policy, unbound).

Pinned by .forgejo/tests/test-grafana-dashboards-restart.sh (offline, ~0.1s,
wired into nix-build-check.yml): the real tree must carry all three pieces,
and deleting the trigger, pointing it at a literal, emptying it, or unlinking
the symlink from the derivation must each turn it red. Eval cannot catch this
class — the host builds green and merely never restarts.
hermes merged commit 26366bc071 into main 2026-10-01 10:13:43 -06:00
hermes deleted branch kb/NCFG-361-grafana-dashboard-restart 2026-10-01 10:13:43 -06:00
Sign in to join this conversation.
No reviewers
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
ymrtech/nix-config!1383
No description provided.