@@ -0,0 +1,107 @@
1+{ config, lib, pkgs, ... }:
2+
3+with lib;
4+
5+let
6+ cfg = config.services.restic.server;
7+in
8+{
9+ meta.maintainers = [ maintainers.bachp ];
10+
11+ options.services.restic.server = {
12+ enable = mkEnableOption "Restic REST Server";
13+
14+ listenAddress = mkOption {
15+ default = ":8000";
16+ example = "127.0.0.1:8080";
17+ type = types.str;
18+ description = "Listen on a specific IP address and port.";
19+ };
20+
21+ dataDir = mkOption {
22+ default = "/var/lib/restic";
23+ type = types.path;
24+ description = "The directory for storing the restic repository.";
25+ };
26+
27+ appendOnly = mkOption {
28+ default = false;
29+ type = types.bool;
30+ description = ''
31+ Enable append only mode.
32+ This mode allows creation of new backups but prevents deletion and modification of existing backups.
33+ This can be useful when backing up systems that have a potential of being hacked.
34+ '';
35+ };
36+
37+ privateRepos = mkOption {
38+ default = false;
39+ type = types.bool;
40+ description = ''
41+ Enable private repos.
42+ Grants access only when a subdirectory with the same name as the user is specified in the repository URL.
43+ '';
44+ };
45+
46+ prometheus = mkOption {
47+ default = false;
48+ type = types.bool;
49+ description = "Enable Prometheus metrics at /metrics.";
50+ };
51+
52+ extraFlags = mkOption {
53+ type = types.listOf types.str;
54+ default = [];
55+ description = ''
56+ Extra commandline options to pass to Restic REST server.
57+ '';
58+ };
59+
60+ package = mkOption {
61+ default = pkgs.restic-rest-server;
62+ defaultText = "pkgs.restic-rest-server";
63+ type = types.package;
64+ description = "Restic REST server package to use.";
65+ };
66+ };
67+
68+ config = mkIf cfg.enable {
69+ systemd.services.restic-rest-server = {
70+ description = "Restic REST Server";
71+ after = [ "network.target" ];
72+ wantedBy = [ "multi-user.target" ];
73+ serviceConfig = {
74+ ExecStart = ''
75+ ${cfg.package}/bin/rest-server \
76+ --listen ${cfg.listenAddress} \
77+ --path ${cfg.dataDir} \
78+ ${optionalString cfg.appendOnly "--append-only"} \
79+ ${optionalString cfg.privateRepos "--private-repos"} \
80+ ${optionalString cfg.prometheus "--prometheus"} \
81+ ${escapeShellArgs cfg.extraFlags} \
82+ '';
83+ Type = "simple";
84+ User = "restic";
85+ Group = "restic";
86+
87+ # Security hardening
88+ ReadWritePaths = [ cfg.dataDir ];
89+ PrivateTmp = true;
90+ ProtectSystem = "strict";
91+ ProtectKernelTunables = true;
92+ ProtectKernelModules = true;
93+ ProtectControlGroups = true;
94+ PrivateDevices = true;
95+ };
96+ };
97+
98+ users.extraUsers.restic = {
99+ group = "restic";
100+ home = cfg.dataDir;
101+ createHome = true;
102+ uid = config.ids.uids.restic;
103+ };
104+
105+ users.extraGroups.restic.gid = config.ids.uids.restic;
106+ };
107+}