summaryrefslogtreecommitdiff
path: root/nix/module.nix
blob: 35eba4c778a2d4a21e3b1f49b3df7d4f730f82db (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
self:
{ pkgs, lib, config, options, ... }:

let
  defaultUser = "webhooklistener";
  defaultGroup = "webhooklistener";

  cfg = config.services.webhook-listener;
in {
  options = with lib; {
    services.webhook-listener = {
      enable = mkEnableOption "Webhook listener";

      package = mkOption {
        description = "Package containing `webhook-listener` binary.";
        type = types.package;
        default = self.packages.${pkgs.system}.webhook-listener;
      };

      user = mkOption {
        description = ''
          The user to run the qBittorrent service as. This is also the
          user that will run the command.

          The user is not automatically created if it is changed from the default value.
        '';
        type = types.str;
        default = defaultUser;
      };

      group = mkOption {
        description = ''
          The group to run the webhook listener service as. This is
          also the group that will run the command.

          The group is not automatically created if it is changed from the default value.
        '';
        type = types.str;
        default = defaultGroup;
      };

      commands = mkOption {
        description = "List of event/command pairs, which will be matched against events from GitHub";
        type = with types; listOf (submodule {
          options = {
            event = mkOption {
              description = ''
                An event from the GitHub API.

                See [the GitHub documentation](https://docs.github.com/en/webhooks/webhook-events-and-payloads) for event types and data.
              '';
              type = types.str;
              example = "push";
            };

            command = mkOption {
              description = "The command to run upon receiving webhook event from GitHub.";
              type = types.str;
              example = "run-ci-or-something";
            };

            args = mkOption {
              description = "Additional arguments to be supplied to `command`.";
              type = with types; listOf str;
              default = [];
              example = [ "--some-option" ];
            };
          };
        });
      };

      secret-path = mkOption {
        description = "Path to file containing the secret given to GitHub.";
        type = types.path;
        example = "/run/github_secret.txt";
      };

      socket-path = mkOption {
        description = ''
          Path of socket file where the server will be listening.

          You should set up a redirect with your reverse proxy such
          that a POST request from GitHub (i.e. to the webhook url you
          give to GitHub) is translated to a request to `/` on this socket.
        '';
        type = types.path;
        readOnly = true;
      };
    };
  };

  config = lib.mkIf cfg.enable {
    # Create the user/group if required.
    users.users = lib.mkIf (cfg.user == defaultUser) {
      ${defaultUser} = {
        description = "Runs ${options.services.webhook-listener.enable.description}";
        group = cfg.group;
        isSystemUser = true;
      };
    };
    users.groups = lib.mkIf (cfg.group == defaultGroup) {
      ${defaultGroup} = {};
    };

    # Create socket for server.
    services.webhook-listener.socket-path = "/run/webhook-listener.sock";
    systemd.sockets."webhook-listener" = {
      unitConfig = {
        Description = "Socket for receiving webhook requests from GitHub";
        PartOf = [ "webhook-listener.service" ];
      };

      socketConfig = {
        ListenStream = config.services.webhook-listener.socket-path;
      };

      wantedBy = [ "sockets.target" ];
    };

    # Create the listening server
    systemd.services.webhook-listener = {
      unitConfig = {
        Description = "listening for webhook requests from GitHub";
        After = [ "network.target" "webhook-listener.socket" ];
        # Otherwise unit would need to create socket itself if started manually.
        Requires = [ "webhook-listener.socket" ];
      };

      serviceConfig =
        let
          config = {
            "secret_path" = cfg.secret-path;
            "commands" = cfg.commands;
          };

          config-file = pkgs.writers.writeJSON "config.json" config;
        in
        {
          Type = "simple";
          User = cfg.user;
          Group = cfg.group;
          ExecStart = "${cfg.package}/bin/webhook-listener ${config-file}";
        };
    };
  };
}