8   # none is the recommended elevator with ZFS (which has its own I/O scheduler)
 
   9   services.udev.extraRules = ''
 
  10     ACTION=="add|change", KERNEL=="sd[a-z]*[0-9]*|mmcblk[0-9]*p[0-9]*|nvme[0-9]*n[0-9]*p[0-9]*", ENV{ID_FS_TYPE}=="zfs_member", ATTR{../queue/scheduler}="none"
 
  13   boot.supportedFilesystems = [ "zfs" ];
 
  14   boot.initrd.supportedFilesystems = [ "zfs" ];
 
  17     # Disabled because the zfs kernel module is not signed
 
  18     "module.sig_enforce=0"
 
  21   # Using ZFS together with hibernation (suspend to disk)
 
  22   # may cause filesystem corruption.
 
  23   # See https://github.com/openzfs/zfs/issues/260
 
  24   boot.zfs.allowHibernation = false;
 
  26   # Stable enough, clearer, and faster than the default /dev/disk/by-id
 
  27   boot.zfs.devNodes = "/dev/disk/by-partlabel";
 
  29   # See also https://github.com/NixOS/nixpkgs/issues/62644#issuecomment-1479523469
 
  30   boot.zfs.forceImportAll = false;
 
  31   # More resilient for remote hosts,
 
  32   # though it may call zpool clear.
 
  33   boot.zfs.forceImportRoot = true;
 
  34   boot.zfs.requestEncryptionCredentials = lib.mkDefault [ "${hostName}/root" ];
 
  36   #boot.zfs.package = pkgs.zfs_unstable;
 
  38   # Enables periodic scrubbing of ZFS pools.
 
  39   services.zfs.autoScrub.enable = true;
 
  40   services.zfs.autoScrub.interval = "Sun *-*-08..14 00:15:00";
 
  42   # According to zpool(8), for consumer hardware
 
  43   # periodic manual TRIM is preferred over the automatic TRIM
 
  44   # that ZFS implements.
 
  45   services.zfs.trim.enable = true;
 
  46   services.zfs.trim.interval = "Sun *-*-01..07 00:15:00";
 
  48   # Hide ZFS mountpoints from gio, hence nautilus or caja
 
  49   systemd.services.zfs-mount.postStart = ''
 
  50     /run/wrappers/bin/mount -t zfs | cut -f 1 -d ' ' |
 
  51     xargs -n 1 -r -t /run/wrappers/bin/mount -o remount,x-gvfs-hide || true
 
  54   environment.systemPackages = [
 
  55     pkgs.lzop # For remote syncoid
 
  56     pkgs.mbuffer # For remote syncoid
 
  60   # Force zpool import, even if the disk has not been exported,
 
  61   # (ie. still imported onto another computer).
 
  62   systemd.services."zfs-import@" = {
 
  63     description = "ZFS import pool: %I";
 
  65       ConditionPathIsDirectory = "/sys/module/zfs";
 
  66       StartLimitIntervalSec = 0;
 
  68     after = [ "systemd-modules-load.service" ];
 
  69     path = lib.mkBefore [ "/run/booted-system/sw" ];
 
  72       RemainAfterExit = true;
 
  74       SyslogIdentifier = "zfs-import@%i";
 
  77         pkgs.writeShellScript "zfs-import" ''
 
  80           zpool status "$pool" ||
 
  81           zpool import -lFd /dev/disk/by-id/ -o cachefile=none "$pool" ||
 
  82           zpool reopen "$pool" ||
 
  83           zpool import -lfd /dev/disk/by-id/ -o cachefile=none "$pool" ||
 
  84           zpool clear -nFX "$pool"
 
  85           ${pkgs.systemd}/bin/systemctl restart zfs-mount.service