1 { pkgs, lib, hostName, ... }:
3 # none is the recommended elevator with ZFS (which has its own I/O scheduler)
4 services.udev.extraRules = ''
5 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"
8 boot.supportedFilesystems = [ "zfs" ];
9 boot.initrd.supportedFilesystems = [ "zfs" ];
11 # Using ZFS together with hibernation (suspend to disk)
12 # may cause filesystem corruption.
13 # See https://github.com/openzfs/zfs/issues/260
14 boot.kernelParams = [ "nohibernate" ];
16 # Stable enough, clearer, and faster than the default /dev/disk/by-id
17 boot.zfs.devNodes = "/dev/disk/by-partlabel";
19 # See also https://github.com/NixOS/nixpkgs/issues/62644#issuecomment-1479523469
20 boot.zfs.forceImportAll = false;
21 # More resilient for remote hosts,
22 # though it may call zpool clear.
23 boot.zfs.forceImportRoot = true;
24 boot.zfs.requestEncryptionCredentials = lib.mkDefault [ "${hostName}/root" ];
26 boot.zfs.enableUnstable = false;
28 # Enables periodic scrubbing of ZFS pools.
29 services.zfs.autoScrub.enable = true;
30 services.zfs.autoScrub.interval = "Sun *-*-08..14 00:15:00";
32 # According to zpool(8), for consumer hardware
33 # periodic manual TRIM is preferred over the automatic TRIM
34 # that ZFS implements.
35 services.zfs.trim.enable = true;
36 services.zfs.trim.interval = "Sun *-*-01..07 00:15:00";
38 # Hide ZFS mountpoints from gio, hence nautilus or caja
39 systemd.services.zfs-mount.postStart = ''
40 /run/wrappers/bin/mount -t zfs | cut -f 1 -d ' ' |
41 xargs -n 1 -r -t /run/wrappers/bin/mount -o remount,x-gvfs-hide
44 environment.systemPackages = [
45 pkgs.lzop # For remote syncoid
46 pkgs.mbuffer # For remote syncoid
50 # Force zpool import, even if the disk has not been exported,
51 # (ie. still imported onto another computer).
52 systemd.services."zfs-import@" = {
53 description = "ZFS import pool: %I";
55 ConditionPathIsDirectory = "/sys/module/zfs";
56 StartLimitIntervalSec = 0;
58 after = [ "systemd-modules-load.service" ];
59 path = lib.mkBefore [ "/run/booted-system/sw" ];
62 RemainAfterExit = true;
64 SyslogIdentifier = "zfs-import@%i";
66 ExecStart = pkgs.writeShellScript "zfs-import" ''
69 zpool import -lFd /dev/disk/by-id/ -o cachefile=none "$pool" ||
70 zpool reopen "$pool" ||
71 zpool import -lfd /dev/disk/by-id/ -o cachefile=none "$pool" ||
72 zpool clear -nFX "$pool"
73 ${pkgs.systemd}/bin/systemctl restart zfs-mount.service