nixos-matrix-modules/synapse-module/default.nix

460 lines
15 KiB
Nix
Raw Permalink Normal View History

2023-01-18 01:23:58 +01:00
{ pkgs, lib, config, ... }:
let
matrix-lib = (import ../lib.nix { inherit lib; });
2020-10-18 23:08:50 +02:00
cfg = config.services.matrix-synapse-next;
wcfg = cfg.workers;
2023-01-18 01:23:58 +01:00
# Used to generate proper defaultTexts.
cfgText = "config.services.matrix-synapse-next";
wcfgText = "config.services.matrix-synapse-next.workers";
2020-10-18 23:08:50 +02:00
format = pkgs.formats.yaml {};
2024-01-13 18:18:21 +01:00
matrix-synapse-common-config = format.generate "matrix-synapse-common-config.yaml" (cfg.settings // {
listeners = map (lib.filterAttrsRecursive (_: v: v != null)) cfg.settings.listeners;
});
2023-12-01 06:07:53 +01:00
# 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"
];
2020-10-18 23:08:50 +02:00
};
2023-01-18 01:23:58 +01:00
inherit (lib)
literalExpression
mkEnableOption
mkIf
mkMerge
mkOption
mkPackageOption
types;
throw' = str: throw ''
matrix-synapse-next error:
${str}
'';
2020-10-18 23:08:50 +02:00
in
{
2023-01-18 01:23:58 +01:00
imports = [
./nginx.nix
(import ./workers.nix {
2023-12-01 06:07:53 +01:00
inherit matrix-lib throw' format matrix-synapse-common-config wrapped;
2023-01-18 01:23:58 +01:00
})
];
2022-12-09 09:33:31 +01:00
2020-10-18 23:08:50 +02:00
options.services.matrix-synapse-next = {
2023-01-18 01:23:58 +01:00
enable = mkEnableOption "matrix-synapse";
2020-10-18 23:08:50 +02:00
2023-01-18 01:23:58 +01:00
package = mkPackageOption pkgs "matrix-synapse" {};
plugins = mkOption {
type = types.listOf types.package;
2020-10-18 23:08:50 +02:00
default = [ ];
2023-01-18 01:23:58 +01:00
example = literalExpression ''
with ${cfgText}.package.plugins; [
2020-10-18 23:08:50 +02:00
matrix-synapse-ldap3
matrix-synapse-pam
];
'';
description = ''
List of additional Matrix plugins to make available.
'';
};
2023-01-18 01:23:58 +01:00
dataDir = mkOption {
type = types.path;
2020-10-18 23:08:50 +02:00
default = "/var/lib/matrix-synapse";
description = ''
The directory where matrix-synapse stores its stateful data such as
certificates, media and uploads.
'';
};
2024-01-13 18:18:21 +01:00
socketDir = mkOption {
type = types.path;
default = "/run/matrix-synapse";
description = ''
The directory where matrix-synapse by default stores the sockets of
all listeners that bind to UNIX sockets.
'';
};
2023-01-18 01:23:58 +01:00
enableNginx = mkEnableOption "The synapse module managing nginx";
2023-01-18 01:23:58 +01:00
public_baseurl = mkOption {
type = types.str;
default = "matrix.${cfg.settings.server_name}";
2023-01-18 01:23:58 +01:00
defaultText =
literalExpression ''matrix.''${${cfgText}.settings.server_name}'';
description = ''
The domain where clients and such will connect.
This may be different from server_name if using delegation.
'';
};
2023-01-18 01:23:58 +01:00
mainLogConfig = mkOption {
type = with types; coercedTo path lib.readFile lines;
default = ./matrix-synapse-log_config.yaml;
2020-10-18 23:08:50 +02:00
description = "A yaml python logging config file";
};
enableSlidingSync = mkEnableOption (lib.mdDoc "automatic Sliding Sync setup at `slidingsync.<domain>`");
2023-01-18 01:23:58 +01:00
settings = mkOption {
type = types.submodule {
freeformType = format.type;
options = {
server_name = mkOption {
type = types.str;
description = ''
The server_name name will appear at the end of usernames and room addresses
created on this server. For example if the server_name was example.com,
usernames on this server would be in the format @user:example.com
In most cases you should avoid using a matrix specific subdomain such as
matrix.example.com or synapse.example.com as the server_name for the same
reasons you wouldn't use user@email.example.com as your email address.
See https://github.com/matrix-org/synapse/blob/master/docs/delegate.md
for information on how to host Synapse on a subdomain while preserving
a clean server_name.
The server_name cannot be changed later so it is important to
configure this correctly before you start Synapse. It should be all
lowercase and may contain an explicit port.
'';
example = "matrix.org";
};
2023-01-18 01:23:58 +01:00
use_presence = mkOption {
type = types.bool;
description = "Disable presence tracking, if you're having perfomance issues this can have a big impact";
default = true;
};
2023-01-18 01:23:58 +01:00
listeners = mkOption {
type = types.listOf (types.submodule {
options = {
port = mkOption {
2024-01-13 18:18:21 +01:00
type = with types; nullOr types.port;
default = null;
description = ''
The TCP port to bind to.
::: {.note}
This option will be ignored if {option}`path` is set to a non-null value.
:::
'';
2023-01-18 01:23:58 +01:00
example = 8448;
};
2024-01-13 18:18:21 +01:00
path = mkOption {
type = with types; nullOr path;
default = null;
description = ''
The UNIX socket to bind to.
::: {.note}
This option will override {option}`bind_addresses` and {option}`port`
if set to a non-null value.
:::
'';
example = literalExpression ''''${${cfgText}.socketDir}/matrix-synapse.sock'';
};
2023-01-18 01:23:58 +01:00
bind_addresses = mkOption {
type = types.listOf types.str;
2024-01-13 18:18:21 +01:00
default = [ ];
description = ''
A list of local addresses to listen on.
::: {.note}
This option will be ignored if {option}`path` is set to a non-null value.
:::
'';
2023-01-18 01:23:58 +01:00
};
2023-01-18 01:23:58 +01:00
type = mkOption {
type = types.enum [ "http" "manhole" "metrics" "replication" ];
description = "The type of the listener";
default = "http";
};
2023-01-18 01:23:58 +01:00
tls = mkOption {
type = types.bool;
description = ''
Set to true to enable TLS for this listener.
2022-12-09 09:33:31 +01:00
2023-01-18 01:23:58 +01:00
Will use the TLS key/cert specified in tls_private_key_path / tls_certificate_path.
'';
default = false;
};
2022-12-09 09:33:31 +01:00
2023-01-18 01:23:58 +01:00
x_forwarded = mkOption {
type = types.bool;
description = ''
Set to true to use the X-Forwarded-For header as the client IP.
2022-12-14 08:21:53 +01:00
2023-01-18 01:23:58 +01:00
Only valid for an 'http' listener.
Useful when Synapse is behind a reverse-proxy.
'';
default = true;
};
2022-12-14 10:10:11 +01:00
2023-01-18 01:23:58 +01:00
resources = mkOption {
type = types.listOf (types.submodule {
options = {
names = mkOption {
type = with types; listOf (enum [
"client"
"consent"
"federation"
"keys"
"media"
"metrics"
"openid"
"replication"
"static"
"webclient"
]);
description = "A list of resources to host on this port";
};
2023-01-18 01:23:58 +01:00
compress = mkEnableOption "HTTP compression for this resource";
};
});
};
};
});
description = "List of ports that Synapse should listen on, their purpose and their configuration";
# TODO: add defaultText
default = [
{
2024-01-13 18:18:21 +01:00
path = "${cfg.socketDir}/matrix-synapse.sock";
2023-01-18 01:23:58 +01:00
resources = [
{ names = [ "client" ]; compress = true; }
{ names = [ "federation" ]; compress = false; }
];
}
(mkIf (wcfg.instances != { }) {
2024-01-13 18:18:21 +01:00
path = "${cfg.socketDir}/matrix-synapse-replication.sock";
2023-01-18 01:23:58 +01:00
resources = [
{ names = [ "replication" ]; }
];
})
(mkIf cfg.settings.enable_metrics {
port = 9000;
bind_addresses = [ "127.0.0.1" ];
resources = [
{ names = [ "metrics" ]; }
];
})
];
};
2023-01-18 01:23:58 +01:00
federation_ip_range_blacklist = mkOption {
type = types.listOf types.str;
description = ''
Prevent federation requests from being sent to the following
blacklist IP address CIDR ranges. If this option is not specified, or
specified with an empty list, no ip range blacklist will be enforced.
'';
default = [
"127.0.0.0/8"
"10.0.0.0/8"
"172.16.0.0/12"
"192.168.0.0/16"
"100.64.0.0/10"
"169.254.0.0/16"
"::1/128"
"fe80::/64"
"fc00::/7"
];
};
2023-01-18 01:23:58 +01:00
log_config = mkOption {
type = types.path;
description = ''
A yaml python logging config file as described by
https://docs.python.org/3.7/library/logging.config.html#configuration-dictionary-schema
'';
default = pkgs.writeText "log_config.yaml" cfg.mainLogConfig;
defaultText = "A config file generated from ${cfgText}.mainLogConfig";
};
2022-10-10 12:59:11 +02:00
2023-01-18 01:23:58 +01:00
media_store_path = mkOption {
type = types.path;
description = "Directory where uploaded images and attachments are stored";
default = "${cfg.dataDir}/media_store";
defaultText = literalExpression ''''${${cfgText}.dataDir}/media_store'';
2020-10-18 23:08:50 +02:00
};
2023-01-18 01:23:58 +01:00
max_upload_size = mkOption {
type = types.str;
description = "The largest allowed upload size in bytes";
default = "50M";
example = "800K";
};
2023-01-18 01:23:58 +01:00
enable_registration = mkEnableOption "registration for new users";
enable_metrics = mkEnableOption "collection and rendering of performance metrics";
report_stats = mkEnableOption "reporting usage stats";
2020-10-18 23:08:50 +02:00
2023-01-18 01:23:58 +01:00
app_service_config_files = mkOption {
type = types.listOf types.path;
description = "A list of application service config files to use";
default = [];
};
2020-10-18 23:08:50 +02:00
2023-01-18 01:23:58 +01:00
signing_key_path = mkOption {
type = types.path;
description = "Path to the signing key to sign messages with";
default = "${cfg.dataDir}/homeserver.signing.key";
defaultText = literalExpression ''''${${cfgText}.dataDir}/homeserver.signing.key'';
};
2020-10-18 23:08:50 +02:00
2023-01-18 01:23:58 +01:00
trusted_key_servers = mkOption {
type = types.listOf (types.submodule {
freeformType = format.type;
2020-10-18 23:08:50 +02:00
2023-01-18 01:23:58 +01:00
options.server_name = mkOption {
type = types.str;
description = "The name of the server. This is required.";
};
});
description = "The trusted servers to download signing keys from";
default = [
{
server_name = "matrix.org";
verify_keys."ed25519:auto" = "Noi6WqcDj0QmPxCNQqgezwTlBKrfqehY1u2FyWP9uYw";
}
];
};
2020-10-18 23:08:50 +02:00
2023-01-18 01:23:58 +01:00
federation_sender_instances = mkOption {
type = types.listOf types.str;
description = ''
This configuration must be shared between all federation sender workers.
2020-10-18 23:08:50 +02:00
2023-01-18 01:23:58 +01:00
When changed, all federation sender workers must be stopped at the same time and
restarted, to ensure that all instances are running with the same config.
Otherwise, events may be dropped.
'';
default = [ ];
};
2020-10-18 23:08:50 +02:00
2023-01-18 01:23:58 +01:00
redis = mkOption {
type = types.submodule {
freeformType = format.type;
2022-10-07 16:16:44 +02:00
2023-01-18 01:23:58 +01:00
options.enabled = mkOption {
type = types.bool;
description = ''
Whether to enable redis within synapse.
2022-10-07 16:16:44 +02:00
2023-01-18 01:23:58 +01:00
This is required for worker support.
'';
default = wcfg.instances != { };
defaultText = literalExpression "${wcfgText}.instances != { }";
};
2022-10-07 16:16:44 +02:00
};
2023-01-18 01:23:58 +01:00
default = { };
description = "Redis configuration for synapse and workers";
2022-10-07 16:16:44 +02:00
};
};
2020-10-18 23:08:50 +02:00
};
};
2023-01-18 01:23:58 +01:00
extraConfigFiles = mkOption {
type = types.listOf types.path;
2020-10-18 23:08:50 +02:00
default = [];
description = ''
Extra config files to include.
The configuration files will be included based on the command line
argument --config-path. This allows to configure secrets without
having to go through the Nix store, e.g. based on deployment keys if
NixOPS is in use.
'';
};
};
2023-01-18 01:23:58 +01:00
config = mkIf cfg.enable {
2024-01-13 18:18:21 +01:00
assertions = [ ]
++ (map (l: {
assertion = l.path == null -> (l.bind_addresses != [ ] && l.port != null);
message = "Some listeners are missing either a socket path or a bind_address + port to listen on";
}) cfg.settings.listeners);
2024-09-27 05:54:58 +02:00
warnings = [ ] ++ lib.optional cfg.enableSlidingSync
"the option services.matrix-synapse-next.enableSlidingSync no longer has any effect (and is enabled by default)";
2023-01-18 01:23:58 +01:00
users.users.matrix-synapse = {
group = "matrix-synapse";
home = cfg.dataDir;
createHome = true;
shell = "${pkgs.bash}/bin/bash";
uid = config.ids.uids.matrix-synapse;
};
users.groups.matrix-synapse = {
gid = config.ids.gids.matrix-synapse;
};
systemd = {
targets.matrix-synapse = {
description = "Matrix synapse parent target";
after = [ "network.target" ];
wantedBy = [ "multi-user.target" ];
};
2020-10-18 23:08:50 +02:00
2023-01-18 01:23:58 +01:00
slices.system-matrix-synapse = {
description = "Matrix synapse slice";
requires= [ "system.slice" ];
after= [ "system.slice" ];
};
services.matrix-synapse = {
2020-10-18 23:08:50 +02:00
description = "Synapse Matrix homeserver";
partOf = [ "matrix-synapse.target" ];
wantedBy = [ "matrix-synapse.target" ];
2023-01-18 01:23:58 +01:00
preStart = let
flags = lib.cli.toGNUCommandLineShell {} {
config-path = [ matrix-synapse-common-config ] ++ cfg.extraConfigFiles;
keys-directory = cfg.dataDir;
generate-keys = true;
};
in "${cfg.package}/bin/synapse_homeserver ${flags}";
2020-10-18 23:08:50 +02:00
serviceConfig = {
Type = "notify";
User = "matrix-synapse";
Group = "matrix-synapse";
2023-01-18 01:23:58 +01:00
Slice = "system-matrix-synapse.slice";
2020-10-18 23:08:50 +02:00
WorkingDirectory = cfg.dataDir;
2024-01-13 18:18:21 +01:00
StateDirectory = "matrix-synapse";
RuntimeDirectory = "matrix-synapse";
2023-01-18 01:23:58 +01:00
ExecStart = let
flags = lib.cli.toGNUCommandLineShell {} {
config-path = [ matrix-synapse-common-config ] ++ cfg.extraConfigFiles;
keys-directory = cfg.dataDir;
};
2023-12-01 06:07:53 +01:00
in "${wrapped}/bin/synapse_homeserver ${flags}";
2020-10-18 23:08:50 +02:00
ExecReload = "${pkgs.utillinux}/bin/kill -HUP $MAINPID";
Restart = "on-failure";
};
};
2023-01-18 01:23:58 +01:00
};
};
}