From 0f94bdd77f8f763c86e984fc8451bb4e94f552b0 Mon Sep 17 00:00:00 2001 From: Daniel Olsen Date: Sun, 5 Jun 2022 03:59:53 +0200 Subject: [PATCH 01/31] init flake --- flake.nix | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 flake.nix diff --git a/flake.nix b/flake.nix new file mode 100644 index 0000000..dd26c2f --- /dev/null +++ b/flake.nix @@ -0,0 +1,9 @@ +{ + description = "NixOS modules for matrix related services"; + + outputs = { self }: { + nixosModules = { + synapse = import ./synapse-module; + }; + }; +} From db5bc1d93c4c9a85541de9be5d6905bee12e9424 Mon Sep 17 00:00:00 2001 From: Daniel Olsen Date: Fri, 7 Oct 2022 12:18:09 +0200 Subject: [PATCH 02/31] Simple configuration of federation senders --- synapse-module/default.nix | 216 ++++++++++++++++++++++--------------- 1 file changed, 127 insertions(+), 89 deletions(-) diff --git a/synapse-module/default.nix b/synapse-module/default.nix index 4d97835..17067e0 100644 --- a/synapse-module/default.nix +++ b/synapse-module/default.nix @@ -55,96 +55,129 @@ in default = lib.readFile ./matrix-synapse-log_config.yaml; }; - workers = lib.mkOption { - type = lib.types.attrsOf (lib.types.submodule ({config, ...}: { - options.settings = lib.mkOption { - type = lib.types.submodule { - freeformType = format.type; - - options.worker_app = lib.mkOption { - type = lib.types.enum [ - "synapse.app.generic_worker" - "synapse.app.pusher" - "synapse.app.appservice" - "synapse.app.federation_sender" - "synapse.app.media_repository" - "synapse.app.user_dir" - "synapse.app.frontend_proxy" - ]; - description = "The type of worker application"; - }; - options.worker_replication_host = lib.mkOption { - type = lib.types.str; - description = "The replication listeners ip on the main synapse process"; - default = "127.0.0.1"; - }; - options.worker_replication_http_port = lib.mkOption { - type = lib.types.port; - description = "The replication listeners port on the main synapse process"; - }; - options.worker_listeners = lib.mkOption { - type = lib.types.listOf (lib.types.submodule { - options.type = lib.mkOption { - type = lib.types.enum [ "http" "metrics" ]; - description = "The type of the listener"; - default = "http"; - }; - options.port = lib.mkOption { - type = lib.types.port; - description = "the TCP port to bind to"; - }; - options.bind_addresses = lib.mkOption { - type = lib.types.listOf lib.types.str; - description = "A list of local addresses to listen on"; - }; - options.tls = lib.mkOption { - type = lib.types.bool; - description = "set to true to enable TLS for this listener. Will use the TLS key/cert specified in tls_private_key_path / tls_certificate_path."; - default = true; - }; - options.x_forwarded = lib.mkOption { - type = lib.types.bool; - description = '' - Only valid for an 'http' listener. Set to true to use the X-Forwarded-For header as the client IP. - Useful when Synapse is behind a reverse-proxy. - ''; - default = false; - }; - options.resources = lib.mkOption { - type = lib.types.listOf (lib.types.submodule { - options.names = lib.mkOption { - type = lib.types.listOf (lib.types.enum [ "client" "consent" "federation" "keys" "media" "metrics" "openid" "replication" "static" "webclient" ]); - description = "A list of resources to host on this port"; - }; - options.compress = lib.mkOption { - type = lib.types.bool; - description = "enable HTTP compression for this resource"; - default = false; - }; - }); - }; - }); - description = "Listener configuration for the worker, similar to the main synapse listener"; - default = []; - }; - }; - }; - })); - default = {}; - description = "Worker configuration"; - example = { - "federation_sender1" = { - settings = { - worker_name = "federation_sender1"; - worker_app = "synapse.app.federation_sender"; + workers = let + inherit (lib.lists) any; + isReplication = l: any (r: any (n: n == "replication") r.names) l.resources; - worker_replication_host = "127.0.0.1"; - worker_replication_http_port = 9093; - worker_listeners = [ ]; + dMRL = lib.lists.findFirst isReplication + (throw "No replication listener configured!") + cfg.settings.listeners; + + dMRH = lib.findFirst (x: true) (throw "Replication listener had no addresses") + dMRL.bind_addresses; + dMRP = dMRL.port; + in { + enable = lib.mkEnableOption "synapse worker support"; + + mainReplicationHost = lib.mkOption { + type = lib.types.str; + default = dMRH; + description = "Host of the main synapse instance's replication listener"; + }; + + mainReplicationPort = lib.mkOption { + type = lib.types.port; + default = dMRP; + description = "Port for the main synapse instance's replication listener"; + }; + + federationSenders = lib.mkOption { + type = lib.types.ints.positive; + description = "How many automatically configured federation senders to set up"; + default = 0; + }; + + instances = lib.mkOption { + type = lib.types.attrsOf (lib.types.submodule ({config, ...}: { + options.settings = lib.mkOption { + type = lib.types.submodule { + freeformType = format.type; + + options.worker_app = lib.mkOption { + type = lib.types.enum [ + "synapse.app.generic_worker" + "synapse.app.pusher" + "synapse.app.appservice" + "synapse.app.federation_sender" + "synapse.app.media_repository" + "synapse.app.user_dir" + ]; + description = "The type of worker application"; + }; + options.worker_replication_host = lib.mkOption { + type = lib.types.str; + default = cfg.workers.mainReplicationHost; + description = "The replication listeners ip on the main synapse process"; + }; + options.worker_replication_http_port = lib.mkOption { + type = lib.types.port; + default = cfg.workers.mainReplicationPort; + description = "The replication listeners port on the main synapse process"; + }; + options.worker_listeners = lib.mkOption { + type = lib.types.listOf (lib.types.submodule { + options.type = lib.mkOption { + type = lib.types.enum [ "http" "metrics" ]; + description = "The type of the listener"; + default = "http"; + }; + options.port = lib.mkOption { + type = lib.types.port; + description = "the TCP port to bind to"; + }; + options.bind_addresses = lib.mkOption { + type = lib.types.listOf lib.types.str; + description = "A list of local addresses to listen on"; + }; + options.tls = lib.mkOption { + type = lib.types.bool; + description = "set to true to enable TLS for this listener. Will use the TLS key/cert specified in tls_private_key_path / tls_certificate_path."; + default = true; + }; + options.x_forwarded = lib.mkOption { + type = lib.types.bool; + description = '' + Only valid for an 'http' listener. Set to true to use the X-Forwarded-For header as the client IP. + Useful when Synapse is behind a reverse-proxy. + ''; + default = false; + }; + options.resources = lib.mkOption { + type = lib.types.listOf (lib.types.submodule { + options.names = lib.mkOption { + type = lib.types.listOf (lib.types.enum [ "client" "consent" "federation" "keys" "media" "metrics" "openid" "replication" "static" "webclient" ]); + description = "A list of resources to host on this port"; + }; + options.compress = lib.mkOption { + type = lib.types.bool; + description = "enable HTTP compression for this resource"; + default = false; + }; + }); + }; + }); + description = "Listener configuration for the worker, similar to the main synapse listener"; + default = [ ]; + }; + }; }; + })); + default = { }; + description = "Worker configuration"; + example = { + "federation_sender1" = { + settings = { + worker_name = "federation_sender1"; + worker_app = "synapse.app.federation_sender"; + + worker_replication_host = "127.0.0.1"; + worker_replication_http_port = 9093; + worker_listeners = [ ]; + }; }; }; }; + }; settings = lib.mkOption { type = lib.types.submodule { @@ -179,7 +212,7 @@ in options.use_presence = lib.mkOption { type = lib.types.bool; - description = "disable presence tracking on this homeserver, if you're having perfomance issues this can have a big impact"; + description = "disable presence tracking, if you're having perfomance issues this can have a big impact"; default = true; }; @@ -343,7 +376,7 @@ in started, to ensure that all instances are running with the same config (otherwise events may be dropped) ''; - default = []; + default = [ ]; }; }; }; @@ -407,11 +440,16 @@ in Restart = "on-failure"; }; }; + + services.matrix-synapse-next.settings.federation_sender_instances = lib.genList (x: "auto-fed-sender${toString x}") cfg.workers.federationSenders; + services.matrix-synapse-next.workers.instances = lib.attrsets.genAttrs (lib.genList + (x: "auto-fed-sender${toString x}") cfg.workers.federationSenders) + (_: { settings.worker_app = "synapse.app.federation_sender";}); }) ({ systemd.services = let - workerList = lib.mapAttrsToList (name: value: lib.nameValuePair name value ) cfg.workers; + workerList = lib.mapAttrsToList (name: value: lib.nameValuePair name value ) cfg.workers.instances; workerName = worker: worker.name; workerSettings = worker: (worker.value.settings // {worker_name = (workerName worker);}); workerConfig = worker: format.generate "matrix-synapse-worker-${workerName worker}-config.yaml" (workerSettings worker); @@ -442,4 +480,4 @@ in ) workerList); }) ]); -} +} \ No newline at end of file From 59f4e969c4710aad82fda89541c8e0897e5317d9 Mon Sep 17 00:00:00 2001 From: Daniel Olsen Date: Fri, 7 Oct 2022 16:16:44 +0200 Subject: [PATCH 03/31] automatically enable redis --- synapse-module/default.nix | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/synapse-module/default.nix b/synapse-module/default.nix index 17067e0..c6b1122 100644 --- a/synapse-module/default.nix +++ b/synapse-module/default.nix @@ -56,8 +56,7 @@ in }; workers = let - inherit (lib.lists) any; - isReplication = l: any (r: any (n: n == "replication") r.names) l.resources; + isReplication = l: lib.lists.any (r: lib.lists.any (n: n == "replication") r.names) l.resources; dMRL = lib.lists.findFirst isReplication (throw "No replication listener configured!") @@ -71,7 +70,7 @@ in mainReplicationHost = lib.mkOption { type = lib.types.str; - default = dMRH; + default = if (lib.lists.all (x: dMRH != x) ["0.0.0.0" "::"]) then dMRH else "127.0.0.1"; description = "Host of the main synapse instance's replication listener"; }; @@ -356,11 +355,6 @@ in } ]; }; - options.suppress_key_server_warning = lib.mkOption { - type = lib.types.bool; - description = "using matrix.org as a trusted key server will generate a warning if this is false"; - default = false; - }; options.send_federation = lib.mkOption { type = lib.types.bool; @@ -378,6 +372,21 @@ in ''; default = [ ]; }; + + options.redis = lib.mkOption { + type = lib.types.submodule { + freeformType = format.type; + + options.enabled = lib.mkOption { + type = lib.types.bool; + description = "Enables using redis, required for worker support"; + default = (lib.lists.count (x: true) + (lib.attrsets.attrValues cfg.workers.instances)) > 0; + }; + }; + default = { }; + description = "configuration of redis for synapse and workers"; + }; }; }; From ccbd09267948d5f51ed0fbb1d799a1aa63c4bdbf Mon Sep 17 00:00:00 2001 From: Daniel Olsen Date: Mon, 10 Oct 2022 10:55:16 +0200 Subject: [PATCH 04/31] federationReceivers first pass, needs a lot of cleanup --- synapse-module/default.nix | 116 +++++++++++++++++++++++++++++++------ 1 file changed, 99 insertions(+), 17 deletions(-) diff --git a/synapse-module/default.nix b/synapse-module/default.nix index c6b1122..52ab075 100644 --- a/synapse-module/default.nix +++ b/synapse-module/default.nix @@ -7,6 +7,11 @@ let pluginsEnv = cfg.package.python.buildEnv.override { extraLibs = cfg.plugins; }; + + typeToResources = t: { + "fed-receiver" = [ "federation" ]; + "fed-sender" = [ ]; + }.t; in { options.services.matrix-synapse-next = { @@ -66,11 +71,9 @@ in dMRL.bind_addresses; dMRP = dMRL.port; in { - enable = lib.mkEnableOption "synapse worker support"; - mainReplicationHost = lib.mkOption { type = lib.types.str; - default = if (lib.lists.all (x: dMRH != x) ["0.0.0.0" "::"]) then dMRH else "127.0.0.1"; + default = if builtins.elem dMRH [ "0.0.0.0" "::" ] then "127.0.0.1" else dMRH; description = "Host of the main synapse instance's replication listener"; }; @@ -80,16 +83,55 @@ in description = "Port for the main synapse instance's replication listener"; }; + defaultListenerAddress = lib.mkOption { + type = lib.types.str; + default = "127.0.0.1"; + description = "The default listener address for the worker"; + }; + + workerStartingPort = lib.mkOption { + type = lib.types.port; + description = "What port should the automatically configured workers start enumerating from"; + default = 8083; + }; + federationSenders = lib.mkOption { - type = lib.types.ints.positive; + type = lib.types.ints.unsigned; description = "How many automatically configured federation senders to set up"; default = 0; }; + federationReceivers = lib.mkOption { + type = lib.types.ints.unsigned; + description = "How many automatically configured federation recievers to set up"; + default = 0; + }; + instances = lib.mkOption { type = lib.types.attrsOf (lib.types.submodule ({config, ...}: { - options.settings = lib.mkOption { - type = lib.types.submodule { + + options.autoType = lib.mkOption { + type = lib.types.enum [ null "fed-sender" "fed-receiver" ]; + internal = true; + default = null; + }; + + options.settings = let + instanceCfg = config; + inherit (instanceCfg) autoType autoPort; + + isAuto = autoType != null; + + mapTypeApp = t: { + "fed-sender" = "synapse.app.federation_sender"; + "fed-receiver" = "synapse.app.generic_worker"; + }.${t}; + + defaultApp = if (instanceCfg.autoType == null) + then "synapse.app.generic_worker" + else mapTypeApp instanceCfg.autoType; + in lib.mkOption { + type = lib.types.submodule ({config, ...}: { freeformType = format.type; options.worker_app = lib.mkOption { @@ -102,6 +144,7 @@ in "synapse.app.user_dir" ]; description = "The type of worker application"; + default = defaultApp; }; options.worker_replication_host = lib.mkOption { type = lib.types.str; @@ -127,11 +170,12 @@ in options.bind_addresses = lib.mkOption { type = lib.types.listOf lib.types.str; description = "A list of local addresses to listen on"; + default = [ cfg.workers.defaultListenerAddress ]; }; options.tls = lib.mkOption { type = lib.types.bool; description = "set to true to enable TLS for this listener. Will use the TLS key/cert specified in tls_private_key_path / tls_certificate_path."; - default = true; + default = false; }; options.x_forwarded = lib.mkOption { type = lib.types.bool; @@ -139,7 +183,7 @@ in Only valid for an 'http' listener. Set to true to use the X-Forwarded-For header as the client IP. Useful when Synapse is behind a reverse-proxy. ''; - default = false; + default = true; }; options.resources = lib.mkOption { type = lib.types.listOf (lib.types.submodule { @@ -158,7 +202,7 @@ in description = "Listener configuration for the worker, similar to the main synapse listener"; default = [ ]; }; - }; + }); }; })); default = { }; @@ -259,17 +303,29 @@ in }; }); description = "List of ports that Synapse should listen on, their purpose and their configuration"; - default = [ + default = let + enableReplication = lib.lists.any + (w: !(builtins.elem w.settings.worker_app [ "federationSender" ])) + cfg.workers.instances; + in [ { - port = 8448; - bind_addresses = [ "0.0.0.0" "::" ]; + port = 8008; + bind_addresses = [ "127.0.0.1" ]; resources = [ { names = [ "client" ]; compress = true; } { names = [ "federation" ]; compress = false; } ]; } + (lib.mkIf enableReplication { + port = 9093; + bind_addresses = [ "127.0.0.1" ]; + resources = [ + { names = [ "replication" ]; } + ]; + }) ]; }; + options.federation_ip_range_blacklist = lib.mkOption { type = lib.types.listOf lib.types.str; description = '' @@ -449,13 +505,32 @@ in Restart = "on-failure"; }; }; - - services.matrix-synapse-next.settings.federation_sender_instances = lib.genList (x: "auto-fed-sender${toString x}") cfg.workers.federationSenders; - services.matrix-synapse-next.workers.instances = lib.attrsets.genAttrs (lib.genList - (x: "auto-fed-sender${toString x}") cfg.workers.federationSenders) - (_: { settings.worker_app = "synapse.app.federation_sender";}); }) + (lib.mkMerge [ + ({ + services.matrix-synapse-next.settings.federation_sender_instances = lib.genList (x: "auto-fed-sender${toString x}") cfg.workers.federationSenders; + + services.matrix-synapse-next.workers.instances = lib.attrsets.genAttrs (lib.genList + (x: "auto-fed-sender${toString x}") cfg.workers.federationSenders) + (_: { autoType = "fed-sender"; }); + }) + + ({ + services.matrix-synapse-next.workers.instances = lib.mapAttrs' + (name: value: lib.attrsets.nameValuePair "auto-fed-receiver${name}" value) + (lib.genAttrs (lib.genList (x: builtins.toString x) cfg.workers.federationReceivers) + (x: { + autoType = "fed-receiver"; + settings.worker_listeners = [ + { port = cfg.workers.workerStartingPort + (lib.strings.toInt x); + resources = [ {names = ["federation"];} ]; + } + ]; + })); + }) + ]) + ({ systemd.services = let workerList = lib.mapAttrsToList (name: value: lib.nameValuePair name value ) cfg.workers.instances; @@ -470,6 +545,7 @@ in partOf = [ "matrix-synapse.target" ]; wantedBy = [ "matrix-synapse.target" ]; after = [ "matrix-synapse.service" ]; + requires = [ "matrix-synapse.service" ]; environment.PYTHONPATH = lib.makeSearchPathOutput "lib" cfg.package.python.sitePackages [ pluginsEnv ]; @@ -478,6 +554,12 @@ in User = "matrix-synapse"; Group = "matrix-synapse"; WorkingDirectory = cfg.dataDir; + ExecStartPre = pkgs.writers.writeBash "wait-for-synapse" '' + # From https://md.darmstadt.ccc.de/synapse-at-work + while ! systemctl is-active -q matrix-synapse.service; do + sleep 1 + done + ''; ExecStart = '' ${cfg.package}/bin/synapse_worker \ ${ lib.concatMapStringsSep "\n " (x: "--config-path ${x} \\") ([ matrix-synapse-common-config (workerConfig worker) ] ++ cfg.extraConfigFiles) } From 74a4cd25d1221aa8138396e92a3a907a0efa2a2a Mon Sep 17 00:00:00 2001 From: Daniel Olsen Date: Mon, 10 Oct 2022 12:59:11 +0200 Subject: [PATCH 05/31] small cleanup --- synapse-module/default.nix | 31 +++++++++++++++---------------- 1 file changed, 15 insertions(+), 16 deletions(-) diff --git a/synapse-module/default.nix b/synapse-module/default.nix index 52ab075..015df07 100644 --- a/synapse-module/default.nix +++ b/synapse-module/default.nix @@ -7,11 +7,6 @@ let pluginsEnv = cfg.package.python.buildEnv.override { extraLibs = cfg.plugins; }; - - typeToResources = t: { - "fed-receiver" = [ "federation" ]; - "fed-sender" = [ ]; - }.t; in { options.services.matrix-synapse-next = { @@ -130,6 +125,11 @@ in defaultApp = if (instanceCfg.autoType == null) then "synapse.app.generic_worker" else mapTypeApp instanceCfg.autoType; + + typeToResources = t: { + "fed-receiver" = [ "federation" ]; + "fed-sender" = [ ]; + }.${t}; in lib.mkOption { type = lib.types.submodule ({config, ...}: { freeformType = format.type; @@ -190,6 +190,7 @@ in options.names = lib.mkOption { type = lib.types.listOf (lib.types.enum [ "client" "consent" "federation" "keys" "media" "metrics" "openid" "replication" "static" "webclient" ]); description = "A list of resources to host on this port"; + default = lib.optionals isAuto (typeToResources instanceCfg.autoType); }; options.compress = lib.mkOption { type = lib.types.bool; @@ -197,6 +198,7 @@ in default = false; }; }); + default = [{ }]; }; }); description = "Listener configuration for the worker, similar to the main synapse listener"; @@ -517,17 +519,14 @@ in }) ({ - services.matrix-synapse-next.workers.instances = lib.mapAttrs' - (name: value: lib.attrsets.nameValuePair "auto-fed-receiver${name}" value) - (lib.genAttrs (lib.genList (x: builtins.toString x) cfg.workers.federationReceivers) - (x: { - autoType = "fed-receiver"; - settings.worker_listeners = [ - { port = cfg.workers.workerStartingPort + (lib.strings.toInt x); - resources = [ {names = ["federation"];} ]; - } - ]; - })); + services.matrix-synapse-next.workers.instances = let + genAttrs' = items: f: g: builtins.listToAttrs (builtins.map (n: lib.attrsets.nameValuePair (f n) (g n)) items); + in genAttrs' (lib.genList (x: x) cfg.workers.federationReceivers) + (x: "auto-fed-receiver${builtins.toString x}") + (x: { + autoType = "fed-receiver"; + settings.worker_listeners = [{ port = cfg.workers.workerStartingPort + x; }]; + }); }) ]) From 35769740bf47d5fb24ce1610c2322694a7e55ad7 Mon Sep 17 00:00:00 2001 From: Daniel Olsen Date: Thu, 20 Oct 2022 23:56:34 +0200 Subject: [PATCH 06/31] Small cleanup --- synapse-module/default.nix | 26 +++++--------------------- 1 file changed, 5 insertions(+), 21 deletions(-) diff --git a/synapse-module/default.nix b/synapse-module/default.nix index 015df07..5060388 100644 --- a/synapse-module/default.nix +++ b/synapse-module/default.nix @@ -40,15 +40,6 @@ in ''; }; - enableMainSynapse = lib.mkOption { - type = lib.types.bool; - default = true; - description = '' - Enable the main synapse process. - Useful if running workers on separate computers. - ''; - }; - mainLogConfig = lib.mkOption { type = lib.types.lines; description = "A yaml python logging config file"; @@ -249,12 +240,6 @@ in example = "matrix.org"; }; - options.pid_file = lib.mkOption { - type = lib.types.path; - description = "When running as a daemon, the file to store the pid in"; - default = "/run/matrix-synapse.pid"; - }; - options.use_presence = lib.mkOption { type = lib.types.bool; description = "disable presence tracking, if you're having perfomance issues this can have a big impact"; @@ -305,10 +290,9 @@ in }; }); description = "List of ports that Synapse should listen on, their purpose and their configuration"; - default = let - enableReplication = lib.lists.any - (w: !(builtins.elem w.settings.worker_app [ "federationSender" ])) - cfg.workers.instances; + default = let enableReplication = lib.lists.any + (w: !(builtins.elem w.settings.worker_app [ "federationSender" ])) + cfg.workers.instances; in [ { port = 8008; @@ -370,7 +354,7 @@ in options.enable_registration = lib.mkOption { type = lib.types.bool; description = "Enable registration for new users"; - default = true; + default = false; }; options.enable_metrics = lib.mkOption { @@ -417,7 +401,7 @@ in options.send_federation = lib.mkOption { type = lib.types.bool; description = "Disables sending of outbound federation transactions on the main process. Set to false if using federation senders"; - default = (lib.lists.count (x: true) cfg.settings.federation_sender_instances) == 0; + default = cfg.settings.federation_sender_instances == []; }; options.federation_sender_instances = lib.mkOption { From b6f34dd2c6b03df7237fc3ac1a601df39288d615 Mon Sep 17 00:00:00 2001 From: Daniel Olsen Date: Fri, 21 Oct 2022 03:07:26 +0200 Subject: [PATCH 07/31] federation receivers and basic nginx setup --- synapse-module/default.nix | 235 ++++++++++++++++++++++++++++++------- 1 file changed, 194 insertions(+), 41 deletions(-) diff --git a/synapse-module/default.nix b/synapse-module/default.nix index 5060388..e888298 100644 --- a/synapse-module/default.nix +++ b/synapse-module/default.nix @@ -2,11 +2,17 @@ let cfg = config.services.matrix-synapse-next; + wcfg = cfg.workers; + format = pkgs.formats.yaml {}; matrix-synapse-common-config = format.generate "matrix-synapse-common-config.yaml" cfg.settings; pluginsEnv = cfg.package.python.buildEnv.override { extraLibs = cfg.plugins; }; + + genAttrs' = items: f: g: builtins.listToAttrs (builtins.map (i: lib.attrsets.nameValuePair (f i) (g i)) items); + + isListenerType = type: listener: lib.lists.any (r: lib.lists.any (n: n == type) r.names) listener.resources; in { options.services.matrix-synapse-next = { @@ -40,6 +46,14 @@ in ''; }; + enableNginx = lib.mkEnableOption "Enable the synapse module managing nginx"; + + public_baseurl = lib.mkOption { + type = lib.types.str; + default = "matrix.${cfg.settings.server_name}"; + description = "The domain where clients and such will connect (May be different from server_name if using delegation)"; + }; + mainLogConfig = lib.mkOption { type = lib.types.lines; description = "A yaml python logging config file"; @@ -81,6 +95,16 @@ in default = 8083; }; + enableMetrics = lib.mkOption { + type = lib.types.bool; + default = cfg.settings.enable_metrics; + }; + + metricsStartingPort = lib.mkOption { + type = lib.types.port; + default = 18083; + }; + federationSenders = lib.mkOption { type = lib.types.ints.unsigned; description = "How many automatically configured federation senders to set up"; @@ -96,36 +120,38 @@ in instances = lib.mkOption { type = lib.types.attrsOf (lib.types.submodule ({config, ...}: { - options.autoType = lib.mkOption { - type = lib.types.enum [ null "fed-sender" "fed-receiver" ]; + options.isAuto = lib.mkOption { + type = lib.types.bool; internal = true; - default = null; + default = false; + }; + + options.index = lib.mkOption { + internal = true; + type = lib.types.ints.positive; + }; + + # The custom string type here is mainly for the name to use for the metrics of custom worker types + options.type = lib.mkOption { + type = lib.types.either (lib.types.str) (lib.types.enum [ "fed-sender" "fed-receiver" ]); }; options.settings = let instanceCfg = config; - inherit (instanceCfg) autoType autoPort; - - isAuto = autoType != null; - - mapTypeApp = t: { - "fed-sender" = "synapse.app.federation_sender"; - "fed-receiver" = "synapse.app.generic_worker"; - }.${t}; - - defaultApp = if (instanceCfg.autoType == null) - then "synapse.app.generic_worker" - else mapTypeApp instanceCfg.autoType; - - typeToResources = t: { - "fed-receiver" = [ "federation" ]; - "fed-sender" = [ ]; - }.${t}; + inherit (instanceCfg) type isAuto; in lib.mkOption { type = lib.types.submodule ({config, ...}: { freeformType = format.type; - options.worker_app = lib.mkOption { + options.worker_app = let + mapTypeApp = t: { + "fed-sender" = "synapse.app.federation_sender"; + "fed-receiver" = "synapse.app.generic_worker"; + }.${t}; + defaultApp = if (!isAuto) + then "synapse.app.generic_worker" + else mapTypeApp type; + in lib.mkOption { type = lib.types.enum [ "synapse.app.generic_worker" "synapse.app.pusher" @@ -176,12 +202,17 @@ in ''; default = true; }; - options.resources = lib.mkOption { + options.resources = let + typeToResources = t: { + "fed-receiver" = [ "federation" ]; + "fed-sender" = [ ]; + }.${t}; + in lib.mkOption { type = lib.types.listOf (lib.types.submodule { options.names = lib.mkOption { type = lib.types.listOf (lib.types.enum [ "client" "consent" "federation" "keys" "media" "metrics" "openid" "replication" "static" "webclient" ]); description = "A list of resources to host on this port"; - default = lib.optionals isAuto (typeToResources instanceCfg.autoType); + default = lib.optionals isAuto (typeToResources type); }; options.compress = lib.mkOption { type = lib.types.bool; @@ -196,6 +227,7 @@ in default = [ ]; }; }); + default = { }; }; })); default = { }; @@ -265,7 +297,7 @@ in options.tls = lib.mkOption { type = lib.types.bool; description = "set to true to enable TLS for this listener. Will use the TLS key/cert specified in tls_private_key_path / tls_certificate_path."; - default = true; + default = false; }; options.x_forwarded = lib.mkOption { type = lib.types.bool; @@ -273,7 +305,7 @@ in Only valid for an 'http' listener. Set to true to use the X-Forwarded-For header as the client IP. Useful when Synapse is behind a reverse-proxy. ''; - default = false; + default = true; }; options.resources = lib.mkOption { type = lib.types.listOf (lib.types.submodule { @@ -290,9 +322,10 @@ in }; }); description = "List of ports that Synapse should listen on, their purpose and their configuration"; - default = let enableReplication = lib.lists.any - (w: !(builtins.elem w.settings.worker_app [ "federationSender" ])) - cfg.workers.instances; + default = let + enableReplication = lib.lists.any + (w: !(builtins.elem w.settings.worker_app [ "synapse.app.federation_sender" ])) + (builtins.attrValues cfg.workers.instances); in [ { port = 8008; @@ -465,7 +498,7 @@ in }; }) - (lib.mkIf cfg.enableMainSynapse { + ({ systemd.services.matrix-synapse = { description = "Synapse Matrix homeserver"; partOf = [ "matrix-synapse.target" ]; @@ -495,21 +528,28 @@ in (lib.mkMerge [ ({ - services.matrix-synapse-next.settings.federation_sender_instances = lib.genList (x: "auto-fed-sender${toString x}") cfg.workers.federationSenders; - - services.matrix-synapse-next.workers.instances = lib.attrsets.genAttrs (lib.genList - (x: "auto-fed-sender${toString x}") cfg.workers.federationSenders) - (_: { autoType = "fed-sender"; }); + services.matrix-synapse-next.settings.federation_sender_instances = lib.genList (i: "auto-fed-sender${toString (i + 1)}") cfg.workers.federationSenders; + services.matrix-synapse-next.workers.instances = genAttrs' (lib.lists.range 1 cfg.workers.federationSenders) + (i: "auto-fed-sender${toString i}") + (i: { + isAuto = true; type = "fed-sender"; index = i; + settings.worker_listeners = lib.mkIf wcfg.enableMetrics [ + { port = cfg.workers.metricsStartingPort + i - 1; + resources = [ { names = [ "metrics" ]; } ]; + } + ]; + }); }) ({ - services.matrix-synapse-next.workers.instances = let - genAttrs' = items: f: g: builtins.listToAttrs (builtins.map (n: lib.attrsets.nameValuePair (f n) (g n)) items); - in genAttrs' (lib.genList (x: x) cfg.workers.federationReceivers) - (x: "auto-fed-receiver${builtins.toString x}") - (x: { - autoType = "fed-receiver"; - settings.worker_listeners = [{ port = cfg.workers.workerStartingPort + x; }]; + services.matrix-synapse-next.workers.instances = genAttrs' (lib.lists.range 1 cfg.workers.federationReceivers) + (i: "auto-fed-receiver${toString i}") + (i: { + isAuto = true; type = "fed-receiver"; index = i; + settings.worker_listeners = [{ port = cfg.workers.workerStartingPort + i - 1; }] + ++ lib.optional wcfg.enableMetrics { port = cfg.workers.metricsStartingPort + cfg.workers.federationSenders + i; + resources = [ { names = [ "metrics" ]; } ]; + }; }); }) ]) @@ -553,5 +593,118 @@ in } ) workerList); }) + + (lib.mkIf cfg.enableNginx { + services.nginx.commonHttpConfig = '' + map $request_uri $synapse_backend { + default synapse_master; + + # Sync requests + ~*^/_matrix/client/(v2_alpha|r0)/sync$ synapse_client; + ~*^/_matrix/client/(api/v1|v2_alpha|r0)/events$ synapse_client; + ~*^/_matrix/client/(api/v1|r0)/initialSync$ synapse_client; + ~*^/_matrix/client/(api/v1|r0)/rooms/[^/]+/initialSync$ synapse_client; + + # Federation requests + ~*^/_matrix/federation/v1/event/ synapse_federation; + ~*^/_matrix/federation/v1/state/ synapse_federation; + ~*^/_matrix/federation/v1/state_ids/ synapse_federation; + ~*^/_matrix/federation/v1/backfill/ synapse_federation; + ~*^/_matrix/federation/v1/get_missing_events/ synapse_federation; + ~*^/_matrix/federation/v1/publicRooms synapse_federation; + ~*^/_matrix/federation/v1/query/ synapse_federation; + ~*^/_matrix/federation/v1/make_join/ synapse_federation; + ~*^/_matrix/federation/v1/make_leave/ synapse_federation; + ~*^/_matrix/federation/v1/send_join/ synapse_federation; + ~*^/_matrix/federation/v2/send_join/ synapse_federation; + ~*^/_matrix/federation/v1/send_leave/ synapse_federation; + ~*^/_matrix/federation/v2/send_leave/ synapse_federation; + ~*^/_matrix/federation/v1/invite/ synapse_federation; + ~*^/_matrix/federation/v2/invite/ synapse_federation; + ~*^/_matrix/federation/v1/query_auth/ synapse_federation; + ~*^/_matrix/federation/v1/event_auth/ synapse_federation; + ~*^/_matrix/federation/v1/exchange_third_party_invite/ synapse_federation; + ~*^/_matrix/federation/v1/user/devices/ synapse_federation; + ~*^/_matrix/federation/v1/get_groups_publicised$ synapse_federation; + ~*^/_matrix/key/v2/query synapse_federation; + + # Inbound federation transaction request + ~*^/_matrix/federation/v1/send/ synapse_federation; + + # Client API requests + ~*^/_matrix/client/(api/v1|r0|unstable)/publicRooms$ synapse_client; + ~*^/_matrix/client/(api/v1|r0|unstable)/rooms/.*/joined_members$ synapse_client; + ~*^/_matrix/client/(api/v1|r0|unstable)/rooms/.*/context/.*$ synapse_client; + ~*^/_matrix/client/(api/v1|r0|unstable)/rooms/.*/members$ synapse_client; + ~*^/_matrix/client/(api/v1|r0|unstable)/rooms/.*/state$ synapse_client; + ~*^/_matrix/client/(api/v1|r0|unstable)/account/3pid$ synapse_client; + ~*^/_matrix/client/(api/v1|r0|unstable)/devices$ synapse_client; + ~*^/_matrix/client/(api/v1|r0|unstable)/keys/query$ synapse_client; + ~*^/_matrix/client/(api/v1|r0|unstable)/keys/changes$ synapse_client; + ~*^/_matrix/client/versions$ synapse_client; + ~*^/_matrix/client/(api/v1|r0|unstable)/voip/turnServer$ synapse_client; + ~*^/_matrix/client/(api/v1|r0|unstable)/joined_groups$ synapse_client; + ~*^/_matrix/client/(api/v1|r0|unstable)/publicised_groups$ synapse_client; + ~*^/_matrix/client/(api/v1|r0|unstable)/publicised_groups/ synapse_client; + ~*^/_matrix/client/(api/v1|r0|unstable)/rooms/.*/event/ synapse_client; + ~*^/_matrix/client/(api/v1|r0|unstable)/joined_rooms$ synapse_client; + ~*^/_matrix/client/(api/v1|r0|unstable)/search$ synapse_client; + + # Registration/login requests + ~*^/_matrix/client/(api/v1|r0|unstable)/login$ synapse_client; + ~*^/_matrix/client/(r0|unstable)/register$ synapse_client; + + # Event sending requests + ~*^/_matrix/client/(api/v1|r0|unstable)/rooms/.*/redact synapse_client; + ~*^/_matrix/client/(api/v1|r0|unstable)/rooms/.*/send synapse_client; + ~*^/_matrix/client/(api/v1|r0|unstable)/rooms/.*/state/ synapse_client; + ~*^/_matrix/client/(api/v1|r0|unstable)/rooms/.*/(join|invite|leave|ban|unban|kick)$ synapse_client; + ~*^/_matrix/client/(api/v1|r0|unstable)/join/ synapse_client; + ~*^/_matrix/client/(api/v1|r0|unstable)/profile/ synapse_client; + } + ''; + + services.nginx.upstreams.synapse_master.servers = let + isMainListener = l: isListenerType "client" l && isListenerType "federation" l; + firstMainListener = lib.findFirst isMainListener + (throw "No cartch-all listener configured") cfg.settings.listeners; + address = lib.findFirst (_: true) (throw "No address in main listener") firstMainListener.bind_addresses; + port = firstMainListener.port; + socketAddress = "${address}:${builtins.toString port}"; + in { + "${socketAddress}" = { }; + }; + + services.nginx.upstreams.synapse_federation.servers = let + fedReceivers = lib.filterAttrs (_: w: w.type == "fed-receiver") cfg.workers.instances; + isListenerType = type: listener: lib.lists.any (r: lib.lists.any (n: n == type) r.names) listener.resources; + isFedListener = l: isListenerType "federation" l; + + firstFedListener = w: lib.lists.findFirst isFedListener (throw "No federation endpoint on receiver") w.settings.worker_listeners; + + wAddress = w: lib.lists.findFirst (_: true) (throw "No address in receiver") (firstFedListener w).bind_addresses; + wPort = w: (firstFedListener w).port; + + socketAddress = w: "${wAddress w}:${builtins.toString (wPort w)}"; + socketAddresses = lib.mapAttrsToList (_: value: "${socketAddress value}") fedReceivers; + in if fedReceivers != [ ] then lib.genAttrs socketAddresses (_: { }) else config.services.nginx.upstreams.synapse_master.servers; + + services.nginx.upstreams.synapse_client.servers = config.services.nginx.upstreams.synapse_master.servers; + + + services.nginx.virtualHosts."${cfg.public_baseurl}" = { + enableACME = true; + forceSSL = true; + locations."/_matrix" = { + proxyPass = "http://$synapse_backend"; + extraConfig = '' + client_max_body_size ${cfg.settings.max_upload_size}; + ''; + }; + locations."/_synapse/client" = { + proxyPass = "http://$synapse_backend"; + }; + }; + }) ]); } \ No newline at end of file From 3053f9b2f7252bd351435534fd054f836ae54bfc Mon Sep 17 00:00:00 2001 From: Daniel Olsen Date: Fri, 21 Oct 2022 19:22:45 +0200 Subject: [PATCH 08/31] Automatic metrics listener --- synapse-module/default.nix | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/synapse-module/default.nix b/synapse-module/default.nix index e888298..3b9dedd 100644 --- a/synapse-module/default.nix +++ b/synapse-module/default.nix @@ -277,7 +277,7 @@ in description = "disable presence tracking, if you're having perfomance issues this can have a big impact"; default = true; }; - +/ options.listeners = lib.mkOption { type = lib.types.listOf (lib.types.submodule { options.port = lib.mkOption { @@ -342,6 +342,13 @@ in { names = [ "replication" ]; } ]; }) + (lib.mkIf cfg.settings.enable_metrics { + port = 9000; + bind_addresses = [ "127.0.0.1" ]; + resources = [ + { names = [ "metrics" ]; } + ]; + }) ]; }; From 95c728601f21e0942fb798cce6377ddc88fb3cff Mon Sep 17 00:00:00 2001 From: Daniel Olsen Date: Fri, 21 Oct 2022 19:40:29 +0200 Subject: [PATCH 09/31] typo --- synapse-module/default.nix | 1 - 1 file changed, 1 deletion(-) diff --git a/synapse-module/default.nix b/synapse-module/default.nix index 3b9dedd..e81bfcd 100644 --- a/synapse-module/default.nix +++ b/synapse-module/default.nix @@ -277,7 +277,6 @@ in description = "disable presence tracking, if you're having perfomance issues this can have a big impact"; default = true; }; -/ options.listeners = lib.mkOption { type = lib.types.listOf (lib.types.submodule { options.port = lib.mkOption { From f890dff8b0edab00eb44dc6ddac81ecfe1d211bc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20L=C3=B8vbr=C3=B8tte=20Olsen?= Date: Fri, 21 Oct 2022 19:46:39 +0200 Subject: [PATCH 10/31] Create README.MD --- README.MD | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 README.MD diff --git a/README.MD b/README.MD new file mode 100644 index 0000000..e76cf8b --- /dev/null +++ b/README.MD @@ -0,0 +1,34 @@ +With matrix.YOURDOMAIN pointing at the server: + +``` +{ + imports = [ ./synapse-module ]; + + services.matrix-synapse-next = { + enable = true; + + workers.federationSenders = 1; + workers.federationReceivers = 1; + + enableNginx = true; + + settings = { + server_name = "YOURDOMAIN"; + + database = { + name = "psycopg2"; + args = { + host = "localhost"; + user = "synapse"; + password = "synapse"; + dbname = "synapse"; + }; + }; + }; + }; + + services.redis.servers."".enable = true; +} +``` + +is ~enough to get a functional matrix-server running one federation sender and one federation receiver From 952bab1d6ec0822b65920c1e8a0c8e8ff330aa39 Mon Sep 17 00:00:00 2001 From: Daniel Olsen Date: Fri, 9 Dec 2022 04:50:14 +0100 Subject: [PATCH 11/31] federation_sender worker type is deprecated Enables replication listener if you have any workers for simplicity send_federation is automatically set to false if federation_sender_instances is set so we don't have to do it --- synapse-module/default.nix | 20 ++++---------------- 1 file changed, 4 insertions(+), 16 deletions(-) diff --git a/synapse-module/default.nix b/synapse-module/default.nix index e81bfcd..aa41bec 100644 --- a/synapse-module/default.nix +++ b/synapse-module/default.nix @@ -145,7 +145,7 @@ in options.worker_app = let mapTypeApp = t: { - "fed-sender" = "synapse.app.federation_sender"; + "fed-sender" = "synapse.app.generic_worker"; "fed-receiver" = "synapse.app.generic_worker"; }.${t}; defaultApp = if (!isAuto) @@ -154,9 +154,7 @@ in in lib.mkOption { type = lib.types.enum [ "synapse.app.generic_worker" - "synapse.app.pusher" "synapse.app.appservice" - "synapse.app.federation_sender" "synapse.app.media_repository" "synapse.app.user_dir" ]; @@ -236,7 +234,7 @@ in "federation_sender1" = { settings = { worker_name = "federation_sender1"; - worker_app = "synapse.app.federation_sender"; + worker_app = "synapse.app.generic_worker"; worker_replication_host = "127.0.0.1"; worker_replication_http_port = 9093; @@ -321,11 +319,7 @@ in }; }); description = "List of ports that Synapse should listen on, their purpose and their configuration"; - default = let - enableReplication = lib.lists.any - (w: !(builtins.elem w.settings.worker_app [ "synapse.app.federation_sender" ])) - (builtins.attrValues cfg.workers.instances); - in [ + default = [ { port = 8008; bind_addresses = [ "127.0.0.1" ]; @@ -334,7 +328,7 @@ in { names = [ "federation" ]; compress = false; } ]; } - (lib.mkIf enableReplication { + (lib.mkIf (wcfg.instances != { }) { port = 9093; bind_addresses = [ "127.0.0.1" ]; resources = [ @@ -437,12 +431,6 @@ in ]; }; - options.send_federation = lib.mkOption { - type = lib.types.bool; - description = "Disables sending of outbound federation transactions on the main process. Set to false if using federation senders"; - default = cfg.settings.federation_sender_instances == []; - }; - options.federation_sender_instances = lib.mkOption { type = lib.types.listOf lib.types.str; description = '' From 240e550d4c35ebcef863ffd4adfe35207b8e53d2 Mon Sep 17 00:00:00 2001 From: Daniel Olsen Date: Fri, 9 Dec 2022 09:33:31 +0100 Subject: [PATCH 12/31] Implement sync workers --- synapse-module/default.nix | 158 ++++++++--------------------- synapse-module/nginx.nix | 199 +++++++++++++++++++++++++++++++++++++ 2 files changed, 243 insertions(+), 114 deletions(-) create mode 100644 synapse-module/nginx.nix diff --git a/synapse-module/default.nix b/synapse-module/default.nix index aa41bec..7fd6463 100644 --- a/synapse-module/default.nix +++ b/synapse-module/default.nix @@ -15,6 +15,8 @@ let isListenerType = type: listener: lib.lists.any (r: lib.lists.any (n: n == type) r.names) listener.resources; in { + imports = [ ./nginx.nix ]; + options.services.matrix-synapse-next = { enable = lib.mkEnableOption "matrix-synapse"; @@ -117,6 +119,18 @@ in default = 0; }; + initialSyncers = lib.mkOption { + type = lib.types.ints.unsigned; + description = "How many automatically configured intial syncers to set up"; + default = 0; + }; + + normalSyncers = lib.mkOption { + type = lib.types.ints.unsigned; + description = "How many automatically configured sync workers to set up"; + default = 0; + }; + instances = lib.mkOption { type = lib.types.attrsOf (lib.types.submodule ({config, ...}: { @@ -147,6 +161,8 @@ in mapTypeApp = t: { "fed-sender" = "synapse.app.generic_worker"; "fed-receiver" = "synapse.app.generic_worker"; + "initial-sync" = "synapse.app.generic_worker"; + "normal-sync" = "synapse.app.generic_worker"; }.${t}; defaultApp = if (!isAuto) then "synapse.app.generic_worker" @@ -204,6 +220,8 @@ in typeToResources = t: { "fed-receiver" = [ "federation" ]; "fed-sender" = [ ]; + "initial-sync" = [ "client" ]; + "normal-sync" = [ "client" ]; }.${t}; in lib.mkOption { type = lib.types.listOf (lib.types.submodule { @@ -546,6 +564,31 @@ in }; }); }) + + + ({ + services.matrix-synapse-next.workers.instances = genAttrs' (lib.lists.range 1 cfg.workers.initialSyncers) + (i: "auto-initial-sync${toString i}") + (i: { + isAuto = true; type = "initial-sync"; index = i; + settings.worker_listeners = [{ port = cfg.workers.workerStartingPort + cfg.workers.federationReceivers + i - 1; }] + ++ lib.optional wcfg.enableMetrics { port = cfg.workers.metricsStartingPort + cfg.workers.federationSenders + cfg.workers.federationReceivers + i; + resources = [ { names = [ "metrics" ]; } ]; + }; + }); + }) + + ({ + services.matrix-synapse-next.workers.instances = genAttrs' (lib.lists.range 1 cfg.workers.normalSyncers) + (i: "auto-normal-sync${toString i}") + (i: { + isAuto = true; type = "normal-sync"; index = i; + settings.worker_listeners = [{ port = cfg.workers.workerStartingPort + cfg.workers.federationReceivers + cfg.workers.initialSyncers + i - 1; }] + ++ lib.optional wcfg.enableMetrics { port = cfg.workers.metricsStartingPort + cfg.workers.federationSenders + cfg.workers.federationReceivers + cfg.workers.initialSyncers + i; + resources = [ { names = [ "metrics" ]; } ]; + }; + }); + }) ]) ({ @@ -586,119 +629,6 @@ in }; } ) workerList); - }) - - (lib.mkIf cfg.enableNginx { - services.nginx.commonHttpConfig = '' - map $request_uri $synapse_backend { - default synapse_master; - - # Sync requests - ~*^/_matrix/client/(v2_alpha|r0)/sync$ synapse_client; - ~*^/_matrix/client/(api/v1|v2_alpha|r0)/events$ synapse_client; - ~*^/_matrix/client/(api/v1|r0)/initialSync$ synapse_client; - ~*^/_matrix/client/(api/v1|r0)/rooms/[^/]+/initialSync$ synapse_client; - - # Federation requests - ~*^/_matrix/federation/v1/event/ synapse_federation; - ~*^/_matrix/federation/v1/state/ synapse_federation; - ~*^/_matrix/federation/v1/state_ids/ synapse_federation; - ~*^/_matrix/federation/v1/backfill/ synapse_federation; - ~*^/_matrix/federation/v1/get_missing_events/ synapse_federation; - ~*^/_matrix/federation/v1/publicRooms synapse_federation; - ~*^/_matrix/federation/v1/query/ synapse_federation; - ~*^/_matrix/federation/v1/make_join/ synapse_federation; - ~*^/_matrix/federation/v1/make_leave/ synapse_federation; - ~*^/_matrix/federation/v1/send_join/ synapse_federation; - ~*^/_matrix/federation/v2/send_join/ synapse_federation; - ~*^/_matrix/federation/v1/send_leave/ synapse_federation; - ~*^/_matrix/federation/v2/send_leave/ synapse_federation; - ~*^/_matrix/federation/v1/invite/ synapse_federation; - ~*^/_matrix/federation/v2/invite/ synapse_federation; - ~*^/_matrix/federation/v1/query_auth/ synapse_federation; - ~*^/_matrix/federation/v1/event_auth/ synapse_federation; - ~*^/_matrix/federation/v1/exchange_third_party_invite/ synapse_federation; - ~*^/_matrix/federation/v1/user/devices/ synapse_federation; - ~*^/_matrix/federation/v1/get_groups_publicised$ synapse_federation; - ~*^/_matrix/key/v2/query synapse_federation; - - # Inbound federation transaction request - ~*^/_matrix/federation/v1/send/ synapse_federation; - - # Client API requests - ~*^/_matrix/client/(api/v1|r0|unstable)/publicRooms$ synapse_client; - ~*^/_matrix/client/(api/v1|r0|unstable)/rooms/.*/joined_members$ synapse_client; - ~*^/_matrix/client/(api/v1|r0|unstable)/rooms/.*/context/.*$ synapse_client; - ~*^/_matrix/client/(api/v1|r0|unstable)/rooms/.*/members$ synapse_client; - ~*^/_matrix/client/(api/v1|r0|unstable)/rooms/.*/state$ synapse_client; - ~*^/_matrix/client/(api/v1|r0|unstable)/account/3pid$ synapse_client; - ~*^/_matrix/client/(api/v1|r0|unstable)/devices$ synapse_client; - ~*^/_matrix/client/(api/v1|r0|unstable)/keys/query$ synapse_client; - ~*^/_matrix/client/(api/v1|r0|unstable)/keys/changes$ synapse_client; - ~*^/_matrix/client/versions$ synapse_client; - ~*^/_matrix/client/(api/v1|r0|unstable)/voip/turnServer$ synapse_client; - ~*^/_matrix/client/(api/v1|r0|unstable)/joined_groups$ synapse_client; - ~*^/_matrix/client/(api/v1|r0|unstable)/publicised_groups$ synapse_client; - ~*^/_matrix/client/(api/v1|r0|unstable)/publicised_groups/ synapse_client; - ~*^/_matrix/client/(api/v1|r0|unstable)/rooms/.*/event/ synapse_client; - ~*^/_matrix/client/(api/v1|r0|unstable)/joined_rooms$ synapse_client; - ~*^/_matrix/client/(api/v1|r0|unstable)/search$ synapse_client; - - # Registration/login requests - ~*^/_matrix/client/(api/v1|r0|unstable)/login$ synapse_client; - ~*^/_matrix/client/(r0|unstable)/register$ synapse_client; - - # Event sending requests - ~*^/_matrix/client/(api/v1|r0|unstable)/rooms/.*/redact synapse_client; - ~*^/_matrix/client/(api/v1|r0|unstable)/rooms/.*/send synapse_client; - ~*^/_matrix/client/(api/v1|r0|unstable)/rooms/.*/state/ synapse_client; - ~*^/_matrix/client/(api/v1|r0|unstable)/rooms/.*/(join|invite|leave|ban|unban|kick)$ synapse_client; - ~*^/_matrix/client/(api/v1|r0|unstable)/join/ synapse_client; - ~*^/_matrix/client/(api/v1|r0|unstable)/profile/ synapse_client; - } - ''; - - services.nginx.upstreams.synapse_master.servers = let - isMainListener = l: isListenerType "client" l && isListenerType "federation" l; - firstMainListener = lib.findFirst isMainListener - (throw "No cartch-all listener configured") cfg.settings.listeners; - address = lib.findFirst (_: true) (throw "No address in main listener") firstMainListener.bind_addresses; - port = firstMainListener.port; - socketAddress = "${address}:${builtins.toString port}"; - in { - "${socketAddress}" = { }; - }; - - services.nginx.upstreams.synapse_federation.servers = let - fedReceivers = lib.filterAttrs (_: w: w.type == "fed-receiver") cfg.workers.instances; - isListenerType = type: listener: lib.lists.any (r: lib.lists.any (n: n == type) r.names) listener.resources; - isFedListener = l: isListenerType "federation" l; - - firstFedListener = w: lib.lists.findFirst isFedListener (throw "No federation endpoint on receiver") w.settings.worker_listeners; - - wAddress = w: lib.lists.findFirst (_: true) (throw "No address in receiver") (firstFedListener w).bind_addresses; - wPort = w: (firstFedListener w).port; - - socketAddress = w: "${wAddress w}:${builtins.toString (wPort w)}"; - socketAddresses = lib.mapAttrsToList (_: value: "${socketAddress value}") fedReceivers; - in if fedReceivers != [ ] then lib.genAttrs socketAddresses (_: { }) else config.services.nginx.upstreams.synapse_master.servers; - - services.nginx.upstreams.synapse_client.servers = config.services.nginx.upstreams.synapse_master.servers; - - - services.nginx.virtualHosts."${cfg.public_baseurl}" = { - enableACME = true; - forceSSL = true; - locations."/_matrix" = { - proxyPass = "http://$synapse_backend"; - extraConfig = '' - client_max_body_size ${cfg.settings.max_upload_size}; - ''; - }; - locations."/_synapse/client" = { - proxyPass = "http://$synapse_backend"; - }; - }; - }) + }) ]); } \ No newline at end of file diff --git a/synapse-module/nginx.nix b/synapse-module/nginx.nix new file mode 100644 index 0000000..6e42a3f --- /dev/null +++ b/synapse-module/nginx.nix @@ -0,0 +1,199 @@ +{ lib, pkgs, config, ...}: +let + cfg = config.services.matrix-synapse-next; + + getWorkersOfType = type: lib.filterAttrs (_: w: w.type == type) cfg.workers.instances; + isListenerType = type: listener: lib.lists.any (r: lib.lists.any (n: n == type) r.names) listener.resources; + firstListenerOfType = type: worker: lib.lists.findFirst (isListenerType type) (throw "No federation endpoint on receiver") worker.settings.worker_listeners; + wAddressOfType = type: w: lib.lists.findFirst (_: true) (throw "No address in receiver") (firstListenerOfType type w).bind_addresses; + wPortOfType = type: w: (firstListenerOfType type w).port; + wSocketAddressOfType = type: w: "${wAddressOfType type w}:${builtins.toString (wPortOfType type w)}"; + generateSocketAddresses = type: workers: lib.mapAttrsToList (_: w: "${wSocketAddressOfType type w}") workers; +in +{ + config = lib.mkIf cfg.enableNginx { + services.nginx.commonHttpConfig = '' + # No since argument means its initialSync + map $arg_since $synapse_unknown_sync { + default synapse_normal_sync; + ''' synapse_initial_sync; + } + + map $request_uri $synapse_uri_group { + # Sync requests + ^/_matrix/client/(r0|v3)/sync$ $synapse_unknown_sync; + ^/_matrix/client/(api/v1|r0|v3)/event$ synapse_normal_sync; + ^/_matrix/client/(api/v1|r0|v3)/initialSync$ synapse_initial_sync; + ^/_matrix/client/(api/v1|r0|v3)/rooms/[^/]+/initialSync$ synapse_initial_sync; + + # Federation requests + ^/_matrix/federation/v1/event/ synapse_federation; + ^/_matrix/federation/v1/state/ synapse_federation; + ^/_matrix/federation/v1/state_ids/ synapse_federation; + ^/_matrix/federation/v1/backfill/ synapse_federation; + ^/_matrix/federation/v1/get_missing_events/ synapse_federation; + ^/_matrix/federation/v1/publicRooms synapse_federation; + ^/_matrix/federation/v1/query/ synapse_federation; + ^/_matrix/federation/v1/make_join/ synapse_federation; + ^/_matrix/federation/v1/make_leave/ synapse_federation; + ^/_matrix/federation/(v1|v2)/send_join/ synapse_federation; + ^/_matrix/federation/(v1|v2)/send_leave/ synapse_federation; + ^/_matrix/federation/(v1|v2)/invite/ synapse_federation; + ^/_matrix/federation/v1/event_auth/ synapse_federation; + ^/_matrix/federation/v1/timestamp_to_event/ synapse_federation; + ^/_matrix/federation/v1/exchange_third_party_invite/ synapse_federation; + ^/_matrix/federation/v1/user/devices/ synapse_federation; + ^/_matrix/key/v2/query synapse_federation; + ^/_matrix/federation/v1/hierarchy/ synapse_federation; + + # Inbound federation transaction request + ^/_matrix/federation/v1/send/ synapse_federation_transaction; + + # Client API requests + ^/_matrix/client/(api/v1|r0|v3|unstable)/createRoom$ synapse_client_interaction; + ^/_matrix/client/(api/v1|r0|v3|unstable)/publicRooms$ synapse_client_interaction; + ^/_matrix/client/(api/v1|r0|v3|unstable)/rooms/.*/joined_members$ synapse_client_interaction; + ^/_matrix/client/(api/v1|r0|v3|unstable)/rooms/.*/context/.*$ synapse_client_interaction; + ^/_matrix/client/(api/v1|r0|v3|unstable)/rooms/.*/members$ synapse_client_interaction; + ^/_matrix/client/(api/v1|r0|v3|unstable)/rooms/.*/state$ synapse_client_interaction; + ^/_matrix/client/v1/rooms/.*/hierarchy$ synapse_client_interaction; + ^/_matrix/client/(v1|unstable)/rooms/.*/relations/ synapse_client_interaction; + ^/_matrix/client/v1/rooms/.*/threads$ synapse_client_interaction; + ^/_matrix/client/unstable/org.matrix.msc2716/rooms/.*/batch_send$ synapse_client_interaction; + ^/_matrix/client/unstable/im.nheko.summary/rooms/.*/summary$ synapse_client_interaction; + ^/_matrix/client/(r0|v3|unstable)/account/3pid$ synapse_client_interaction; + ^/_matrix/client/(r0|v3|unstable)/account/whoami$ synapse_client_interaction; + ^/_matrix/client/(r0|v3|unstable)/devices$ synapse_client_interaction; + ^/_matrix/client/versions$ synapse_client_interaction; + ^/_matrix/client/(api/v1|r0|v3|unstable)/voip/turnServer$ synapse_client_interaction; + ^/_matrix/client/(api/v1|r0|v3|unstable)/rooms/.*/event/ synapse_client_interaction; + ^/_matrix/client/(api/v1|r0|v3|unstable)/joined_rooms$ synapse_client_interaction; + ^/_matrix/client/v1/rooms/.*/timestamp_to_event$ synapse_client_interaction; + ^/_matrix/client/(api/v1|r0|v3|unstable)/search$ synapse_client_interaction; + + # Encryption requests + ^/_matrix/client/(r0|v3|unstable)/keys/query$ synapse_client_encryption; + ^/_matrix/client/(r0|v3|unstable)/keys/changes$ synapse_client_encryption; + ^/_matrix/client/(r0|v3|unstable)/keys/claim$ synapse_client_encryption; + ^/_matrix/client/(r0|v3|unstable)/room_keys/ synapse_client_encryption; + ^/_matrix/client/(r0|v3|unstable)/keys/upload/ synapse_client_encryption; + + # Registration/login requests + ^/_matrix/client/(api/v1|r0|v3|unstable)/login$ synapse_client_login; + ^/_matrix/client/(r0|v3|unstable)/register$ synapse_client_login; + ^/_matrix/client/v1/register/m.login.registration_token/validity$ synapse_client_login; + + # Event sending requests + ^/_matrix/client/(api/v1|r0|v3|unstable)/rooms/.*/redact synapse_client_transaction; + ^/_matrix/client/(api/v1|r0|v3|unstable)/rooms/.*/send synapse_client_transaction; + ^/_matrix/client/(api/v1|r0|v3|unstable)/rooms/.*/state/ synapse_client_transaction; + ^/_matrix/client/(api/v1|r0|v3|unstable)/rooms/.*/(join|invite|leave|ban|unban|kick)$ synapse_client_transaction; + ^/_matrix/client/(api/v1|r0|v3|unstable)/join/ synapse_client_transaction; + ^/_matrix/client/(api/v1|r0|v3|unstable)/profile/ synapse_client_transaction; + + # Account data requests + ^/_matrix/client/(r0|v3|unstable)/.*/tags synapse_client_data; + ^/_matrix/client/(r0|v3|unstable)/.*/account_data synapse_client_data; + + # Receipts requests + ^/_matrix/client/(r0|v3|unstable)/rooms/.*/receipt synapse_client_interaction; + ^/_matrix/client/(r0|v3|unstable)/rooms/.*/read_markers synapse_client_interaction; + + # Presence requests + ^/_matrix/client/(api/v1|r0|v3|unstable)/presence/ synapse_client_presence; + + # User directory search requests; + ^/_matrix/client/(r0|v3|unstable)/user_directory/search$ synapse_client_search; + } + + #Plugboard for url -> workers + map $synapse_uri_group $synapse_backend { + default synapse_master; + + # synapse_initial_sync synapse_worker_initial_sync; + # synapse_normal_sync synapse_worker_normal_sync; + + synapse_federation synapse_worker_federation; + synapse_federation_transaction synapse_worker_federation; + } + + # from https://github.com/tswfi/synapse/commit/b3704b936663cc692241e978dce4ac623276b1a6 + map $arg_access_token $accesstoken_from_urlparam { + # Defaults to just passing back the whole accesstoken + default $arg_access_token; + # Try to extract username part from accesstoken URL parameter + "~syt_(?.*?)_.*" $username; + } + + map $http_authorization $mxid_localpart { + # Defaults to just passing back the whole accesstoken + default $http_authorization; + # Try to extract username part from accesstoken header + "~Bearer syt_(?.*?)_.*" $username; + # if no authorization-header exist, try mapper for URL parameter "access_token" + "" $accesstoken_from_urlparam; + } + ''; + + services.nginx.upstreams.synapse_master.servers = let + isMainListener = l: isListenerType "client" l && isListenerType "federation" l; + firstMainListener = lib.findFirst isMainListener + (throw "No cartch-all listener configured") cfg.settings.listeners; + address = lib.findFirst (_: true) (throw "No address in main listener") firstMainListener.bind_addresses; + port = firstMainListener.port; + socketAddress = "${address}:${builtins.toString port}"; + in { + "${socketAddress}" = { }; + }; + + + services.nginx.upstreams.synapse_worker_federation = { + servers = let + fedReceivers = getWorkersOfType "fed-receiver"; + socketAddresses = generateSocketAddresses "federation" fedReceivers; + in if fedReceivers != [ ] then + lib.genAttrs socketAddresses (_: { }) + else config.services.nginx.upstreams.synapse_master.servers; + extraConfig = '' + hash $mxid_localpart consistent; + ''; + }; + + + services.nginx.upstreams.synapse_worker_initial_sync = { + servers = let + initialSyncers = getWorkersOfType "inital-sync"; + socketAddresses = generateSocketAddresses "client" initialSyncers; + in if initialSyncers != [ ] then + lib.genAttrs socketAddresses (_: { }) + else config.services.nginx.upstreams.synapse_master.server; + extraConfig = '' + hash $mxid_localpart consistent; + ''; + }; + + + services.nginx.upstreams.synapse_worker_normal_sync.servers = let + normalSyncers = getWorkersOfType "normal-sync"; + socketAddresses = generateSocketAddresses "client" normalSyncers; + in if normalSyncers != [ ] then + lib.genAttrs socketAddresses (_: { }) + else config.services.nginx.upstreams.synapse_master.server; + + + + services.nginx.virtualHosts."${cfg.public_baseurl}" = { + enableACME = true; + forceSSL = true; + locations."/_matrix" = { + proxyPass = "http://$synapse_backend"; + extraConfig = '' + client_max_body_size ${cfg.settings.max_upload_size}; + ''; + }; + locations."/_synapse/client" = { + proxyPass = "http://$synapse_backend"; + }; + }; +}; +} \ No newline at end of file From 3bbfebb5c1874096a000882faf3c5da4223f8c1b Mon Sep 17 00:00:00 2001 From: Daniel Olsen Date: Fri, 9 Dec 2022 10:25:25 +0100 Subject: [PATCH 13/31] fix hashing for upstream normal_sync --- synapse-module/nginx.nix | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/synapse-module/nginx.nix b/synapse-module/nginx.nix index 6e42a3f..eb42346 100644 --- a/synapse-module/nginx.nix +++ b/synapse-module/nginx.nix @@ -154,9 +154,6 @@ in in if fedReceivers != [ ] then lib.genAttrs socketAddresses (_: { }) else config.services.nginx.upstreams.synapse_master.servers; - extraConfig = '' - hash $mxid_localpart consistent; - ''; }; @@ -173,12 +170,17 @@ in }; - services.nginx.upstreams.synapse_worker_normal_sync.servers = let - normalSyncers = getWorkersOfType "normal-sync"; - socketAddresses = generateSocketAddresses "client" normalSyncers; - in if normalSyncers != [ ] then - lib.genAttrs socketAddresses (_: { }) - else config.services.nginx.upstreams.synapse_master.server; + services.nginx.upstreams.synapse_worker_normal_sync = { + servers = let + normalSyncers = getWorkersOfType "normal-sync"; + socketAddresses = generateSocketAddresses "client" normalSyncers; + in if normalSyncers != [ ] then + lib.genAttrs socketAddresses (_: { }) + else config.services.nginx.upstreams.synapse_master.server; + extraConfig = '' + hash $mxid_localpart consistent; + ''; + }; From 9568a59b1b144c2337e5e75adb5e6c72475cd9f3 Mon Sep 17 00:00:00 2001 From: Daniel Olsen Date: Fri, 9 Dec 2022 10:34:58 +0100 Subject: [PATCH 14/31] fix typo --- synapse-module/nginx.nix | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/synapse-module/nginx.nix b/synapse-module/nginx.nix index eb42346..58b0e20 100644 --- a/synapse-module/nginx.nix +++ b/synapse-module/nginx.nix @@ -159,7 +159,7 @@ in services.nginx.upstreams.synapse_worker_initial_sync = { servers = let - initialSyncers = getWorkersOfType "inital-sync"; + initialSyncers = getWorkersOfType "initial-sync"; socketAddresses = generateSocketAddresses "client" initialSyncers; in if initialSyncers != [ ] then lib.genAttrs socketAddresses (_: { }) From d44e015ae818df3838f9f0fcc0645455c2444903 Mon Sep 17 00:00:00 2001 From: Daniel Olsen Date: Sat, 10 Dec 2022 05:43:25 +0100 Subject: [PATCH 15/31] hash incoming federation by source ip --- synapse-module/nginx.nix | 3 +++ 1 file changed, 3 insertions(+) diff --git a/synapse-module/nginx.nix b/synapse-module/nginx.nix index 58b0e20..3d1ff9f 100644 --- a/synapse-module/nginx.nix +++ b/synapse-module/nginx.nix @@ -154,6 +154,9 @@ in in if fedReceivers != [ ] then lib.genAttrs socketAddresses (_: { }) else config.services.nginx.upstreams.synapse_master.servers; + extraConfig = '' + ip_hash; + ''; }; From 632a6f26addcfac7bc5b96e7079ce53fbfc23d07 Mon Sep 17 00:00:00 2001 From: Daniel Olsen Date: Sat, 10 Dec 2022 07:08:35 +0100 Subject: [PATCH 16/31] debugging headers --- synapse-module/nginx.nix | 2 ++ 1 file changed, 2 insertions(+) diff --git a/synapse-module/nginx.nix b/synapse-module/nginx.nix index 3d1ff9f..8cc2bd0 100644 --- a/synapse-module/nginx.nix +++ b/synapse-module/nginx.nix @@ -193,6 +193,8 @@ in locations."/_matrix" = { proxyPass = "http://$synapse_backend"; extraConfig = '' + add_header X-debug-backend $synapse_backend; + add_header X-debug-group $synapse_uri_group; client_max_body_size ${cfg.settings.max_upload_size}; ''; }; From 7e5023a7415ee15b52d71973977840236d120b1d Mon Sep 17 00:00:00 2001 From: Daniel Olsen Date: Sat, 10 Dec 2022 07:44:43 +0100 Subject: [PATCH 17/31] plug inital sync workers in --- synapse-module/nginx.nix | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/synapse-module/nginx.nix b/synapse-module/nginx.nix index 8cc2bd0..cd323ea 100644 --- a/synapse-module/nginx.nix +++ b/synapse-module/nginx.nix @@ -110,8 +110,8 @@ in map $synapse_uri_group $synapse_backend { default synapse_master; - # synapse_initial_sync synapse_worker_initial_sync; - # synapse_normal_sync synapse_worker_normal_sync; + synapse_initial_sync synapse_worker_initial_sync; + synapse_normal_sync synapse_worker_normal_sync; synapse_federation synapse_worker_federation; synapse_federation_transaction synapse_worker_federation; From f2ed2ea7bc75f70fa95e816b7b106913d75029e7 Mon Sep 17 00:00:00 2001 From: Daniel Olsen Date: Sat, 10 Dec 2022 08:06:19 +0100 Subject: [PATCH 18/31] more debug --- synapse-module/nginx.nix | 1 + 1 file changed, 1 insertion(+) diff --git a/synapse-module/nginx.nix b/synapse-module/nginx.nix index cd323ea..3658db7 100644 --- a/synapse-module/nginx.nix +++ b/synapse-module/nginx.nix @@ -195,6 +195,7 @@ in extraConfig = '' add_header X-debug-backend $synapse_backend; add_header X-debug-group $synapse_uri_group; + add_header X-debug-uri $request_uri; client_max_body_size ${cfg.settings.max_upload_size}; ''; }; From facc54ab80d7df7de973b29498080c9219c8e027 Mon Sep 17 00:00:00 2001 From: Daniel Olsen Date: Sat, 10 Dec 2022 08:12:18 +0100 Subject: [PATCH 19/31] use uri instead of request_uri --- synapse-module/nginx.nix | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/synapse-module/nginx.nix b/synapse-module/nginx.nix index 3658db7..c8341bb 100644 --- a/synapse-module/nginx.nix +++ b/synapse-module/nginx.nix @@ -19,7 +19,7 @@ in ''' synapse_initial_sync; } - map $request_uri $synapse_uri_group { + map $uri $synapse_uri_group { # Sync requests ^/_matrix/client/(r0|v3)/sync$ $synapse_unknown_sync; ^/_matrix/client/(api/v1|r0|v3)/event$ synapse_normal_sync; From 8ab05856ca7054d523eea65dec8a8059d6810428 Mon Sep 17 00:00:00 2001 From: Daniel Olsen Date: Sat, 10 Dec 2022 08:19:54 +0100 Subject: [PATCH 20/31] mark regexes as regexes --- synapse-module/nginx.nix | 126 +++++++++++++++++++-------------------- 1 file changed, 63 insertions(+), 63 deletions(-) diff --git a/synapse-module/nginx.nix b/synapse-module/nginx.nix index c8341bb..3c1ebac 100644 --- a/synapse-module/nginx.nix +++ b/synapse-module/nginx.nix @@ -21,89 +21,89 @@ in map $uri $synapse_uri_group { # Sync requests - ^/_matrix/client/(r0|v3)/sync$ $synapse_unknown_sync; - ^/_matrix/client/(api/v1|r0|v3)/event$ synapse_normal_sync; - ^/_matrix/client/(api/v1|r0|v3)/initialSync$ synapse_initial_sync; - ^/_matrix/client/(api/v1|r0|v3)/rooms/[^/]+/initialSync$ synapse_initial_sync; + ~^/_matrix/client/(r0|v3)/sync$ $synapse_unknown_sync; + ~^/_matrix/client/(api/v1|r0|v3)/event$ synapse_normal_sync; + ~^/_matrix/client/(api/v1|r0|v3)/initialSync$ synapse_initial_sync; + ~^/_matrix/client/(api/v1|r0|v3)/rooms/[^/]+/initialSync$ synapse_initial_sync; # Federation requests - ^/_matrix/federation/v1/event/ synapse_federation; - ^/_matrix/federation/v1/state/ synapse_federation; - ^/_matrix/federation/v1/state_ids/ synapse_federation; - ^/_matrix/federation/v1/backfill/ synapse_federation; - ^/_matrix/federation/v1/get_missing_events/ synapse_federation; - ^/_matrix/federation/v1/publicRooms synapse_federation; - ^/_matrix/federation/v1/query/ synapse_federation; - ^/_matrix/federation/v1/make_join/ synapse_federation; - ^/_matrix/federation/v1/make_leave/ synapse_federation; - ^/_matrix/federation/(v1|v2)/send_join/ synapse_federation; - ^/_matrix/federation/(v1|v2)/send_leave/ synapse_federation; - ^/_matrix/federation/(v1|v2)/invite/ synapse_federation; - ^/_matrix/federation/v1/event_auth/ synapse_federation; - ^/_matrix/federation/v1/timestamp_to_event/ synapse_federation; - ^/_matrix/federation/v1/exchange_third_party_invite/ synapse_federation; - ^/_matrix/federation/v1/user/devices/ synapse_federation; - ^/_matrix/key/v2/query synapse_federation; - ^/_matrix/federation/v1/hierarchy/ synapse_federation; + ~^/_matrix/federation/v1/event/ synapse_federation; + ~^/_matrix/federation/v1/state/ synapse_federation; + ~^/_matrix/federation/v1/state_ids/ synapse_federation; + ~^/_matrix/federation/v1/backfill/ synapse_federation; + ~^/_matrix/federation/v1/get_missing_events/ synapse_federation; + ~^/_matrix/federation/v1/publicRooms synapse_federation; + ~^/_matrix/federation/v1/query/ synapse_federation; + ~^/_matrix/federation/v1/make_join/ synapse_federation; + ~^/_matrix/federation/v1/make_leave/ synapse_federation; + ~^/_matrix/federation/(v1|v2)/send_join/ synapse_federation; + ~^/_matrix/federation/(v1|v2)/send_leave/ synapse_federation; + ~^/_matrix/federation/(v1|v2)/invite/ synapse_federation; + ~^/_matrix/federation/v1/event_auth/ synapse_federation; + ~^/_matrix/federation/v1/timestamp_to_event/ synapse_federation; + ~^/_matrix/federation/v1/exchange_third_party_invite/ synapse_federation; + ~^/_matrix/federation/v1/user/devices/ synapse_federation; + ~^/_matrix/key/v2/query synapse_federation; + ~^/_matrix/federation/v1/hierarchy/ synapse_federation; # Inbound federation transaction request - ^/_matrix/federation/v1/send/ synapse_federation_transaction; + ~^/_matrix/federation/v1/send/ synapse_federation_transaction; # Client API requests - ^/_matrix/client/(api/v1|r0|v3|unstable)/createRoom$ synapse_client_interaction; - ^/_matrix/client/(api/v1|r0|v3|unstable)/publicRooms$ synapse_client_interaction; - ^/_matrix/client/(api/v1|r0|v3|unstable)/rooms/.*/joined_members$ synapse_client_interaction; - ^/_matrix/client/(api/v1|r0|v3|unstable)/rooms/.*/context/.*$ synapse_client_interaction; - ^/_matrix/client/(api/v1|r0|v3|unstable)/rooms/.*/members$ synapse_client_interaction; - ^/_matrix/client/(api/v1|r0|v3|unstable)/rooms/.*/state$ synapse_client_interaction; - ^/_matrix/client/v1/rooms/.*/hierarchy$ synapse_client_interaction; - ^/_matrix/client/(v1|unstable)/rooms/.*/relations/ synapse_client_interaction; - ^/_matrix/client/v1/rooms/.*/threads$ synapse_client_interaction; - ^/_matrix/client/unstable/org.matrix.msc2716/rooms/.*/batch_send$ synapse_client_interaction; - ^/_matrix/client/unstable/im.nheko.summary/rooms/.*/summary$ synapse_client_interaction; - ^/_matrix/client/(r0|v3|unstable)/account/3pid$ synapse_client_interaction; - ^/_matrix/client/(r0|v3|unstable)/account/whoami$ synapse_client_interaction; - ^/_matrix/client/(r0|v3|unstable)/devices$ synapse_client_interaction; - ^/_matrix/client/versions$ synapse_client_interaction; - ^/_matrix/client/(api/v1|r0|v3|unstable)/voip/turnServer$ synapse_client_interaction; - ^/_matrix/client/(api/v1|r0|v3|unstable)/rooms/.*/event/ synapse_client_interaction; - ^/_matrix/client/(api/v1|r0|v3|unstable)/joined_rooms$ synapse_client_interaction; - ^/_matrix/client/v1/rooms/.*/timestamp_to_event$ synapse_client_interaction; - ^/_matrix/client/(api/v1|r0|v3|unstable)/search$ synapse_client_interaction; + ~^/_matrix/client/(api/v1|r0|v3|unstable)/createRoom$ synapse_client_interaction; + ~^/_matrix/client/(api/v1|r0|v3|unstable)/publicRooms$ synapse_client_interaction; + ~^/_matrix/client/(api/v1|r0|v3|unstable)/rooms/.*/joined_members$ synapse_client_interaction; + ~^/_matrix/client/(api/v1|r0|v3|unstable)/rooms/.*/context/.*$ synapse_client_interaction; + ~^/_matrix/client/(api/v1|r0|v3|unstable)/rooms/.*/members$ synapse_client_interaction; + ~^/_matrix/client/(api/v1|r0|v3|unstable)/rooms/.*/state$ synapse_client_interaction; + ~^/_matrix/client/v1/rooms/.*/hierarchy$ synapse_client_interaction; + ~^/_matrix/client/(v1|unstable)/rooms/.*/relations/ synapse_client_interaction; + ~^/_matrix/client/v1/rooms/.*/threads$ synapse_client_interaction; + ~^/_matrix/client/unstable/org.matrix.msc2716/rooms/.*/batch_send$ synapse_client_interaction; + ~^/_matrix/client/unstable/im.nheko.summary/rooms/.*/summary$ synapse_client_interaction; + ~^/_matrix/client/(r0|v3|unstable)/account/3pid$ synapse_client_interaction; + ~^/_matrix/client/(r0|v3|unstable)/account/whoami$ synapse_client_interaction; + ~^/_matrix/client/(r0|v3|unstable)/devices$ synapse_client_interaction; + ~^/_matrix/client/versions$ synapse_client_interaction; + ~^/_matrix/client/(api/v1|r0|v3|unstable)/voip/turnServer$ synapse_client_interaction; + ~^/_matrix/client/(api/v1|r0|v3|unstable)/rooms/.*/event/ synapse_client_interaction; + ~^/_matrix/client/(api/v1|r0|v3|unstable)/joined_rooms$ synapse_client_interaction; + ~^/_matrix/client/v1/rooms/.*/timestamp_to_event$ synapse_client_interaction; + ~^/_matrix/client/(api/v1|r0|v3|unstable)/search$ synapse_client_interaction; # Encryption requests - ^/_matrix/client/(r0|v3|unstable)/keys/query$ synapse_client_encryption; - ^/_matrix/client/(r0|v3|unstable)/keys/changes$ synapse_client_encryption; - ^/_matrix/client/(r0|v3|unstable)/keys/claim$ synapse_client_encryption; - ^/_matrix/client/(r0|v3|unstable)/room_keys/ synapse_client_encryption; - ^/_matrix/client/(r0|v3|unstable)/keys/upload/ synapse_client_encryption; + ~^/_matrix/client/(r0|v3|unstable)/keys/query$ synapse_client_encryption; + ~^/_matrix/client/(r0|v3|unstable)/keys/changes$ synapse_client_encryption; + ~^/_matrix/client/(r0|v3|unstable)/keys/claim$ synapse_client_encryption; + ~^/_matrix/client/(r0|v3|unstable)/room_keys/ synapse_client_encryption; + ~^/_matrix/client/(r0|v3|unstable)/keys/upload/ synapse_client_encryption; # Registration/login requests - ^/_matrix/client/(api/v1|r0|v3|unstable)/login$ synapse_client_login; - ^/_matrix/client/(r0|v3|unstable)/register$ synapse_client_login; - ^/_matrix/client/v1/register/m.login.registration_token/validity$ synapse_client_login; + ~^/_matrix/client/(api/v1|r0|v3|unstable)/login$ synapse_client_login; + ~^/_matrix/client/(r0|v3|unstable)/register$ synapse_client_login; + ~^/_matrix/client/v1/register/m.login.registration_token/validity$ synapse_client_login; # Event sending requests - ^/_matrix/client/(api/v1|r0|v3|unstable)/rooms/.*/redact synapse_client_transaction; - ^/_matrix/client/(api/v1|r0|v3|unstable)/rooms/.*/send synapse_client_transaction; - ^/_matrix/client/(api/v1|r0|v3|unstable)/rooms/.*/state/ synapse_client_transaction; - ^/_matrix/client/(api/v1|r0|v3|unstable)/rooms/.*/(join|invite|leave|ban|unban|kick)$ synapse_client_transaction; - ^/_matrix/client/(api/v1|r0|v3|unstable)/join/ synapse_client_transaction; - ^/_matrix/client/(api/v1|r0|v3|unstable)/profile/ synapse_client_transaction; + ~^/_matrix/client/(api/v1|r0|v3|unstable)/rooms/.*/redact synapse_client_transaction; + ~^/_matrix/client/(api/v1|r0|v3|unstable)/rooms/.*/send synapse_client_transaction; + ~^/_matrix/client/(api/v1|r0|v3|unstable)/rooms/.*/state/ synapse_client_transaction; + ~^/_matrix/client/(api/v1|r0|v3|unstable)/rooms/.*/(join|invite|leave|ban|unban|kick)$ synapse_client_transaction; + ~^/_matrix/client/(api/v1|r0|v3|unstable)/join/ synapse_client_transaction; + ~^/_matrix/client/(api/v1|r0|v3|unstable)/profile/ synapse_client_transaction; # Account data requests - ^/_matrix/client/(r0|v3|unstable)/.*/tags synapse_client_data; - ^/_matrix/client/(r0|v3|unstable)/.*/account_data synapse_client_data; + ~^/_matrix/client/(r0|v3|unstable)/.*/tags synapse_client_data; + ~^/_matrix/client/(r0|v3|unstable)/.*/account_data synapse_client_data; # Receipts requests - ^/_matrix/client/(r0|v3|unstable)/rooms/.*/receipt synapse_client_interaction; - ^/_matrix/client/(r0|v3|unstable)/rooms/.*/read_markers synapse_client_interaction; + ~^/_matrix/client/(r0|v3|unstable)/rooms/.*/receipt synapse_client_interaction; + ~^/_matrix/client/(r0|v3|unstable)/rooms/.*/read_markers synapse_client_interaction; # Presence requests - ^/_matrix/client/(api/v1|r0|v3|unstable)/presence/ synapse_client_presence; + ~^/_matrix/client/(api/v1|r0|v3|unstable)/presence/ synapse_client_presence; # User directory search requests; - ^/_matrix/client/(r0|v3|unstable)/user_directory/search$ synapse_client_search; + ~^/_matrix/client/(r0|v3|unstable)/user_directory/search$ synapse_client_search; } #Plugboard for url -> workers From 07417577d75455812d80858c32fe58cf18220916 Mon Sep 17 00:00:00 2001 From: Daniel Olsen Date: Sat, 10 Dec 2022 08:29:15 +0100 Subject: [PATCH 21/31] remove uri debug --- synapse-module/nginx.nix | 1 - 1 file changed, 1 deletion(-) diff --git a/synapse-module/nginx.nix b/synapse-module/nginx.nix index 3c1ebac..02933a9 100644 --- a/synapse-module/nginx.nix +++ b/synapse-module/nginx.nix @@ -195,7 +195,6 @@ in extraConfig = '' add_header X-debug-backend $synapse_backend; add_header X-debug-group $synapse_uri_group; - add_header X-debug-uri $request_uri; client_max_body_size ${cfg.settings.max_upload_size}; ''; }; From e6dfcbe3acc66e333230d9b907f57c474f7258b1 Mon Sep 17 00:00:00 2001 From: Daniel Olsen Date: Tue, 13 Dec 2022 07:34:10 +0100 Subject: [PATCH 22/31] increase proxy timeouts initial sync can take a while maybe normal sync polling will be more reliable with a longer timeout as well? --- synapse-module/nginx.nix | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/synapse-module/nginx.nix b/synapse-module/nginx.nix index 02933a9..3e789ea 100644 --- a/synapse-module/nginx.nix +++ b/synapse-module/nginx.nix @@ -196,6 +196,25 @@ in add_header X-debug-backend $synapse_backend; add_header X-debug-group $synapse_uri_group; client_max_body_size ${cfg.settings.max_upload_size}; + proxy_read_timeout 10m; + ''; + }; + locations."~ ^/_matrix/client/(r0|v3)/sync$" = { + proxyPass = "http://$synapse_unknown_sync"; + extraConfig = '' + proxy_read_timeout 1h; + ''; + }; + locations."~ ^/_matrix/client/(api/v1|r0|v3)/initialSync$" = { + proxyPass = "http://$synapse_initial_sync"; + extraConfig = '' + proxy_read_timeout 1h; + ''; + }; + locations."~ ^/_matrix/client/(api/v1|r0|v3)/rooms/[^/]+/initialSync$" = { + proxyPass = "http://$synapse_initial_sync"; + extraConfig = '' + proxy_read_timeout 1h; ''; }; locations."/_synapse/client" = { From a2b4d2e341c57b34628fbe683c930e4d64c7317d Mon Sep 17 00:00:00 2001 From: Daniel Olsen Date: Wed, 14 Dec 2022 07:44:23 +0100 Subject: [PATCH 23/31] fix initalSync nginx locations --- synapse-module/nginx.nix | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/synapse-module/nginx.nix b/synapse-module/nginx.nix index 3e789ea..2a39482 100644 --- a/synapse-module/nginx.nix +++ b/synapse-module/nginx.nix @@ -206,13 +206,13 @@ in ''; }; locations."~ ^/_matrix/client/(api/v1|r0|v3)/initialSync$" = { - proxyPass = "http://$synapse_initial_sync"; + proxyPass = "http://synapse_worker_initial_sync"; extraConfig = '' proxy_read_timeout 1h; ''; }; locations."~ ^/_matrix/client/(api/v1|r0|v3)/rooms/[^/]+/initialSync$" = { - proxyPass = "http://$synapse_initial_sync"; + proxyPass = "http://synapse_worker_initial_sync"; extraConfig = '' proxy_read_timeout 1h; ''; From c7f34ff60bb57b0a061056470b27496fdfa29bfd Mon Sep 17 00:00:00 2001 From: Daniel Olsen Date: Wed, 14 Dec 2022 08:21:53 +0100 Subject: [PATCH 24/31] eventPersister workers --- synapse-module/default.nix | 51 ++++++++++++++++++++++++++++++++------ 1 file changed, 43 insertions(+), 8 deletions(-) diff --git a/synapse-module/default.nix b/synapse-module/default.nix index 7fd6463..9d5241c 100644 --- a/synapse-module/default.nix +++ b/synapse-module/default.nix @@ -131,6 +131,12 @@ in default = 0; }; + eventPersisters = lib.mkOption { + type = lib.types.ints.unsigned; + description = "How many automatically configured event-persisters to set up"; + default = 0; + }; + instances = lib.mkOption { type = lib.types.attrsOf (lib.types.submodule ({config, ...}: { @@ -159,14 +165,15 @@ in options.worker_app = let mapTypeApp = t: { - "fed-sender" = "synapse.app.generic_worker"; - "fed-receiver" = "synapse.app.generic_worker"; - "initial-sync" = "synapse.app.generic_worker"; - "normal-sync" = "synapse.app.generic_worker"; + "fed-sender" = "synapse.app.generic_worker"; + "fed-receiver" = "synapse.app.generic_worker"; + "initial-sync" = "synapse.app.generic_worker"; + "normal-sync" = "synapse.app.generic_worker"; + "event-persist" = "synapse.app.generic_worker"; }.${t}; defaultApp = if (!isAuto) then "synapse.app.generic_worker" - else mapTypeApp type; + else mapTypeApp type; in lib.mkOption { type = lib.types.enum [ "synapse.app.generic_worker" @@ -218,10 +225,11 @@ in }; options.resources = let typeToResources = t: { - "fed-receiver" = [ "federation" ]; - "fed-sender" = [ ]; + "fed-receiver" = [ "federation" ]; + "fed-sender" = [ ]; "initial-sync" = [ "client" ]; - "normal-sync" = [ "client" ]; + "normal-sync" = [ "client" ]; + "event-persist" = [ "replication" ]; }.${t}; in lib.mkOption { type = lib.types.listOf (lib.types.submodule { @@ -589,6 +597,33 @@ in }; }); }) + + ({ + services.matrix-synapse-next.settings.instance_map = genAttrs' (lib.lists.range 1 cfg.workers.eventPersisters) + (i: "auto-event-persist${toString i}") + (i: let + isReplication = l: lib.lists.any (r: lib.lists.any (n: n == "replication") r.names) l.resources; + wRL = lib.lists.findFirst isReplication + (throw "No replication listener configured!") + cfg.workers.instances."auto-event-persist${toString i}".settings.listeners; + wRH = lib.findFirst (x: true) (throw "Replication listener had no addresses") + wRL.bind_addresses; + wRP = wRL.port; + in { + host = wRH; + port = wRP; + } + ); + services.matrix-synapse-next.settings.stream_writers.events = lib.genList (i: "auto-event-persist${toString (i + 1)}") cfg.workers.eventPersisters; + services.matrix-synapse-next.workers.instances = genAttrs' (lib.lists.range 1 cfg.workers.eventPersisters) + (i: "auto-event-persist${toString i}") + (i: { + isAuto = true; type = "event-persist"; index = i; + settings.worker_listeners = [{ port = cfg.workers.workerStartingPort + cfg.workers.federationReceivers + cfg.workers.initialSyncers + cfg.workers.normalSyncers + i - 1;}] + ++ lib.optional wcfg.enableMetrics { port = cfg.workers.metricsStartingPort + cfg.workers.federationSenders + cfg.workers.federationReceivers + cfg.workers.initialSyncers + cfg.workers.normalSyncers + i; + }; + }); + }) ]) ({ From 976c8710ba1ea14ba4eb46657cd9ebf5c8d957a3 Mon Sep 17 00:00:00 2001 From: Daniel Olsen Date: Wed, 14 Dec 2022 08:38:01 +0100 Subject: [PATCH 25/31] just route through the synapse backends --- synapse-module/nginx.nix | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/synapse-module/nginx.nix b/synapse-module/nginx.nix index 2a39482..52cfa3e 100644 --- a/synapse-module/nginx.nix +++ b/synapse-module/nginx.nix @@ -200,7 +200,7 @@ in ''; }; locations."~ ^/_matrix/client/(r0|v3)/sync$" = { - proxyPass = "http://$synapse_unknown_sync"; + proxyPass = "http://$synapse_backend"; extraConfig = '' proxy_read_timeout 1h; ''; From 103f4bd378b004d8cd4054e29803f7ba3585a0f8 Mon Sep 17 00:00:00 2001 From: Daniel Olsen Date: Wed, 14 Dec 2022 08:44:25 +0100 Subject: [PATCH 26/31] don't specify empty event streamer map --- synapse-module/default.nix | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/synapse-module/default.nix b/synapse-module/default.nix index 9d5241c..4652c87 100644 --- a/synapse-module/default.nix +++ b/synapse-module/default.nix @@ -614,7 +614,7 @@ in port = wRP; } ); - services.matrix-synapse-next.settings.stream_writers.events = lib.genList (i: "auto-event-persist${toString (i + 1)}") cfg.workers.eventPersisters; + services.matrix-synapse-next.settings.stream_writers.events = lib.optional (cfg.workers.eventPersisters > 0) (lib.genList (i: "auto-event-persist${toString (i + 1)}") cfg.workers.eventPersisters); services.matrix-synapse-next.workers.instances = genAttrs' (lib.lists.range 1 cfg.workers.eventPersisters) (i: "auto-event-persist${toString i}") (i: { From 476d66f8b5fe3c97326c51d736afc42e1284d8d3 Mon Sep 17 00:00:00 2001 From: Daniel Olsen Date: Wed, 14 Dec 2022 08:57:04 +0100 Subject: [PATCH 27/31] mkif instaed of optional to make it not be in the config at all --- synapse-module/default.nix | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/synapse-module/default.nix b/synapse-module/default.nix index 4652c87..b2b09f2 100644 --- a/synapse-module/default.nix +++ b/synapse-module/default.nix @@ -614,7 +614,7 @@ in port = wRP; } ); - services.matrix-synapse-next.settings.stream_writers.events = lib.optional (cfg.workers.eventPersisters > 0) (lib.genList (i: "auto-event-persist${toString (i + 1)}") cfg.workers.eventPersisters); + services.matrix-synapse-next.settings.stream_writers.events = lib.mkIf (cfg.workers.eventPersisters > 0) (lib.genList (i: "auto-event-persist${toString (i + 1)}") cfg.workers.eventPersisters); services.matrix-synapse-next.workers.instances = genAttrs' (lib.lists.range 1 cfg.workers.eventPersisters) (i: "auto-event-persist${toString i}") (i: { From d900b8f288838e21b956f69ce16c20c897bd6df6 Mon Sep 17 00:00:00 2001 From: Daniel Olsen Date: Wed, 14 Dec 2022 09:12:35 +0100 Subject: [PATCH 28/31] workers listeneres go in worker_listeners --- synapse-module/default.nix | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/synapse-module/default.nix b/synapse-module/default.nix index b2b09f2..137efca 100644 --- a/synapse-module/default.nix +++ b/synapse-module/default.nix @@ -605,7 +605,7 @@ in isReplication = l: lib.lists.any (r: lib.lists.any (n: n == "replication") r.names) l.resources; wRL = lib.lists.findFirst isReplication (throw "No replication listener configured!") - cfg.workers.instances."auto-event-persist${toString i}".settings.listeners; + cfg.workers.instances."auto-event-persist${toString i}".settings.worker_listeners; wRH = lib.findFirst (x: true) (throw "Replication listener had no addresses") wRL.bind_addresses; wRP = wRL.port; From bb105192c2f5476661eb600bcbd94b13fda611b6 Mon Sep 17 00:00:00 2001 From: Daniel Olsen Date: Wed, 14 Dec 2022 09:15:57 +0100 Subject: [PATCH 29/31] add metrics listenr to event persister --- synapse-module/default.nix | 1 + 1 file changed, 1 insertion(+) diff --git a/synapse-module/default.nix b/synapse-module/default.nix index 137efca..b5484c3 100644 --- a/synapse-module/default.nix +++ b/synapse-module/default.nix @@ -621,6 +621,7 @@ in isAuto = true; type = "event-persist"; index = i; settings.worker_listeners = [{ port = cfg.workers.workerStartingPort + cfg.workers.federationReceivers + cfg.workers.initialSyncers + cfg.workers.normalSyncers + i - 1;}] ++ lib.optional wcfg.enableMetrics { port = cfg.workers.metricsStartingPort + cfg.workers.federationSenders + cfg.workers.federationReceivers + cfg.workers.initialSyncers + cfg.workers.normalSyncers + i; + resources = [ { names = [ "metrics" ]; } ]; }; }); }) From 43dbc17526576cb8e0980cef51c48b6598f97550 Mon Sep 17 00:00:00 2001 From: Daniel Olsen Date: Wed, 14 Dec 2022 10:10:11 +0100 Subject: [PATCH 30/31] user-dir updater worker --- synapse-module/default.nix | 19 +++++++++++++++++-- synapse-module/nginx.nix | 16 +++++++++++++--- 2 files changed, 30 insertions(+), 5 deletions(-) diff --git a/synapse-module/default.nix b/synapse-module/default.nix index b5484c3..ec5f3d8 100644 --- a/synapse-module/default.nix +++ b/synapse-module/default.nix @@ -137,6 +137,8 @@ in default = 0; }; + useUserDirectoryWorker = lib.mkEnableOption "user directory worker"; + instances = lib.mkOption { type = lib.types.attrsOf (lib.types.submodule ({config, ...}: { @@ -170,6 +172,7 @@ in "initial-sync" = "synapse.app.generic_worker"; "normal-sync" = "synapse.app.generic_worker"; "event-persist" = "synapse.app.generic_worker"; + "user-dir" = "synapse.app.generic_worker"; }.${t}; defaultApp = if (!isAuto) then "synapse.app.generic_worker" @@ -230,6 +233,7 @@ in "initial-sync" = [ "client" ]; "normal-sync" = [ "client" ]; "event-persist" = [ "replication" ]; + "user-dir" = [ "client" ]; }.${t}; in lib.mkOption { type = lib.types.listOf (lib.types.submodule { @@ -620,11 +624,22 @@ in (i: { isAuto = true; type = "event-persist"; index = i; settings.worker_listeners = [{ port = cfg.workers.workerStartingPort + cfg.workers.federationReceivers + cfg.workers.initialSyncers + cfg.workers.normalSyncers + i - 1;}] - ++ lib.optional wcfg.enableMetrics { port = cfg.workers.metricsStartingPort + cfg.workers.federationSenders + cfg.workers.federationReceivers + cfg.workers.initialSyncers + cfg.workers.normalSyncers + i; - resources = [ { names = [ "metrics" ]; } ]; + ++ lib.optional wcfg.enableMetrics { port = cfg.workers.metricsStartingPort + cfg.workers.federationSenders + cfg.workers.federationReceivers + cfg.workers.initialSyncers + cfg.workers.normalSyncers + i; + resources = [ { names = [ "metrics" ]; } ]; }; }); }) + + (lib.mkIf cfg.workers.useUserDirectoryWorker { + services.matrix-synapse-next.workers.instances."auto-user-dir" = { + isAuto = true; type = "user-dir"; index = 1; + settings.worker_listeners = [{ port = cfg.workers.workerStartingPort + cfg.workers.federationReceivers + cfg.workers.initialSyncers + cfg.workers.normalSyncers + cfg.workers.eventPersisters + 1 - 1;}] + ++ lib.optional wcfg.enableMetrics { port = cfg.workers.metricsStartingPort + cfg.workers.federationSenders + cfg.workers.federationReceivers + cfg.workers.initialSyncers + cfg.workers.normalSyncers + cfg.workers.eventPersisters + 1; + resources = [ { names = [ "metrics"]; } ]; + }; + }; + services.matrix-synapse-next.settings.update_user_directory_from_worker = "auto-user-dir"; + }) ]) ({ diff --git a/synapse-module/nginx.nix b/synapse-module/nginx.nix index 52cfa3e..db3fe5a 100644 --- a/synapse-module/nginx.nix +++ b/synapse-module/nginx.nix @@ -103,7 +103,7 @@ in ~^/_matrix/client/(api/v1|r0|v3|unstable)/presence/ synapse_client_presence; # User directory search requests; - ~^/_matrix/client/(r0|v3|unstable)/user_directory/search$ synapse_client_search; + ~^/_matrix/client/(r0|v3|unstable)/user_directory/search$ synapse_client_user-dir; } #Plugboard for url -> workers @@ -115,6 +115,8 @@ in synapse_federation synapse_worker_federation; synapse_federation_transaction synapse_worker_federation; + + synapse_client_user-dir synapse_worker_user-dir; } # from https://github.com/tswfi/synapse/commit/b3704b936663cc692241e978dce4ac623276b1a6 @@ -166,7 +168,7 @@ in socketAddresses = generateSocketAddresses "client" initialSyncers; in if initialSyncers != [ ] then lib.genAttrs socketAddresses (_: { }) - else config.services.nginx.upstreams.synapse_master.server; + else config.services.nginx.upstreams.synapse_master.servers; extraConfig = '' hash $mxid_localpart consistent; ''; @@ -179,13 +181,21 @@ in socketAddresses = generateSocketAddresses "client" normalSyncers; in if normalSyncers != [ ] then lib.genAttrs socketAddresses (_: { }) - else config.services.nginx.upstreams.synapse_master.server; + else config.services.nginx.upstreams.synapse_master.servers; extraConfig = '' hash $mxid_localpart consistent; ''; }; + services.nginx.upstreams.synapse_worker_user-dir = { + servers = let + workers = getWorkersOfType "user-dir"; + socketAddresses = generateSocketAddresses "client" workers; + in if workers != [ ] then + lib.genAttrs socketAddresses (_: { }) + else config.services.nginx.upstreams.synapse_master.servers; + }; services.nginx.virtualHosts."${cfg.public_baseurl}" = { enableACME = true; From f42306e0a2df064f1beb6dfcc1776ea33e7ae9df Mon Sep 17 00:00:00 2001 From: Daniel Olsen Date: Mon, 19 Dec 2022 21:04:15 +0100 Subject: [PATCH 31/31] nginx: make upstream generator check empty attrset rather than empty list --- synapse-module/nginx.nix | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/synapse-module/nginx.nix b/synapse-module/nginx.nix index db3fe5a..e6ced1d 100644 --- a/synapse-module/nginx.nix +++ b/synapse-module/nginx.nix @@ -153,7 +153,7 @@ in servers = let fedReceivers = getWorkersOfType "fed-receiver"; socketAddresses = generateSocketAddresses "federation" fedReceivers; - in if fedReceivers != [ ] then + in if fedReceivers != { } then lib.genAttrs socketAddresses (_: { }) else config.services.nginx.upstreams.synapse_master.servers; extraConfig = '' @@ -166,7 +166,7 @@ in servers = let initialSyncers = getWorkersOfType "initial-sync"; socketAddresses = generateSocketAddresses "client" initialSyncers; - in if initialSyncers != [ ] then + in if initialSyncers != { } then lib.genAttrs socketAddresses (_: { }) else config.services.nginx.upstreams.synapse_master.servers; extraConfig = '' @@ -179,7 +179,7 @@ in servers = let normalSyncers = getWorkersOfType "normal-sync"; socketAddresses = generateSocketAddresses "client" normalSyncers; - in if normalSyncers != [ ] then + in if normalSyncers != { } then lib.genAttrs socketAddresses (_: { }) else config.services.nginx.upstreams.synapse_master.servers; extraConfig = '' @@ -192,7 +192,7 @@ in servers = let workers = getWorkersOfType "user-dir"; socketAddresses = generateSocketAddresses "client" workers; - in if workers != [ ] then + in if workers != { } then lib.genAttrs socketAddresses (_: { }) else config.services.nginx.upstreams.synapse_master.servers; };