@@ -0,0 +1,150 @@
1+{ config, lib, pkgs, ... }:
2+
3+with lib;
4+{
5+ options.services.restic.backups = mkOption {
6+ description = ''
7+ Periodic backups to create with Restic.
8+ '';
9+ type = types.attrsOf (types.submodule ({ name, config, ... }: {
10+ options = {
11+ passwordFile = mkOption {
12+ type = types.str;
13+ description = ''
14+ Read the repository password from a file.
15+ '';
16+ example = "/etc/nixos/restic-password";
17+
18+ };
19+
20+ repository = mkOption {
21+ type = types.str;
22+ description = ''
23+ repository to backup to.
24+ '';
25+ example = "sftp:backup@192.168.1.100:/backups/${name}";
26+ };
27+
28+ paths = mkOption {
29+ type = types.listOf types.str;
30+ default = [];
31+ description = ''
32+ Which paths to backup.
33+ '';
34+ example = [
35+ "/var/lib/postgresql"
36+ "/home/user/backup"
37+ ];
38+ };
39+
40+ timerConfig = mkOption {
41+ type = types.attrsOf types.str;
42+ default = {
43+ OnCalendar = "daily";
44+ };
45+ description = ''
46+ When to run the backup. See man systemd.timer for details.
47+ '';
48+ example = {
49+ OnCalendar = "00:05";
50+ RandomizedDelaySec = "5h";
51+ };
52+ };
53+
54+ user = mkOption {
55+ type = types.str;
56+ default = "root";
57+ description = ''
58+ As which user the backup should run.
59+ '';
60+ example = "postgresql";
61+ };
62+
63+ extraBackupArgs = mkOption {
64+ type = types.listOf types.str;
65+ default = [];
66+ description = ''
67+ Extra arguments passed to restic backup.
68+ '';
69+ example = [
70+ "--exclude-file=/etc/nixos/restic-ignore"
71+ ];
72+ };
73+
74+ extraOptions = mkOption {
75+ type = types.listOf types.str;
76+ default = [];
77+ description = ''
78+ Extra extended options to be passed to the restic --option flag.
79+ '';
80+ example = [
81+ "sftp.command='ssh backup@192.168.1.100 -i /home/user/.ssh/id_rsa -s sftp'"
82+ ];
83+ };
84+
85+ initialize = mkOption {
86+ type = types.bool;
87+ default = false;
88+ description = ''
89+ Create the repository if it doesn't exist.
90+ '';
91+ };
92+ };
93+ }));
94+ default = {};
95+ example = {
96+ localbackup = {
97+ paths = [ "/home" ];
98+ repository = "/mnt/backup-hdd";
99+ passwordFile = "/etc/nixos/secrets/restic-password";
100+ initialize = true;
101+ };
102+ remotebackup = {
103+ paths = [ "/home" ];
104+ repository = "sftp:backup@host:/backups/home";
105+ passwordFile = "/etc/nixos/secrets/restic-password";
106+ extraOptions = [
107+ "sftp.command='ssh backup@host -i /etc/nixos/secrets/backup-private-key -s sftp'"
108+ ];
109+ timerConfig = {
110+ OnCalendar = "00:05";
111+ RandomizedDelaySec = "5h";
112+ };
113+ };
114+ };
115+ };
116+
117+ config = {
118+ systemd.services =
119+ mapAttrs' (name: backup:
120+ let
121+ extraOptions = concatMapStrings (arg: " -o ${arg}") backup.extraOptions;
122+ connectTo = elemAt (splitString ":" backup.repository) 1;
123+ resticCmd = "${pkgs.restic}/bin/restic${extraOptions}";
124+ in nameValuePair "restic-backups-${name}" ({
125+ environment = {
126+ RESTIC_PASSWORD_FILE = backup.passwordFile;
127+ RESTIC_REPOSITORY = backup.repository;
128+ };
129+ path = with pkgs; [
130+ openssh
131+ ];
132+ restartIfChanged = false;
133+ serviceConfig = {
134+ Type = "oneshot";
135+ ExecStart = "${resticCmd} backup ${concatStringsSep " " backup.extraBackupArgs} ${concatStringsSep " " backup.paths}";
136+ User = backup.user;
137+ };
138+ } // optionalAttrs backup.initialize {
139+ preStart = ''
140+ ${resticCmd} snapshots || ${resticCmd} init
141+ '';
142+ })
143+ ) config.services.restic.backups;
144+ systemd.timers =
145+ mapAttrs' (name: backup: nameValuePair "restic-backups-${name}" {
146+ wantedBy = [ "timers.target" ];
147+ timerConfig = backup.timerConfig;
148+ }) config.services.restic.backups;
149+ };
150+}