katzen-cafe/modules/conduit.nix

165 lines
4.2 KiB
Nix
Raw Permalink Normal View History

2024-05-01 17:57:28 +02:00
{
config,
pkgsUnstable,
inputs,
...
}: let
2023-05-21 09:50:51 +02:00
# You'll need to edit these values
# The hostname that will appear in your user and room IDs
2023-05-21 14:51:02 +02:00
server_name = "katzen.cafe";
2023-05-21 09:50:51 +02:00
# The hostname that Conduit actually runs on
#
# This can be the same as `server_name` if you want. This is only necessary
# when Conduit is running on a different machine than the one hosting your
# root domain. This configuration also assumes this is all running on a single
# machine, some tweaks will need to be made if this is not the case.
matrix_hostname = "matrix.${server_name}";
# An admin email for TLS certificate notifications
admin_email = "jade@schrottkatze.de";
# These ones you can leave alone
# Build a dervation that stores the content of `${server_name}/.well-known/matrix/server`
2023-05-21 14:51:02 +02:00
well_known_server = pkgsUnstable.writeText "well-known-matrix-server" ''
2023-05-21 09:50:51 +02:00
{
"m.server": "${matrix_hostname}"
}
'';
# Build a dervation that stores the content of `${server_name}/.well-known/matrix/client`
2023-05-21 14:51:02 +02:00
well_known_client = pkgsUnstable.writeText "well-known-matrix-client" ''
2023-05-21 09:50:51 +02:00
{
"m.homeserver": {
"base_url": "https://${matrix_hostname}"
2024-05-01 17:57:28 +02:00
},
"org.matrix.msc3575.proxy": {
"url": "https://${matrix_hostname}"
2023-05-21 09:50:51 +02:00
}
}
'';
2024-05-01 17:57:28 +02:00
in {
2023-05-21 09:50:51 +02:00
# Configure Conduit itself
services.matrix-conduit = {
enable = true;
# This causes NixOS to use the flake defined in this repository instead of
2023-05-21 14:51:02 +02:00
# the build of Conduit built into nixpkgsUnstable.
2023-07-30 16:24:46 +02:00
# package = pkgsUnstable.matrix-conduit;
package = inputs.conduit.packages.${pkgsUnstable.system}.default;
2023-05-21 09:50:51 +02:00
settings.global = {
inherit server_name;
2023-05-21 14:51:02 +02:00
allow_registration = false;
2023-05-21 09:50:51 +02:00
};
};
# Configure automated TLS acquisition/renewal
security.acme = {
acceptTerms = true;
defaults = {
email = admin_email;
};
2023-07-30 16:24:46 +02:00
certs = {
"katzen.cafe" = {
group = "nginx";
keyType = "rsa4096";
};
"matrix.katzen.cafe" = {
group = "nginx";
keyType = "rsa4096";
};
};
2023-05-21 09:50:51 +02:00
};
# Configure NGINX as a reverse proxy
services.nginx = {
enable = true;
recommendedProxySettings = true;
virtualHosts = {
"${matrix_hostname}" = {
forceSSL = true;
enableACME = true;
listen = [
{
addr = "0.0.0.0";
port = 443;
ssl = true;
}
{
2023-07-30 16:24:46 +02:00
addr = "[::]";
port = 443;
ssl = true;
2024-05-01 17:57:28 +02:00
}
{
2023-05-21 09:50:51 +02:00
addr = "0.0.0.0";
port = 8448;
ssl = true;
}
2023-07-30 16:24:46 +02:00
{
addr = "[::]";
port = 8448;
ssl = true;
}
2023-05-21 09:50:51 +02:00
];
locations."/_matrix/" = {
proxyPass = "http://backend_conduit$request_uri";
proxyWebsockets = true;
extraConfig = ''
proxy_set_header Host $host;
proxy_buffering off;
'';
};
extraConfig = ''
merge_slashes off;
'';
};
"${server_name}" = {
forceSSL = true;
enableACME = true;
locations."=/.well-known/matrix/server" = {
# Use the contents of the derivation built previously
alias = "${well_known_server}";
extraConfig = ''
# Set the header since by default NGINX thinks it's just bytes
default_type application/json;
'';
};
locations."=/.well-known/matrix/client" = {
# Use the contents of the derivation built previously
alias = "${well_known_client}";
extraConfig = ''
# Set the header since by default NGINX thinks it's just bytes
default_type application/json;
# https://matrix.org/docs/spec/client_server/r0.4.0#web-browser-clients
add_header Access-Control-Allow-Origin "*";
'';
};
};
};
upstreams = {
"backend_conduit" = {
servers = {
2024-05-01 17:57:28 +02:00
"[::1]:${toString config.services.matrix-conduit.settings.global.port}" = {};
2023-05-21 09:50:51 +02:00
};
};
};
};
# Open firewall ports for HTTP, HTTPS, and Matrix federation
2024-05-01 17:57:28 +02:00
networking.firewall.allowedTCPPorts = [80 443 8448];
networking.firewall.allowedUDPPorts = [80 443 8448];
2023-04-26 23:25:04 +02:00
}