1
2
mirror of https://github.com/dali99/nixos-matrix-modules.git synced 2026-01-20 06:28:21 +01:00

3 Commits

Author SHA1 Message Date
046194cdad v0.5.0
This is mostly a maintainance release to be compatible with nixos-23.11 but comes with some small improvements as well
2023-12-02 09:58:52 +01:00
3f92b5f197 use nixpkgs sliding sync package 2023-12-02 09:49:03 +01:00
a24a5e5da4 update to 23.11 2023-12-02 09:44:45 +01:00
11 changed files with 154 additions and 212 deletions

1
.gitignore vendored
View File

@@ -1 +0,0 @@
result

View File

@@ -2,9 +2,9 @@
This is a best effort document descibing neccecary changes you might have to do when updating This is a best effort document descibing neccecary changes you might have to do when updating
## 0.5.0 UNRELEASED ## 0.5.0
The module has been renamed from `synapse` to `default`
* The module has been renamed from `synapse` to `default`
* The synapse module now expects a wrapper-style package. This means the module is now incompatible with nixpkgs < 23.11.

16
flake.lock generated
View File

@@ -1,20 +1,5 @@
{ {
"nodes": { "nodes": {
"nixpkgs": {
"locked": {
"lastModified": 1690789960,
"narHash": "sha256-3K+2HuyGTiJUSZNJxXXvc0qj4xFx1FHC/ItYtEa7/Xs=",
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "fb942492b7accdee4e6d17f5447091c65897dde4",
"type": "github"
},
"original": {
"id": "nixpkgs",
"ref": "nixos-unstable",
"type": "indirect"
}
},
"nixpkgs-lib": { "nixpkgs-lib": {
"locked": { "locked": {
"lastModified": 1673743903, "lastModified": 1673743903,
@@ -32,7 +17,6 @@
}, },
"root": { "root": {
"inputs": { "inputs": {
"nixpkgs": "nixpkgs",
"nixpkgs-lib": "nixpkgs-lib" "nixpkgs-lib": "nixpkgs-lib"
} }
} }

View File

@@ -2,27 +2,13 @@
description = "NixOS modules for matrix related services"; description = "NixOS modules for matrix related services";
inputs = { inputs = {
nixpkgs-lib.url = "github:nix-community/nixpkgs.lib"; nixpkgs-lib.url = github:nix-community/nixpkgs.lib;
nixpkgs.url = "nixpkgs/nixos-unstable";
}; };
outputs = { self, nixpkgs, nixpkgs-lib }: { outputs = { self, nixpkgs-lib }: {
nixosModules = { nixosModules = {
default = import ./module.nix; default = import ./module.nix;
}; };
lib = import ./lib.nix { lib = nixpkgs-lib.lib; }; lib = import ./lib.nix { lib = nixpkgs-lib.lib; };
packages = let
system = "x86_64-linux";
pkgs = nixpkgs.legacyPackages.${system};
in {
${system}.tests = import ./tests {
inherit system;
inherit nixpkgs;
inherit pkgs;
nixosModule = self.outputs.nixosModules.synapse;
};
};
}; };
} }

View File

@@ -1,37 +0,0 @@
{ lib
, buildGoModule
, fetchFromGitHub
}:
buildGoModule rec {
pname = "matrix-sliding-sync";
version = "0.99.11";
src = fetchFromGitHub {
owner = "matrix-org";
repo = "sliding-sync";
rev = "refs/tags/v${version}";
hash = "sha256-Wd/nnJhKg+BDyOIz42zEScjzQRrpEq6YG9/9Tk24hgg=";
};
vendorHash = "sha256-0QSyYhOht1j1tWNxHQh+NUZA/W1xy7ANu+29H/gusOE=";
subPackages = [ "cmd/syncv3" ];
ldflags = [
"-s"
"-w"
"-X main.GitCommit=${src.rev}"
];
# requires a running matrix-synapse
doCheck = false;
meta = with lib; {
description = "A sliding sync implementation of MSC3575 for matrix";
homepage = "https://github.com/matrix-org/sliding-sync";
license = with licenses; [ asl20 ];
maintainers = with maintainers; [ emilylange ];
mainProgram = "syncv3";
};
}

View File

@@ -11,7 +11,7 @@ in
package = lib.mkOption { package = lib.mkOption {
type = lib.types.package; type = lib.types.package;
default = pkgs.callPackage ../pkgs/matrix-sliding-sync { }; default = pkgs.matrix-sliding-sync;
description = "What package to use for the sliding-sync proxy."; description = "What package to use for the sliding-sync proxy.";
}; };

View File

@@ -11,8 +11,22 @@ let
format = pkgs.formats.yaml {}; format = pkgs.formats.yaml {};
matrix-synapse-common-config = format.generate "matrix-synapse-common-config.yaml" cfg.settings; matrix-synapse-common-config = format.generate "matrix-synapse-common-config.yaml" cfg.settings;
pluginsEnv = cfg.package.python.buildEnv.override {
extraLibs = cfg.plugins; # TODO: Align better with the upstream module
wrapped = cfg.package.override {
inherit (cfg) plugins;
extras = [
"postgres"
"saml2"
"oidc"
"systemd"
"url-preview"
"sentry"
"jwt"
"redis"
"cache-memory"
"user-search"
];
}; };
inherit (lib) inherit (lib)
@@ -33,7 +47,7 @@ in
imports = [ imports = [
./nginx.nix ./nginx.nix
(import ./workers.nix { (import ./workers.nix {
inherit matrix-lib throw' format matrix-synapse-common-config pluginsEnv; inherit matrix-lib throw' format matrix-synapse-common-config wrapped;
}) })
]; ];
@@ -376,9 +390,6 @@ in
}; };
in "${cfg.package}/bin/synapse_homeserver ${flags}"; in "${cfg.package}/bin/synapse_homeserver ${flags}";
environment.PYTHONPATH =
lib.makeSearchPathOutput "lib" cfg.package.python.sitePackages [ pluginsEnv ];
serviceConfig = { serviceConfig = {
Type = "notify"; Type = "notify";
User = "matrix-synapse"; User = "matrix-synapse";
@@ -390,7 +401,7 @@ in
config-path = [ matrix-synapse-common-config ] ++ cfg.extraConfigFiles; config-path = [ matrix-synapse-common-config ] ++ cfg.extraConfigFiles;
keys-directory = cfg.dataDir; keys-directory = cfg.dataDir;
}; };
in "${cfg.package}/bin/synapse_homeserver ${flags}"; in "${wrapped}/bin/synapse_homeserver ${flags}";
ExecReload = "${pkgs.utillinux}/bin/kill -HUP $MAINPID"; ExecReload = "${pkgs.utillinux}/bin/kill -HUP $MAINPID";
Restart = "on-failure"; Restart = "on-failure";
}; };

