summaryrefslogtreecommitdiffstats
path: root/configs/btrfs-scrub.nix
blob: 3fa168b321055888298befb73c72c2c9d4f487f4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
{ config, lib, utils, ... }: let

  names =
    map (fs: "btrfs-scrub-${utils.escapeSystemdPath fs}")
        config.services.btrfs.autoScrub.fileSystems;

in {

  assertions = [
    {
      assertion = names != [];
      message = ''
        configs/btrfs-scrub.nix: services.btrfs.autoScrub.fileSystems is empty.
        Most likely the system doesn't use btrfs but imports this file.
      '';
    }
    {
      assertion = lib.all (name: builtins.hasAttr name config.systemd.timers) names;
      message = ''
        configs/btrfs-scrub.nix: services.btrfs.autoScrub has no timers named
        after its file systems.  Most likely it uses template units now.
      '';
    }
  ];

  services.btrfs.autoScrub.enable = true;

  # Not on battery.  A scrub skipped for that waits for the next week's.
  systemd.services = lib.genAttrs names (_: {
    unitConfig.ConditionACPower = true;
  });

  systemd.timers = lib.genAttrs names (_: {
    timerConfig = {
      OnCalendar = lib.mkForce "Mon *-*-* 07:00:00";
      # NixOS allows a day, which would move the scrub out of its window.
      AccuracySec = lib.mkForce "1min";
      RandomizedDelaySec = "4h";
    };
  });

  krebs.on-failure.plans = lib.genAttrs names (_: {});
}