Files
Amicitia-Tabellarius/deploy/nixos/module.nix
T

164 lines
6.2 KiB
Nix

# 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
# };
{ self }:
{ config, lib, pkgs, ... }:
let
cfg = config.services.tabellarius;
system = pkgs.stdenv.hostPlatform.system;
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.
'';
};
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 {
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)
];
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.
logFormat = ''
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
}
'';
};
};
networking.firewall.allowedTCPPorts = lib.mkIf cfg.openFirewall [ 80 443 ];
# 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;
};
}