fruitbasket/modules/matrix.nix

128 lines
3.1 KiB
Nix
Raw Normal View History

2022-12-17 19:11:37 +01:00
{ config, pkgs, lib, ... }:
let
2022-12-17 21:23:46 +01:00
domainServer = "matrix.${config.fsr.domain}";
domainClient = "chat.${config.fsr.domain}";
2022-12-17 19:11:37 +01:00
clientConfig = {
"m.homeserver" = {
base_url = "https://${domainServer}:443";
server_name = domainServer;
};
2022-12-17 21:23:46 +01:00
"m.identity_server" = { };
2022-12-17 19:11:37 +01:00
};
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}';
'';
in
{
2022-12-17 21:23:46 +01:00
# sops.secrets = {
# synapse_registration_secret = {
# owner = "matrix-synapse";
# group = "matrix-synapse";
# };
# };
2022-12-17 19:11:37 +01:00
services = {
postgresql = {
enable = true;
ensureUsers = [
{
name = "matrix-synapse";
}
];
};
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;
};
};
};
};
};
matrix-synapse = {
enable = true;
settings = {
server_name = domainServer;
listeners = [{
port = 8008;
bind_addresses = [ "::1" ];
type = "http";
tls = false;
x_forwarded = true;
resources = [{
names = [ "client" "federation" ];
compress = false;
}];
}];
# TODO: ldap
registration_shared_secret = "registration_shared_secret";
};
2022-12-17 21:23:46 +01:00
# extraConfigFiles = [
# (pkgs.writeTextFile {
# name = "matrix-synapse-extra-config.yml";
# text = ''
# '';
# })
# ];
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 already exists
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
'';
};
}