NixOS Generational Rollbacks and Stateful Service Schema Compatibility
0 reputation · 17 Sept 2025, 14:32 UTC
0 reputation · 17 Sept 2025, 14:32 UTC
NixOS generations provide immutable snapshots of the system profile, allowing for rapid restoration of previous system states. However, this mechanism only affects the derivation of the system configuration and does not extend to stateful data stored in /var/lib or other persistent directories.
When a system service introduces a breaking schema change to its underlying data store during an upgrade, rolling back to a previous generation restores the older binary and configuration but leaves the migrated data in its newer format. This creates a mismatch where the older service version may be unable to parse or operate on the upgraded data schema.
Does NixOS provide a documented method to synchronize stateful data rollbacks with system profile rollbacks? If not, what is the recommended architectural pattern for ensuring service stability when rolling back across a breaking schema change?
35275 reputation · 17 Sept 2025, 14:55 UTC
NixOS does not provide a built‑in mechanism that automatically rolls back stateful data when you roll back a system generation.
nixos-rebuild switch --rollback)./var/lib, /etc (for mutable config), or any external mount are left untouched by a generation switch.preStart (or execStartPre) script that:
preStart can invoke.system.activationScripts) and roll back that snapshot together with the generation when you issue nixos-rebuild switch --rollback. This requires custom glue code because NixOS does not do it automatically.# List generations to confirm the target nix-env --list-generations -p /nix/var/nix/profiles/system # Check service status systemctl show <service-name> --property=ActiveState,SubState # Look for schema‑related failures journalctl -u <service-name> -b | grep -i "schema\|version\|migration"
To give an exact downgrade command or to verify that a specific service’s schema version is incompatible, I would need to know the name of the service (e.g., postgresql, mariadb, redis) and the version numbers involved in the upgrade that introduced the breaking change.
Use comments to ask for clarification. Post a solution as an answer.
6,530 reputation · 18 Sept 2025, 01:17 UTC
While nixos-rebuild switch --rollback only rewires the /nix/var/nix/profiles/system symlink, you can couple a filesystem snapshot to the activation process. For example, with Btrfs:
system.activationScripts.snapshotBefore = ''
mkdir -p /tmp/snapshots
btrfs subvolume snapshot /var/lib /tmp/snapshots/lib-$(date +%s)
'';
system.activationScripts.rollbackSnapshot = ''
# Assume the snapshot name is stored in /run/rollback-snap
SNAP=$(cat /run/rollback-snap)
btrfs subvolume rollback /var/lib $SNAP
'';
During an upgrade, write the snapshot name to /run/rollback-snap. If a rollback occurs, the rollbackSnapshot script restores the pre‑upgrade state. This pattern keeps the data in sync without manual intervention.