Expand backup and restore capabilities w.r.t. postgresql. #1582

Merged
clan-bot merged 10 commits from synapse into main 2024-06-10 13:24:08 +00:00
Showing only changes of commit ba6840d978 - Show all commits

View File

@ -6,6 +6,15 @@
}: }:
let let
cfg = config.clan.matrix-synapse; cfg = config.clan.matrix-synapse;
nginx-vhost = "matrix.${config.clan.matrix-synapse.domain}";
element-web =
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
'';
in in
{ {
options.clan.matrix-synapse = { options.clan.matrix-synapse = {
@ -13,6 +22,7 @@ in
domain = lib.mkOption { domain = lib.mkOption {
type = lib.types.str; type = lib.types.str;
description = "The domain name of the matrix server"; description = "The domain name of the matrix server";
example = "example.com";
}; };
}; };
config = lib.mkIf cfg.enable { config = lib.mkIf cfg.enable {
@ -49,14 +59,40 @@ in
} }
]; ];
}; };
extraConfigFiles = [ "/var/lib/matrix-synapse/registration_shared_secret.yaml" ]; extraConfigFiles = [ "/run/synapse-registration-shared-secret.yaml" ];
}; };
systemd.tmpfiles.settings."synapse" = {
"/run/synapse-registration-shared-secret.yaml" = {
C.argument = config.clanCore.facts.services.matrix-synapse.secret.synapse-registration_shared_secret.path;
z = {
mode = "0400";
user = "matrix-synapse";
};
};
};
systemd.services.matrix-synapse.serviceConfig.ExecStartPre = [ systemd.services.matrix-synapse.serviceConfig.ExecStartPre = [
"+${pkgs.writeScript "copy_registration_shared_secret" '' "+${pkgs.writeShellScript "create-matrix-synapse-db" ''
#!/bin/sh export PATH=${
cp ${config.clanCore.facts.services.matrix-synapse.secret.synapse-registration_shared_secret.path} /var/lib/matrix-synapse/registration_shared_secret.yaml lib.makeBinPath [
chown matrix-synapse:matrix-synapse /var/lib/matrix-synapse/registration_shared_secret.yaml config.services.postgresql.package
chmod 600 /var/lib/matrix-synapse/registration_shared_secret.yaml 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
''}" ''}"
]; ];
@ -72,22 +108,6 @@ in
}; };
services.postgresql.enable = true; services.postgresql.enable = true;
# we need to use both ensusureDatabases and initialScript, because the former runs everytime but with the wrong collation
services.postgresql = {
ensureDatabases = [ "matrix-synapse" ];
ensureUsers = [
{
name = "matrix-synapse";
ensureDBOwnership = true;
}
];
initialScript = pkgs.writeText "synapse-init.sql" ''
CREATE DATABASE "matrix-synapse"
TEMPLATE template0
LC_COLLATE = "C"
LC_CTYPE = "C";
'';
};
services.nginx = { services.nginx = {
enable = true; enable = true;
virtualHosts = { virtualHosts = {
@ -102,7 +122,7 @@ in
return 200 '${ return 200 '${
builtins.toJSON { builtins.toJSON {
"m.homeserver" = { "m.homeserver" = {
"base_url" = "https://matrix.${cfg.domain}"; "base_url" = "https://${nginx-vhost}";
}; };
"m.identity_server" = { "m.identity_server" = {
"base_url" = "https://vector.im"; "base_url" = "https://vector.im";
@ -111,15 +131,12 @@ in
}'; }';
''; '';
}; };
"matrix.${cfg.domain}" = { ${nginx-vhost} = {
forceSSL = true; forceSSL = true;
enableACME = true; enableACME = true;
locations."/_matrix" = { locations."/_matrix".proxyPass = "http://localhost:8008";
proxyPass = "http://localhost:8008"; locations."/_synapse".proxyPass = "http://localhost:8008";
}; locations."/".root = element-web;
locations."/test".extraConfig = ''
return 200 "Hello, world!";
'';
}; };
}; };
}; };