1{ config, lib, pkgs, ... }:
2
3with lib;
4
5let
6 cfg = config.services.matrix-conduit;
7
8 format = pkgs.formats.toml {};
9 configFile = format.generate "conduit.toml" cfg.settings;
10in
11 {
12 meta.maintainers = with maintainers; [ pstn piegames ];
13 options.services.matrix-conduit = {
14 enable = mkEnableOption (lib.mdDoc "matrix-conduit");
15
16 extraEnvironment = mkOption {
17 type = types.attrsOf types.str;
18 description = lib.mdDoc "Extra Environment variables to pass to the conduit server.";
19 default = {};
20 example = { RUST_BACKTRACE="yes"; };
21 };
22
23 package = mkOption {
24 type = types.package;
25 default = pkgs.matrix-conduit;
26 defaultText = "pkgs.matrix-conduit";
27 example = "pkgs.matrix-conduit";
28 description = lib.mdDoc ''
29 Package of the conduit matrix server to use.
30 '';
31 };
32
33 settings = mkOption {
34 type = types.submodule {
35 freeformType = format.type;
36 options = {
37 global.server_name = mkOption {
38 type = types.str;
39 example = "example.com";
40 description = lib.mdDoc "The server_name is the name of this server. It is used as a suffix for user # and room ids.";
41 };
42 global.port = mkOption {
43 type = types.port;
44 default = 6167;
45 description = lib.mdDoc "The port Conduit will be running on. You need to set up a reverse proxy in your web server (e.g. apache or nginx), so all requests to /_matrix on port 443 and 8448 will be forwarded to the Conduit instance running on this port";
46 };
47 global.max_request_size = mkOption {
48 type = types.ints.positive;
49 default = 20000000;
50 description = lib.mdDoc "Max request size in bytes. Don't forget to also change it in the proxy.";
51 };
52 global.allow_registration = mkOption {
53 type = types.bool;
54 default = false;
55 description = lib.mdDoc "Whether new users can register on this server.";
56 };
57 global.allow_encryption = mkOption {
58 type = types.bool;
59 default = true;
60 description = lib.mdDoc "Whether new encrypted rooms can be created. Note: existing rooms will continue to work.";
61 };
62 global.allow_federation = mkOption {
63 type = types.bool;
64 default = true;
65 description = lib.mdDoc ''
66 Whether this server federates with other servers.
67 '';
68 };
69 global.trusted_servers = mkOption {
70 type = types.listOf types.str;
71 default = [ "matrix.org" ];
72 description = lib.mdDoc "Servers trusted with signing server keys.";
73 };
74 global.address = mkOption {
75 type = types.str;
76 default = "::1";
77 description = lib.mdDoc "Address to listen on for connections by the reverse proxy/tls terminator.";
78 };
79 global.database_path = mkOption {
80 type = types.str;
81 default = "/var/lib/matrix-conduit/";
82 readOnly = true;
83 description = lib.mdDoc ''
84 Path to the conduit database, the directory where conduit will save its data.
85 Note that due to using the DynamicUser feature of systemd, this value should not be changed
86 and is set to be read only.
87 '';
88 };
89 global.database_backend = mkOption {
90 type = types.enum [ "sqlite" "rocksdb" ];
91 default = "sqlite";
92 example = "rocksdb";
93 description = lib.mdDoc ''
94 The database backend for the service. Switching it on an existing
95 instance will require manual migration of data.
96 '';
97 };
98 };
99 };
100 default = {};
101 description = lib.mdDoc ''
102 Generates the conduit.toml configuration file. Refer to
103 <https://gitlab.com/famedly/conduit/-/blob/master/conduit-example.toml>
104 for details on supported values.
105 Note that database_path can not be edited because the service's reliance on systemd StateDir.
106 '';
107 };
108 };
109
110 config = mkIf cfg.enable {
111 systemd.services.conduit = {
112 description = "Conduit Matrix Server";
113 documentation = [ "https://gitlab.com/famedly/conduit/" ];
114 wantedBy = [ "multi-user.target" ];
115 environment = lib.mkMerge ([
116 { CONDUIT_CONFIG = configFile; }
117 cfg.extraEnvironment
118 ]);
119 serviceConfig = {
120 DynamicUser = true;
121 User = "conduit";
122 LockPersonality = true;
123 MemoryDenyWriteExecute = true;
124 ProtectClock = true;
125 ProtectControlGroups = true;
126 ProtectHostname = true;
127 ProtectKernelLogs = true;
128 ProtectKernelModules = true;
129 ProtectKernelTunables = true;
130 PrivateDevices = true;
131 PrivateMounts = true;
132 PrivateUsers = true;
133 RestrictAddressFamilies = [ "AF_INET" "AF_INET6" ];
134 RestrictNamespaces = true;
135 RestrictRealtime = true;
136 SystemCallArchitectures = "native";
137 SystemCallFilter = [
138 "@system-service"
139 "~@privileged"
140 ];
141 StateDirectory = "matrix-conduit";
142 ExecStart = "${cfg.package}/bin/conduit";
143 Restart = "on-failure";
144 RestartSec = 10;
145 StartLimitBurst = 5;
146 };
147 };
148 };
149 }