2024-03-19 13:20:56 +00:00
|
|
|
{
|
|
|
|
config,
|
|
|
|
lib,
|
|
|
|
pkgs,
|
|
|
|
...
|
|
|
|
}:
|
|
|
|
let
|
|
|
|
cfg = config.clan.matrix-synapse;
|
2024-05-29 12:24:36 +00:00
|
|
|
nginx-vhost = "matrix.${config.clan.matrix-synapse.domain}";
|
|
|
|
element-web =
|
2024-05-31 14:36:37 +00:00
|
|
|
pkgs.runCommand "element-web-with-config" { nativeBuildInputs = [ pkgs.buildPackages.jq ]; }
|
|
|
|
''
|
|
|
|
cp -r ${pkgs.element-web} $out
|
|
|
|
chmod -R u+w $out
|
|
|
|
jq '."default_server_config"."m.homeserver" = { "base_url": "https://${nginx-vhost}:443", "server_name": "${config.clan.matrix-synapse.domain}" }' \
|
|
|
|
> $out/config.json < ${pkgs.element-web}/config.json
|
|
|
|
ln -s $out/config.json $out/config.${nginx-vhost}.json
|
|
|
|
'';
|
2024-03-19 13:20:56 +00:00
|
|
|
in
|
|
|
|
{
|
|
|
|
options.clan.matrix-synapse = {
|
|
|
|
domain = lib.mkOption {
|
|
|
|
type = lib.types.str;
|
|
|
|
description = "The domain name of the matrix server";
|
2024-05-29 12:24:36 +00:00
|
|
|
example = "example.com";
|
2024-03-19 13:20:56 +00:00
|
|
|
};
|
|
|
|
};
|
2024-05-29 14:12:06 +00:00
|
|
|
imports = [
|
|
|
|
(lib.mkRemovedOptionModule [
|
|
|
|
"clan"
|
|
|
|
"matrix-synapse"
|
|
|
|
"enable"
|
|
|
|
] "Importing the module will already enable the service.")
|
|
|
|
|
|
|
|
../postgresql
|
|
|
|
];
|
|
|
|
config = {
|
2024-03-19 13:20:56 +00:00
|
|
|
services.matrix-synapse = {
|
|
|
|
enable = true;
|
|
|
|
settings = {
|
|
|
|
server_name = cfg.domain;
|
|
|
|
database = {
|
|
|
|
args.user = "matrix-synapse";
|
|
|
|
args.database = "matrix-synapse";
|
|
|
|
name = "psycopg2";
|
|
|
|
};
|
|
|
|
turn_uris = [
|
|
|
|
"turn:turn.matrix.org?transport=udp"
|
|
|
|
"turn:turn.matrix.org?transport=tcp"
|
|
|
|
];
|
|
|
|
listeners = [
|
|
|
|
{
|
|
|
|
port = 8008;
|
|
|
|
bind_addresses = [ "::1" ];
|
|
|
|
type = "http";
|
|
|
|
tls = false;
|
|
|
|
x_forwarded = true;
|
|
|
|
resources = [
|
|
|
|
{
|
|
|
|
names = [ "client" ];
|
|
|
|
compress = true;
|
|
|
|
}
|
|
|
|
{
|
|
|
|
names = [ "federation" ];
|
|
|
|
compress = false;
|
|
|
|
}
|
|
|
|
];
|
|
|
|
}
|
|
|
|
];
|
|
|
|
};
|
2024-05-29 12:24:36 +00:00
|
|
|
extraConfigFiles = [ "/run/synapse-registration-shared-secret.yaml" ];
|
2024-03-19 13:20:56 +00:00
|
|
|
};
|
2024-05-29 12:24:36 +00:00
|
|
|
systemd.tmpfiles.settings."synapse" = {
|
|
|
|
"/run/synapse-registration-shared-secret.yaml" = {
|
2024-05-31 14:36:37 +00:00
|
|
|
C.argument =
|
|
|
|
config.clanCore.facts.services.matrix-synapse.secret.synapse-registration_shared_secret.path;
|
2024-05-29 12:24:36 +00:00
|
|
|
z = {
|
|
|
|
mode = "0400";
|
|
|
|
user = "matrix-synapse";
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2024-03-19 13:20:56 +00:00
|
|
|
systemd.services.matrix-synapse.serviceConfig.ExecStartPre = [
|
2024-05-29 12:24:36 +00:00
|
|
|
"+${pkgs.writeShellScript "create-matrix-synapse-db" ''
|
|
|
|
export PATH=${
|
|
|
|
lib.makeBinPath [
|
|
|
|
config.services.postgresql.package
|
|
|
|
pkgs.util-linux
|
|
|
|
pkgs.gnugrep
|
|
|
|
]
|
|
|
|
}
|
|
|
|
psql() { runuser -u postgres -- psql "$@"; }
|
|
|
|
# wait for postgres to be ready
|
|
|
|
while ! runuser -u postgres pg_isready; do
|
|
|
|
sleep 1
|
|
|
|
done
|
|
|
|
if ! psql -tAc "SELECT 1 FROM pg_database WHERE datname = 'matrix-synapse'" | grep -q 1; then
|
|
|
|
psql -c "CREATE DATABASE \"matrix-synapse\" TEMPLATE template0 LC_COLLATE = 'C' LC_CTYPE = 'C'"
|
|
|
|
fi
|
|
|
|
# create user if it doesn't exist and make it owner of the database
|
|
|
|
if ! psql -tAc "SELECT 1 FROM pg_user WHERE usename = 'matrix-synapse'" | grep -q 1; then
|
|
|
|
psql -c "CREATE USER \"matrix-synapse\""
|
|
|
|
psql -c "ALTER DATABASE \"matrix-synapse\" OWNER TO \"matrix-synapse\""
|
|
|
|
fi
|
2024-03-19 13:20:56 +00:00
|
|
|
''}"
|
|
|
|
];
|
|
|
|
|
2024-03-28 09:30:37 +00:00
|
|
|
clanCore.facts.services."matrix-synapse" = {
|
|
|
|
secret."synapse-registration_shared_secret" = { };
|
2024-03-19 13:20:56 +00:00
|
|
|
generator.path = with pkgs; [
|
|
|
|
coreutils
|
|
|
|
pwgen
|
|
|
|
];
|
|
|
|
generator.script = ''
|
|
|
|
echo "registration_shared_secret: $(pwgen -s 32 1)" > "$secrets"/synapse-registration_shared_secret
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
services.postgresql.enable = true;
|
|
|
|
services.nginx = {
|
|
|
|
enable = true;
|
|
|
|
virtualHosts = {
|
|
|
|
${cfg.domain} = {
|
|
|
|
locations."= /.well-known/matrix/server".extraConfig = ''
|
|
|
|
add_header Content-Type application/json;
|
|
|
|
return 200 '${builtins.toJSON { "m.server" = "matrix.${cfg.domain}:443"; }}';
|
|
|
|
'';
|
|
|
|
locations."= /.well-known/matrix/client".extraConfig = ''
|
|
|
|
add_header Content-Type application/json;
|
|
|
|
add_header Access-Control-Allow-Origin *;
|
|
|
|
return 200 '${
|
|
|
|
builtins.toJSON {
|
|
|
|
"m.homeserver" = {
|
2024-05-29 12:24:36 +00:00
|
|
|
"base_url" = "https://${nginx-vhost}";
|
2024-03-19 13:20:56 +00:00
|
|
|
};
|
|
|
|
"m.identity_server" = {
|
|
|
|
"base_url" = "https://vector.im";
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}';
|
|
|
|
'';
|
|
|
|
};
|
2024-05-29 12:24:36 +00:00
|
|
|
${nginx-vhost} = {
|
2024-03-19 13:20:56 +00:00
|
|
|
forceSSL = true;
|
|
|
|
enableACME = true;
|
2024-05-29 12:24:36 +00:00
|
|
|
locations."/_matrix".proxyPass = "http://localhost:8008";
|
|
|
|
locations."/_synapse".proxyPass = "http://localhost:8008";
|
|
|
|
locations."/".root = element-web;
|
2024-03-19 13:20:56 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|