]> Git — Sourcephile - julm/julm-nix.git/blob - nixos/profiles/zfs.nix
zfs: better zfs-import@ integration
[julm/julm-nix.git] / nixos / profiles / zfs.nix
1 { pkgs, lib, hostName, ... }:
2 {
3 # none is the recommended elevator with ZFS (which has its own I/O scheduler)
4 # and/or for SSD, whereas HDD could use mq-deadline.
5 services.udev.extraRules = ''
6 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"
7 ACTION=="add|change", KERNEL=="sd[a-z][0-9]*", ATTR{../queue/rotational}=="0", ATTR{../queue/scheduler}="none"
8 ACTION=="add|change", KERNEL=="nvme[0-9]*n[0-9]*p[0-9]*", ATTR{../queue/rotational}=="0", ATTR{../queue/scheduler}="none"
9 '';
10
11 boot.supportedFilesystems = [ "zfs" ];
12 boot.initrd.supportedFilesystems = [ "zfs" ];
13
14 # Using ZFS together with hibernation (suspend to disk)
15 # may cause filesystem corruption.
16 # See https://github.com/openzfs/zfs/issues/260
17 boot.kernelParams = [ "nohibernate" ];
18
19 # Stable enough, clearer, and faster than the default /dev/disk/by-id
20 boot.zfs.devNodes = "/dev/disk/by-partlabel";
21 # Not useful so far.
22 # See also https://github.com/NixOS/nixpkgs/issues/62644#issuecomment-1479523469
23 boot.zfs.forceImportAll = false;
24 # More resilient for remote hosts,
25 # though it may call zpool clear.
26 boot.zfs.forceImportRoot = true;
27 boot.zfs.requestEncryptionCredentials = lib.mkDefault [ "${hostName}/root" ];
28
29 boot.zfs.enableUnstable = false;
30
31 # Enables periodic scrubbing of ZFS pools.
32 services.zfs.autoScrub.enable = true;
33 services.zfs.autoScrub.interval = "Sun *-*-08..14 00:15:00";
34
35 # According to zpool(8), for consumer hardware
36 # periodic manual TRIM is preferred over the automatic TRIM
37 # that ZFS implements.
38 services.zfs.trim.enable = true;
39 services.zfs.trim.interval = "Sun *-*-01..07 00:15:00";
40
41 # Hide ZFS mountpoints from gio, hence nautilus or caja
42 systemd.services.zfs-mount.postStart = ''
43 /run/wrappers/bin/mount -t zfs | cut -f 1 -d ' ' |
44 xargs -n 1 -r -t /run/wrappers/bin/mount -o remount,x-gvfs-hide
45 '';
46
47 environment.systemPackages = [
48 pkgs.lzop # For remote syncoid
49 pkgs.mbuffer # For remote syncoid
50 pkgs.sanoid
51 ];
52
53 # Force zpool import, even if the disk has not been exported,
54 # (ie. still imported onto another computer).
55 systemd.services."zfs-import@" = {
56 description = "ZFS import pool: %I";
57 unitConfig = {
58 ConditionPathIsDirectory = "/sys/module/zfs";
59 StartLimitIntervalSec = 0;
60 };
61 after = [ "systemd-modules-load.service" ];
62 path = lib.mkBefore [ "/run/booted-system/sw" ];
63 serviceConfig = {
64 Type = "oneshot";
65 RemainAfterExit = true;
66 PrivateTmp = true;
67 SyslogIdentifier = "zfs-import@%i";
68 Restart = "no";
69 ExecStart = pkgs.writeShellScript "zfs-import" ''
70 pool="$1"
71 set -eux
72 zpool import -lFd /dev/disk/by-id/ -o cachefile=none "$pool" ||
73 zpool reopen "$pool" ||
74 zpool import -lfd /dev/disk/by-id/ -o cachefile=none "$pool" ||
75 zpool clear -nFX "$pool"
76 ${pkgs.systemd}/bin/systemctl restart zfs-mount.service
77 '' + " %I";
78 };
79 };
80 }