impermanence: port btrfs rollback to systemd-stage-1 initrd

nixos-26.05 asserts against boot.initrd.postDeviceCommands under the
systemd stage-1 initrd. Move the root-subvolume rollback into an initrd
systemd oneshot (ordered after the labelled device, before sysroot.mount).
Same rollback logic; awk swapped for cut since coreutils' cut is in the
systemd initrd but awk is not.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
randogoth 2026-08-28 12:14:53 +03:00
parent 72941092d2
commit 17cce973fc

View file

@ -10,7 +10,20 @@
config = lib.mkIf config.nanuqsaurus.impermanence.enable {
boot.initrd.supportedFilesystems = [ "btrfs" ];
boot.initrd.postDeviceCommands = lib.mkAfter ''
# Reset the /root subvolume to a pristine snapshot on every boot.
# systemd stage-1 initrd does not support boot.initrd.postDeviceCommands, so
# this runs as an initrd systemd service, ordered after the labelled device
# appears and before the root filesystem is mounted (sysroot.mount).
# Note: `cut` (coreutils) is used instead of `awk`, which is not in the
# systemd initrd.
boot.initrd.systemd.services.rollback = {
description = "Rollback btrfs root subvolume to a pristine state";
wantedBy = [ "initrd.target" ];
after = [ "dev-disk-by\\x2dlabel-nixos.device" ];
before = [ "sysroot.mount" ];
unitConfig.DefaultDependencies = "no";
serviceConfig.Type = "oneshot";
script = ''
mkdir -p /mnt
mount -t btrfs -o subvol=/ /dev/disk/by-label/nixos /mnt
@ -25,7 +38,7 @@
btrfs subvolume snapshot /mnt/root /mnt/root-previous
while true; do
subvols="$(btrfs subvolume list -o /mnt/root | awk '{print $9}')"
subvols="$(btrfs subvolume list -o /mnt/root | cut -f9 -d' ')"
if [ -z "$subvols" ]; then
break
fi
@ -40,6 +53,7 @@
btrfs subvolume snapshot /mnt/root-blank /mnt/root
umount /mnt
'';
};
fileSystems."/persist".neededForBoot = true;