View File

@@ -1,6 +1,6 @@
{ matrix-synapse-common-config, { matrix-synapse-common-config,
matrix-lib, matrix-lib,
pluginsEnv, wrapped,
throw', throw',
format format
}: }:
@@ -19,7 +19,6 @@
type = types.ints.unsigned; type = types.ints.unsigned;
description = "How many automatically configured ${workerType} workers to set up"; description = "How many automatically configured ${workerType} workers to set up";
default = 0; default = 0;
example = 1;
}; };
genAttrs' = items: f: g: builtins.listToAttrs (map (i: lib.nameValuePair (f i) (g i)) items); genAttrs' = items: f: g: builtins.listToAttrs (map (i: lib.nameValuePair (f i) (g i)) items);
@@ -34,25 +33,11 @@ in {
type = types.bool; type = types.bool;
internal = true; internal = true;
default = false; default = false;
description = ''
This is an internal flag that signals that this worker is part of the
workers generated by either of the following:
- federationSenders
- federationReceivers
- initialSyncers
- normalSyncers
- eventPersisters
- useUserDirectoryWorker
'';
}; };
index = mkOption { index = mkOption {
internal = true; internal = true;
type = types.ints.positive; type = types.ints.positive;
description = ''
This is an internal variable that indexes the worker of this type.
'';
}; };
# The custom string type here is mainly for the name to use # The custom string type here is mainly for the name to use
@@ -100,25 +85,17 @@ in {
default = "http"; default = "http";
}; };
path = mkOption { port = mkOption {
type = types.path; type = types.port;
default = instanceCfg.name; description = "The TCP port to bind to";
description = ''
A path and filename for a Unix socket.
'';
}; };
# port = mkOption { bind_addresses = mkOption {
# type = types.port; type = with types; listOf str;
# description = "The TCP port to bind to"; description = "A list of local addresses to listen on";
# }; default = [ wcfg.defaultListenerAddress ];
defaultText = literalExpression "[ ${wcfgText}.defaultListenerAddress ]";
# bind_addresses = mkOption { };
# type = with types; listOf str;
# description = "A list of local addresses to listen on";
# default = [ wcfg.defaultListenerAddress ];
# defaultText = literalExpression "[ ${wcfgText}.defaultListenerAddress ]";
# };
tls = mkOption { tls = mkOption {
type = types.bool; type = types.bool;
@@ -183,6 +160,38 @@ in {
}; };
}; };
in { in {
mainReplicationHost = mkOption {
type = types.str;
default = let
host = (matrix-lib.connectionInfo mainReplicationListener).host;
in
# To avoid connecting to 0.0.0.0 and so on
if builtins.elem host [ "0.0.0.0" "::" ]
then "127.0.0.1"
else host;
# TODO: add defaultText
description = "Host of the main synapse instance's replication listener";
};
mainReplicationPort = mkOption {
type = types.port;
default = mainReplicationListener.port;
# TODO: add defaultText
description = "Port for the main synapse instance's replication listener";
};
defaultListenerAddress = mkOption {
type = types.str;
default = "127.0.0.1";
description = "The default listener address for the worker";
};
workerStartingPort = mkOption {
type = types.port;
description = "What port should the automatically configured workers start enumerating from";
default = 8083;
};
enableMetrics = mkOption { enableMetrics = mkOption {
type = types.bool; type = types.bool;
default = cfg.settings.enable_metrics; default = cfg.settings.enable_metrics;
@@ -190,6 +199,12 @@ in {
# TODO: add description # TODO: add description
}; };
metricsStartingPort = mkOption {
type = types.port;
default = 18083;
# TODO: add description
};
federationSenders = mkWorkerCountOption "federation-sender"; federationSenders = mkWorkerCountOption "federation-sender";
federationReceivers = mkWorkerCountOption "federation-reciever"; federationReceivers = mkWorkerCountOption "federation-reciever";
initialSyncers = mkWorkerCountOption "initial-syncer"; initialSyncers = mkWorkerCountOption "initial-syncer";
@@ -203,14 +218,13 @@ in {
default = { }; default = { };
description = "Worker configuration"; description = "Worker configuration";
example = { example = {
"federation-sender-1" = { "federation_sender1" = {
settings = { settings = {
worker_name = "federation-sender-1"; worker_name = "federation_sender1";
worker_app = "synapse.app.generic_worker"; worker_app = "synapse.app.generic_worker";
path = "/run/matrix-synapse/federation-sender-1.sock"; worker_replication_host = "127.0.0.1";
# worker_replication_host = "127.0.0.1"; worker_replication_http_port = 9093;
# worker_replication_http_port = 9093;
worker_listeners = [ ]; worker_listeners = [ ];
}; };
}; };
@@ -218,59 +232,94 @@ in {
}; };
}; };
config = let config = {
genList1 = f: builtins.genList (i: f (i + 1));
in {
services.matrix-synapse-next.settings = { services.matrix-synapse-next.settings = {
federation_sender_instances = federation_sender_instances =
genList1 (i: "auto-fed-sender-${toString i}") wcfg.federationSenders; lib.genList (i: "auto-fed-sender${toString (i + 1)}") wcfg.federationSenders;
instance_map = lib.mkIf (cfg.workers.instances != { }) ({ instance_map = (lib.mkIf (cfg.workers.instances != { }) ({
main.path = "/run/matrix-synapse/main-replication-worker.sock"; main = let
} // builtins.mapAttrs (n: v: { host = lib.head mainReplicationListener.bind_addresses;
inherit (builtins.head v.settings.worker_listeners) path; in {
}) wcfg.instances); host = if builtins.elem host [ "0.0.0.0" "::"] then "127.0.0.1" else host;
port = mainReplicationListener.port;
};
} // genAttrs' (lib.lists.range 1 wcfg.eventPersisters)
(i: "auto-event-persist${toString i}")
(i: let
wRL = matrix-lib.firstListenerOfType "replication" wcfg.instances."auto-event-persist${toString i}".settings.worker_listeners;
in matrix-lib.connectionInfo wRL)));
stream_writers.events = stream_writers.events =
mkIf (wcfg.eventPersisters > 0) mkIf (wcfg.eventPersisters > 0)
(genList1 (i: "auto-event-persist-${toString i}") wcfg.eventPersisters); (lib.genList (i: "auto-event-persist${toString (i + 1)}") wcfg.eventPersisters);
update_user_directory_from_worker = update_user_directory_from_worker =
mkIf wcfg.useUserDirectoryWorker "auto-user-dir-1"; mkIf wcfg.useUserDirectoryWorker "auto-user-dir";
}; };
services.matrix-synapse-next.workers.instances = services.matrix-synapse-next.workers.instances = let
let sum = lib.foldl lib.add 0;
workerInstances = { workerListenersWithMetrics = portOffset:
"fed-sender" = wcfg.federationSenders; lib.singleton ({
"fed-receiver" = wcfg.federationReceivers; port = wcfg.workerStartingPort + portOffset - 1;
"initial-sync" = wcfg.initialSyncers; })
"normal-sync" = wcfg.normalSyncers; ++ lib.optional wcfg.enableMetrics {
"event-persist" = wcfg.eventPersisters; port = wcfg.metricsStartingPort + portOffset;
"user-dir" = if wcfg.useUserDirectoryWorker then 1 else 0; resources = [ { names = [ "metrics" ]; } ];
}; };
in
lib.pipe workerInstances [ makeWorkerInstances = {
(lib.mapAttrsToList (type: count: { inherit type count; })) type,
(map ({ type, count }: genList1 (i: rec { numberOfWorkers,
name = "auto-${type}-${toString i}"; portOffset ? 0,
value = { nameFn ? i: "auto-${type}${toString i}",
inherit type; workerListenerFn ? i: workerListenersWithMetrics (portOffset + i)
isAuto = true; }: genAttrs'
index = i; (lib.lists.range 1 numberOfWorkers)
settings.worker_listeners = nameFn
[ (i: {
{ path = "/run/matrix-synapse/${name}.sock"; } isAuto = true;
] ++ lib.optionals wcfg.enableMetrics [{ inherit type;
path = "/run/matrix-synapse/${name}-metrics.sock"; index = i;
resources = [{ names = [ "metrics" ]; }]; settings.worker_listeners = workerListenerFn i;
type = "metrics"; });
}];
}; workerInstances = {
}) count)) "fed-sender" = wcfg.federationSenders;
lib.flatten "fed-receiver" = wcfg.federationReceivers;
builtins.listToAttrs "initial-sync" = wcfg.initialSyncers;
]; "normal-sync" = wcfg.normalSyncers;
"event-persist" = wcfg.eventPersisters;
} // (lib.optionalAttrs wcfg.useUserDirectoryWorker {
"user-dir" = {
numberOfWorkers = 1;
nameFn = _: "auto-user-dir";
};
});
coerceWorker = { name, value }: if builtins.isInt value then {
type = name;
numberOfWorkers = value;
} else { type = name; } // value;
# Like foldl, but keeps all intermediate values
#
# (b -> a -> b) -> b -> [a] -> [b]
scanl = f: x1: list: let
x2 = lib.head list;
x1' = f x1 x2;
in if list == [] then [] else [x1'] ++ (scanl f x1' (lib.tail list));
f = { portOffset, numberOfWorkers, ... }: x: x // { portOffset = portOffset + numberOfWorkers; };
init = { portOffset = 0; numberOfWorkers = 0; };
in lib.pipe workerInstances [
(lib.mapAttrsToList lib.nameValuePair)
(map coerceWorker)
(scanl f init)
(map makeWorkerInstances)
mkMerge
];
systemd.services = let systemd.services = let
workerList = lib.mapAttrsToList lib.nameValuePair wcfg.instances; workerList = lib.mapAttrsToList lib.nameValuePair wcfg.instances;
@@ -279,26 +328,17 @@ in {
in builtins.listToAttrs (lib.flip map workerList (worker: { in builtins.listToAttrs (lib.flip map workerList (worker: {
name = "matrix-synapse-worker-${worker.name}"; name = "matrix-synapse-worker-${worker.name}";
value = { value = {
documentation = [ "https://github.com/matrix-org/synapse/blob/develop/docs/workers.md" ];
description = "Synapse Matrix Worker"; description = "Synapse Matrix Worker";
partOf = [ "matrix-synapse.target" ]; partOf = [ "matrix-synapse.target" ];
wantedBy = [ "matrix-synapse.target" ]; wantedBy = [ "matrix-synapse.target" ];
after = [ "matrix-synapse.service" ]; after = [ "matrix-synapse.service" ];
requires = [ "matrix-synapse.service" ]; requires = [ "matrix-synapse.service" ];
restartTriggers = [ matrix-synapse-common-config (workerConfig worker) ] ++ cfg.extraConfigFiles;
environment = {
PYTHONPATH = lib.makeSearchPathOutput "lib" cfg.package.python.sitePackages [
pluginsEnv
];
};
serviceConfig = { serviceConfig = {
Restart = "always";
Type = "notify"; Type = "notify";
User = "matrix-synapse"; User = "matrix-synapse";
Group = "matrix-synapse"; Group = "matrix-synapse";
Slice = "system-matrix-synapse.slice"; Slice = "system-matrix-synapse.slice";
WorkingDirectory = cfg.dataDir; WorkingDirectory = cfg.dataDir;
RuntimeDirectory = [ "matrix-synapse" ];
ExecStartPre = pkgs.writers.writeBash "wait-for-synapse" '' ExecStartPre = pkgs.writers.writeBash "wait-for-synapse" ''
# From https://md.darmstadt.ccc.de/synapse-at-work # From https://md.darmstadt.ccc.de/synapse-at-work
while ! systemctl is-active -q matrix-synapse.service; do while ! systemctl is-active -q matrix-synapse.service; do
@@ -310,7 +350,7 @@ in {
config-path = [ matrix-synapse-common-config (workerConfig worker) ] ++ cfg.extraConfigFiles; config-path = [ matrix-synapse-common-config (workerConfig worker) ] ++ cfg.extraConfigFiles;
keys-directory = cfg.dataDir; keys-directory = cfg.dataDir;
}; };
in "${cfg.package}/bin/synapse_worker ${flags}"; in "${wrapped}/bin/synapse_worker ${flags}";
}; };
}; };
})); }));

View File

@@ -1,18 +0,0 @@
{ pkgs, lib, ... }:
{
services.matrix-synapse-next = {
enable = true;
settings.server_name = "matrix.example.com";
workers = {
enableMetrics = true;
federationSenders = 2;
federationReceivers = 2;
initialSyncers = 2;
normalSyncers = 2;
eventPersisters = 2;
useUserDirectoryWorker = true;
};
};
}

View File

@@ -1,7 +0,0 @@
{ pkgs, lib, ... }:
{
services.matrix-synapse-next = {
enable = true;
settings.server_name = "matrix.example.com";
};
}

View File

@@ -1,16 +0,0 @@
{ nixpkgs, pkgs, system ? pkgs.system, nixosModule, ... }: let
buildSystemWithConfig = configPath: (nixpkgs.lib.nixosSystem {
inherit system;
modules = [
nixosModule
configPath
{
boot.isContainer = true;
}
];
}).config.system.build.toplevel;
in {
a = pkgs.writeText "hello-world" ''a'';
base-config = buildSystemWithConfig ./base-config.nix;
auto-workers-config = buildSystemWithConfig ./auto-workers-config.nix;
}