blob: 967e9d9bfee87badd1b06068023a0381078af3ab [file] [log] [blame]
Skyler Greya78aa672023-05-20 13:48:18 +02001{ base, config, lib, pkgs, ... }:
Skyler Greya78aa672023-05-20 13:48:18 +02002{
Skyler Grey8e32c832023-05-20 22:54:30 +02003 services.matrix-synapse = {
Skyler Greya78aa672023-05-20 13:48:18 +02004 enable = true;
Skyler Grey8e32c832023-05-20 22:54:30 +02005 withJemalloc = true;
Skyler Greya78aa672023-05-20 13:48:18 +02006
Skyler Grey8e32c832023-05-20 22:54:30 +02007 settings = {
8 server_name = "coded.codes";
9 enable_registration = true;
10 registration_requires_token = true;
11 registration_shared_secret = "!!registration_shared_secret!!";
12 public_baseurl = "https://matrix-backend.coded.codes/";
13 max_upload_size = "100M";
14 listeners = [{
15 x_forwarded = true;
16 tls = false;
17 resources = [{
18 names = [
19 "client"
20 "federation"
21 ];
22 compress = true;
23 }];
24 port = 4527;
25 }];
26 enable_metrics = true;
27 database.args.database = "synapse";
Skyler Greya78aa672023-05-20 13:48:18 +020028 };
29 };
30
Skyler Grey8e32c832023-05-20 22:54:30 +020031 sops.secrets = {
32 registration_shared_secret = {
Skyler Greya78aa672023-05-20 13:48:18 +020033 mode = "0400";
Skyler Grey8e32c832023-05-20 22:54:30 +020034 owner = config.users.users.root.name;
35 group = config.users.users.nobody.group;
36 sopsFile = ../secrets/matrix.json;
37 format = "json";
38 };
39 matrix_private_key = {
40 mode = "0600";
41 owner = config.users.users.matrix-synapse.name;
42 group = config.users.users.matrix-synapse.group;
Skyler Greya78aa672023-05-20 13:48:18 +020043 sopsFile = ../secrets/matrix_private_key.pem;
44 format = "binary";
Skyler Grey8e32c832023-05-20 22:54:30 +020045 path = config.services.matrix-synapse.settings.signing_key_path;
Skyler Greya78aa672023-05-20 13:48:18 +020046 };
47 };
48} // (
49 let
50 isDerived = base != null;
51 in
52 if isDerived
53 # We cannot use mkIf as both sides are evaluated no matter the condition value
54 # Given we use base as an attrset, mkIf will error if base is null in here
55 then
56 let
Skyler Grey8e32c832023-05-20 22:54:30 +020057 synapse_cfgfile = config.services.matrix-synapse.configFile;
Skyler Greya78aa672023-05-20 13:48:18 +020058 in
59 {
Skyler Grey8e32c832023-05-20 22:54:30 +020060 scalpel.trafos."synapse.yaml" = {
61 source = toString synapse_cfgfile;
Skyler Greya78aa672023-05-20 13:48:18 +020062 matchers."registration_shared_secret".secret =
63 config.sops.secrets.registration_shared_secret.path;
Skyler Grey8e32c832023-05-20 22:54:30 +020064 owner = config.users.users.matrix-synapse.name;
65 group = config.users.users.matrix-synapse.group;
Skyler Greya78aa672023-05-20 13:48:18 +020066 mode = "0400";
67 };
68
Skyler Grey8e32c832023-05-20 22:54:30 +020069 systemd.services.matrix-synapse.serviceConfig.ExecStart = lib.mkForce (
Skyler Greya78aa672023-05-20 13:48:18 +020070 builtins.replaceStrings
Skyler Grey8e32c832023-05-20 22:54:30 +020071 [ "${synapse_cfgfile}" ]
72 [ "${config.scalpel.trafos."synapse.yaml".destination}" ]
73 "${base.config.systemd.services.matrix-synapse.serviceConfig.ExecStart}"
Skyler Greya78aa672023-05-20 13:48:18 +020074 );
Skyler Grey8e32c832023-05-20 22:54:30 +020075
76 systemd.services.matrix-synapse.preStart = lib.mkForce (
77 builtins.replaceStrings
78 [ "${synapse_cfgfile}" ]
79 [ "${config.scalpel.trafos."synapse.yaml".destination}" ]
80 "${base.config.systemd.services.matrix-synapse.preStart}"
81 );
82
83 environment.systemPackages =
84 with lib; let
85 cfg = config.services.matrix-synapse;
86 registerNewMatrixUser =
87 let
88 isIpv6 = x: lib.length (lib.splitString ":" x) > 1;
89 listener =
90 lib.findFirst
91 (
92 listener: lib.any
93 (
94 resource: lib.any
95 (
96 name: name == "client"
97 )
98 resource.names
99 )
100 listener.resources
101 )
102 (lib.last cfg.settings.listeners)
103 cfg.settings.listeners;
104 # FIXME: Handle cases with missing client listener properly,
105 # don't rely on lib.last, this will not work.
106
107 # add a tail, so that without any bind_addresses we still have a useable address
108 bindAddress = head (listener.bind_addresses ++ [ "127.0.0.1" ]);
109 listenerProtocol =
110 if listener.tls
111 then "https"
112 else "http";
113 in
114 pkgs.writeShellScriptBin "matrix-synapse-register_new_matrix_user" ''
115 exec ${cfg.package}/bin/register_new_matrix_user \
116 $@ \
117 ${lib.concatMapStringsSep " " (x: "-c ${x}") ([
118 config.scalpel.trafos."synapse.yaml".destination ] ++ cfg.extraConfigFiles)} \
119 "${listenerProtocol}://${
120 if (isIpv6 bindAddress) then
121 "[${bindAddress}]"
122 else
123 "${bindAddress}"
124 }:${builtins.toString listener.port}/"
125 '';
126 in
127 [ (lib.meta.hiPrio registerNewMatrixUser) ];
Skyler Greya78aa672023-05-20 13:48:18 +0200128 }
129 else { }
130)