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