{ pkgs
, lib
, config
, ... }: with lib; let
  cfg = config.global.fs;
in {
  # -o ashift=12
  # -O encryption=on -O keyformat=passphrase -O keylocation=prompt
  # -O compression=on -O mountpoint=none -O xattr=sa -O acltype=posixacl
  options.global.fs.zfs = {
    persist = mkOption {
      type = with types; str;
      default = cfg.store;
      description = ''
        pool for persist dataset
        defaults to nix store dataset
      '';
    };
    mountpoints = mkOption {
      type = with types; attrsOf str;
      description = "zfs dataset mountpoints";
    };
  };

  config = mkIf (cfg.type == "zfs") {
    fileSystems = (mapAttrs (path: dataset: {
      device = "${cfg.zfs.persist}/${dataset}";
      fsType = "zfs";
      # required by impermanence
      neededForBoot = true;
    }) cfg.zfs.mountpoints) // {
      "/nix" =
        { device = "${cfg.store}/nix";
          fsType = "zfs";
        };
    };
    global.fs.zfs.mountpoints."/nix/persist" = "persist";

    services.zfs.trim.enable = true;
    services.zfs.autoSnapshot.enable = true;
    services.zfs.autoScrub.enable = true;
    boot.zfs.devNodes = mkDefault "/dev/disk/by-partuuid";
    boot.kernelPackages = config.boot.zfs.package.latestCompatibleLinuxPackages;
  };
}