nanuqsaurus/modules/system/impermanence.nix

67 lines
1.7 KiB
Nix
Raw Normal View History

2026-02-10 09:25:12 +02:00
{ config, lib, ... }:
{
2026-02-10 09:34:51 +02:00
options.nanuqsaurus.impermanence.enable = lib.mkOption {
type = lib.types.bool;
default = false;
description = "Enable impermanence with a /persist Btrfs subvolume.";
};
config = lib.mkIf config.nanuqsaurus.impermanence.enable {
2026-02-10 09:25:12 +02:00
boot.initrd.supportedFilesystems = [ "btrfs" ];
boot.initrd.postDeviceCommands = lib.mkAfter ''
mkdir -p /mnt
mount -t btrfs -o subvol=/ /dev/disk/by-label/nixos /mnt
if [ ! -d /mnt/root-blank ]; then
btrfs subvolume snapshot -r /mnt/root /mnt/root-blank
fi
if [ -d /mnt/root ]; then
if [ -d /mnt/root-previous ]; then
btrfs subvolume delete /mnt/root-previous
fi
btrfs subvolume snapshot /mnt/root /mnt/root-previous
while true; do
subvols="$(btrfs subvolume list -o /mnt/root | awk '{print $9}')"
if [ -z "$subvols" ]; then
break
fi
for subvol in $subvols; do
btrfs subvolume delete "/mnt/$subvol"
done
done
2026-02-10 09:25:12 +02:00
btrfs subvolume delete /mnt/root
fi
btrfs subvolume snapshot /mnt/root-blank /mnt/root
umount /mnt
'';
fileSystems."/persist".neededForBoot = true;
environment.persistence."/persist" = {
hideMounts = true;
directories = [
"/etc/nixos"
"/etc/ssh"
"/var/lib/bluetooth"
2026-02-10 09:25:12 +02:00
"/var/lib/cups"
"/var/lib/fprint"
2026-02-10 09:25:12 +02:00
"/var/lib/libvirt"
"/var/lib/NetworkManager"
2026-02-10 09:25:12 +02:00
"/var/lib/secureboot"
"/var/lib/tailscale"
"/var/lib/systemd"
"/var/lib/nixos"
];
files = [
"/etc/machine-id"
];
};
};
}