feat(matrix): Synapse + MAS on jupiter, published by neptun

Homeserver for mgaction.town (@user:mgaction.town). Synapse and the
Matrix Authentication Service share jupiter's postgres; logins are
OIDC-only through Authentik via MAS, since Element X can't use
Synapse's own SSO. neptun serves the apex .well-known delegation plus
matrix. and account. vhosts; the admin API stays tailnet-only.

Synapse's signing key is a sops secret so the federation identity
survives losing the RAID0 array. Covered by checks.matrix.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-10-21 13:04:52 +02:00
co-authored by Claude Opus 5.5
parent 2719ed7707
commit 7a68716c1c
10 changed files with 461 additions and 3 deletions
+46
View File
@@ -0,0 +1,46 @@
{ config, lib, ... }:
# Public side of services/comms/matrix.nix, on neptun. User IDs are
# @user:mgaction.town; the apex only serves delegation, so federation and clients
# both land on matrix.mgaction.town:443 (no :8448, no SRV records).
let
upstream = config.homelab.matrixProxy.upstream;
in
{
options.homelab.matrixProxy.upstream = lib.mkOption {
type = lib.types.str;
default = "jupiter.orbit.sol";
};
config.services.caddy.virtualHosts = {
"mgaction.town".extraConfig = ''
header /.well-known/matrix/* Content-Type application/json
header /.well-known/matrix/client Access-Control-Allow-Origin *
respond /.well-known/matrix/server `{"m.server":"matrix.mgaction.town:443"}`
respond /.well-known/matrix/client `{"m.homeserver":{"base_url":"https://matrix.mgaction.town"}}`
respond 404
'';
# /_synapse/admin and /_synapse/mas stay tailnet-only.
"matrix.mgaction.town".extraConfig = ''
request_body {
max_size 100MB
}
@mas path_regexp ^/_matrix/client/[^/]+/(login|logout|refresh)
handle @mas {
reverse_proxy http://${upstream}:8080
}
@synapse path /_matrix/* /_synapse/client/*
handle @synapse {
reverse_proxy http://${upstream}:8008
}
handle {
respond 404
}
'';
"account.mgaction.town".extraConfig = ''
reverse_proxy http://${upstream}:8080
'';
};
}
+157
View File
@@ -0,0 +1,157 @@
# VM test for matrix.nix + matrix-proxy.nix. Run:
# nix build .#checks.x86_64-linux.matrix -L
#
# jupiter runs Synapse + MAS on postgres with /mnt/data's real traversal perms;
# neptun runs the public caddy vhosts (local CA). Authentik isn't in the loop: the
# test user comes from `mas manage register-user` and logs in with a compat token.
{ pkgs }:
let
# Throwaway, generated per build.
keys = pkgs.runCommand "matrix-test-keys" { nativeBuildInputs = [ pkgs.openssl ]; } ''
mkdir $out
printf %s "ed25519 a_test $(openssl rand 32 | base64 | tr -d '=')" > $out/signing.key
printf %s "$(openssl rand -hex 32)" > $out/macaroon
printf %s "$(openssl rand -hex 32)" > $out/form
printf %s "$(openssl rand -hex 32)" > $out/mas-shared
printf %s "$(openssl rand -hex 32)" > $out/mas-encryption
openssl genrsa -out $out/mas-rsa.pem 2048
printf dummy > $out/oidc
'';
secret = name: user: group: mode: {
source = "${keys}/${name}";
inherit user group mode;
};
in
pkgs.testers.runNixOSTest {
name = "matrix";
nodes.jupiter = { config, ... }: {
imports = [ ./matrix.nix ];
virtualisation.memorySize = 2048;
environment.etc = {
"matrix/signing.key" = secret "signing.key" "matrix-synapse" "matrix-synapse" "0400";
"matrix/macaroon" = secret "macaroon" "matrix-synapse" "matrix-synapse" "0400";
"matrix/form" = secret "form" "matrix-synapse" "matrix-synapse" "0400";
"matrix/mas-shared" = secret "mas-shared" "matrix-synapse" "matrix-synapse" "0440";
"matrix/mas-encryption" = secret "mas-encryption" "matrix-authentication-service" "matrix-authentication-service" "0400";
"matrix/mas-rsa.pem" = secret "mas-rsa.pem" "matrix-authentication-service" "matrix-authentication-service" "0400";
"matrix/oidc" = secret "oidc" "matrix-authentication-service" "matrix-authentication-service" "0400";
};
homelab.matrix = {
signingKeyFile = "/etc/matrix/signing.key";
macaroonSecretFile = "/etc/matrix/macaroon";
formSecretFile = "/etc/matrix/form";
masSecretFile = "/etc/matrix/mas-shared";
masEncryptionFile = "/etc/matrix/mas-encryption";
masSigningKeyFile = "/etc/matrix/mas-rsa.pem";
oidcClientSecretFile = "/etc/matrix/oidc";
};
# jupiter's /mnt/data/AppData is drwx--x--- darman:users; the non-root owner
# matters (tmpfiles' unsafe-path-transition check).
users.users.darman = { isNormalUser = true; group = "users"; };
systemd.tmpfiles.rules = [ "d /mnt/data/AppData 0710 darman users -" ];
# Stand-in for the trusted tailscale0.
networking.firewall.allowedTCPPorts = [ 8008 8080 ];
};
nodes.neptun = {
imports = [ ../network/caddy.nix ./matrix-proxy.nix ];
homelab.matrixProxy.upstream = "jupiter";
services.caddy.globalConfig = ''
local_certs
skip_install_trust
'';
};
testScript = ''
import json
import re
from urllib.parse import quote
start_all()
jupiter.wait_for_unit("matrix-synapse.service")
jupiter.wait_for_unit("matrix-authentication-service.service")
jupiter.wait_for_open_port(8008)
jupiter.wait_for_open_port(8080)
neptun.wait_for_unit("caddy.service")
neptun.wait_for_open_port(443)
with subtest("synapse database has C collation"):
out = jupiter.succeed(
"sudo -u postgres psql -tAc \"SELECT datcollate || ',' || datctype FROM pg_database WHERE datname = 'matrix-synapse'\""
).strip()
assert out == "C,C", out
def req(path, host="matrix.mgaction.town", args=""):
return neptun.succeed(
f"curl -sSf -k --resolve {host}:443:127.0.0.1 {args} 'https://{host}{path}'"
)
def status(path, host="matrix.mgaction.town"):
return neptun.succeed(
f"curl -s -k -o /dev/null -w '%{{http_code}}' --resolve {host}:443:127.0.0.1 'https://{host}{path}'"
)
with subtest("apex delegates to matrix.mgaction.town"):
assert json.loads(req("/.well-known/matrix/server", "mgaction.town")) == {
"m.server": "matrix.mgaction.town:443"
}
assert json.loads(req("/.well-known/matrix/client", "mgaction.town")) == {
"m.homeserver": {"base_url": "https://matrix.mgaction.town"}
}
headers = req("/.well-known/matrix/client", "mgaction.town", "-D - -o /dev/null").lower()
assert "access-control-allow-origin: *" in headers, headers
assert status("/", "mgaction.town") == "404"
with subtest("clients are pointed at MAS, SSO only"):
meta = json.loads(req("/_matrix/client/v1/auth_metadata"))
assert meta["issuer"] == "https://account.mgaction.town/", meta
flows = [f["type"] for f in json.loads(req("/_matrix/client/v3/login"))["flows"]]
assert "m.login.sso" in flows and "m.login.password" not in flows, flows
req("/.well-known/openid-configuration", "account.mgaction.town")
with subtest("admin API is not public"):
assert status("/_synapse/admin/v1/server_version") == "404"
jupiter.succeed("curl -sf http://localhost:8008/_synapse/admin/v1/server_version")
with subtest("user registered in MAS can chat"):
jupiter.succeed("sudo -u matrix-authentication-service mas manage register-user alice --yes --display-name Alice")
out = jupiter.succeed("sudo -u matrix-authentication-service mas manage issue-compatibility-token alice 2>&1")
m = re.search(r"mct_\w+", out)
assert m, out
token = m.group(0)
auth = f"-H 'Authorization: Bearer {token}'"
# Provisioning into synapse is an async MAS job.
neptun.wait_until_succeeds(
f"curl -sSf -k --resolve matrix.mgaction.town:443:127.0.0.1 {auth} https://matrix.mgaction.town/_matrix/client/v3/account/whoami | grep -F '@alice:mgaction.town'",
timeout=60,
)
room = json.loads(req("/_matrix/client/v3/createRoom", args=f"{auth} -X POST -d '{{}}'"))["room_id"]
r = quote(room)
req(f"/_matrix/client/v3/rooms/{r}/send/m.room.message/t1",
args=f"""{auth} -X PUT -d '{{"msgtype":"m.text","body":"hello from the vm"}}'""")
msgs = req(f"/_matrix/client/v3/rooms/{r}/messages?dir=b", args=auth)
assert "hello from the vm" in msgs, msgs
with subtest("media upload lands on the array"):
neptun.succeed("head -c 5000000 /dev/urandom > /tmp/blob")
up = json.loads(req("/_matrix/media/v3/upload?filename=blob",
args=f"{auth} -X POST -H 'Content-Type: application/octet-stream' --data-binary @/tmp/blob"))
assert up["content_uri"].startswith("mxc://mgaction.town/"), up
jupiter.succeed("find /mnt/data/AppData/matrix-synapse -type f -size +4M | grep -q .")
with subtest("survives a restart"):
jupiter.succeed("systemctl restart matrix-authentication-service matrix-synapse")
jupiter.wait_for_open_port(8008)
jupiter.wait_for_open_port(8080)
neptun.wait_until_succeeds(
f"curl -sSf -k --resolve matrix.mgaction.town:443:127.0.0.1 {auth} https://matrix.mgaction.town/_matrix/client/v3/account/whoami",
timeout=60,
)
'';
}
+193
View File
@@ -0,0 +1,193 @@
{ config, pkgs, lib, ... }:
# Matrix homeserver for mgaction.town: Synapse + Matrix Authentication Service (MAS),
# both on jupiter's postgres. Published by neptun (services/comms/matrix-proxy.nix).
#
# Login is OIDC only: MAS owns every login and delegates to Authentik (app slug
# `matrix`, client id below). Element X only supports SSO through MAS, not through
# Synapse's own oidc_providers, and adding MAS later is a one-way migration.
# Admin: `sudo -u matrix-authentication-service mas manage promote-admin <user>`.
let
cfg = config.homelab.matrix;
mas = pkgs.matrix-authentication-service;
synapseUser = config.users.users.matrix-synapse.name;
masUser = "matrix-authentication-service";
mediaDir = "/mnt/data/AppData/matrix-synapse";
# Only file paths in here, the config itself is world-readable in the store.
masConfig = (pkgs.formats.yaml { }).generate "mas-config.yaml" {
http = {
public_base = "https://account.mgaction.town/";
# neptun proxies over the tailnet, outside MAS's default private ranges.
trusted_proxies = [ "127.0.0.0/8" "::1/128" "100.64.0.0/10" "fd7a:115c:a1e0::/48" ];
listeners = [{
name = "web";
resources = map (name: { inherit name; })
[ "discovery" "human" "oauth" "compat" "graphql" "assets" ];
binds = [{ address = "[::]:8080"; }];
}];
};
database = {
socket = "/run/postgresql";
username = masUser;
database = masUser;
};
matrix = {
kind = "synapse";
homeserver = "mgaction.town";
endpoint = "http://localhost:8008";
secret_file = cfg.masSecretFile;
};
secrets = {
encryption_file = cfg.masEncryptionFile;
keys = [{ key_file = cfg.masSigningKeyFile; }];
};
passwords.enabled = false;
upstream_oauth2.providers = [{
id = "01M5EYGX82FRWPDQDB8EY79JYW"; # stable, part of the Authentik redirect URI
human_name = "Authentik";
issuer = "https://auth.mgaction.town/application/o/matrix/";
client_id = cfg.oidcClientId;
client_secret_file = cfg.oidcClientSecretFile;
token_endpoint_auth_method = "client_secret_basic";
scope = "openid profile email";
claims_imports = {
# Matrix localparts must be lowercase; fixed at first login.
localpart = { action = "require"; template = "{{ user.preferred_username | lower }}"; };
displayname = { action = "force"; template = "{{ user.name }}"; };
email = { action = "force"; template = "{{ user.email }}"; };
};
# Otherwise "sign out" bounces straight back in through Authentik's session.
additional_authorization_parameters.prompt = "{% if logged_out %}login{% endif %}";
}];
};
masCli = pkgs.writeShellScriptBin "mas" ''
MAS_CONFIG=${masConfig} exec ${mas}/bin/mas-cli "$@"
'';
in
{
options.homelab.matrix = {
signingKeyFile = lib.mkOption { type = lib.types.str; description = "Synapse federation signing key; losing it breaks federation under mgaction.town."; };
macaroonSecretFile = lib.mkOption { type = lib.types.str; };
formSecretFile = lib.mkOption { type = lib.types.str; };
masSecretFile = lib.mkOption { type = lib.types.str; description = "Shared Synapse<->MAS secret, readable by group matrix-synapse."; };
masEncryptionFile = lib.mkOption { type = lib.types.str; description = "64 hex chars; never change after first start."; };
masSigningKeyFile = lib.mkOption { type = lib.types.str; description = "RSA private key (PEM)."; };
oidcClientId = lib.mkOption { type = lib.types.str; default = "matrix"; };
oidcClientSecretFile = lib.mkOption { type = lib.types.str; };
};
config = {
services.matrix-synapse = {
enable = true;
settings = {
server_name = "mgaction.town";
public_baseurl = "https://matrix.mgaction.town/";
listeners = [
{
# Not in allowedTCPPorts: only neptun reaches it, via trusted tailscale0.
port = 8008;
bind_addresses = [ "::" ]; # dual-stack, covers IPv4 too
type = "http";
tls = false;
x_forwarded = true;
resources = [{ names = [ "client" "federation" ]; compress = false; }];
}
{
port = 9001;
bind_addresses = [ "127.0.0.1" ];
type = "metrics";
tls = false;
resources = [ ];
}
];
enable_metrics = true;
report_stats = false;
signing_key_path = cfg.signingKeyFile;
macaroon_secret_key_path = cfg.macaroonSecretFile;
form_secret_path = cfg.formSecretFile;
matrix_authentication_service = {
enabled = true;
endpoint = "http://localhost:8080/";
secret_path = cfg.masSecretFile;
};
password_config.enabled = false; # MAS refuses to start alongside it
media_store_path = mediaDir;
max_upload_size = "100M";
# Federated rooms cache every remote image forever otherwise.
media_retention.remote_media_lifetime = "90d";
# Federation is open, so keep members' profiles to people they share rooms with.
require_auth_for_profile_requests = true;
limit_profile_requests_to_users_who_share_rooms = true;
# No public room directory on a friends & family server.
room_list_publication_rules = [{ action = "deny"; }];
};
};
users.users.matrix-synapse.extraGroups = [ "users" ]; # traverse /mnt/data/AppData
systemd.services.matrix-synapse.unitConfig.RequiresMountsFor = [ mediaDir ];
users.users.${masUser} = {
isSystemUser = true;
group = masUser;
extraGroups = [ synapseUser ]; # read cfg.masSecretFile
};
users.groups.${masUser} = { };
systemd.services.matrix-authentication-service = {
description = "Matrix Authentication Service";
after = [ "postgresql.target" "network-online.target" ];
requires = [ "postgresql.target" ];
wants = [ "network-online.target" ];
wantedBy = [ "multi-user.target" ];
environment.MAS_CONFIG = masConfig;
serviceConfig = {
User = masUser;
Group = masUser;
ExecStart = "${mas}/bin/mas-cli server";
Restart = "on-failure";
NoNewPrivileges = true;
PrivateTmp = true;
PrivateDevices = true;
ProtectSystem = "strict";
ProtectHome = true;
ProtectKernelTunables = true;
ProtectKernelModules = true;
ProtectControlGroups = true;
RestrictAddressFamilies = [ "AF_INET" "AF_INET6" "AF_UNIX" ];
};
};
environment.systemPackages = [ masCli ];
# Synapse requires C collation, so its DB can't come from ensureDatabases (cluster
# default locale); immich's existing cluster also means initialScript never runs.
services.postgresql = {
enable = true;
ensureDatabases = [ masUser ];
ensureUsers = [
{ name = synapseUser; }
{ name = masUser; ensureDBOwnership = true; }
];
};
systemd.services.postgresql-setup.serviceConfig.ExecStartPost =
let
sqlFile = pkgs.writeText "matrix-synapse-db.sql" ''
SELECT format('CREATE DATABASE %I OWNER %I TEMPLATE template0 ENCODING %L LC_COLLATE %L LC_CTYPE %L',
'${synapseUser}', '${synapseUser}', 'UTF8', 'C', 'C')
WHERE NOT EXISTS (SELECT FROM pg_database WHERE datname = '${synapseUser}') \gexec
'';
in
[ "${lib.getExe' config.services.postgresql.package "psql"} -d postgres -f ${sqlFile}" ];
# A missing ReadWritePaths entry fails synapse's namespace setup. One level only:
# tmpfiles skips anything nested under a dir owned by another non-root user
# ("unsafe path transition"), and AppData is darman's.
systemd.tmpfiles.rules = [ "d ${mediaDir} 0700 matrix-synapse matrix-synapse -" ];
};
}
+11
View File
@@ -67,6 +67,17 @@
}
];
}
{
# services/comms/matrix.nix's metrics listener.
job_name = "synapse";
metrics_path = "/_synapse/metrics";
static_configs = [
{
targets = [ "127.0.0.1:9001" ];
labels.host = "jupiter";
}
];
}
];
};
};