aboutsummaryrefslogtreecommitdiff
path: root/modules/backup/web.nix
blob: b89d0a90d40228aed8c960f822bee9b83d081027 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
{
  pkgs,
  lib,
  config,
  mkInstanceServices,
  ...
}:
let
  instanceType = lib.types.submodule {
    options = {
      web = {
        enable = lib.mkEnableOption "enable Kopia web interface";
        guiAddress = lib.mkOption {
          type = lib.types.str;
          default = "127.0.0.1:51515";
        };
        serverUsername = lib.mkOption {
          type = lib.types.str;
          default = "admin";
          description = "Username for the Kopia web server(basic auth).";
        };
        environmentFile = lib.mkOption {
          type = lib.types.nullOr lib.types.path;
          default = null;
          description = "File containing environment variables for kopia web server like password.";
        };
      };
    };
  };
in
{
  options.services.kopia.instances = lib.mkOption {
    type = lib.types.attrsOf instanceType;
  };

  config = lib.mkIf config.services.kopia.enable {
    # systemd service for repositories open
    systemd.services =
      let
        mkWebService =
          # refactor with mkRepositoryArgs
          name: instance:
          lib.attrsets.nameValuePair "kopia-web-${name}" {
            description = "Kopia S3 web service";
            wants = [
              "kopia-repository-${name}.service"
            ];
            after = [ "kopia-repository-${name}.service" ];
            environment = {
              KOPIA_SERVER_USERNAME = instance.web.serverUsername;
            };
            script = ''
              export KOPIA_SERVER_USERNAME=${instance.web.serverUsername}
              # Start Kopia web server
              ${pkgs.kopia}/bin/kopia server start --insecure --address ${instance.web.guiAddress}
            '';
            serviceConfig = {
              Type = "simple";
              User = "${instance.user}";
              WorkingDirectory = "~";
              SetLoginEnvironment = true;
              EnvironmentFile = lib.mkIf (instance.web.environmentFile != null) instance.web.environmentFile;
              # retry on failure
              Restart = "on-failure";
              # wait 30 seconds before restarting
              RestartSec = "30";
            };
            unitConfig = {
              # limit the number of restarts to 5 in 1 day
              StartLimitInterval = "1d";
              StartLimitBurst = "5";
            };
          };
      in
      mkInstanceServices config.services.kopia.instances mkWebService;
  };
}