Impermanence
With impermanence, the root file system starts empty on every boot and only the paths you
list survive. cattery wraps
nix-community/impermanence in
cattery.system.impermanence for NixOS and for Home Manager, and stateful modules add
their own paths to it.
You need three parts:
- A root file system that resets, for example a Btrfs subvolume recreated in the initrd.
- A persistent volume mounted at
persistencePath,/persistentby default. cattery.system.impermanence.enable = true.
NixOS
{
cattery.system.impermanence = {
enable = true;
directories = [ "/srv" "/etc/nixos" ];
files = [ "/etc/adjtime" ];
};
}| Option | Default | Description |
|---|---|---|
enable | false | Sets environment.persistence.<persistencePath> |
persistencePath | "/persistent" | Mount point of the persistent volume |
directories | [ ] | Extra directories to persist, in the impermanence format |
files | [ ] | Extra files to persist |
extraOptions | { } | Merged into environment.persistence.<persistencePath> with lib.mkMerge |
These paths are always persisted, with hideMounts = true:
| Kind | Paths |
|---|---|
| Directories | /var/log, /var/lib, /etc/NetworkManager/system-connections, /etc/ssh |
| Files | /etc/machine-id |
Because /etc/ssh lives on the persistent volume, cattery sets age.identityPaths with
lib.mkDefault to the persistent copies of the host's ed25519 and RSA keys. agenix can then
decrypt secrets after a reboot.
/etc/shadow does not survive a reboot. Manage passwords declaratively, for example with
cattery.user.useSecretPasswordFile = true, which reads the hash from the shadow/<name>
secret and defaults users.mutableUsers to false.
Btrfs rollback
cattery.system.fileSystems.btrfs.impermanence recreates Btrfs subvolumes on every boot.
For each entry, a service in the systemd initrd runs before sysroot.mount:
- Mounts the Btrfs file system on
tempDir. - Moves the current subvolume to
oldSubvolDir/<timestamp>as a snapshot. - Deletes archived snapshots older than 30 days.
- Creates an empty subvolume in its place.
{
boot.initrd.systemd.enable = true;
fileSystems = {
"/" = {
device = "/dev/disk/by-label/nixos";
fsType = "btrfs";
options = [ "subvol=root" "compress=zstd" ];
};
"/nix" = {
device = "/dev/disk/by-label/nixos";
fsType = "btrfs";
options = [ "subvol=nix" "compress=zstd" "noatime" ];
};
"/persistent" = {
device = "/dev/disk/by-label/nixos";
fsType = "btrfs";
options = [ "subvol=persistent" "compress=zstd" ];
neededForBoot = true;
};
};
cattery.system = {
# The attribute name is the subvolume to recreate.
fileSystems.btrfs.impermanence.root.device = "/dev/disk/by-label/nixos";
impermanence.enable = true;
};
}| Option | Default | Description |
|---|---|---|
device | required | Block device of the Btrfs file system |
subvol | the attribute name | Subvolume to recreate |
oldSubvolDir | "old_<subvol>" | Directory below the Btrfs root that holds the archived snapshots |
tempDir | "/btrfs_<subvol>_tmp" | Mount point used during the rollback |
The service is declared under boot.initrd.systemd, so it needs the systemd initrd. Mark
the persistent file system with neededForBoot = true so the bind mounts exist before
services start. An archived snapshot is a quick way to recover a file you forgot to
persist.
Home Manager
The Home Manager side persists paths below the user's home directory:
{
cattery.system.impermanence = {
enable = true;
directories = [ "Projects" ".local/share/keyrings" ];
files = [ ".config/monitors.xml" ];
xdg.cache.full = true;
};
}It only takes effect when Home Manager runs as a NixOS module next to the impermanence NixOS module, which the cattery NixOS bundle includes. On macOS and in standalone Home Manager the options exist but do nothing, so the same home configuration still evaluates.
| Option | Default | Description |
|---|---|---|
enable | false | Sets home.persistence.<persistencePath> |
persistencePath | "/persistent" | Must match the NixOS value |
directories, files | [ ] | Extra paths, relative to the home directory |
xdg.userDirs.enable | true | Persists the XDG user directories (Desktop, Documents, Downloads, and so on) |
xdg.<dir>.enable | true | For cache, config, data, and state: persist the entries that modules list |
xdg.<dir>.full | false | Persist the whole XDG directory instead of single entries |
xdg.<dir>.directories, xdg.<dir>.files | [ ] | Extra entries, relative to that XDG directory |
extraOptions | { } | Merged into home.persistence.<persistencePath> |
~/.ssh and ~/.pki are always persisted, and age.identityPaths defaults to the
persistent copies of ~/.ssh/id_ed25519 and ~/.ssh/id_rsa. Entries are deduplicated
before they reach impermanence, and directory modes are dropped. Set permissions on the
persistent copies yourself.
The Home Manager room.server-mini, and every room built on it, turns
xdg.userDirs.enable off, since servers rarely need those directories.
The persistence option
Modules that keep state have a persistence option, true by default. When it is on,
the module adds its paths to cattery.system.impermanence. They are only used when
impermanence itself is enabled. For example, cli-apps.shell.atuin persists its
database:
# modules/home/cli-apps/shell/atuin/default.nix
${namespace}.system.impermanence = lib.mkIf cfg.persistence {
xdg.data.directories = [ "atuin" ];
};Turn it off per module to let that state reset:
{
cattery.cli-apps.shell.atuin.persistence = false;
}When you write a module, add a persistence option for any state that should survive a
reboot, and add the paths through cattery.system.impermanence rather than to
environment.persistence or home.persistence directly.