lifecycle: stop before update for boot-style apply (mara@#372)

This commit is contained in:
damocles 2026-05-24 13:15:26 +02:00 committed by Mara
parent bc872fffb9
commit 14b79f43cf

View file

@ -337,24 +337,30 @@ pub async fn rebuild_no_meta(
let flake_ref = format!("{}#{name}", crate::meta::meta_dir().display()); let flake_ref = format!("{}#{name}", crate::meta::meta_dir().display());
if container_exists(name).await { if container_exists(name).await {
// Existing container: preserve the prior running state across // Existing container: preserve the prior running state across
// rebuild (closes #371). `nixos-container update` itself is // rebuild (closes #371) and apply both the new system profile
// already state-preserving — its only side effect on the running // AND any `/etc/nixos-containers/<c>.conf` / drop-in changes
// unit is a `systemctl reload container@<c>` gated on // in a single start rather than `update`'s reload-then-outer-
// `isContainerRunning`, so a stopped container stays stopped // restart double-bounce.
// after the build + profile bump. We only need to recycle the //
// outer unit when the container was running, so that any // `nixos-container update` only runs `systemctl reload
// changes to `/etc/nixos-containers/<c>.conf` (EXTRA_NSPAWN_FLAGS) // container@<c>` when the container is up (per the
// or our systemd drop-ins (resource limits, stale-mount // `isContainerRunning` check in nixos-container.pl), so
// cleanup) are re-read by the next nspawn invocation. If a // stopping first makes `update` boot-style: build + nix-env
// racing operator action started the container during the // --set the new profile, skip the in-container
// rebuild, that's their intent — don't override. // switch-to-configuration, let the next `start` apply both
// the new profile and the new EXTRA_NSPAWN_FLAGS in one go.
// If the container was already stopped, `update` builds + sets
// the profile and we leave it stopped.
let was_running = is_running(name).await; let was_running = is_running(name).await;
set_nspawn_flags(&container, agent_dir, claude_dir, notes_dir)?; set_nspawn_flags(&container, agent_dir, claude_dir, notes_dir)?;
set_resource_limits(&container)?; set_resource_limits(&container)?;
systemd_daemon_reload().await?; systemd_daemon_reload().await?;
if was_running {
run(&["stop", &container]).await?;
}
run(&["update", &container, "--flake", &flake_ref]).await?; run(&["update", &container, "--flake", &flake_ref]).await?;
if was_running { if was_running {
run(&["restart", &container]).await run(&["start", &container]).await
} else { } else {
Ok(()) Ok(())
} }