143 lines
4.9 KiB
Nix
143 lines
4.9 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.";
|
|
};
|
|
|
|
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";
|
|
|
|
# It talks to Caddy on loopback and to nothing else, even if it had a bug.
|
|
IPAddressDeny = "any";
|
|
IPAddressAllow = "localhost";
|
|
RestrictAddressFamilies = [ "AF_INET" "AF_INET6" ];
|
|
|
|
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;
|
|
};
|
|
}
|