nixos/containers: Hash rendered nspawn file
CI / Check, build and cache nixfiles (push) Failing after 3h20m33s
Update docs / update (push) Canceled after 7m47s

The container restart trigger hashed the whole `systemd.nspawn.<name>`
option value. Newer NixOS adds a `warnings` list to it, which embeds
module source paths. With lazy trees those paths change on every
evaluation, so the toplevel differed between `nix build` and `nix eval`
and CI failed to push `system-kelder`.

Hash the rendered `.nspawn` text instead, so containers restart exactly
when the file systemd reads changes. Older NixOS renders all units into
one directory, so fall back to the JSON hash there until 26.11.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
jackos1998andClaude Opus 5.5 committed 2026-11-24 10:17:08 +00:00
1 parent fabbb43fbf
commit 7694945041
1 file changed
+13 -3
+13 -3
View File
@@ -10,6 +10,18 @@ let
devVMKeyPath = "/run/dev.key";
ctrProfiles = n: "/nix/var/nix/profiles/per-container/${n}";
# Restart a container when its rendered .nspawn file changes
# TODO: Drop the fallback when 26.11 is stable. Older NixOS renders all .nspawn files into one directory.
nspawnTrigger = n:
let
hash = hashString "sha256" (
if options.systemd.nspawn.type.getSubOptions [ ] ? settings
then config.environment.etc."systemd/nspawn/${n}.nspawn".text
else toJSON config.systemd.nspawn."${n}"
);
in
"${n}.nspawn:${hash}";
dummyReady = pkgs.runCommandCC "dummy-sd-ready" {
buildInputs = [ pkgs.systemd ];
passAsFile = [ "code" ];
@@ -188,9 +200,7 @@ in
# Without this, systemd-nspawn will do cgroupsv1
SYSTEMD_NSPAWN_UNIFIED_HIERARCHY = "1";
};
restartTriggers = [
(''${n}.nspawn:${hashString "sha256" (toJSON config.systemd.nspawn."${n}")}'')
];
restartTriggers = [ (nspawnTrigger n) ];
preStart =
''