summary refs log tree commit diff
path: root/nixos/modules/services/misc/dendrite.nix
blob: b2885b094153a9155761698fcced5c7d636dca2b (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
{ config, lib, pkgs, ... }:
let
  cfg = config.services.dendrite;
  settingsFormat = pkgs.formats.yaml { };
  configurationYaml = settingsFormat.generate "dendrite.yaml" cfg.settings;
  workingDir = "/var/lib/dendrite";
in
{
  options.services.dendrite = {
    enable = lib.mkEnableOption "matrix.org dendrite";
    httpPort = lib.mkOption {
      type = lib.types.nullOr lib.types.port;
      default = 8008;
      description = ''
        The port to listen for HTTP requests on.
      '';
    };
    httpsPort = lib.mkOption {
      type = lib.types.nullOr lib.types.port;
      default = null;
      description = ''
        The port to listen for HTTPS requests on.
      '';
    };
    tlsCert = lib.mkOption {
      type = lib.types.nullOr lib.types.path;
      example = "/var/lib/dendrite/server.cert";
      default = null;
      description = ''
        The path to the TLS certificate.

        <programlisting>
          nix-shell -p dendrite --command "generate-keys --tls-cert server.crt --tls-key server.key"
        </programlisting>
      '';
    };
    tlsKey = lib.mkOption {
      type = lib.types.nullOr lib.types.path;
      example = "/var/lib/dendrite/server.key";
      default = null;
      description = ''
        The path to the TLS key.

        <programlisting>
          nix-shell -p dendrite --command "generate-keys --tls-cert server.crt --tls-key server.key"
        </programlisting>
      '';
    };
    environmentFile = lib.mkOption {
      type = lib.types.nullOr lib.types.path;
      example = "/var/lib/dendrite/registration_secret";
      default = null;
      description = ''
        Environment file as defined in <citerefentry>
        <refentrytitle>systemd.exec</refentrytitle><manvolnum>5</manvolnum>
        </citerefentry>.
        Secrets may be passed to the service without adding them to the world-readable
        Nix store, by specifying placeholder variables as the option value in Nix and
        setting these variables accordingly in the environment file. Currently only used
        for the registration secret to allow secure registration when
        client_api.registration_disabled is true.

        <programlisting>
          # snippet of dendrite-related config
          services.dendrite.settings.client_api.registration_shared_secret = "$REGISTRATION_SHARED_SECRET";
        </programlisting>

        <programlisting>
          # content of the environment file
          REGISTRATION_SHARED_SECRET=verysecretpassword
        </programlisting>

        Note that this file needs to be available on the host on which
        <literal>dendrite</literal> is running.
      '';
    };
    settings = lib.mkOption {
      type = lib.types.submodule {
        freeformType = settingsFormat.type;
        options.global = {
          server_name = lib.mkOption {
            type = lib.types.str;
            example = "example.com";
            description = ''
              The domain name of the server, with optional explicit port.
              This is used by remote servers to connect to this server.
              This is also the last part of your UserID.
            '';
          };
          private_key = lib.mkOption {
            type = lib.types.path;
            example = "${workingDir}/matrix_key.pem";
            description = ''
              The path to the signing private key file, used to sign
              requests and events.

              <programlisting>
                nix-shell -p dendrite --command "generate-keys --private-key matrix_key.pem"
              </programlisting>
            '';
          };
          trusted_third_party_id_servers = lib.mkOption {
            type = lib.types.listOf lib.types.str;
            example = [ "matrix.org" ];
            default = [ "matrix.org" "vector.im" ];
            description = ''
              Lists of domains that the server will trust as identity
              servers to verify third party identifiers such as phone
              numbers and email addresses
            '';
          };
        };
        options.app_service_api.database = {
          connection_string = lib.mkOption {
            type = lib.types.str;
            default = "file:federationapi.db";
            description = ''
              Database for the Appservice API.
            '';
          };
        };
        options.client_api = {
          registration_disabled = lib.mkOption {
            type = lib.types.bool;
            default = true;
            description = ''
              Whether to disable user registration to the server
              without the shared secret.
            '';
          };
        };
        options.federation_api.database = {
          connection_string = lib.mkOption {
            type = lib.types.str;
            default = "file:federationapi.db";
            description = ''
              Database for the Federation API.
            '';
          };
        };
        options.key_server.database = {
          connection_string = lib.mkOption {
            type = lib.types.str;
            default = "file:keyserver.db";
            description = ''
              Database for the Key Server (for end-to-end encryption).
            '';
          };
        };
        options.media_api = {
          database = {
            connection_string = lib.mkOption {
              type = lib.types.str;
              default = "file:mediaapi.db";
              description = ''
                Database for the Media API.
              '';
            };
          };
          base_path = lib.mkOption {
            type = lib.types.str;
            default = "${workingDir}/media_store";
            description = ''
              Storage path for uploaded media.
            '';
          };
        };
        options.room_server.database = {
          connection_string = lib.mkOption {
            type = lib.types.str;
            default = "file:roomserver.db";
            description = ''
              Database for the Room Server.
            '';
          };
        };
        options.sync_api.database = {
          connection_string = lib.mkOption {
            type = lib.types.str;
            default = "file:syncserver.db";
            description = ''
              Database for the Sync API.
            '';
          };
        };
        options.user_api = {
          account_database = {
            connection_string = lib.mkOption {
              type = lib.types.str;
              default = "file:userapi_accounts.db";
              description = ''
                Database for the User API, accounts.
              '';
            };
          };
          device_database = {
            connection_string = lib.mkOption {
              type = lib.types.str;
              default = "file:userapi_devices.db";
              description = ''
                Database for the User API, devices.
              '';
            };
          };
        };
        options.mscs = {
          database = {
            connection_string = lib.mkOption {
              type = lib.types.str;
              default = "file:mscs.db";
              description = ''
                Database for exerimental MSC's.
              '';
            };
          };
        };
      };
      default = { };
      description = ''
        Configuration for dendrite, see:
        <link xlink:href="https://github.com/matrix-org/dendrite/blob/master/dendrite-config.yaml"/>
        for available options with which to populate settings.
      '';
    };
  };

  config = lib.mkIf cfg.enable {
    assertions = [{
      assertion = cfg.httpsPort != null -> (cfg.tlsCert != null && cfg.tlsKey != null);
      message = ''
        If Dendrite is configured to use https, tlsCert and tlsKey must be provided.

        nix-shell -p dendrite --command "generate-keys --tls-cert server.crt --tls-key server.key"
      '';
    }];

    systemd.services.dendrite = {
      description = "Dendrite Matrix homeserver";
      after = [
        "network.target"
      ];
      wantedBy = [ "multi-user.target" ];
      serviceConfig = {
        Type = "simple";
        DynamicUser = true;
        StateDirectory = "dendrite";
        WorkingDirectory = workingDir;
        RuntimeDirectory = "dendrite";
        RuntimeDirectoryMode = "0700";
        EnvironmentFile = lib.mkIf (cfg.environmentFile != null) cfg.environmentFile;
        ExecStartPre =
          if (cfg.environmentFile != null) then ''
            ${pkgs.envsubst}/bin/envsubst \
              -i ${configurationYaml} \
              -o /run/dendrite/dendrite.yaml
          '' else ''
            ${pkgs.coreutils}/bin/cp ${configurationYaml} /run/dendrite/dendrite.yaml
          '';
        ExecStart = lib.strings.concatStringsSep " " ([
          "${pkgs.dendrite}/bin/dendrite-monolith-server"
          "--config /run/dendrite/dendrite.yaml"
        ] ++ lib.optionals (cfg.httpPort != null) [
          "--http-bind-address :${builtins.toString cfg.httpPort}"
        ] ++ lib.optionals (cfg.httpsPort != null) [
          "--https-bind-address :${builtins.toString cfg.httpsPort}"
          "--tls-cert ${cfg.tlsCert}"
          "--tls-key ${cfg.tlsKey}"
        ]);
        ExecReload = "${pkgs.coreutils}/bin/kill -HUP $MAINPID";
        Restart = "on-failure";
      };
    };
  };
  meta.maintainers = lib.teams.matrix.members;
}