clanModules: Init nginx module. matrix-synapse: don't assume domain names

This commit is contained in:
Qubasa
2024-08-19 11:33:17 +02:00
parent f8c20fcbe1
commit efd0a0f056
3 changed files with 81 additions and 20 deletions

View File

@@ -6,25 +6,31 @@
}: }:
let let
cfg = config.clan.matrix-synapse; cfg = config.clan.matrix-synapse;
nginx-vhost = "matrix.${config.clan.matrix-synapse.domain}";
element-web = element-web =
pkgs.runCommand "element-web-with-config" { nativeBuildInputs = [ pkgs.buildPackages.jq ]; } pkgs.runCommand "element-web-with-config" { nativeBuildInputs = [ pkgs.buildPackages.jq ]; }
'' ''
cp -r ${pkgs.element-web} $out cp -r ${pkgs.element-web} $out
chmod -R u+w $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}" }' \ jq '."default_server_config"."m.homeserver" = { "base_url": "https://${cfg.domain.server}:443", "server_name": "${cfg.domain.server}" }' \
> $out/config.json < ${pkgs.element-web}/config.json > $out/config.json < ${pkgs.element-web}/config.json
ln -s $out/config.json $out/config.${nginx-vhost}.json ln -s $out/config.json $out/config.${cfg.domain.server}.json
''; '';
in in
# FIXME: This was taken from upstream. Drop this when our patch is upstream # FIXME: This was taken from upstream. Drop this when our patch is upstream
{ {
options.services.matrix-synapse.package = lib.mkOption { readOnly = false; }; options.services.matrix-synapse.package = lib.mkOption { readOnly = false; };
options.clan.matrix-synapse = { options.clan.matrix-synapse = {
domain = lib.mkOption { domain = {
server = 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"; example = "matrix.example.com";
};
client = lib.mkOption {
type = lib.types.str;
description = "The domain name of the matrix client";
example = "element.example.com";
};
}; };
users = lib.mkOption { users = lib.mkOption {
default = { }; default = { };
@@ -61,14 +67,14 @@ in
"matrix-synapse" "matrix-synapse"
"enable" "enable"
] "Importing the module will already enable the service.") ] "Importing the module will already enable the service.")
../nginx
../postgresql
]; ];
config = { config = {
services.matrix-synapse = { services.matrix-synapse = {
enable = true; enable = true;
settings = { settings = {
server_name = cfg.domain; server_name = cfg.domain.server;
enable_registration = false;
database = { database = {
args.user = "matrix-synapse"; args.user = "matrix-synapse";
args.database = "matrix-synapse"; args.database = "matrix-synapse";
@@ -169,18 +175,13 @@ in
]; ];
}; };
networking.firewall.allowedTCPPorts = [
80
443
];
services.nginx = { services.nginx = {
enable = true; enable = true;
virtualHosts = { virtualHosts = {
${cfg.domain} = { "${cfg.domain.server}" = {
locations."= /.well-known/matrix/server".extraConfig = '' locations."= /.well-known/matrix/server".extraConfig = ''
add_header Content-Type application/json; add_header Content-Type application/json;
return 200 '${builtins.toJSON { "m.server" = "matrix.${cfg.domain}:443"; }}'; return 200 '${builtins.toJSON { "m.server" = "${cfg.domain.server}:443"; }}';
''; '';
locations."= /.well-known/matrix/client".extraConfig = '' locations."= /.well-known/matrix/client".extraConfig = ''
add_header Content-Type application/json; add_header Content-Type application/json;
@@ -188,7 +189,7 @@ in
return 200 '${ return 200 '${
builtins.toJSON { builtins.toJSON {
"m.homeserver" = { "m.homeserver" = {
"base_url" = "https://${nginx-vhost}"; "base_url" = "https://${cfg.domain.server}";
}; };
"m.identity_server" = { "m.identity_server" = {
"base_url" = "https://vector.im"; "base_url" = "https://vector.im";
@@ -196,12 +197,14 @@ in
} }
}'; }';
''; '';
};
${nginx-vhost} = {
forceSSL = true; forceSSL = true;
enableACME = true; enableACME = true;
locations."/_matrix".proxyPass = "http://localhost:8008"; locations."/_matrix".proxyPass = "http://localhost:8008";
locations."/_synapse".proxyPass = "http://localhost:8008"; locations."/_synapse".proxyPass = "http://localhost:8008";
};
"${cfg.domain.client}" = {
forceSSL = true;
enableACME = true;
locations."/".root = element-web; locations."/".root = element-web;
}; };
}; };

View File

@@ -0,0 +1,3 @@
---
description = "Good defaults for the nginx webserver"
---

View File

@@ -0,0 +1,55 @@
{ config, lib, ... }:
{
imports = [
(lib.mkRemovedOptionModule [
"clan"
"nginx"
"enable"
] "Importing the module will already enable the service.")
];
config = {
networking.firewall.allowedTCPPorts = [
443
80
];
services.nginx = {
enable = true;
statusPage = lib.mkDefault true;
recommendedBrotliSettings = lib.mkDefault true;
recommendedGzipSettings = lib.mkDefault true;
recommendedOptimisation = lib.mkDefault true;
recommendedProxySettings = lib.mkDefault true;
recommendedTlsSettings = lib.mkDefault true;
recommendedZstdSettings = lib.mkDefault true;
# Nginx sends all the access logs to /var/log/nginx/access.log by default.
# instead of going to the journal!
commonHttpConfig = "access_log syslog:server=unix:/dev/log;";
resolver.addresses =
let
isIPv6 = addr: builtins.match ".*:.*:.*" addr != null;
escapeIPv6 = addr: if isIPv6 addr then "[${addr}]" else addr;
cloudflare = [
"1.1.1.1"
"2606:4700:4700::1111"
];
resolvers =
if config.networking.nameservers == [ ] then cloudflare else config.networking.nameservers;
in
map escapeIPv6 resolvers;
sslDhparam = config.security.dhparams.params.nginx.path;
};
security.dhparams = {
enable = true;
params.nginx = { };
};
};
}