Skip to content
LogoLogo

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:

  1. A root file system that resets, for example a Btrfs subvolume recreated in the initrd.
  2. A persistent volume mounted at persistencePath, /persistent by default.
  3. cattery.system.impermanence.enable = true.

NixOS

{
  cattery.system.impermanence = {
    enable = true;
    directories = [ "/srv" "/etc/nixos" ];
    files = [ "/etc/adjtime" ];
  };
}
OptionDefaultDescription
enablefalseSets 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:

KindPaths
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:

  1. Mounts the Btrfs file system on tempDir.
  2. Moves the current subvolume to oldSubvolDir/<timestamp> as a snapshot.
  3. Deletes archived snapshots older than 30 days.
  4. 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;
  };
}
OptionDefaultDescription
devicerequiredBlock device of the Btrfs file system
subvolthe attribute nameSubvolume 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.

OptionDefaultDescription
enablefalseSets home.persistence.<persistencePath>
persistencePath"/persistent"Must match the NixOS value
directories, files[ ]Extra paths, relative to the home directory
xdg.userDirs.enabletruePersists the XDG user directories (Desktop, Documents, Downloads, and so on)
xdg.<dir>.enabletrueFor cache, config, data, and state: persist the entries that modules list
xdg.<dir>.fullfalsePersist 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.