base16-schemes: unstable-2024-06-21 -> unstable-2024-11-12
[NixPkgs.git] / nixos / modules / services / matrix / mautrix-facebook.nix
blob0e81b390732a9c9fcd050e0d149a6d50f243b498
1 { config, pkgs, lib, ... }:
2 let
3   cfg = config.services.mautrix-facebook;
4   settingsFormat = pkgs.formats.json {};
5   settingsFile = settingsFormat.generate "mautrix-facebook-config.json" cfg.settings;
7   puppetRegex = lib.concatStringsSep
8     ".*"
9     (map
10       lib.escapeRegex
11       (lib.splitString
12         "{userid}"
13         cfg.settings.bridge.username_template));
14 in {
15   options = {
16     services.mautrix-facebook = {
17       enable = lib.mkEnableOption "Mautrix-Facebook, a Matrix-Facebook hybrid puppeting/relaybot bridge";
19       settings = lib.mkOption rec {
20         apply = lib.recursiveUpdate default;
21         type = settingsFormat.type;
22         default = {
23           homeserver = {
24             address = "http://localhost:8008";
25             software = "standard";
26           };
28           appservice = rec {
29             id = "facebook";
30             address = "http://${hostname}:${toString port}";
31             hostname = "localhost";
32             port = 29319;
34             database = "postgresql://";
36             bot_username = "facebookbot";
37           };
39           metrics.enabled = false;
40           manhole.enabled = false;
42           bridge = {
43             encryption = {
44               allow = true;
45               default = true;
47               verification_levels = {
48                 receive = "cross-signed-tofu";
49                 send = "cross-signed-tofu";
50                 share = "cross-signed-tofu";
51               };
52             };
53             username_template = "facebook_{userid}";
54           };
56           logging = {
57             version = 1;
58             formatters.journal_fmt.format = "%(name)s: %(message)s";
59             handlers.journal = {
60               class = "systemd.journal.JournalHandler";
61               formatter = "journal_fmt";
62               SYSLOG_IDENTIFIER = "mautrix-facebook";
63             };
64             root = {
65               level = "INFO";
66               handlers = ["journal"];
67             };
68           };
69         };
70         example = lib.literalExpression ''
71           {
72             homeserver = {
73               address = "http://localhost:8008";
74               domain = "mydomain.example";
75             };
77             bridge.permissions = {
78               "@admin:mydomain.example" = "admin";
79               "mydomain.example" = "user";
80             };
81           }
82         '';
83         description = ''
84           {file}`config.yaml` configuration as a Nix attribute set.
85           Configuration options should match those described in
86           [example-config.yaml](https://github.com/mautrix/facebook/blob/master/mautrix_facebook/example-config.yaml).
88           Secret tokens should be specified using {option}`environmentFile`
89           instead of this world-readable attribute set.
90         '';
91       };
93       environmentFile = lib.mkOption {
94         type = lib.types.nullOr lib.types.path;
95         default = null;
96         description = ''
97           File containing environment variables to be passed to the mautrix-facebook service.
99           Any config variable can be overridden by setting `MAUTRIX_FACEBOOK_SOME_KEY` to override the `some.key` variable.
100         '';
101       };
103       configurePostgresql = lib.mkOption {
104         type = lib.types.bool;
105         default = true;
106         description = ''
107           Enable PostgreSQL and create a user and database for mautrix-facebook. The default `settings` reference this database, if you disable this option you must provide a database URL.
108         '';
109       };
111       registrationData = lib.mkOption {
112         type = lib.types.attrs;
113         default = {};
114         description = ''
115           Output data for appservice registration. Simply make any desired changes and serialize to JSON. Note that this data contains secrets so think twice before putting it into the nix store.
117           Currently `as_token` and `hs_token` need to be added as they are not known to this module.
118         '';
119       };
120     };
121   };
123   config = lib.mkIf cfg.enable {
124     users.groups.mautrix-facebook = {};
126     users.users.mautrix-facebook = {
127       group = "mautrix-facebook";
128       isSystemUser = true;
129     };
131     services.postgresql = lib.mkIf cfg.configurePostgresql {
132       ensureDatabases = ["mautrix-facebook"];
133       ensureUsers = [{
134         name = "mautrix-facebook";
135         ensureDBOwnership = true;
136       }];
137     };
139     systemd.services.mautrix-facebook = rec {
140       wantedBy = [ "multi-user.target" ];
141       wants = [
142         "network-online.target"
143       ] ++ lib.optional config.services.matrix-synapse.enable config.services.matrix-synapse.serviceUnit
144         ++ lib.optional cfg.configurePostgresql "postgresql.service";
145       after = wants;
147       serviceConfig = {
148         Type = "simple";
149         Restart = "always";
151         User = "mautrix-facebook";
153         ProtectSystem = "strict";
154         ProtectHome = true;
155         ProtectKernelTunables = true;
156         ProtectKernelModules = true;
157         ProtectControlGroups = true;
158         PrivateTmp = true;
160         EnvironmentFile = cfg.environmentFile;
162         ExecStart = ''
163           ${pkgs.mautrix-facebook}/bin/mautrix-facebook --config=${settingsFile}
164         '';
165       };
166     };
168     services.mautrix-facebook = {
169       registrationData = {
170         id = cfg.settings.appservice.id;
172         namespaces = {
173           users = [
174             {
175               exclusive = true;
176               regex = lib.escapeRegex "@${cfg.settings.appservice.bot_username}:${cfg.settings.homeserver.domain}";
177             }
178             {
179               exclusive = true;
180               regex = "@${puppetRegex}:${lib.escapeRegex cfg.settings.homeserver.domain}";
181             }
182           ];
183           aliases = [];
184         };
186         url = cfg.settings.appservice.address;
187         sender_localpart = "mautrix-facebook-sender";
189         rate_limited = false;
190         "de.sorunome.msc2409.push_ephemeral" = true;
191         push_ephemeral = true;
192       };
193     };
194   };
196   meta.maintainers = with lib.maintainers; [ kevincox ];