boot.initrd.postDeviceCommands not executed by NixOS

I am trying to use impermanence with my NixOS VM.
Therefor I added this setting to my configuration.nix:

  boot.initrd.postDeviceCommands = pkgs.lib.mkBefore ''
    mkdir /btrfs_tmp
    mount /dev/root_vg/root /btrfs_tmp
    if [[ -e /btrfs_tmp/root ]]; then
        mkdir -p /btrfs_tmp/old_roots
        timestamp=$(date --date="@$(stat -c %Y /btrfs_tmp/root)" "+%Y-%m-%-d_%H:%M:%S")
        mv /btrfs_tmp/root "/btrfs_tmp/old_roots/$timestamp"
    fi

    delete_subvolume_recursively() {
        IFS=$'\n'
        for i in $(btrfs subvolume list -o "$1" | cut -f 9- -d ' '); do
            delete_subvolume_recursively "/btrfs_tmp/$i"
        done
        btrfs subvolume delete "$1"
    }

    for i in $(find /btrfs_tmp/old_roots/ -maxdepth 1 -mtime +30); do
        delete_subvolume_recursively "$i"
    done

    btrfs subvolume create /btrfs_tmp/root
    umount /btrfs_tmp
  '';

However it does not seem like it gets executed by NixOS. I already tried some echo statements and had a look at journalctl but the content of the system log is inconspicuous:

 Jul 11 19:40:28 work stage-1-init: [Thu Jul 11 17:40:28 UTC 2024] starting device mapper and LVM...
Jul 11 19:40:28 work stage-1-init: [Thu Jul 11 17:40:28 UTC 2024] 1 logical volume(s) in volume group "root_vg" now active
Jul 11 19:40:28 work kernel: BTRFS: device fsid 0b2d25c1-927b-4d0a-95b6-a5dfa30a4f75 devid 1 transid 576 /dev/mapper/root_vg-root scanned by btrfs (208)
Jul 11 19:40:28 work stage-1-init: [Thu Jul 11 17:40:28 UTC 2024] Scanning for Btrfs filesystems
Jul 11 19:40:28 work stage-1-init: [Thu Jul 11 17:40:28 UTC 2024] registered: /dev/mapper/root_vg-root
Jul 11 19:40:28 work stage-1-init: [Thu Jul 11 17:40:28 UTC 2024] mounting /dev/root_vg/root on /...
Jul 11 19:40:28 work kernel: BTRFS info (device dm-0): first mount of filesystem 0b2d25c1-927b-4d0a-95b6-a5dfa30a4f75
Jul 11 19:40:28 work kernel: BTRFS info (device dm-0): using crc32c (crc32c-intel) checksum algorithm
Jul 11 19:40:28 work kernel: BTRFS info (device dm-0): use zstd compression, level 3
Jul 11 19:40:28 work kernel: BTRFS info (device dm-0): using free space tree
Jul 11 19:40:28 work kernel: BTRFS info (device dm-0): auto enabling async discard
Jul 11 19:40:28 work stage-1-init: [Thu Jul 11 17:40:28 UTC 2024] mounting /dev/root_vg/root on /nix...
Jul 11 19:40:28 work unknown: booting system configuration /nix/store/s31czkb2lgsh3bq1ckqcps6znzpgc65a-nixos-system-work-24.05.20240709.249fbde
Jul 11 19:40:28 work stage-2-init: running activation script...
Jul 11 19:40:28 work stage-2-init: setting up /etc...
Jul 11 19:40:28 work stage-2-init: removing obsolete symlink ‘/etc/machine-id’...
Jul 11 19:40:28 work systemd[1]: Inserted module 'autofs4'

Any idea what could be missing so that the postDeviceCommands script is executed?

I added a sleep 30 command in the first line and did not notice any delay during boot. This confirms my assumption that the commands in this setting are not executed.

I have the same problem, did you ever got around to solve it?

Use postResumeCommands instead.

I mean, yes, impermanence style setups should be using postResumeCommands, but that doesn’t explain why a postDeviceCommands would have not been running. That’s rather confusing.

Do you have boot.initrd.systemd.enable?

Thank you all for the quick reply, this is my first interaction in this community and it is so encouraging to receive help so promptly :raised_hands:

However, it turns out that I was wrong, once I instrumented it with a few echo and sleep statements I realised that the script is actually running… but it does not work as intended (or I am being silly and don’t understand where data is stored). I opened a - hopefully valid - ticket here