{
  config,
  lib,
  ...
}: {
  options.weinturm = with lib; {
    impermanence = {
      enable = mkOption {
        type = types.bool;
        default = true;
        description = "Whether to enable impermanence";
      };
      rootDevice = with types;
        mkOption {
          type = nullOr str;
          default = null;
          description = ''
            The device which contains the btrfs root subvolume
          '';
        };
    };
  };
  config = let
    cfg = config.weinturm.impermanence;
  in
    lib.mkIf cfg.enable {
      users.mutableUsers = false;

      fileSystems."/persist".neededForBoot = true;

      environment.persistence."/persist".directories = [
        "/var/lib/nixos"
        "/var/lib/acme"
      ];

      boot.initrd.postDeviceCommands = let
        rootDevice =
          if cfg.rootDevice == null
          then ""
          else cfg.rootDevice;
      in
        lib.mkAfter ''
          mkdir /mnt
          mount -t btrfs "${rootDevice}" /mnt
          btrfs subvolume list -o /mnt/root | cut -f9 -d' ' | while read subvolume; do
            btrfs subvolume delete "/mnt/$subvolume"
          done
          btrfs subvolume delete /mnt/root
          btrfs subvolume snapshot /mnt/root-blank /mnt/root
        '';
    };
}
