]> Git — Sourcephile - julm/julm-nix.git/blob - nixos/profiles/zfs.nix
+user/updatability(nixos-unstable): pin latest
[julm/julm-nix.git] / nixos / profiles / zfs.nix
1 {
2 pkgs,
3 lib,
4 hostName,
5 ...
6 }:
7 {
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"
11 '';
12
13 boot.supportedFilesystems = [ "zfs" ];
14 boot.initrd.supportedFilesystems = [ "zfs" ];
15
16 boot.kernelParams = [
17 # Disabled because the zfs kernel module is not signed
18 "module.sig_enforce=0"
19 ];
20
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;
25 #boot.zfs.unsafeAllowHibernation = false;
26
27 # Stable enough, clearer, and faster than the default /dev/disk/by-id
28 boot.zfs.devNodes = "/dev/disk/by-partlabel";
29 # Not useful so far.
30 # See also https://github.com/NixOS/nixpkgs/issues/62644#issuecomment-1479523469
31 boot.zfs.forceImportAll = false;
32 # More resilient for remote hosts,
33 # though it may call zpool clear.
34 boot.zfs.forceImportRoot = true;
35 boot.zfs.requestEncryptionCredentials = lib.mkDefault [ "${hostName}/root" ];
36
37 #boot.zfs.package = pkgs.zfs_unstable;
38
39 # Enables periodic scrubbing of ZFS pools.
40 services.zfs.autoScrub.enable = true;
41 services.zfs.autoScrub.interval = "Sun *-*-08..14 00:15:00";
42
43 # According to zpool(8), for consumer hardware
44 # periodic manual TRIM is preferred over the automatic TRIM
45 # that ZFS implements.
46 services.zfs.trim.enable = true;
47 services.zfs.trim.interval = "Sun *-*-01..07 00:15:00";
48
49 # Hide ZFS mountpoints from gio, hence nautilus or caja
50 systemd.services.zfs-mount.postStart = ''
51 /run/wrappers/bin/mount -t zfs | cut -f 1 -d ' ' |
52 xargs -n 1 -r -t /run/wrappers/bin/mount -o remount,x-gvfs-hide || true
53 '';
54
55 environment.systemPackages = [
56 pkgs.lzop # For remote syncoid
57 pkgs.mbuffer # For remote syncoid
58 pkgs.sanoid
59 ];
60
61 # Force zpool import, even if the disk has not been exported,
62 # (ie. still imported onto another computer).
63 systemd.services."zfs-import@" = {
64 description = "ZFS import pool: %I";
65 unitConfig = {
66 ConditionPathIsDirectory = "/sys/module/zfs";
67 StartLimitIntervalSec = 0;
68 };
69 after = [ "systemd-modules-load.service" ];
70 path = lib.mkBefore [ "/run/booted-system/sw" ];
71 serviceConfig = {
72 Type = "oneshot";
73 RemainAfterExit = true;
74 PrivateTmp = true;
75 SyslogIdentifier = "zfs-import@%i";
76 Restart = "no";
77 ExecStart =
78 pkgs.writeShellScript "zfs-import" ''
79 pool="$1"
80 set -eux
81 zpool status "$pool" ||
82 zpool import -lFd /dev/disk/by-id/ -o cachefile=none "$pool" ||
83 zpool reopen "$pool" ||
84 zpool import -lfd /dev/disk/by-id/ -o cachefile=none "$pool" ||
85 zpool clear -nFX "$pool"
86 ${pkgs.systemd}/bin/systemctl restart zfs-mount.service
87 ''
88 + " %I";
89 };
90 };
91 }