persist.nix 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. {pkgs, ...}: {
  2. networking.hostId = "97999349"; # head -c8 /etc/machine-id
  3. environment.persistence."/persist" = {
  4. hideMounts = true;
  5. directories = [
  6. # "/var/log" # i have my own subvolume for this
  7. "/var/lib/bluetooth"
  8. "/var/lib/libvirt"
  9. "/var/lib/nixos"
  10. "/var/lib/pipewire"
  11. "/var/lib/systemd/coredump"
  12. "/etc/NetworkManager/system-connections"
  13. "/etc/nixos"
  14. "/etc/nix"
  15. ];
  16. files = [
  17. "/etc/machine-id"
  18. ];
  19. };
  20. systemd.tmpfiles.rules = [
  21. "L /var/lib/NetworkManager/secret_key - - - - /persist/var/lib/NetworkManager/secret_key"
  22. "L /var/lib/NetworkManager/seen-bssids - - - - /persist/var/lib/NetworkManager/seen-bssids"
  23. "L /var/lib/NetworkManager/timestamps - - - - /persist/var/lib/NetworkManager/timestamps"
  24. ];
  25. security.sudo.extraConfig = ''
  26. # rollback results in sudo lectures after each reboot
  27. Defaults lecture = never
  28. '';
  29. boot.initrd.postDeviceCommands = pkgs.lib.mkBefore ''
  30. mkdir -p /mnt
  31. # We first mount the btrfs root to /mnt
  32. # so we can manipulate btrfs subvolumes.
  33. mount -o subvol=/ /dev/mapper/enc /mnt
  34. # While we're tempted to just delete /root and create
  35. # a new snapshot from /root-blank, /root is already
  36. # populated at this point with a number of subvolumes,
  37. # which makes `btrfs subvolume delete` fail.
  38. # So, we remove them first.
  39. #
  40. # /root contains subvolumes:
  41. # - /root/var/lib/portables
  42. # - /root/var/lib/machines
  43. #
  44. # I suspect these are related to systemd-nspawn, but
  45. # since I don't use it I'm not 100% sure.
  46. # Anyhow, deleting these subvolumes hasn't resulted
  47. # in any issues so far, except for fairly
  48. # benign-looking errors from systemd-tmpfiles.
  49. btrfs subvolume list -o /mnt/root |
  50. cut -f9 -d' ' |
  51. while read subvolume; do
  52. echo "deleting /$subvolume subvolume..."
  53. btrfs subvolume delete "/mnt/$subvolume"
  54. done &&
  55. echo "deleting /root subvolume..." &&
  56. btrfs subvolume delete /mnt/root
  57. echo "restoring blank /root subvolume..."
  58. btrfs subvolume snapshot /mnt/root-blank /mnt/root
  59. # Once we're done rolling back to a blank snapshot,
  60. # we can unmount /mnt and continue on the boot process.
  61. umount /mnt
  62. '';
  63. }