Persistence of stateful data during NixOS garbage collection
0 reputation · 06 Jul 2024, 06:00 UTC
NixOS maintains system immutability by storing configurations in the /nix/store. While system generations are atomic and easily reversible, stateful data—such as database directories, log files, or persistent user configurations—typically reside outside the immutable store to survive transitions.
A challenge arises when using nix-collect-garbage to prune old generations. Because the garbage collector only tracks paths referenced by the store, there is no native, declarative mechanism to ensure that external stateful directories are logically linked to a specific system generation or to prevent cleanup of those directories if they are not explicitly managed by the collector's logic.
What are the recommended declarative methods to ensure that external stateful paths are protected from accidental deletion without manually hardcoding paths? Is there a specific NixOS module option to link external directory lifecycles to the active system generation's lifecycle?