{ config, lib, ... }: let # matrix server name (user ids look like @user:darkest.space) serverName = "darkest.space"; # host that actually serves the matrix API domain = "matrix.darkest.space"; # created by the tuwunel unit (RuntimeDirectory=tuwunel, group tuwunel, mode 0750) socket = "/run/tuwunel/tuwunel.sock"; stateDir = "/var/lib/${config.services.matrix-tuwunel.stateDirectory}"; in { services.matrix-tuwunel = { enable = true; settings.global = { server_name = serverName; unix_socket_path = socket; # 10 MB upload limit; nginx client_max_body_size follows this max_request_size = 10000000; allow_registration = true; # default appends a heart emoji to every new user's display name new_user_displayname_suffix = ""; # token placed manually, owned by tuwunel; the first registered user becomes admin registration_token_file = "/etc/secrets/tuwunel-registration-token"; }; }; # nginx reaches the socket through the tuwunel group users.users.nginx.extraGroups = [ config.services.matrix-tuwunel.group ]; # Run as the static tuwunel user (not DynamicUser) so the dedicated volume # can be mounted directly at /var/lib/tuwunel. systemd.services.tuwunel = { serviceConfig.DynamicUser = lib.mkForce false; unitConfig.RequiresMountsFor = [ stateDir ]; }; # dedicated hetzner volume "tuwunel" (ext4), so the database can never end up on the root disk fileSystems.${stateDir} = { device = "/dev/disk/by-id/scsi-0HC_Volume_107575600"; fsType = "ext4"; options = [ "discard" "nofail" "defaults" ]; }; services.nginx.virtualHosts = { ${domain} = { enableACME = true; forceSSL = true; extraConfig = '' client_max_body_size ${toString config.services.matrix-tuwunel.settings.global.max_request_size}; ''; locations = { "/_matrix/".proxyPass = "http://unix:${socket}"; "/".return = "404 \" \""; }; }; # apex: only delegate the matrix server name to ${domain}, 404 on everything else ${serverName} = { enableACME = true; forceSSL = true; locations = { "= /.well-known/matrix/server".extraConfig = '' default_type application/json; add_header Access-Control-Allow-Origin *; return 200 '${builtins.toJSON { "m.server" = "${domain}:443"; }}'; ''; "= /.well-known/matrix/client".extraConfig = '' default_type application/json; add_header Access-Control-Allow-Origin *; return 200 '${builtins.toJSON { "m.homeserver".base_url = "https://${domain}"; }}'; ''; "/".return = "404 \" \""; }; }; }; }