nixos-config/hosts/nuc/modules/matrix/default.nix

123 lines
3.1 KiB
Nix
Raw Normal View History

2023-10-17 10:39:14 +02:00
{ config, pkgs, ... }:
let
domain = "matrix.${config.networking.domain}";
2024-01-27 23:03:29 +01:00
domainClient = "chat.${config.networking.domain}";
clientConfig = {
"m.homeserver" = {
base_url = "https://${domain}:443";
};
};
2023-10-17 10:39:14 +02:00
in
{
2023-11-16 14:40:40 +01:00
age.secrets = {
"matrix/shared" = {
file = ../../../../secrets/nuc/matrix/shared.age;
2023-10-17 10:39:14 +02:00
owner = config.systemd.services.matrix-synapse.serviceConfig.User;
};
2023-11-16 14:40:40 +01:00
"matrix/sync" = {
file = ../../../../secrets/nuc/matrix/sync.age;
2023-10-17 10:39:14 +02:00
};
};
services = {
postgresql = {
enable = true;
ensureUsers = [{
name = "matrix-synapse";
}];
};
matrix-synapse = {
enable = true;
configureRedisLocally = true;
2024-05-21 18:44:04 +02:00
enableRegistrationScript = false;
2023-11-16 14:40:40 +01:00
extraConfigFiles = [ config.age.secrets."matrix/shared".path ];
log = {
root.level = "WARNING";
};
2023-10-17 10:39:14 +02:00
settings = {
server_name = config.networking.domain;
listeners = [{
2024-05-21 18:44:04 +02:00
path = "/run/matrix-synapse/server.sock";
mode = "666";
2023-10-17 10:39:14 +02:00
type = "http";
x_forwarded = true;
resources = [{
names = [ "client" "federation" ];
compress = false;
}];
}];
};
2024-01-04 00:13:32 +01:00
};
matrix-sliding-sync = {
enable = true;
settings = {
SYNCV3_SERVER = "https://${domain}";
2024-05-21 18:44:04 +02:00
SYNCV3_BINDADDR = "/run/matrix-sliding-sync/server.sock";
2023-10-17 10:39:14 +02:00
};
2024-01-04 00:13:32 +01:00
environmentFile = config.age.secrets."matrix/sync".path;
2023-10-17 10:39:14 +02:00
};
2024-05-21 18:44:04 +02:00
caddy = {
2023-10-17 10:39:14 +02:00
virtualHosts = {
# synapse
2024-05-21 18:44:04 +02:00
"${domain}".extraConfig = ''
reverse_proxy /client/* unix//run/matrix-sliding-sync/server.sock
reverse_proxy /_matrix/client/unstable/org.matrix.msc3575/sync* unix//run/matrix-sliding-sync/server.sock
reverse_proxy unix//run/matrix-synapse/server.sock
'';
2024-01-27 23:03:29 +01:00
# element
2024-05-21 18:44:04 +02:00
"${domainClient}".extraConfig = ''
root '${pkgs.element-web.override {
2024-01-27 23:03:29 +01:00
conf = {
default_server_config = {
inherit (clientConfig) "m.homeserver";
"m.identity_server".base_url = "";
};
disable_3pid_login = true;
};
2024-05-21 18:44:04 +02:00
}}'
'';
2023-10-17 10:39:14 +02:00
};
};
};
2024-05-21 18:44:04 +02:00
systemd.services.matrix-synapse = {
after = [ "matrix-synapse-pgsetup.service" ];
serviceConfig = {
RuntimeDirectory = "matrix-synapse";
};
};
systemd.services.matrix-sliding-sync = {
serviceConfig = {
RuntimeDirectory = "matrix-sliding-sync";
};
};
2023-10-17 10:39:14 +02:00
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
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
'';
};
}