aboutsummaryrefslogtreecommitdiffstats
path: root/module/default.nix
blob: 0151f65659892b2ec6a755291b9de46e58409c30 (plain) (blame)
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
flake: { lib, config, pkgs, ... }:
with lib;
let
  inherit (flake.packages.${pkgs.stdenv.hostPlatform.system}) oeuf-recvkv6;
  inherit (flake.packages.${pkgs.stdenv.hostPlatform.system}) oeuf-archiver;

  cfg = config.services.oeuf-recvkv6;
  archiverCfg = config.services.oeuf-archiver;
in
{
  options.services.oeuf-recvkv6 = {
    enable = mkEnableOption "oeuf-recvkv6";
    ndovProduction = mkEnableOption "usage of the NDOV Loket production ZeroMQ server";
    metricsAddr = mkOption {
      type = types.str;
    };
  };

  options.services.oeuf-archiver = with types; {
    enable = mkEnableOption "oeuf-archiver";
    s3 = mkOption {
      type = submodule {
        options = {
          accessKeyIDFile = mkOption {
            type = str;
          };
          secretAccessKeyFile = mkOption {
            type = str;
          };
          provider = mkOption {
            type = str;
          };
          region = mkOption {
            type = str;
          };
          endpoint = mkOption {
            type = str;
          };
          bucket = mkOption {
            type = str;
          };
        };
      };
    };
    prometheusPushURL = mkOption {
      type = str;
    };
    supplementaryServiceGroups = mkOption {
      type = listOf str;
    };
  };

  config = mkIf (cfg.enable || archiverCfg.enable) (mkMerge [
    {
      users.users.oeuf = {
        description = "oeuf service user";
        isSystemUser = true;
        group = "oeuf";
      };

      users.groups.oeuf = { };
    }
    (mkIf cfg.enable {
      systemd.services.oeuf-recvkv6 = {
        wants = [ "network-online.target" ];
        wantedBy = [ "multi-user.target" ];
        environment = {
          METRICS_ADDR = cfg.metricsAddr;
          NDOV_PRODUCTION = lib.boolToString cfg.ndovProduction;
        };
        serviceConfig = {
          User = config.users.users.oeuf.name;
          Group = config.users.users.oeuf.group;
          Restart = "always";
          StateDirectory = "oeuf";
          WorkingDirectory = "/var/lib/oeuf";
          ExecStart = "${lib.getBin oeuf-recvkv6}/bin/oeuf-recvkv6";
        };
      };
    })
    (mkIf archiverCfg.enable {
      systemd.timers.oeuf-archiver = {
        wantedBy = [ "timers.target" ];
        partOf = [ "oeuf-archiver.service" ];
        timerConfig = {
          OnBootSec = "5m";
          OnUnitActiveSec = "5m";
          Unit = "oeuf-archiver.service";
        };
      };

      systemd.services.oeuf-archiver = {
        wants = [ "network-online.target" ];
        environment = {
          S3_PROVIDER = archiverCfg.s3.provider;
          S3_REGION = archiverCfg.s3.region;
          S3_ENDPOINT = archiverCfg.s3.endpoint;
          S3_BUCKET = archiverCfg.s3.bucket;
          PROMETHEUS_PUSH_URL = archiverCfg.prometheusPushURL;
        };
        script = ''
          export S3_ACCESS_KEY_ID="$(cat ${archiverCfg.s3.accessKeyIDFile})"
          export S3_SECRET_ACCESS_KEY="$(cat ${archiverCfg.s3.secretAccessKeyFile})"
          ${lib.getBin oeuf-archiver}/bin/oeuf-archiver
        '';
        serviceConfig = {
          Type = "oneshot";
          User = config.users.users.oeuf.name;
          Group = config.users.users.oeuf.group;
          SupplementaryGroups = archiverCfg.supplementaryServiceGroups;
          StateDirectory = "oeuf";
          WorkingDirectory = "/var/lib/oeuf";
          AmbientCapabilities = "CAP_NET_BIND_SERVICE";
        };
      };
    })
  ]);
}