summaryrefslogtreecommitdiff
path: root/module.nix
blob: a418c293d270e6f7fb73f50f63eac05a7cfa91d4 (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
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
{ self }:

{ config, pkgs, lib, ... }:

with lib;
let
  cfg = config.services.label-tracker;
in {
  options = {
    services.label-tracker = {
      enable = mkEnableOption "the github label tracer";

      apiToken = mkOption {
        type = types.path;
        description = ''
          Path to the github api token file. Must follow the format
          of systemd EnvironmentFile and contain a GITHUB_API_TOKEN
          with an appropriate token.
        '';
      };

      group = mkOption {
        type = types.str;
        description = ''
          Group to run with. Should usually be the webserver group.
        '';
      };

      startAt = mkOption {
        type = types.either types.str (types.listOf types.str);
        default = [];
        description = ''
          When to run syncs for all tracked labels. See systemd.time(7).
        '';
      };

      track = mkOption {
        type = types.attrsOf (types.submodule {
          options = {
            owner = mkOption {
              type = types.str;
              description = "Owner of the repo.";
            };

            repo = mkOption {
              type = types.str;
              description = "Name of the repo.";
            };

            label = mkOption {
              type = types.str;
              description = "Name of the label.";
            };

            channels = mkOption {
              type = types.attrsOf (types.listOf types.str);
              description = ''
                Branches to check for PR landing events (ie. changes of a
                PR showing up in a given branch). Useful for channels.

                Branch names my regular expressions. Targets can refer to
                captures in the base ref regex with $1, $2 etc.
              '';
              default = {};
            };
          };
        });
        default = [];
        description = ''
          Repos and labels to track.
        '';
      };

      feedAgeLimit = mkOption {
        type = types.ints.unsigned;
        default = 240;
        description = ''
          Age cutoff for generated feed entries, in hours.
        '';
      };
    };
  };

  config = mkIf cfg.enable {
    users.users.label-tracker = {
      isSystemUser = true;
      group = cfg.group;
    };

    systemd.services.label-tracker = {
      startAt = cfg.startAt;

      path = [ pkgs.git self.packages.${config.nixpkgs.system}.label-tracker ];
      environment.RUST_LOG = "info";
      script = ''
        set -euo pipefail
        shopt -s inherit_errexit failglob

        ${concatStringsSep "\n"
          (mapAttrsToList
            (key: args:
              let
                name = escapeShellArg key;
                owner = escapeShellArg args.owner;
                repo = escapeShellArg args.repo;
                label = escapeShellArg args.label;
                patterns = escapeShellArg
                  (concatStringsSep ","
                    (mapAttrsToList
                      (base: targets: "${base}:${concatStringsSep " " targets}")
                      args.channels));
              in ''
                (
                  umask 0077
                  if ! [ -e states/${name} ]; then
                    mkdir -p states
                    label-tracker init states/${name} ${owner} ${repo} ${label}
                  fi
                  label-tracker sync-issues states/${name}
                  label-tracker sync-prs states/${name} \
                    -l states/${name}.git \
                    -p ${patterns}
                )
                (
                  umask 0027
                  mkdir -p results/${name}
                  umask 0037
                  label-tracker emit-issues states/${name} \
                    -a ${toString cfg.feedAgeLimit} \
                    > results/${name}/.issues.xml.tmp
                  mv results/${name}/.issues.xml.tmp results/${name}/issues.xml
                  label-tracker emit-prs states/${name} \
                    -a ${toString cfg.feedAgeLimit} \
                    > results/${name}/.prs.xml.tmp
                  mv results/${name}/.prs.xml.tmp results/${name}/prs.xml
                )
              '')
            cfg.track)}
      '';

      serviceConfig = {
        EnvironmentFile = cfg.apiToken;

        User = "label-tracker";
        Group = cfg.group;
        StateDirectory = "label-tracker";
        WorkingDirectory = "/var/lib/label-tracker";
        AmbientCapabilities = [ "" ];
        CapabilityBoundingSet = [ "" ];
        LockPersonality = true;
        MemoryDenyWriteExecute = true;
        NoNewPrivileges = true;
        PrivateDevices = true;
        PrivateTmp = true;
        ProcSubset = "pid";
        ProtectClock = true;
        ProtectControlGroups = true;
        ProtectHome = true;
        ProtectHostname = true;
        ProtectKernelLogs = true;
        ProtectKernelModules = true;
        ProtectKernelTunables = true;
        ProtectProc = "invisible";
        ProtectSystem = true;
        RemoveIPC = true;
        RestrictAddressFamilies = "AF_INET AF_INET6";
        RestrictNamespaces = true;
        RestrictRealtime = true;
        RestrictSUIDSGID = true;
        SystemCallArchitectures = "native";
        SystemCallFilter = [
          "@system-service"
          "~ @resources @privileged"
        ];
        UMask = "0027";
      };
    };
  };
}