summary refs log tree commit diff
path: root/nixos/modules/services/misc/matrix-conduit.nix
blob: 108f64de7aa9583fd56490beda5c209b163c5c2b (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
{ config, lib, pkgs, ... }:

with lib;

let
  cfg = config.services.matrix-conduit;

  format = pkgs.formats.toml {};
  configFile = format.generate "conduit.toml" cfg.settings;
in
  {
    meta.maintainers = with maintainers; [ pstn piegames ];
    options.services.matrix-conduit = {
      enable = mkEnableOption "matrix-conduit";

      extraEnvironment = mkOption {
        type = types.attrsOf types.str;
        description = "Extra Environment variables to pass to the conduit server.";
        default = {};
        example = { RUST_BACKTRACE="yes"; };
      };

      package = mkOption {
        type = types.package;
        default = pkgs.matrix-conduit;
        defaultText = "pkgs.matrix-conduit";
        example = "pkgs.matrix-conduit";
        description = ''
          Package of the conduit matrix server to use.
        '';
      };

      settings = mkOption {
        type = types.submodule {
          freeformType = format.type;
          options = {
            global.server_name = mkOption {
              type = types.str;
              example = "example.com";
              description = "The server_name is the name of this server. It is used as a suffix for user # and room ids.";
            };
            global.port = mkOption {
              type = types.port;
              default = 6167;
              description = "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";
            };
            global.max_request_size = mkOption {
              type = types.ints.positive;
              default = 20000000;
              description = "Max request size in bytes. Don't forget to also change it in the proxy.";
            };
            global.allow_registration = mkOption {
              type = types.bool;
              default = false;
              description = "Whether new users can register on this server.";
            };
            global.allow_encryption = mkOption {
              type = types.bool;
              default = true;
              description = "Whether new encrypted rooms can be created. Note: existing rooms will continue to work.";
            };
            global.allow_federation = mkOption {
              type = types.bool;
              default = true;
              description = ''
                Whether this server federates with other servers.
              '';
            };
            global.trusted_servers = mkOption {
              type = types.listOf types.str;
              default = [ "matrix.org" ];
              description = "Servers trusted with signing server keys.";
            };
            global.address = mkOption {
              type = types.str;
              default = "::1";
              description = "Address to listen on for connections by the reverse proxy/tls terminator.";
            };
            global.database_path = mkOption {
              type = types.str;
              default = "/var/lib/matrix-conduit/";
              readOnly = true;
              description = ''
                Path to the conduit database, the directory where conduit will save its data.
                Note that due to using the DynamicUser feature of systemd, this value should not be changed
                and is set to be read only.
              '';
            };
            global.database_backend = mkOption {
              type = types.enum [ "sqlite" "rocksdb" ];
              default = "sqlite";
              example = "rocksdb";
              description = ''
                The database backend for the service. Switching it on an existing
                instance will require manual migration of data.
              '';
            };
          };
        };
        default = {};
        description = ''
            Generates the conduit.toml configuration file. Refer to
            <link xlink:href="https://gitlab.com/famedly/conduit/-/blob/master/conduit-example.toml"/>
            for details on supported values.
            Note that database_path can not be edited because the service's reliance on systemd StateDir.
        '';
      };
    };

    config = mkIf cfg.enable {
      systemd.services.conduit = {
        description = "Conduit Matrix Server";
        documentation = [ "https://gitlab.com/famedly/conduit/" ];
        wantedBy = [ "multi-user.target" ];
        environment = lib.mkMerge ([
          { CONDUIT_CONFIG = configFile; }
          cfg.extraEnvironment
        ]);
        serviceConfig = {
          DynamicUser = true;
          User = "conduit";
          LockPersonality = true;
          MemoryDenyWriteExecute = true;
          ProtectClock = true;
          ProtectControlGroups = true;
          ProtectHostname = true;
          ProtectKernelLogs = true;
          ProtectKernelModules = true;
          ProtectKernelTunables = true;
          PrivateDevices = true;
          PrivateMounts = true;
          PrivateUsers = true;
          RestrictAddressFamilies = [ "AF_INET" "AF_INET6" ];
          RestrictNamespaces = true;
          RestrictRealtime = true;
          SystemCallArchitectures = "native";
          SystemCallFilter = [
            "@system-service"
            "~@privileged"
          ];
          StateDirectory = "matrix-conduit";
          ExecStart = "${cfg.package}/bin/conduit";
          Restart = "on-failure";
          RestartSec = 10;
          StartLimitBurst = 5;
        };
      };
    };
  }