summary refs log tree commit diff
path: root/host/Module-dev/set/matrix/root.nix
blob: 83636d2eb9c0893d8d645b1d43b0ffb90936de93 (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
{ pkgs, config, ... }:

let
  mkWorker =
    name: tasks:
    import ../../../../modules/software-templates/synapse-workers/generic.nix {
      workerName = name;
      tasks = tasks;
    };
in
{
  # Worker plumbing examples: https://github.com/element-hq/synapse/blob/master/docker/configure_workers_and_start.py
  # Documentation: https://github.com/element-hq/synapse/blob/develop/docs/workers.md
  imports = [
    ../../../../modules/software-templates/synapse-workers/module.nix
    ./postgres.nix

    (mkWorker "sync" [ "sync" ])
  ];

  services.matrix-synapse = {
    enable = true;
    withJemalloc = true;

    nginxVirtualHostName = "matrix.rory.gay";
    enableWorkers = true;

    federationSenders = 16; # 16
    pushers = 1;
    mediaRepoWorkers = 2; # 4
    clientReaders = 2; # 4
    syncWorkers = 2; # 4
    authWorkers = 0;

    eventCreators = 16;

    federationReaders = 8; # 8
    federationInboundWorkers = 16; # 8

    enableAppserviceWorker = true;
    enableBackgroundWorker = true;
    enableUserDirWorker = true;

    accountDataStreamWriters = 1;
    eventStreamWriters = 2; # 8
    presenceStreamWriters = 1;
    pushRuleStreamWriters = 1;
    receiptStreamWriters = 1;
    toDeviceStreamWriters = 1;
    typingStreamWriters = 1;

    # https://matrix-org.github.io/synapse/latest/usage/configuration/config_documentation.html
    settings = {
      server_name = "rory.gay";

      dummy_devents_treshold = 2;
      cleanup_extremities_with_dummy_events = true;

      enable_registration = true;
      registration_requires_token = true;

      require_membership_for_aliases = false;
      redaction_retention_period = null;
      user_ips_max_age = null;
      allow_device_name_lookup_over_federation = true;

      federation = {
        client_timeout = "30s"; # default=60s
        max_short_retries = 12;
        max_short_retry_delay = "5s";
        max_long_retries = 5;
        max_long_retry_delay = "30s";

        # rapid retry, small increments
        destination_min_retry_interval = "5m"; # default=10m
        destination_max_retry_interval = "12h"; # default=7d
        destination_retry_multiplier = 1.2; # default=2
      };

      registration_shared_secret_path = pkgs.writeText "registration_shared_secret.txt" ''
        sometext
      '';

      listeners = [
        {
          port = 8008;
          bind_addresses = [ "127.0.0.1" ];
          type = "http";
          tls = false;
          x_forwarded = true;
          resources = [
            {
              names = [
                "client"
                "federation"
              ];
              compress = false;
            }
          ];
        }
        {
          type = "http";
          path = "/run/matrix-synapse/main.sock";
          resources = [
            {
              names = [ "replication" ];
              compress = false;
            }
          ];
        }
      ];
      presence = {
        enablee = true;
        update_interval = 60;
      };
      database = {
        name = "psycopg2";
        args = {
          user = "matrix-synapse-rory-gay";
          password = "somepassword";
          database = "matrix-synapse-rory-gay";
          host = "/run/postgresql";
          application_name = "matrix-synapse (rory.gay) - main";
          cp_min = 2;
          cp_max = 5;

          # cp_reconnect - default=True - https://github.com/element-hq/synapse/blob/develop/synapse/storage/database.py#L129
          # cp_noisy - default=False - https://docs.twisted.org/en/stable/api/twisted.enterprise.adbapi.ConnectionPool.html#__init__ - info logs during operation
          # check_same_thread - default=False - https://github.com/element-hq/synapse/blob/develop/synapse/config/database.py#L65 - can this even be set?
        };

        # synchronous_commit - default=True - https://github.com/element-hq/synapse/blob/develop/synapse/storage/engines/postgres.py#L56
        # statement_timeout - default=60 * 60 * 1000 ms - https://github.com/element-hq/synapse/blob/develop/synapse/storage/engines/postgres.py#L63
        # allow_unsafe_locale - default=False - https://github.com/element-hq/synapse/blob/develop/synapse/storage/engines/postgres.py#L99
        # allow_outdated_version - default=False - https://github.com/element-hq/synapse/blob/develop/synapse/storage/engines/postgres.py#L92 - needs source link
        # txn_limit - default=0 - https://github.com/element-hq/synapse/blob/develop/synapse/storage/database.py#L564

        statement_timeout = 24 * 60 * 60 * 1000; # 24 hours, good for bg jobs
        txn_limit = 500; # maybe dropping old data from pg caches helps?
      };

      ui_auth = {
        session_timeout = "1m";
      };

      login_via_existing_session = {
        enabled = true;
        require_ui_auth = true;
        token_timeout = "1y";
      };

      report_stats = false;

      user_directory = {
        enabled = true;
        search_all_users = true;
        prefer_local_users = true;
      };

      # https://github.com/element-hq/synapse/blob/master/synapse/config/experimental.py
      experimental_features = {
        "msc2815_enabled" = true; # Redacted event content
        "msc3026_enabled" = true; # Busy presence
        "msc3266_enabled" = true; # Room summary API
        "msc3916_authenticated_media_enabled" = true; # Authenticated media
        "msc3823_account_suspension" = true; # Account suspension
        "msc4151_enabled" = true; # Report room API (CS-API)
      };

      redis = {
        enabled = true;
        path = "/run/redis-matrix-synapse/redis.sock";
      };

      instance_map = {
        main = {
          # replication listener
          path = "/run/matrix-synapse/main.sock";
        };
      };
    };
    #      // import ./ratelimits.nix
    #      // import ./caches.nix;
  };

  services.redis = {
    package = pkgs.valkey;
    servers.matrix-synapse = {
      enable = true;
      user = "matrix-synapse";
    };
  };

  services.postgresql = {
    initialScript = pkgs.writeText "synapse-init.sql" ''
      CREATE USER "${config.services.matrix-synapse.settings.database.args.user}" WITH PASSWORD '${config.services.matrix-synapse.settings.database.args.password}';
      CREATE DATABASE "${config.services.matrix-synapse.settings.database.args.database}" OWNER '${config.services.matrix-synapse.settings.database.args.user}' LOCALE 'C' ENCODING 'UTF8' TEMPLATE "template0";
    '';
  };

  systemd.tmpfiles.rules = [ "D /run/redis-matrix-synapse 0755 matrix-synapse matrix-synapse" ];
}