Self-host your own digital island

add turn server for matrix

Changed files
+97 -23
modules
+1
modules/default.nix
···
./hosting/gitea.nix
./hosting/dns.nix
./hosting/matrix.nix
+
./hosting/turn.nix
./wireguard/server.nix
./wireguard/default.nix
];
+37 -23
modules/hosting/matrix.nix
···
let cfg = config.hosting; in
{
-
options.hosting.matrix.enable = lib.mkEnableOption "matrix";
+
options.hosting.matrix = {
+
enable = lib.mkEnableOption "matrix";
+
turn = lib.mkOption {
+
type = lib.types.bool;
+
default = true;
+
};
+
};
config = lib.mkIf cfg.matrix.enable {
services.postgresql.enable = true;
···
services.matrix-synapse = {
enable = true;
-
settings = {
-
server_name = config.networking.domain;
-
enable_registration = true;
-
registration_requires_token = true;
-
auto_join_rooms = [ "#freumh:freumh.org" ];
-
registration_shared_secret_path = "${config.custom.secretsDir}/matrix-shared-secret";
-
listeners = [
-
{
-
port = 8008;
-
bind_addresses = [ "::1" "127.0.0.1" ];
-
type = "http";
-
tls = false;
-
x_forwarded = true;
-
resources = [
-
{
-
names = [ "client" "federation" ];
-
compress = false;
-
}
-
];
-
}
-
];
-
};
+
settings = lib.mkMerge [
+
{
+
server_name = config.networking.domain;
+
enable_registration = true;
+
registration_requires_token = true;
+
auto_join_rooms = [ "#freumh:freumh.org" ];
+
registration_shared_secret_path = "${config.custom.secretsDir}/matrix-shared-secret";
+
listeners = [
+
{
+
port = 8008;
+
bind_addresses = [ "::1" "127.0.0.1" ];
+
type = "http";
+
tls = false;
+
x_forwarded = true;
+
resources = [
+
{
+
names = [ "client" "federation" ];
+
compress = false;
+
}
+
];
+
}
+
];
+
max_upload_size = "100M";
+
}
+
(with config.services.coturn; lib.mkIf cfg.matrix.turn {
+
turn_uris = ["turn:${realm}:3478?transport=udp" "turn:${realm}:3478?transport=tcp"];
+
turn_shared_secret = static-auth-secret;
+
turn_user_lifetime = "1h";
+
})
+
];
};
dns.records = [
+59
modules/hosting/turn.nix
···
+
{ config, pkgs, lib, ... }:
+
+
let
+
cfg = config.hosting;
+
domain = config.networking.domain;
+
in
+
{
+
options.hosting.turn.enable = lib.mkEnableOption "TURN server";
+
+
config = lib.mkIf cfg.turn.enable {
+
services.coturn = rec {
+
enable = true;
+
no-cli = true;
+
no-tcp-relay = true;
+
min-port = 49000;
+
max-port = 50000;
+
use-auth-secret = true;
+
static-auth-secret-file = "${config.custom.secretsDir}/coturn";
+
realm = "turn.${domain}";
+
cert = "${config.security.acme.certs.${realm}.directory}/full.pem";
+
pkey = "${config.security.acme.certs.${realm}.directory}/key.pem";
+
secure-stun = true;
+
};
+
+
networking.firewall =
+
let
+
turn-range = with config.services.coturn; {
+
from = min-port;
+
to = max-port;
+
};
+
stun-port = 3478;
+
in {
+
allowedTCPPorts = lib.mkForce [ stun-port ];
+
allowedTCPPortRanges = [ turn-range ];
+
allowedUDPPorts = lib.mkForce [ stun-port ];
+
allowedUDPPortRanges = [ turn-range ];
+
};
+
+
security.acme.certs.${config.services.coturn.realm} = {
+
postRun = "systemctl reload nginx.service; systemctl restart coturn.service";
+
group = "turnserver";
+
};
+
services.nginx.virtualHosts = {
+
"turn.${domain}" = {
+
forceSSL = true;
+
enableACME = true;
+
};
+
};
+
users.groups."turnserver".members = [ config.services.nginx.user ];
+
+
dns.records = [
+
{
+
name = "turn";
+
type = "CNAME";
+
data = "vps";
+
}
+
];
+
};
+
}