@@ -1,8 +1,6 @@
11{ config, lib, pkgs, utils, ... }:
22#
3−# todo:
4−# - crontab for scrubs, etc
5−# - zfs tunables
3+# TODO: zfs tunables
64
75with utils;
86with lib;
@@ -13,6 +11,7 @@ let
1311 cfgSnapshots = config.services.zfs.autoSnapshot;
1412 cfgSnapFlags = cfgSnapshots.flags;
1513 cfgScrub = config.services.zfs.autoScrub;
14+ cfgTrim = config.services.zfs.trim;
1615
1716 inInitrd = any (fs: fs == "zfs") config.boot.initrd.supportedFilesystems;
1817 inSystem = any (fs: fs == "zfs") config.boot.supportedFilesystems;
@@ -268,14 +267,26 @@ in
268267 };
269268 };
270269
271− services.zfs.autoScrub = {
272− enable = mkOption {
273− default = false;
274− type = types.bool;
270+ services.zfs.trim = {
271+ enable = mkEnableOption "Enables periodic TRIM on all ZFS pools.";
272+
273+ interval = mkOption {
274+ default = "weekly";
275+ type = types.str;
276+ example = "daily";
275277 description = ''
276− Enables periodic scrubbing of ZFS pools.
278+ How often we run trim. For most desktop and server systems
279+ a sufficient trimming frequency is once a week.
280+
281+ The format is described in
282+ <citerefentry><refentrytitle>systemd.time</refentrytitle>
283+ <manvolnum>7</manvolnum></citerefentry>.
277284 '';
278285 };
286+ };
287+
288+ services.zfs.autoScrub = {
289+ enable = mkEnableOption "Enables periodic scrubbing of ZFS pools.";
279290
280291 interval = mkOption {
281292 default = "Sun, 02:00";
@@ -535,5 +546,17 @@ in
535546 };
536547 };
537548 })
549+
550+ (mkIf cfgTrim.enable {
551+ systemd.services.zpool-trim = {
552+ description = "ZFS pools trim";
553+ after = [ "zfs-import.target" ];
554+ path = [ packages.zfsUser ];
555+ startAt = cfgTrim.interval;
556+ script = ''
557+ zpool list -H -o name | xargs -n1 zpool trim
558+ '';
559+ };
560+ })
538561 ];
539562}