]> Git — Sourcephile - julm/julm-nix.git/blob - nixos/profiles/zfs.nix
git: tweak config
[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
26 # Stable enough, clearer, and faster than the default /dev/disk/by-id
27 boot.zfs.devNodes = "/dev/disk/by-partlabel";
28 # Not useful so far.
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" ];
35
36 #boot.zfs.package = pkgs.zfs_unstable;
37
38 # Enables periodic scrubbing of ZFS pools.
39 services.zfs.autoScrub.enable = true;
40 services.zfs.autoScrub.interval = "Sun *-*-08..14 00:15:00";
41
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";
47
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
52 '';
53
54 environment.systemPackages = [
55 pkgs.lzop # For remote syncoid
56 pkgs.mbuffer # For remote syncoid
57 pkgs.sanoid
58 ];
59
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";
64 unitConfig = {
65 ConditionPathIsDirectory = "/sys/module/zfs";
66 StartLimitIntervalSec = 0;
67 };
68 after = [ "systemd-modules-load.service" ];
69 path = lib.mkBefore [ "/run/booted-system/sw" ];
70 serviceConfig = {
71 Type = "oneshot";
72 RemainAfterExit = true;
73 PrivateTmp = true;
74 SyslogIdentifier = "zfs-import@%i";
75 Restart = "no";
76 ExecStart =
77 pkgs.writeShellScript "zfs-import" ''
78 pool="$1"
79 set -eux
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
86 ''
87 + " %I";
88 };
89 };
90 }