2023-09-17 20:14:32 +02:00
|
|
|
{ config, pkgs, ... }:
|
2022-12-17 19:11:37 +01:00
|
|
|
let
|
2023-10-08 13:42:29 +02:00
|
|
|
domainServer = "matrix.${config.networking.domain}";
|
|
|
|
domainClient = "chat.${config.networking.domain}";
|
2022-12-17 21:23:46 +01:00
|
|
|
|
2022-12-17 19:11:37 +01:00
|
|
|
clientConfig = {
|
|
|
|
"m.homeserver" = {
|
|
|
|
base_url = "https://${domainServer}:443";
|
|
|
|
server_name = domainServer;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
serverConfig = {
|
|
|
|
"m.server" = "${domainServer}:443";
|
|
|
|
};
|
2022-12-17 21:23:46 +01:00
|
|
|
|
2022-12-17 19:11:37 +01:00
|
|
|
mkWellKnown = data: ''
|
|
|
|
add_header Content-Type application/json;
|
|
|
|
add_header Access-Control-Allow-Origin *;
|
|
|
|
return 200 '${builtins.toJSON data}';
|
|
|
|
'';
|
2023-01-21 21:26:24 +01:00
|
|
|
|
2023-11-17 20:38:36 +01:00
|
|
|
matrix-synapse-ldap3 = config.services.matrix-synapse.package.plugins.matrix-synapse-ldap3;
|
2022-12-17 19:11:37 +01:00
|
|
|
in
|
|
|
|
{
|
2024-03-12 12:34:20 +01:00
|
|
|
imports = [ ./mautrix-telegram.nix ];
|
2023-01-18 14:12:03 +01:00
|
|
|
sops.secrets.matrix_ldap_search = {
|
2023-07-07 17:12:59 +02:00
|
|
|
key = "portunus/search-password";
|
2023-01-18 14:12:03 +01:00
|
|
|
owner = config.systemd.services.matrix-synapse.serviceConfig.User;
|
|
|
|
};
|
2022-12-17 19:11:37 +01:00
|
|
|
|
|
|
|
services = {
|
|
|
|
postgresql = {
|
|
|
|
enable = true;
|
2023-01-18 14:12:03 +01:00
|
|
|
ensureUsers = [{
|
|
|
|
name = "matrix-synapse";
|
|
|
|
}];
|
2022-12-17 19:11:37 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
nginx = {
|
|
|
|
recommendedProxySettings = true;
|
|
|
|
virtualHosts = {
|
|
|
|
# synapse
|
|
|
|
"${domainServer}" = {
|
|
|
|
# homeserver discovery
|
|
|
|
locations."= /.well-known/matrix/client".extraConfig = mkWellKnown clientConfig;
|
|
|
|
locations."= /.well-known/matrix/server".extraConfig = mkWellKnown serverConfig;
|
|
|
|
|
|
|
|
# 404 on /
|
|
|
|
locations."/".extraConfig = "return 404;";
|
|
|
|
|
|
|
|
# proxy to synapse
|
|
|
|
locations."/_matrix".proxyPass = "http://[::1]:8008";
|
|
|
|
locations."/_synapse/client".proxyPass = "http://[::1]:8008";
|
|
|
|
};
|
|
|
|
|
|
|
|
# element
|
|
|
|
"${domainClient}" = {
|
|
|
|
root = pkgs.element-web.override {
|
|
|
|
conf = {
|
2023-11-10 15:42:58 +01:00
|
|
|
default_server_config = {
|
|
|
|
inherit (clientConfig) "m.homeserver";
|
|
|
|
"m.identity_server".base_url = "";
|
|
|
|
};
|
2023-01-18 14:12:03 +01:00
|
|
|
disable_3pid_login = true;
|
2022-12-17 19:11:37 +01:00
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
matrix-synapse = {
|
|
|
|
enable = true;
|
|
|
|
|
2023-01-21 21:26:24 +01:00
|
|
|
plugins = [ matrix-synapse-ldap3 ];
|
2023-01-18 14:12:03 +01:00
|
|
|
|
2024-01-06 16:01:32 +01:00
|
|
|
|
|
|
|
log = {
|
|
|
|
root.level = "WARNING";
|
|
|
|
};
|
2022-12-17 19:11:37 +01:00
|
|
|
settings = {
|
|
|
|
server_name = domainServer;
|
|
|
|
|
|
|
|
listeners = [{
|
|
|
|
port = 8008;
|
|
|
|
bind_addresses = [ "::1" ];
|
|
|
|
type = "http";
|
|
|
|
tls = false;
|
|
|
|
x_forwarded = true;
|
|
|
|
resources = [{
|
|
|
|
names = [ "client" "federation" ];
|
|
|
|
compress = false;
|
|
|
|
}];
|
|
|
|
}];
|
|
|
|
};
|
2023-01-18 14:12:03 +01:00
|
|
|
|
|
|
|
extraConfigFiles = [
|
|
|
|
(pkgs.writeTextFile {
|
|
|
|
name = "matrix-synapse-extra-config.yml";
|
2023-09-15 13:04:24 +02:00
|
|
|
text = let portunus = config.services.portunus; in
|
|
|
|
''
|
|
|
|
modules:
|
|
|
|
- module: ldap_auth_provider.LdapAuthProviderModule
|
|
|
|
config:
|
|
|
|
enabled: true
|
|
|
|
uri: ldap://localhost
|
|
|
|
base: ou=users,${portunus.ldap.suffix}
|
|
|
|
# taken from kaki config
|
|
|
|
attributes:
|
|
|
|
uid: uid
|
|
|
|
mail: uid
|
|
|
|
name: cn
|
|
|
|
bind_dn: uid=search,ou=users,${portunus.ldap.suffix}
|
|
|
|
bind_password_file: ${config.sops.secrets.matrix_ldap_search.path}
|
|
|
|
'';
|
2023-01-18 14:12:03 +01:00
|
|
|
})
|
|
|
|
];
|
2022-12-17 19:11:37 +01:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
systemd.services.matrix-synapse.after = [ "matrix-synapse-pgsetup.service" ];
|
|
|
|
|
|
|
|
systemd.services.matrix-synapse-pgsetup = {
|
|
|
|
description = "Prepare Synapse postgres database";
|
|
|
|
wantedBy = [ "multi-user.target" ];
|
|
|
|
after = [ "networking.target" "postgresql.service" ];
|
|
|
|
serviceConfig.Type = "oneshot";
|
|
|
|
|
|
|
|
path = [ pkgs.sudo config.services.postgresql.package ];
|
|
|
|
|
2023-01-18 14:12:03 +01:00
|
|
|
# create database for synapse. will silently fail if it already exists
|
2022-12-17 19:11:37 +01:00
|
|
|
script = ''
|
|
|
|
sudo -u ${config.services.postgresql.superUser} psql <<SQL
|
|
|
|
CREATE DATABASE "matrix-synapse" WITH OWNER "matrix-synapse"
|
|
|
|
ENCODING 'UTF8'
|
|
|
|
TEMPLATE template0
|
|
|
|
LC_COLLATE = "C"
|
|
|
|
LC_CTYPE = "C";
|
|
|
|
SQL
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
}
|