summaryrefslogtreecommitdiffstats
path: root/krebs/2configs/shack/prometheus/irc-hooks.nix
blob: 07bb2423b937c609a207ccd253a675c915f028db (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
{ config
, lib
, pkgs
, ...
}:
let
  irc-alerts = pkgs.writers.writePython3 "irc-alerts" { 
    flakeIgnore = [ "E501" ];
  } (builtins.readFile ./irc-alerts.py);
  endpoints = {
    binaergewitter = {
      url = "irc+tls://puyak-alerts@irc.libera.chat:6697/#binaergewitter-alerts";
      port = 9223;
    };
  };
in
{
  systemd.sockets =
    lib.mapAttrs'
      (name: opts:
        lib.nameValuePair "irc-alerts-${name}" {
          description = "Receive http hook and send irc message for ${name}";
          wantedBy = [ "sockets.target" ];
          listenStreams = [ "[::]:${builtins.toString opts.port}" ];
        }) endpoints;

  systemd.services =
    lib.mapAttrs'
      (name: opts:
        let
          serviceName = "irc-alerts-${name}";
          hasPassword = opts.passwordFile or null != null;
        in
        lib.nameValuePair serviceName {
          description = "Receive http hook and send irc message for ${name}";
          requires = [ "irc-alerts-${name}.socket" ];
          serviceConfig =
            {
              Environment =
                [
                  "IRC_URL=${opts.url}"
                  "DEBUG=y"
                ]
                ++ lib.optional hasPassword "IRC_PASSWORD_FILE=/run/${serviceName}/password";
              DynamicUser = true;
              User = serviceName;
              ExecStart = irc-alerts;
            }
            // lib.optionalAttrs hasPassword {
              PermissionsStartOnly = true;
              ExecStartPre =
                "${pkgs.coreutils}/bin/install -m400 "
                + "-o ${serviceName} -g ${serviceName} "
                + "${config.sops.secrets.prometheus-irc-password.path} "
                + "/run/${serviceName}/password";
              RuntimeDirectory = serviceName;
            };
        }) endpoints;
}