# The Tabellarius relay on NixOS, behind Caddy with TLS, and no log that holds an address # (.agents/features/home-relay.md). Import it from the flake: `tabellarius.nixosModules.default`. # # services.tabellarius = { # enable = true; # domain = "relay.example.cz"; # its DNS name points at this machine; ports 80 and 443 open # # Calls (optional): TURN credentials from the relay, coturn on this machine with the same secret. # # turn.secretFile = "/run/secrets/tabellarius-turn"; # outside the Nix store # # turn.urls = [ "turn:relay.example.cz:3478?transport=udp" "turn:relay.example.cz:3478?transport=tcp" ]; # # turn.coturn.enable = true; # }; { self }: { config, lib, pkgs, ... }: let cfg = config.services.tabellarius; system = pkgs.stdenv.hostPlatform.system; coturn = cfg.turn.coturn; # Peers TURN may never relay to: private, loopback, link-local, shared, documentation and reserved ranges, so a # call cannot be turned into a way into the server's own network (call-kinds.md section 2). deniedPeers = [ "0.0.0.0-0.255.255.255" "10.0.0.0-10.255.255.255" "100.64.0.0-100.127.255.255" "127.0.0.0-127.255.255.255" "169.254.0.0-169.254.255.255" "172.16.0.0-172.31.255.255" "192.0.0.0-192.0.0.255" "192.0.2.0-192.0.2.255" "192.168.0.0-192.168.255.255" "198.18.0.0-198.19.255.255" "198.51.100.0-198.51.100.255" "203.0.113.0-203.0.113.255" "224.0.0.0-255.255.255.255" "::1" "::ffff:0:0-::ffff:ffff:ffff" "64:ff9b::-64:ff9b::ffff:ffff" "fc00::-fdff:ffff:ffff:ffff:ffff:ffff:ffff:ffff" "fe80::-febf:ffff:ffff:ffff:ffff:ffff:ffff:ffff" "ff00::-ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff" ]; in { options.services.tabellarius = { enable = lib.mkEnableOption "the Tabellarius message relay, behind Caddy with TLS and no logs"; domain = lib.mkOption { type = lib.types.str; example = "relay.example.cz"; description = "The relay's DNS name. Caddy gets its certificate from Let's Encrypt."; }; package = lib.mkOption { type = lib.types.package; default = self.packages.${system}.default; defaultText = lib.literalExpression "tabellarius.packages.\${system}.default"; description = "The relay, built from this flake: the code that runs is the code published."; }; port = lib.mkOption { type = lib.types.port; default = 8470; description = "The loopback port between Caddy and the relay. Never exposed."; }; maxBytes = lib.mkOption { type = lib.types.ints.positive; default = 1073741824; description = "Waiting blobs the relay holds, in bytes, before it answers 507 (PROTOCOL.md section 8)."; }; maxMailboxes = lib.mkOption { type = lib.types.ints.positive; default = 100000; description = "Mailboxes the relay holds before it answers 507."; }; maxInvites = lib.mkOption { type = lib.types.ints.positive; default = 10000; description = "Unclaimed invites the relay holds before it answers 507."; }; wake = lib.mkOption { type = lib.types.bool; default = true; description = '' Wake apps whose owners gave a push endpoint (PROTOCOL.md section 6): the relay may then connect out to global addresses, and only to those; every private, loopback-other-than-Caddy, link-local and reserved range stays denied here as well as in the relay. Names are looked up through the system's resolver (nscd on NixOS), so local sockets are allowed. Off: the relay talks to Caddy on loopback and to nothing else. ''; }; turn = { secretFile = lib.mkOption { # A string, not a path: a path would copy the secret into the world-readable Nix store. type = lib.types.nullOr lib.types.str; default = null; description = '' The secret the relay shares with the TURN server for calls (coturn `use-auth-secret`), as a file outside the Nix store (e.g. written by Custos). Null: no TURN route (`POST /v0/mailboxes/{id}/turn` is 404). ''; }; urls = lib.mkOption { type = lib.types.listOf lib.types.str; default = [ ]; example = [ "turn:turn.example.org:3478?transport=udp" "turn:turn.example.org:3478?transport=tcp" ]; description = "The TURN servers named in each credential. Required with `secretFile`."; }; coturn = { enable = lib.mkEnableOption '' coturn on this machine, for calls, with the relay's TURN secret (`use-auth-secret`), no logs, and no relaying to private or reserved addresses. Needs `turn.secretFile`. Opens 3478 (UDP and TCP) and the relay ports (UDP) when `openFirewall` is on''; realm = lib.mkOption { type = lib.types.str; default = cfg.domain; defaultText = lib.literalExpression "config.services.tabellarius.domain"; description = "coturn's realm."; }; minPort = lib.mkOption { type = lib.types.port; default = 49152; description = "The first UDP port coturn relays media on."; }; maxPort = lib.mkOption { type = lib.types.port; default = 49999; description = "The last UDP port coturn relays media on."; }; externalIp = lib.mkOption { type = lib.types.nullOr lib.types.str; default = null; example = "198.51.100.7/192.168.1.10"; description = '' Behind NAT: the public address, then `/` and this machine's own (coturn `external-ip`). The router forwards 3478 (UDP and TCP) and the relay ports (UDP) to this machine. ''; }; allowedPeerIps = lib.mkOption { type = lib.types.listOf lib.types.str; default = [ ]; example = [ "192.168.1.0-192.168.1.255" ]; description = "Ranges coturn may relay to although they are denied by default (a test network). Leave empty."; }; }; }; openFirewall = lib.mkOption { type = lib.types.bool; default = true; description = "Open ports 80 (the ACME challenge, then a redirect) and 443."; }; }; config = lib.mkIf cfg.enable { assertions = [ { assertion = (cfg.turn.secretFile == null) == (cfg.turn.urls == [ ]); message = "services.tabellarius.turn: secretFile and urls come together, or neither."; } { assertion = coturn.enable -> cfg.turn.secretFile != null; message = "services.tabellarius.turn.coturn needs turn.secretFile: coturn and the relay share it."; } ]; systemd.services.tabellarius = { description = "Tabellarius message relay (loopback only; Caddy in front)"; wantedBy = [ "multi-user.target" ]; after = [ "network.target" ]; serviceConfig = { ExecStart = lib.escapeShellArgs [ "${cfg.package}/bin/tabellarius-relay" "--listen" "127.0.0.1:${toString cfg.port}" "--db" "/var/lib/tabellarius/relay.db" "--max-bytes" (toString cfg.maxBytes) "--max-mailboxes" (toString cfg.maxMailboxes) "--max-invites" (toString cfg.maxInvites) ] + lib.optionalString (cfg.turn.secretFile != null) (" " + lib.escapeShellArgs ( # systemd hands the secret to the DynamicUser under its credentials directory (%d); it is never in the store. [ "--turn-secret-file" "%d/turn-secret" ] ++ lib.concatMap (u: [ "--turn-url" u ]) cfg.turn.urls )); LoadCredential = lib.mkIf (cfg.turn.secretFile != null) [ "turn-secret:${toString cfg.turn.secretFile}" ]; Restart = "on-failure"; DynamicUser = true; StateDirectory = "tabellarius"; StateDirectoryMode = "0700"; UMask = "0077"; # Without wakes it talks to Caddy on loopback and to nothing else, even if it had a bug. With # wakes it may also reach global addresses: the non-global ranges stay denied here, a second # wall behind the relay's own check (crates/relay/src/wake.rs). An address in both lists is # allowed, so localhost (Caddy) stays reachable. IPAddressAllow = "localhost"; IPAddressDeny = if cfg.wake then [ "0.0.0.0/8" "10.0.0.0/8" "100.64.0.0/10" "169.254.0.0/16" "172.16.0.0/12" "192.0.0.0/24" "192.0.2.0/24" "192.168.0.0/16" "198.18.0.0/15" "198.51.100.0/24" "203.0.113.0/24" "224.0.0.0/3" "::/96" "::ffff:0:0/96" "64:ff9b::/96" "100::/64" "2001::/32" "2001:db8::/32" "2002::/16" "fc00::/7" "fe80::/10" "fec0::/10" "ff00::/8" ] else "any"; RestrictAddressFamilies = [ "AF_INET" "AF_INET6" ] ++ lib.optional cfg.wake "AF_UNIX"; CapabilityBoundingSet = ""; NoNewPrivileges = true; ProtectSystem = "strict"; ProtectHome = true; PrivateTmp = true; PrivateDevices = true; PrivateUsers = true; ProtectHostname = true; ProtectClock = true; ProtectKernelTunables = true; ProtectKernelModules = true; ProtectKernelLogs = true; ProtectControlGroups = true; ProtectProc = "invisible"; ProcSubset = "pid"; RestrictNamespaces = true; RestrictRealtime = true; RestrictSUIDSGID = true; RemoveIPC = true; LockPersonality = true; MemoryDenyWriteExecute = true; SystemCallArchitectures = "native"; SystemCallFilter = [ "@system-service" "~@privileged" ]; }; }; services.caddy = { enable = true; # Caddy's own logger: errors about TLS handshakes or upstreams can name a client's address. # No log is the rule; an operator who needs one turns it on for a while, knowing why. mkDefault: it is the # logger of every site on the machine, so a host that serves others may keep errors (home-relay.md). logFormat = lib.mkDefault '' output discard ''; virtualHosts.${cfg.domain} = { # The NixOS module writes an access log per host by default; this one writes none. logFormat = '' output discard ''; extraConfig = '' reverse_proxy 127.0.0.1:${toString cfg.port} { # The relay never learns who connected: Caddy adds these by default. header_up -X-Forwarded-For header_up -X-Real-IP header_up -Forwarded } ''; }; }; # TURN for calls (call-kinds.md section 2): the relay's secret reaches coturn as a systemd credential, read by # its start script into /run/coturn (0700); never the Nix store. No log: coturn's sessions name both devices' # addresses. TLS and DTLS are off: the media inside is DTLS-SRTP and frame-sealed, and the credentials are HMACs. services.coturn = lib.mkIf coturn.enable { enable = true; realm = coturn.realm; use-auth-secret = true; static-auth-secret-file = "/run/credentials/coturn.service/turn-secret"; listening-port = 3478; min-port = coturn.minPort; max-port = coturn.maxPort; no-cli = true; no-tls = true; no-dtls = true; no-tcp-relay = true; extraConfig = lib.concatStringsSep "\n" ( [ "no-stdout-log" "log-file=/dev/null" "simple-log" "no-multicast-peers" ] ++ map (r: "denied-peer-ip=${r}") deniedPeers ++ map (r: "allowed-peer-ip=${r}") coturn.allowedPeerIps ++ lib.optional (coturn.externalIp != null) "external-ip=${coturn.externalIp}" ); }; systemd.services.coturn.serviceConfig.LoadCredential = lib.mkIf coturn.enable [ "turn-secret:${toString cfg.turn.secretFile}" ]; networking.firewall.allowedTCPPorts = lib.mkIf cfg.openFirewall ([ 80 443 ] ++ lib.optional coturn.enable 3478); networking.firewall.allowedUDPPorts = lib.mkIf (cfg.openFirewall && coturn.enable) [ 3478 ]; networking.firewall.allowedUDPPortRanges = lib.mkIf (cfg.openFirewall && coturn.enable) [ { from = coturn.minPort; to = coturn.maxPort; } ]; # NixOS logs every refused connection with its source address by default. Those are strangers, # not users, but a relay that keeps no address should not keep theirs either. mkDefault: the # machine's owner can turn it back on. networking.firewall.logRefusedConnections = lib.mkDefault false; }; }