{
  config,
  inputs,
  lib,
  namespace,
  ...
}: let
  inherit (lib) types mkIf mkEnableOption concatLists;
  inherit (lib.${namespace}) getAttrByNamespace mkOptionsWithNamespace mkRequiredOpt mkNullableOpt mkListOpt;
  base = "${namespace}.services.storage.impermanence";
  cfg = getAttrByNamespace config base;
in {
  imports = [
    inputs.impermanence.nixosModules.impermanence
  ];

  options = with types;
    mkOptionsWithNamespace base {
      enable = mkEnableOption "impermanence";
      folders = mkListOpt (either str (submodule {
        options = {
          directory = mkRequiredOpt str "Directory path to persist";
          user = mkNullableOpt str null "User owning the directory";
          group = mkNullableOpt str null "Group owning the directory";
          mode = mkNullableOpt str null "Permissions mode for the directory";
        };
      })) [] "List of folders to persist";
    };

  config = mkIf cfg.enable {
    fileSystems."/persist".neededForBoot = true;

    boot.initrd.systemd.services.rollback = {
      description = "Rollback ZFS root to blank snapshot";
      wantedBy = ["initrd.target"];
      after = ["zfs-import-zroot.service"];
      before = ["sysroot.mount"];
      path = [config.boot.zfs.package];

      unitConfig.DefaultDependencies = "no";
      serviceConfig.Type = "oneshot";

      script = ''
        zfs rollback -r zroot/root@blank
      '';
    };

    environment.persistence."/persist" = {
      hideMounts = true;

      directories = let
        static = [
          "/opt"

          "/var/db/sudo/lectured"
          "/var/lib/nixos"
          "/var/log"
        ];
      in
        concatLists [static cfg.folders];

      users.c4patino = {
        directories = [
          ".android"
          ".cache/direnv"
          ".cache/spotify"
          ".cache/spotify-player"
          ".cache/thunderbird"
          ".cache/vivaldi"
          ".conan2"
          ".config/Google"
          ".config/JetBrains"
          ".config/Postman"
          ".config/Slack"
          ".config/VirtualBox"
          ".config/autostart"
          ".config/discord"
          ".config/gh"
          ".config/libreoffice"
          ".config/nushell"
          ".config/obs-studio"
          ".config/obsidian"
          ".config/pnpm"
          ".config/pypoetry"
          ".config/spotify"
          ".config/spotify-player"
          ".config/teamviewer"
          ".config/variety"
          ".config/vivaldi"
          ".gnupg"
          ".java"
          ".local/share/Google"
          ".local/share/JetBrains"
          ".local/share/PrismLauncher"
          ".local/share/Steam"
          ".local/share/applications"
          ".local/share/containers"
          ".local/share/direnv"
          ".local/share/nvim"
          ".local/share/opencode"
          ".local/share/pnpm"
          ".local/share/racket"
          ".local/share/zoxide"
          ".local/share/zsh"
          ".local/state/lazygit"
          ".local/state/opencode"
          ".local/state/wireplumber"
          ".m2"
          ".pm2"
          ".stack"
          ".steam"
          ".thunderbird"
          ".vim"
          ".zotero"
          "Android"
          "Documents"
          "Pictures"
          "Programming"
          "VirtualBox VMs"
          "Zotero"
          "dotfiles"
        ];

        files = [
          ".ssh/known_hosts"
        ];
      };
    };
  };
}
