summaryrefslogtreecommitdiffstats
path: root/nixos/modules/services/matrix/draupnir.nix
blob: e4dfa5d2917b29d9876d3681192fa9553cc7dee0 (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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
{
  config,
  options,
  lib,
  pkgs,
  ...
}:

let
  cfg = config.services.draupnir;
  opt = options.services.draupnir;

  format = pkgs.formats.yaml { };
  configFile = format.generate "draupnir.yaml" cfg.settings;

  inherit (lib)
    literalExpression
    mkEnableOption
    mkOption
    mkPackageOption
    mkRemovedOptionModule
    mkRenamedOptionModule
    types
    ;
in
{
  imports = [
    # Removed options for those migrating from the Mjolnir module
    (mkRenamedOptionModule
      [ "services" "draupnir" "dataPath" ]
      [ "services" "draupnir" "settings" "dataPath" ]
    )
    (mkRenamedOptionModule
      [ "services" "draupnir" "homeserverUrl" ]
      [ "services" "draupnir" "settings" "homeserverUrl" ]
    )
    (mkRenamedOptionModule
      [ "services" "draupnir" "managementRoom" ]
      [ "services" "draupnir" "settings" "managementRoom" ]
    )
    (mkRenamedOptionModule
      [ "services" "draupnir" "accessTokenFile" ]
      [ "services" "draupnir" "secrets" "accessToken" ]
    )
    (mkRemovedOptionModule [ "services" "draupnir" "pantalaimon" ] ''
      `services.draupnir.pantalaimon.*` has been removed because it depends on the deprecated and vulnerable
      libolm library for end-to-end encryption and upstream support for Pantalaimon in Draupnir is limited.
      See <https://the-draupnir-project.github.io/draupnir-documentation/bot/encryption> for details.
      If you nontheless require E2EE via Pantalaimon, you can configure `services.pantalaimon-headless.instances`
      yourself and use that with `services.draupnir.settings.pantalaimon` and `services.draupnir.secrets.pantalaimon.password`.
    '')
  ];

  options.services.draupnir = {
    enable = mkEnableOption "Draupnir, a moderations bot for Matrix";

    package = mkPackageOption pkgs "draupnir" { };

    settings = mkOption {
      example = literalExpression ''
        {
          homeserverUrl = "https://matrix.org";
          managementRoom = "#moderators:example.org";

          autojoinOnlyIfManager = true;
          automaticallyRedactForReasons = [ "spam" "advertising" ];
        }
      '';
      description = ''
        Free-form settings written to Draupnir's configuration file.
        See [Draupnir's default configuration](https://github.com/the-draupnir-project/Draupnir/blob/main/config/default.yaml) for available settings.
      '';
      default = { };
      type = types.submodule {
        freeformType = format.type;
        options = {
          homeserverUrl = mkOption {
            type = types.str;
            example = "https://matrix.org";
            description = ''
              Base URL of the Matrix homeserver that provides the Client-Server API.

              ::: {.note}
              When using Pantalaimon, set this to the Pantalaimon URL and
              {option}`${opt.settings}.rawHomeserverUrl` to the public URL.
              :::
            '';
          };

          rawHomeserverUrl = mkOption {
            type = types.str;
            example = "https://matrix.org";
            default = cfg.settings.homeserverUrl;
            defaultText = literalExpression "config.${opt.settings}.homeserverUrl";
            description = ''
              Public base URL of the Matrix homeserver that provides the Client-Server API when using the Draupnir's
              [Report forwarding feature](https://the-draupnir-project.github.io/draupnir-documentation/bot/homeserver-administration#report-forwarding).

              ::: {.warning}
              When using Pantalaimon, do not set this to the Pantalaimon URL!
              :::
            '';
          };

          managementRoom = mkOption {
            type = types.str;
            example = "#moderators:example.org";
            description = ''
              The room ID or alias where moderators can use the bot's functionality.

              The bot has no access controls, so anyone in this room can use the bot - secure this room!
              Do not enable end-to-end encryption for this room, unless set up with Pantalaimon.

              ::: {.warning}
              When using a room alias, make sure the alias used is on the local homeserver!
              This prevents an issue where the control room becomes undefined when the alias can't be resolved.
              :::
            '';
          };

          dataPath = mkOption {
            type = types.path;
            readOnly = true;
            default = "/var/lib/draupnir";
            description = ''
              The path Draupnir will store its state/data in.

              ::: {.warning}
              This option is read-only.
              :::

              ::: {.note}
              If you want to customize where this data is stored, use a bind mount.
              :::
            '';
          };
        };
      };
    };

    secrets = {
      accessToken = mkOption {
        type = types.nullOr types.path;
        default = null;
        description = ''
          File containing the access token for Draupnir's Matrix account
          to be used in place of {option}`${opt.settings}.accessToken`.
        '';
      };

      pantalaimon.password = mkOption {
        type = types.nullOr types.path;
        default = null;
        description = ''
          File containing the password for Draupnir's Matrix account when used in
          conjunction with Pantalaimon to be used in place of
          {option}`${opt.settings}.pantalaimon.password`.

          ::: {.warning}
          Take note that upstream has limited Pantalaimon and E2EE support:
          <https://the-draupnir-project.github.io/draupnir-documentation/bot/encryption> and
          <https://the-draupnir-project.github.io/draupnir-documentation/shared/dogfood#e2ee-support>.
          :::
        '';
      };

      web.synapseHTTPAntispam.authorization = mkOption {
        type = types.nullOr types.path;
        default = null;
        description = ''
          File containing the secret token when using the Synapse HTTP Antispam module
          to be used in place of
          {option}`${opt.settings}.web.synapseHTTPAntispam.authorization`.

          See <https://the-draupnir-project.github.io/draupnir-documentation/bot/synapse-http-antispam> for details.
        '';
      };
    };
  };

  config = lib.mkIf cfg.enable {
    assertions = [
      {
        # Removed option for those migrating from the Mjolnir module - mkRemovedOption module does *not* work with submodules.
        assertion = !(cfg.settings ? protectedRooms);
        message = "Unset ${opt.settings}.protectedRooms, as it is unsupported on Draupnir. Add these rooms via `!draupnir rooms add` instead.";
      }
    ];

    systemd.services.draupnir = {
      description = "Draupnir - a moderation bot for Matrix";
      wants = [
        "network-online.target"
        "matrix-synapse.service"
        "conduit.service"
        "dendrite.service"
      ];
      after = [
        "network-online.target"
        "matrix-synapse.service"
        "conduit.service"
        "dendrite.service"
      ];
      wantedBy = [ "multi-user.target" ];

      startLimitIntervalSec = 0;
      serviceConfig = {
        ExecStart = toString (
          [
            (lib.getExe cfg.package)
            "--draupnir-config"
            configFile
          ]
          ++ lib.optionals (cfg.secrets.accessToken != null) [
            "--access-token-path"
            "%d/access_token"
          ]
          ++ lib.optionals (cfg.secrets.pantalaimon.password != null) [
            "--pantalaimon-password-path"
            "%d/pantalaimon_password"
          ]
          ++ lib.optionals (cfg.secrets.web.synapseHTTPAntispam.authorization != null) [
            "--http-antispam-authorization-path"
            "%d/http_antispam_authorization"
          ]
        );

        WorkingDirectory = "/var/lib/draupnir";
        StateDirectory = "draupnir";
        StateDirectoryMode = "0700";
        ProtectHome = true;
        PrivateDevices = true;
        Restart = "on-failure";
        RestartSec = "5s";
        DynamicUser = true;
        LoadCredential =
          lib.optionals (cfg.secrets.accessToken != null) [
            "access_token:${cfg.secrets.accessToken}"
          ]
          ++ lib.optionals (cfg.secrets.pantalaimon.password != null) [
            "pantalaimon_password:${cfg.secrets.pantalaimon.password}"
          ]
          ++ lib.optionals (cfg.secrets.web.synapseHTTPAntispam.authorization != null) [
            "http_antispam_authorization:${cfg.secrets.web.synapseHTTPAntispam.authorization}"
          ];
      };
    };
  };

  meta = {
    doc = ./draupnir.md;
    maintainers = with lib.maintainers; [
      RorySys
      emilylange
    ];
  };
}