diff --git a/CLAUDE.md b/CLAUDE.md index 5677b2e..a1f9a1f 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -13,7 +13,7 @@ Pi 3B+ DNS/DHCP, aarch64), **terra** (desktop), **titan** (Zenbook Pro Duo lapto flake.nix # nixosConfigurations: real hosts + test/util targets common.nix # shared base: user darman (key-only ssh), nix settings, firewall :22, tz services//*.nix # one reusable NixOS module per service, grouped by category - # (media, network, vpn, identity, dev, desktop); each opens + # (media, network, vpn, identity, dev, desktop, comms); each opens # ITS OWN firewall ports. services/containers.nix (podman # backend) stays at the top level, shared across categories. hosts// # configuration.nix + disk-config.nix (disko) + hardware-configuration.nix + secrets.nix @@ -98,6 +98,9 @@ nix build .#checks.x86_64-linux.pass-broker -L # two-node VM test of the gateway on mars with a broker using it: labels, # cancel, expiry, per-host token isolation, gateway down nix build .#checks.x86_64-linux.pass-broker-gateway -L +# Synapse + MAS on jupiter behind neptun's caddy: C collation, delegation, +# SSO-only login flows, admin API not public, chat + media upload, restart +nix build .#checks.x86_64-linux.matrix -L ``` `checks.kexec-local` is the only way to exercise `kexec-local` at all: it jumps the @@ -273,3 +276,14 @@ kept its ssh host key. Run it after ANY change to the kexec paths. `hyprctl dispatch 'hl.dsp.exit()'`. Test the flow without touching the real greetd by running greetd's `fakegreet "qs -p …/greeter.qml"` inside a nested Hyprland (user `user`, password `password`, then answer `9`). +- **Matrix logins go through MAS, not Synapse's `oidc_providers`** (`services/comms/matrix.nix`). + Element X only supports SSO via Matrix Authentication Service, and moving an existing + Synapse onto MAS is a one-way `syn2mas` migration — so it was there from day one. With + MAS enabled Synapse refuses to start if password login, registration or any SSO + section is on. Admin rights come from MAS: + `sudo -u matrix-authentication-service mas manage promote-admin `. +- **Synapse's database needs `LC_COLLATE`/`LC_CTYPE` = C**, which `ensureDatabases` + can't give (cluster default locale) and `initialScript` never runs on jupiter's + existing immich cluster — hence the `\gexec` CREATE DATABASE in `postgresql-setup`'s + `ExecStartPost`. Synapse's signing key is a sops secret, not generated on first + start: it IS the server's federation identity, and the array it would live on is RAID0. diff --git a/flake.nix b/flake.nix index a16b549..8788c0e 100644 --- a/flake.nix +++ b/flake.nix @@ -586,6 +586,12 @@ qbittorrent-vpn = import ./services/desktop/qbittorrent-vpn-test.nix { pkgs = nixpkgs.legacyPackages.${system}; }; + + # Synapse + MAS on jupiter behind neptun's caddy vhosts: + # nix build .#checks.x86_64-linux.matrix -L + matrix = import ./services/comms/matrix-test.nix { + pkgs = nixpkgs.legacyPackages.${system}; + }; }; # `nix develop` — hot-reload loop for dotfiles/quickshell. diff --git a/hosts/jupiter/configuration.nix b/hosts/jupiter/configuration.nix index b329de0..0caedbd 100644 --- a/hosts/jupiter/configuration.nix +++ b/hosts/jupiter/configuration.nix @@ -26,6 +26,7 @@ ../../services/dev/gitea.nix ../../services/dev/gitea-mcp.nix ../../services/dev/obsidian-livesync.nix + ../../services/comms/matrix.nix ]; # sabnzbd's unrar dependency is unfree; scope the allowance to just that diff --git a/hosts/jupiter/secrets.nix b/hosts/jupiter/secrets.nix index 5c1d19f..ff1fc0c 100644 --- a/hosts/jupiter/secrets.nix +++ b/hosts/jupiter/secrets.nix @@ -55,6 +55,28 @@ sops.secrets.sabnzbd_eweka_username.owner = "sabnzbd"; sops.secrets.sabnzbd_eweka_password.owner = "sabnzbd"; + # Matrix (services/comms/matrix.nix). The synapse signing key lives here rather than + # being generated on first start, so losing the RAID0 array doesn't lose the + # server's federation identity. mas_shared_secret is read by synapse and MAS + # (MAS is in group matrix-synapse). matrix_oidc_client_secret is the Authentik + # `matrix` provider's. + sops.secrets.synapse_signing_key.owner = "matrix-synapse"; + sops.secrets.synapse_macaroon_secret_key.owner = "matrix-synapse"; + sops.secrets.synapse_form_secret.owner = "matrix-synapse"; + sops.secrets.mas_shared_secret = { owner = "matrix-synapse"; group = "matrix-synapse"; mode = "0440"; }; + sops.secrets.mas_encryption_secret.owner = "matrix-authentication-service"; + sops.secrets.mas_signing_key.owner = "matrix-authentication-service"; + sops.secrets.matrix_oidc_client_secret.owner = "matrix-authentication-service"; + homelab.matrix = { + signingKeyFile = config.sops.secrets.synapse_signing_key.path; + macaroonSecretFile = config.sops.secrets.synapse_macaroon_secret_key.path; + formSecretFile = config.sops.secrets.synapse_form_secret.path; + masSecretFile = config.sops.secrets.mas_shared_secret.path; + masEncryptionFile = config.sops.secrets.mas_encryption_secret.path; + masSigningKeyFile = config.sops.secrets.mas_signing_key.path; + oidcClientSecretFile = config.sops.secrets.matrix_oidc_client_secret.path; + }; + # CouchDB admin account for Obsidian LiveSync — rendered into an [admins] ini # fragment instead of services.couchdb.adminPass, which would put the plaintext in # the world-readable store. diff --git a/hosts/neptun/configuration.nix b/hosts/neptun/configuration.nix index a29223a..341d05c 100644 --- a/hosts/neptun/configuration.nix +++ b/hosts/neptun/configuration.nix @@ -13,6 +13,7 @@ ../../services/identity/authentik.nix ../../services/vpn/headscale.nix ../../services/vpn/headplane.nix + ../../services/comms/matrix-proxy.nix # mgaction.town delegation, matrix. + account. vhosts ]; # ---- Boot (UEFI) ---- diff --git a/secrets/jupiter.yaml b/secrets/jupiter.yaml index 66561eb..3e991e9 100644 --- a/secrets/jupiter.yaml +++ b/secrets/jupiter.yaml @@ -17,6 +17,13 @@ sabnzbd_eweka_password: ENC[AES256_GCM,data:Mt3ZHAe2wzacCQq3x9Uy8WxjrVNad1SmU6sl gitea_hermes_webhook_secret: ENC[AES256_GCM,data:Q8e+mj05MJI7CEJwRonpOmQphAZ0CfnZFoGxrDSSiyHoH3BNhqBU5gBmzuu+6NK9OS33kN+JnFvrwCeEzVxooA==,iv:mdsKOMD5B0Jzh1YRmRh71P8Io9RFtI6aqAky5x+WxOQ=,tag:v3LKz5a8ayl7WAzIPbwj6Q==,type:str] couchdb_admin_password: ENC[AES256_GCM,data:QHkCFUwLbQdd5yKETI5qAz4CkEfsPcl2iCU8F9mX3PA=,iv:2dPNKjjoXEgm7wfC6MlhQTMvAXSNDtaXnjWl2ldl4fk=,tag:1ZltqH94Q5/6GbXAnaIgdg==,type:str] pass_broker_pat: ENC[AES256_GCM,data:4rO5srDthuvDObbhVkHCCm5vblkwbIDtT3c/Eg0KxiVavfoQbmK2RBPOylBLR3v/9wcXEkdQzk8f0N98SkaSr9dB4oe8CbZc91aA9zgCnI8qR7coWCmBJin/wYjcVhX91sxg9Xz81sBWZK3PtrgELFo=,iv:znQrJzdWcsBtK4Rnz/vUIQcU8M3anRm1UGlCJQ/t35U=,tag:fHZLaMm3a3Y/4kf/KEL12A==,type:str] +synapse_signing_key: ENC[AES256_GCM,data:EiccDw3y+gjoCNCYjtrUNd7xkjcqm1IOSDnWfAerA7HI9O0EBJaBkra9Yu9SdrImNT9a/UqHENLMbw==,iv:7G/F9MZKU8BPGP2o+rrnEBvpCy3wor3HkgEjR5gQ0Nw=,tag:uUZfJb3MzMmug8Sv+lNaDA==,type:str] +synapse_macaroon_secret_key: ENC[AES256_GCM,data:wqARXZK555HvnC3oA0zvHiWqT0RoFVqAgR2rfN1JCwBvuUVCfIgGSCv26UwWl8f9CTLvoGylS8TR4zqrNpgFNA==,iv:IwnkRJwvuLY5bhlCJwWPPN1RobEu15TUEiBJ42Ursic=,tag:3GOYYImBpbVR1ux4IRP31w==,type:str] +synapse_form_secret: ENC[AES256_GCM,data:cHFQco6+akxN/OcSxJPeILY25BIVIy7q9rVj1PFmdhoFoczfmcu0l97zO69PsgtPTeH9e09onepyNMFTn9R+Nw==,iv:fCy43xQFP2GlbwOQpvcv90FfBPUWuOIjkF5oD/wDqD8=,tag:JMopCPNzAAjuEKlQDoEUCA==,type:str] +mas_shared_secret: ENC[AES256_GCM,data:Jb5ueYVtm/hcr43jtyIBGLcmIxBks4WfYLX60ig0emssqMegQjE4kjtC2w556mJPRfkBesSO4J6xl6Egho8KhQ==,iv:8Ajn0tOWK3fofDspZHj13yAJTBneHojkuXnQjdEEg+0=,tag:T2gemWQnNu2GyiOlWO5Piw==,type:str] +mas_encryption_secret: ENC[AES256_GCM,data:v0Xjjtyhkdj3qI0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x8qj2gmv/WSGFfD0QDBhnJRbssCqpzlOdyBm/ptfwv24BbhN1nDgGCeA6RseKwk1SJksSGhhM5MuToX3P3z3o0dTV37v8J0pC8co22QJm2k9afxgFESm3lf03zUI59Ldm0jCj2lXrAfr2X10m6SJ6GyYlbv36OFEz/LsWcj5Ht8XU4K1t/lKJiI/Q/GEyElKrW8PxUmgn5n4hayKUSJGVXTjoIeAnI=,iv:HWZabaltKmKqdPoPfBYnwzl+HEAtTL5mMvSKBYJrXNo=,tag:iWcmWHBX+yxa+Q1gC1KgEg==,type:str] +matrix_oidc_client_secret: ENC[AES256_GCM,data:0f6Am5JQJCvG8dw3f15/qMl+iu6O2DXqMMD0SNOlvwRm/V/dGvjUsHyPWIkAYR2qOSP1r8/1sfnyoP3PUJheEnd0Ek4v1ocalDCsj68HIBkz4efq2NM5GswSbf0czMacbU2XGAHPGMk7XFzHIYoysWCkAoUEWROsnnLLV8ZNnng=,iv:mC5f1dLIPLz4oNr+AlUzpvAAF5ju5itHoix2CmsiiE0=,tag:fA/3a/IEtHhOMrurEjj5ew==,type:str] sops: age: - enc: | @@ -37,7 +44,7 @@ sops: CzjSDQZTcseEXZNwuzZcfB5Mvq0BQvjOj7lGuxzuE4qwWkdJWGfVLQ== -----END AGE ENCRYPTED FILE----- recipient: age1zak7glavmg4026p2389fyqe769vqm4jrryknuqckgqq4merz5f7q44rkkt - lastmodified: "2026-10-18T14:38:08Z" - mac: ENC[AES256_GCM,data:nMh+Wk3KR5tz152aELRpA4yBhY2xIGf5EOMIn7Zakg7en3Uk3LIUyK88uz4MqBhGBU1gRjPcM1OqbTnotFK+kDf9ZL6FtJJY2NX0A8B/X60R+I1Afx/KXeDF/wGUqmVkMC0aVaUhyp2FdN5MT9YoHe256ZT/Bjurrvylf54nxNY=,iv:Pu7SUboIGGcpVluF3qSc8JUIv2Y8XwDJZh5mEfw24/w=,tag:I/uoYTHal1YYPwco6mBqZQ==,type:str] + lastmodified: "2026-10-21T10:29:28Z" + mac: ENC[AES256_GCM,data:oUJMbaDKpYsnOCDUQSAUTyJZo7lOj4QYzvTW3LaEk5/jfdJXjRpsM0Euxup8EP+wH3hOeymVmnOjm4U0s9kCok1NNeWu16ElbdRP6lz6OtAP9tbDCvb1Lj1JChg53tOQmyoO5LS2UwxjteSS0FWiqfJek8nsKwgpRc+4XwjSnyo=,iv:j/YBxILwRVQiMHjEl+vbOma/QRxmH7M+S54qVKqK0xo=,tag:z0r6fzQ7uYwT4f79D4AcLA==,type:str] unencrypted_suffix: _unencrypted version: 3.13.3 diff --git a/services/comms/matrix-proxy.nix b/services/comms/matrix-proxy.nix new file mode 100644 index 0000000..6dd7317 --- /dev/null +++ b/services/comms/matrix-proxy.nix @@ -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 + ''; + }; +} diff --git a/services/comms/matrix-test.nix b/services/comms/matrix-test.nix new file mode 100644 index 0000000..47e94ce --- /dev/null +++ b/services/comms/matrix-test.nix @@ -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, + ) + ''; +} diff --git a/services/comms/matrix.nix b/services/comms/matrix.nix new file mode 100644 index 0000000..fcbedb2 --- /dev/null +++ b/services/comms/matrix.nix @@ -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 `. +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 -" ]; + }; +} diff --git a/services/monitoring/victoriametrics.nix b/services/monitoring/victoriametrics.nix index 1c95820..1778c33 100644 --- a/services/monitoring/victoriametrics.nix +++ b/services/monitoring/victoriametrics.nix @@ -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"; + } + ]; + } ]; }; };