fruitbasket/modules/matrix.nix

142 lines
3.9 KiB
Nix
Raw Normal View History

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
# build ldap3 plugin from git because it's very outdated in nixpkgs
2023-02-02 21:16:55 +01:00
matrix-synapse-ldap3 = pkgs.python3.pkgs.callPackage ../pkgs/matrix-synapse-ldap3.nix { };
2023-01-21 21:26:24 +01:00
# matrix-synapse-ldap3 = config.services.matrix-synapse.package.plugins.matrix-synapse-ldap3;
2022-12-17 19:11:37 +01:00
in
{
sops.secrets.matrix_ldap_search = {
key = "portunus/search-password";
owner = config.systemd.services.matrix-synapse.serviceConfig.User;
};
2022-12-17 19:11:37 +01:00
services = {
postgresql = {
enable = true;
ensureUsers = [{
name = "matrix-synapse";
}];
2022-12-17 19:11:37 +01:00
};
nginx = {
recommendedProxySettings = true;
virtualHosts = {
# synapse
"${domainServer}" = {
enableACME = true;
forceSSL = true;
# 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}" = {
enableACME = true;
forceSSL = true;
root = pkgs.element-web.override {
conf = {
default_server_config = clientConfig;
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 ];
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;
}];
}];
};
extraConfigFiles = [
(pkgs.writeTextFile {
name = "matrix-synapse-extra-config.yml";
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}
'';
})
];
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 ];
# 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
'';
};
}