1{
2 config,
3 lib,
4 pkgs,
5 ...
6}:
7let
8 cfg = config.services.rsnapshot;
9 cfgfile = pkgs.writeText "rsnapshot.conf" ''
10 config_version 1.2
11 cmd_cp ${pkgs.coreutils}/bin/cp
12 cmd_rm ${pkgs.coreutils}/bin/rm
13 cmd_rsync ${pkgs.rsync}/bin/rsync
14 cmd_ssh ${pkgs.openssh}/bin/ssh
15 cmd_logger ${pkgs.inetutils}/bin/logger
16 cmd_du ${pkgs.coreutils}/bin/du
17 cmd_rsnapshot_diff ${pkgs.rsnapshot}/bin/rsnapshot-diff
18 lockfile /run/rsnapshot.pid
19 link_dest 1
20
21 ${cfg.extraConfig}
22 '';
23in
24{
25 options = {
26 services.rsnapshot = {
27 enable = lib.mkEnableOption "rsnapshot backups";
28 enableManualRsnapshot = lib.mkOption {
29 description = "Whether to enable manual usage of the rsnapshot command with this module.";
30 default = true;
31 type = lib.types.bool;
32 };
33
34 extraConfig = lib.mkOption {
35 default = "";
36 example = ''
37 retains hourly 24
38 retain daily 365
39 backup /home/ localhost/
40 '';
41 type = lib.types.lines;
42 description = ''
43 rsnapshot configuration option in addition to the defaults from
44 rsnapshot and this module.
45
46 Note that tabs are required to separate option arguments, and
47 directory names require trailing slashes.
48
49 The "extra" in the option name might be a little misleading right
50 now, as it is required to get a functional configuration.
51 '';
52 };
53
54 cronIntervals = lib.mkOption {
55 default = { };
56 example = {
57 hourly = "0 * * * *";
58 daily = "50 21 * * *";
59 };
60 type = lib.types.attrsOf lib.types.str;
61 description = ''
62 Periodicity at which intervals should be run by cron.
63 Note that the intervals also have to exist in configuration
64 as retain options.
65 '';
66 };
67 };
68 };
69
70 config = lib.mkIf cfg.enable (
71 lib.mkMerge [
72 {
73 services.cron.systemCronJobs = lib.mapAttrsToList (
74 interval: time: "${time} root ${pkgs.rsnapshot}/bin/rsnapshot -c ${cfgfile} ${interval}"
75 ) cfg.cronIntervals;
76 }
77 (lib.mkIf cfg.enableManualRsnapshot {
78 environment.systemPackages = [ pkgs.rsnapshot ];
79 environment.etc."rsnapshot.conf".source = cfgfile;
80 })
81 ]
82 );
83}