office: simple-scan: install
[julm/julm-nix.git] / nixos / profiles / zfs.nix
index e5b4db7c7d15b401037a0b7f469b91adc1fe0eaf..dff137f2fe193b6c9274f669e62b9af80e007747 100644 (file)
@@ -1,31 +1,34 @@
 { pkgs, lib, hostName, ... }:
 {
   # none is the recommended elevator with ZFS (which has its own I/O scheduler)
-  # and/or for SSD, whereas HDD could use mq-deadline.
   services.udev.extraRules = ''
     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"
-    ACTION=="add|change", KERNEL=="sd[a-z][0-9]*", ATTR{../queue/rotational}=="0", ATTR{../queue/scheduler}="none"
-    ACTION=="add|change", KERNEL=="nvme[0-9]*n[0-9]*p[0-9]*", ATTR{../queue/rotational}=="0", ATTR{../queue/scheduler}="none"
   '';
 
   boot.supportedFilesystems = [ "zfs" ];
   boot.initrd.supportedFilesystems = [ "zfs" ];
 
+  boot.kernelParams = [
+    # Disabled because the zfs kernel module is not signed
+    "module.sig_enforce=0"
+  ];
+
   # Using ZFS together with hibernation (suspend to disk)
   # may cause filesystem corruption.
   # See https://github.com/openzfs/zfs/issues/260
-  boot.kernelParams = [ "nohibernate" ];
+  boot.zfs.allowHibernation = false;
 
   # Stable enough, clearer, and faster than the default /dev/disk/by-id
   boot.zfs.devNodes = "/dev/disk/by-partlabel";
   # Not useful so far.
+  # See also https://github.com/NixOS/nixpkgs/issues/62644#issuecomment-1479523469
   boot.zfs.forceImportAll = false;
   # More resilient for remote hosts,
   # though it may call zpool clear.
   boot.zfs.forceImportRoot = true;
   boot.zfs.requestEncryptionCredentials = lib.mkDefault [ "${hostName}/root" ];
 
-  boot.zfs.enableUnstable = false;
+  #boot.zfs.package = pkgs.zfs_unstable;
 
   # Enables periodic scrubbing of ZFS pools.
   services.zfs.autoScrub.enable = true;
   services.zfs.trim.enable = true;
   services.zfs.trim.interval = "Sun *-*-01..07 00:15:00";
 
+  # Hide ZFS mountpoints from gio, hence nautilus or caja
+  systemd.services.zfs-mount.postStart = ''
+    /run/wrappers/bin/mount -t zfs | cut -f 1 -d ' ' |
+    xargs -n 1 -r -t /run/wrappers/bin/mount -o remount,x-gvfs-hide || true
+  '';
+
   environment.systemPackages = [
     pkgs.lzop # For remote syncoid
     pkgs.mbuffer # For remote syncoid
     pkgs.sanoid
   ];
+
+  # Force zpool import, even if the disk has not been exported,
+  # (ie. still imported onto another computer).
+  systemd.services."zfs-import@" = {
+    description = "ZFS import pool: %I";
+    unitConfig = {
+      ConditionPathIsDirectory = "/sys/module/zfs";
+      StartLimitIntervalSec = 0;
+    };
+    after = [ "systemd-modules-load.service" ];
+    path = lib.mkBefore [ "/run/booted-system/sw" ];
+    serviceConfig = {
+      Type = "oneshot";
+      RemainAfterExit = true;
+      PrivateTmp = true;
+      SyslogIdentifier = "zfs-import@%i";
+      Restart = "no";
+      ExecStart = pkgs.writeShellScript "zfs-import" ''
+        pool="$1"
+        set -eux
+        zpool status "$pool" ||
+        zpool import -lFd /dev/disk/by-id/ -o cachefile=none "$pool" ||
+        zpool reopen "$pool" ||
+        zpool import -lfd /dev/disk/by-id/ -o cachefile=none "$pool" ||
+        zpool clear -nFX "$pool"
+        ${pkgs.systemd}/bin/systemctl restart zfs-mount.service
+      '' + " %I";
+    };
+  };
 }