{ config, lib, ... }: let cfg = config.services.matrix-synapse; dbGroup = "solo"; workerName = "user_dir"; workerRoutes = { client = [ "~ ^/_matrix/client/(api/v1|r0|v3|unstable)/user_directory/search$" ] ++ lib.optionals (cfg.authWorkers == 0) [ "~ ^/_matrix/client/v3/profile/.*$" "~ ^/_matrix/client/v3/profile/.*/(displayname|avatar_url)$" ]; federation = [ ]; media = [ ]; }; in { config = lib.mkIf cfg.enableUserDirWorker { services.matrix-synapse = { settings = { instance_map = { ${workerName} = { path = "/run/matrix-synapse/${workerName}.sock"; }; }; update_user_directory_from_worker = workerName; }; workers = { ${workerName} = { worker_app = "synapse.app.generic_worker"; worker_listeners = [ { type = "http"; path = "/run/matrix-synapse/${workerName}.sock"; resources = [ { names = [ "replication" ]; compress = false; } ]; } ] ++ lib.optional (lib.length workerRoutes.client > 0) { type = "http"; path = "/run/matrix-synapse/${workerName}-client.sock"; mode = "666"; resources = [ { names = [ "client" ]; compress = false; } ]; } ++ lib.optional (lib.length workerRoutes.federation > 0) { type = "http"; path = "/run/matrix-synapse/${workerName}-federation.sock"; mode = "666"; resources = [ { names = [ "federation" ]; compress = false; } ]; }; database = (import ../../db.nix { inherit workerName dbGroup; }); }; }; }; services.nginx = { virtualHosts."${cfg.nginxVirtualHostName}".locations = lib.listToAttrs ( lib.flatten ( lib.forEach [ "client" "federation" "media" ] ( type: lib.map (route: { name = route; value = { proxyPass = "http://unix:/run/matrix-synapse/${workerName}-${type}.sock"; }; }) workerRoutes.${type} ) ) ); }; }; }