about summary refs log tree commit diff
path: root/nixos/modules/services/matrix/mautrix-meta.nix
blob: b8a5cdc72065bda6c130d81546098eea3f3691b4 (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
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
{ config, pkgs, lib, ... }:

let
  settingsFormat = pkgs.formats.yaml {};

  upperConfig = config;
  cfg = config.services.mautrix-meta;
  upperCfg = cfg;

  fullDataDir = cfg: "/var/lib/${cfg.dataDir}";

  settingsFile = cfg: "${fullDataDir cfg}/config.yaml";
  settingsFileUnsubstituted = cfg: settingsFormat.generate "mautrix-meta-config.yaml" cfg.settings;

  metaName = name: "mautrix-meta-${name}";

  enabledInstances = lib.filterAttrs (name: config: config.enable) config.services.mautrix-meta.instances;
  registerToSynapseInstances = lib.filterAttrs (name: config: config.enable && config.registerToSynapse) config.services.mautrix-meta.instances;
in {
  options = {
    services.mautrix-meta = {

      package = lib.mkPackageOption pkgs "mautrix-meta" { };

      instances = lib.mkOption {
        type = lib.types.attrsOf (lib.types.submodule ({ config, name, ... }: {

          options = {

            enable = lib.mkEnableOption "Mautrix-Meta, a Matrix <-> Facebook and Matrix <-> Instagram hybrid puppeting/relaybot bridge";

            dataDir = lib.mkOption {
              type = lib.types.str;
              default = metaName name;
              description = ''
                Path to the directory with database, registration, and other data for the bridge service.
                This path is relative to `/var/lib`, it cannot start with `../` (it cannot be outside of `/var/lib`).
              '';
            };

            registrationFile = lib.mkOption {
              type = lib.types.path;
              readOnly = true;
              description = ''
                Path to the yaml registration file of the appservice.
              '';
            };

            registerToSynapse = lib.mkOption {
              type = lib.types.bool;
              default = true;
              description = ''
                Whether to add registration file to `services.matrix-synapse.settings.app_service_config_files` and
                make Synapse wait for registration service.
              '';
            };

            settings = lib.mkOption rec {
              apply = lib.recursiveUpdate default;
              inherit (settingsFormat) type;
              default = {
                homeserver = {
                  software = "standard";

                  domain = "";
                  address = "";
                };

                appservice = {
                  id = "";

                  database = {
                    type = "sqlite3-fk-wal";
                    uri = "file:${fullDataDir config}/mautrix-meta.db?_txlock=immediate";
                  };

                  bot = {
                    username = "";
                  };

                  hostname = "localhost";
                  port = 29319;
                  address = "http://${config.settings.appservice.hostname}:${toString config.settings.appservice.port}";
                };

                meta = {
                  mode = "";
                };

                bridge = {
                  # Enable encryption by default to make the bridge more secure
                  encryption = {
                    allow = true;
                    default = true;
                    require = true;

                    # Recommended options from mautrix documentation
                    # for additional security.
                    delete_keys = {
                      dont_store_outbound = true;
                      ratchet_on_decrypt = true;
                      delete_fully_used_on_decrypt = true;
                      delete_prev_on_new_session = true;
                      delete_on_device_delete = true;
                      periodically_delete_expired = true;
                      delete_outdated_inbound = true;
                    };

                    verification_levels = {
                      receive = "cross-signed-tofu";
                      send = "cross-signed-tofu";
                      share = "cross-signed-tofu";
                    };
                  };

                  permissions = {};
                };

                logging = {
                  min_level = "info";
                  writers = lib.singleton {
                    type = "stdout";
                    format = "pretty-colored";
                    time_format = " ";
                  };
                };
              };
              defaultText = ''
              {
                homeserver = {
                  software = "standard";
                  address = "https://''${config.settings.homeserver.domain}";
                };

                appservice = {
                  database = {
                    type = "sqlite3-fk-wal";
                    uri = "file:''${fullDataDir config}/mautrix-meta.db?_txlock=immediate";
                  };

                  hostname = "localhost";
                  port = 29319;
                  address = "http://''${config.settings.appservice.hostname}:''${toString config.settings.appservice.port}";
                };

                bridge = {
                  # Require encryption by default to make the bridge more secure
                  encryption = {
                    allow = true;
                    default = true;
                    require = true;

                    # Recommended options from mautrix documentation
                    # for optimal security.
                    delete_keys = {
                      dont_store_outbound = true;
                      ratchet_on_decrypt = true;
                      delete_fully_used_on_decrypt = true;
                      delete_prev_on_new_session = true;
                      delete_on_device_delete = true;
                      periodically_delete_expired = true;
                      delete_outdated_inbound = true;
                    };

                    verification_levels = {
                      receive = "cross-signed-tofu";
                      send = "cross-signed-tofu";
                      share = "cross-signed-tofu";
                    };
                  };
                };

                logging = {
                  min_level = "info";
                  writers = lib.singleton {
                    type = "stdout";
                    format = "pretty-colored";
                    time_format = " ";
                  };
                };
              };
              '';
              description = ''
                {file}`config.yaml` configuration as a Nix attribute set.
                Configuration options should match those described in
                [example-config.yaml](https://github.com/mautrix/meta/blob/main/example-config.yaml).

                Secret tokens should be specified using {option}`environmentFile`
                instead
              '';
            };

            environmentFile = lib.mkOption {
              type = lib.types.nullOr lib.types.path;
              default = null;
              description = ''
                File containing environment variables to substitute when copying the configuration
                out of Nix store to the `services.mautrix-meta.dataDir`.

                Can be used for storing the secrets without making them available in the Nix store.

                For example, you can set `services.mautrix-meta.settings.appservice.as_token = "$MAUTRIX_META_APPSERVICE_AS_TOKEN"`
                and then specify `MAUTRIX_META_APPSERVICE_AS_TOKEN="{token}"` in the environment file.
                This value will get substituted into the configuration file as as token.
              '';
            };

            serviceDependencies = lib.mkOption {
              type = lib.types.listOf lib.types.str;
              default =
                [ config.registrationServiceUnit ] ++
                (lib.lists.optional upperConfig.services.matrix-synapse.enable upperConfig.services.matrix-synapse.serviceUnit) ++
                (lib.lists.optional upperConfig.services.matrix-conduit.enable "matrix-conduit.service") ++
                (lib.lists.optional upperConfig.services.dendrite.enable "dendrite.service");

              defaultText = ''
                [ config.registrationServiceUnit ] ++
                (lib.lists.optional upperConfig.services.matrix-synapse.enable upperConfig.services.matrix-synapse.serviceUnit) ++
                (lib.lists.optional upperConfig.services.matrix-conduit.enable "matrix-conduit.service") ++
                (lib.lists.optional upperConfig.services.dendrite.enable "dendrite.service");
              '';
              description = ''
                List of Systemd services to require and wait for when starting the application service.
              '';
            };

            serviceUnit = lib.mkOption {
              type = lib.types.str;
              readOnly = true;
              description = ''
                The systemd unit (a service or a target) for other services to depend on if they
                need to be started after matrix-synapse.

                This option is useful as the actual parent unit for all matrix-synapse processes
                changes when configuring workers.
              '';
            };

            registrationServiceUnit = lib.mkOption {
              type = lib.types.str;
              readOnly = true;
              description = ''
                The registration service that generates the registration file.

                Systemd unit (a service or a target) for other services to depend on if they
                need to be started after mautrix-meta registration service.

                This option is useful as the actual parent unit for all matrix-synapse processes
                changes when configuring workers.
              '';
            };
          };

          config = {
            serviceUnit = (metaName name) + ".service";
            registrationServiceUnit = (metaName name) + "-registration.service";
            registrationFile = (fullDataDir config) + "/meta-registration.yaml";
          };
        }));

        description = ''
          Configuration of multiple `mautrix-meta` instances.
          `services.mautrix-meta.instances.facebook` and `services.mautrix-meta.instances.instagram`
          come preconfigured with meta.mode, appservice.id, bot username, display name and avatar.
        '';

        example = ''
          {
            facebook = {
              enable = true;
              settings = {
                homeserver.domain = "example.com";
              };
            };

            instagram = {
              enable = true;
              settings = {
                homeserver.domain = "example.com";
              };
            };

            messenger = {
              enable = true;
              settings = {
                meta.mode = "messenger";
                homeserver.domain = "example.com";
                appservice = {
                  id = "messenger";
                  bot = {
                    username = "messengerbot";
                    displayname = "Messenger bridge bot";
                    avatar = "mxc://maunium.net/ygtkteZsXnGJLJHRchUwYWak";
                  };
                };
              };
            };
          }
        '';
      };
    };
  };

  config = lib.mkMerge [
    (lib.mkIf (enabledInstances != []) {
      assertions = lib.mkMerge (lib.attrValues (lib.mapAttrs (name: cfg: [
        {
          assertion = cfg.settings.homeserver.domain != "" && cfg.settings.homeserver.address != "";
          message = ''
            The options with information about the homeserver:
            `services.mautrix-meta.instances.${name}.settings.homeserver.domain` and
            `services.mautrix-meta.instances.${name}.settings.homeserver.address` have to be set.
          '';
        }
        {
          assertion = builtins.elem cfg.settings.meta.mode [ "facebook" "facebook-tor" "messenger" "instagram" ];
          message = ''
            The option `services.mautrix-meta.instances.${name}.settings.meta.mode` has to be set
            to one of: facebook, facebook-tor, messenger, instagram.
            This configures the mode of the bridge.
          '';
        }
        {
          assertion = cfg.settings.bridge.permissions != {};
          message = ''
            The option `services.mautrix-meta.instances.${name}.settings.bridge.permissions` has to be set.
          '';
        }
        {
          assertion = cfg.settings.appservice.id != "";
          message = ''
            The option `services.mautrix-meta.instances.${name}.settings.appservice.id` has to be set.
          '';
        }
        {
          assertion = cfg.settings.appservice.bot.username != "";
          message = ''
            The option `services.mautrix-meta.instances.${name}.settings.appservice.bot.username` has to be set.
          '';
        }
      ]) enabledInstances));

      users.users = lib.mapAttrs' (name: cfg: lib.nameValuePair "mautrix-meta-${name}" {
        isSystemUser = true;
        group = "mautrix-meta";
        extraGroups = [ "mautrix-meta-registration" ];
        description = "Mautrix-Meta-${name} bridge user";
      }) enabledInstances;

      users.groups.mautrix-meta = {};
      users.groups.mautrix-meta-registration = {
        members = lib.lists.optional config.services.matrix-synapse.enable "matrix-synapse";
      };

      services.matrix-synapse = lib.mkIf (config.services.matrix-synapse.enable) (let
        registrationFiles = lib.attrValues
          (lib.mapAttrs (name: cfg: cfg.registrationFile) registerToSynapseInstances);
      in {
        settings.app_service_config_files = registrationFiles;
      });

      systemd.services = lib.mkMerge [
        {
          matrix-synapse = lib.mkIf (config.services.matrix-synapse.enable) (let
            registrationServices = lib.attrValues
              (lib.mapAttrs (name: cfg: cfg.registrationServiceUnit) registerToSynapseInstances);
          in {
            wants = registrationServices;
            after = registrationServices;
          });
        }

        (lib.mapAttrs' (name: cfg: lib.nameValuePair "${metaName name}-registration" {
          description = "Mautrix-Meta registration generation service - ${metaName name}";

          path = [
            pkgs.yq
            pkgs.envsubst
            upperCfg.package
          ];

          script = ''
            # substitute the settings file by environment variables
            # in this case read from EnvironmentFile
            rm -f '${settingsFile cfg}'
            old_umask=$(umask)
            umask 0177
            envsubst \
              -o '${settingsFile cfg}' \
              -i '${settingsFileUnsubstituted cfg}'

            config_has_tokens=$(yq '.appservice | has("as_token") and has("hs_token")' '${settingsFile cfg}')
            registration_already_exists=$([[ -f '${cfg.registrationFile}' ]] && echo "true" || echo "false")

            echo "There are tokens in the config: $config_has_tokens"
            echo "Registration already existed: $registration_already_exists"

            # tokens not configured from config/environment file, and registration file
            # is already generated, override tokens in config to make sure they are not lost
            if [[ $config_has_tokens == "false" && $registration_already_exists == "true" ]]; then
              echo "Copying as_token, hs_token from registration into configuration"
              yq -sY '.[0].appservice.as_token = .[1].as_token
                | .[0].appservice.hs_token = .[1].hs_token
                | .[0]' '${settingsFile cfg}' '${cfg.registrationFile}' \
                > '${settingsFile cfg}.tmp'
              mv '${settingsFile cfg}.tmp' '${settingsFile cfg}'
            fi

            # make sure --generate-registration does not affect config.yaml
            cp '${settingsFile cfg}' '${settingsFile cfg}.tmp'

            echo "Generating registration file"
            mautrix-meta \
              --generate-registration \
              --config='${settingsFile cfg}.tmp' \
              --registration='${cfg.registrationFile}'

            rm '${settingsFile cfg}.tmp'

            # no tokens configured, and new were just generated by generate registration for first time
            if [[ $config_has_tokens == "false" && $registration_already_exists == "false" ]]; then
              echo "Copying newly generated as_token, hs_token from registration into configuration"
              yq -sY '.[0].appservice.as_token = .[1].as_token
                | .[0].appservice.hs_token = .[1].hs_token
                | .[0]' '${settingsFile cfg}' '${cfg.registrationFile}' \
                > '${settingsFile cfg}.tmp'
              mv '${settingsFile cfg}.tmp' '${settingsFile cfg}'
            fi

            # Make sure correct tokens are in the registration file
            if [[ $config_has_tokens == "true" || $registration_already_exists == "true" ]]; then
              echo "Copying as_token, hs_token from configuration to the registration file"
              yq -sY '.[1].as_token = .[0].appservice.as_token
                | .[1].hs_token = .[0].appservice.hs_token
                | .[1]' '${settingsFile cfg}' '${cfg.registrationFile}' \
                > '${cfg.registrationFile}.tmp'
              mv '${cfg.registrationFile}.tmp' '${cfg.registrationFile}'
            fi

            umask $old_umask

            chown :mautrix-meta-registration '${cfg.registrationFile}'
            chmod 640 '${cfg.registrationFile}'
          '';

          serviceConfig = {
            Type = "oneshot";
            UMask = 0027;

            User = "mautrix-meta-${name}";
            Group = "mautrix-meta";

            SystemCallFilter = [ "@system-service" ];

            ProtectSystem = "strict";
            ProtectHome = true;

            ReadWritePaths = fullDataDir cfg;
            StateDirectory = cfg.dataDir;
            EnvironmentFile = cfg.environmentFile;
          };

          restartTriggers = [ (settingsFileUnsubstituted cfg) ];
        }) enabledInstances)

        (lib.mapAttrs' (name: cfg: lib.nameValuePair "${metaName name}" {
          description = "Mautrix-Meta bridge - ${metaName name}";
          wantedBy = [ "multi-user.target" ];
          wants = [ "network-online.target" ] ++ cfg.serviceDependencies;
          after = [ "network-online.target" ] ++ cfg.serviceDependencies;

          serviceConfig = {
            Type = "simple";

            User = "mautrix-meta-${name}";
            Group = "mautrix-meta";
            PrivateUsers = true;

            LockPersonality = true;
            MemoryDenyWriteExecute = true;
            NoNewPrivileges = true;
            PrivateDevices = true;
            PrivateTmp = true;
            ProtectClock = true;
            ProtectControlGroups = true;
            ProtectHome = true;
            ProtectHostname = true;
            ProtectKernelLogs = true;
            ProtectKernelModules = true;
            ProtectKernelTunables = true;
            ProtectSystem = "strict";
            Restart = "on-failure";
            RestartSec = "30s";
            RestrictRealtime = true;
            RestrictSUIDSGID = true;
            SystemCallArchitectures = "native";
            SystemCallErrorNumber = "EPERM";
            SystemCallFilter = ["@system-service"];
            UMask = 0027;

            WorkingDirectory = fullDataDir cfg;
            ReadWritePaths = fullDataDir cfg;
            StateDirectory = cfg.dataDir;
            EnvironmentFile = cfg.environmentFile;

            ExecStart = lib.escapeShellArgs [
              (lib.getExe upperCfg.package)
              "--config=${settingsFile cfg}"
            ];
          };
          restartTriggers = [ (settingsFileUnsubstituted cfg) ];
        }) enabledInstances)
      ];
    })
    {
      services.mautrix-meta.instances = let
        inherit (lib.modules) mkDefault;
      in {
        instagram = {
          settings = {
            meta.mode = mkDefault "instagram";

            bridge = {
              username_template = mkDefault "instagram_{{.}}";
            };

            appservice = {
              id = mkDefault "instagram";
              port = mkDefault 29320;
              bot = {
                username = mkDefault "instagrambot";
                displayname = mkDefault "Instagram bridge bot";
                avatar = mkDefault "mxc://maunium.net/JxjlbZUlCPULEeHZSwleUXQv";
              };
            };
          };
        };
        facebook = {
          settings = {
            meta.mode = mkDefault "facebook";

            bridge = {
              username_template = mkDefault "facebook_{{.}}";
            };

            appservice = {
              id = mkDefault "facebook";
              port = mkDefault 29321;
              bot = {
                username = mkDefault "facebookbot";
                displayname = mkDefault "Facebook bridge bot";
                avatar = mkDefault "mxc://maunium.net/ygtkteZsXnGJLJHRchUwYWak";
              };
            };
          };
        };
      };
    }
  ];

  meta.maintainers = with lib.maintainers; [ rutherther ];
}