fork/home-manager

git clone https://git.julia.blue/fork/home-manager

Robert HelgessonMerge PR #421575790441

master
2.2 KiB85 linesraw
1{ config, lib, pkgs, ... }:
2
3with lib;
4
5let
6  serviceConfig = config.services.borgmatic;
7  programConfig = config.programs.borgmatic;
8in {
9  meta.maintainers = [ maintainers.DamienCassou ];
10
11  options = {
12    services.borgmatic = {
13      enable = mkEnableOption "Borgmatic service";
14
15      frequency = mkOption {
16        type = types.str;
17        default = "hourly";
18        description = ''
19          How often to run borgmatic when
20          `services.borgmatic.enable = true`.
21          This value is passed to the systemd timer configuration as
22          the onCalendar option. See
23          {manpage}`systemd.time(7)`
24          for more information about the format.
25        '';
26      };
27    };
28  };
29
30  config = mkIf serviceConfig.enable {
31    assertions = [
32      (lib.hm.assertions.assertPlatform "services.borgmatic" pkgs
33        lib.platforms.linux)
34    ];
35
36    systemd.user = {
37      services.borgmatic = {
38        Unit = {
39          Description = "borgmatic backup";
40          # Prevent borgmatic from running unless the machine is
41          # plugged into power:
42          ConditionACPower = true;
43        };
44        Service = {
45          Type = "oneshot";
46
47          # Lower CPU and I/O priority:
48          Nice = 19;
49          CPUSchedulingPolicy = "batch";
50          IOSchedulingClass = "best-effort";
51          IOSchedulingPriority = 7;
52          IOWeight = 100;
53
54          Restart = "no";
55          LogRateLimitIntervalSec = 0;
56
57          # Delay start to prevent backups running during boot:
58          ExecStartPre = "${pkgs.coreutils}/bin/sleep 3m";
59
60          ExecStart = ''
61            ${pkgs.systemd}/bin/systemd-inhibit \
62              --who="borgmatic" \
63              --what="sleep:shutdown" \
64              --why="Prevent interrupting scheduled backup" \
65              ${programConfig.package}/bin/borgmatic \
66                --stats \
67                --verbosity -1 \
68                --list \
69                --syslog-verbosity 1
70          '';
71        };
72      };
73
74      timers.borgmatic = {
75        Unit.Description = "Run borgmatic backup";
76        Timer = {
77          OnCalendar = serviceConfig.frequency;
78          Persistent = true;
79          RandomizedDelaySec = "10m";
80        };
81        Install.WantedBy = [ "timers.target" ];
82      };
83    };
84  };
85}