]> Git — Sourcephile - julm/julm-nix.git/blob - nixos/profiles/zfs.nix
udev: move to default the rules not specific to ZFS
[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 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"
6 '';
7
8 boot.supportedFilesystems = [ "zfs" ];
9 boot.initrd.supportedFilesystems = [ "zfs" ];
10
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" ];
15
16 # Stable enough, clearer, and faster than the default /dev/disk/by-id
17 boot.zfs.devNodes = "/dev/disk/by-partlabel";
18 # Not useful so far.
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" ];
25
26 boot.zfs.enableUnstable = false;
27
28 # Enables periodic scrubbing of ZFS pools.
29 services.zfs.autoScrub.enable = true;
30 services.zfs.autoScrub.interval = "Sun *-*-08..14 00:15:00";
31
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";
37
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
42 '';
43
44 environment.systemPackages = [
45 pkgs.lzop # For remote syncoid
46 pkgs.mbuffer # For remote syncoid
47 pkgs.sanoid
48 ];
49
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";
54 unitConfig = {
55 ConditionPathIsDirectory = "/sys/module/zfs";
56 StartLimitIntervalSec = 0;
57 };
58 after = [ "systemd-modules-load.service" ];
59 path = lib.mkBefore [ "/run/booted-system/sw" ];
60 serviceConfig = {
61 Type = "oneshot";
62 RemainAfterExit = true;
63 PrivateTmp = true;
64 SyslogIdentifier = "zfs-import@%i";
65 Restart = "no";
66 ExecStart = pkgs.writeShellScript "zfs-import" ''
67 pool="$1"
68 set -eux
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
74 '' + " %I";
75 };
76 };
77 }