1 import ../make-test-python.nix ({ pkgs, ... } : let
4 runWithOpenSSL = file: cmd: pkgs.runCommand file {
5 buildInputs = [ pkgs.openssl ];
9 ca_key = runWithOpenSSL "ca-key.pem" "openssl genrsa -out $out 2048";
10 ca_pem = runWithOpenSSL "ca.pem" ''
12 -x509 -new -nodes -key ${ca_key} \
13 -days 10000 -out $out -subj "/CN=snakeoil-ca"
15 key = runWithOpenSSL "matrix_key.pem" "openssl genrsa -out $out 2048";
16 csr = runWithOpenSSL "matrix.csr" ''
19 -out $out -subj "/CN=localhost" \
21 cert = runWithOpenSSL "matrix_cert.pem" ''
24 -CA ${ca_pem} -CAkey ${ca_key} \
25 -CAcreateserial -out $out \
30 mailerCerts = import ../common/acme/server/snakeoil-certs.nix;
31 mailerDomain = mailerCerts.domain;
32 registrationSharedSecret = "unsecure123";
34 testPassword = "alicealice";
35 testEmail = "alice@example.com";
61 name = "matrix-synapse";
62 meta = with pkgs.lib; {
63 maintainers = teams.matrix.members;
67 # Since 0.33.0, matrix-synapse doesn't allow underscores in server names
68 serverpostgres = { pkgs, nodes, ... }: let
69 mailserverIP = nodes.mailserver.config.networking.primaryIPAddress;
72 services.matrix-synapse = {
78 args.password = "synapse";
80 tls_certificate_path = "${cert}";
81 tls_private_key_path = "${key}";
82 registration_shared_secret = registrationSharedSecret;
83 public_baseurl = "https://example.com";
85 smtp_host = mailerDomain;
87 require_transport_security = true;
88 notif_from = "matrix <matrix@${mailerDomain}>";
93 services.postgresql = {
96 # The database name and user are configured by the following options:
97 # - services.matrix-synapse.database_name
98 # - services.matrix-synapse.database_user
100 # The values used here represent the default values of the module.
101 initialScript = pkgs.writeText "synapse-init.sql" ''
102 CREATE ROLE "matrix-synapse" WITH LOGIN PASSWORD 'synapse';
103 CREATE DATABASE "matrix-synapse" WITH OWNER "matrix-synapse"
110 networking.extraHosts = ''
111 ${mailserverIP} ${mailerDomain}
114 security.pki.certificateFiles = [
115 mailerCerts.ca.cert ca_pem
118 environment.systemPackages = let
119 sendTestMailStarttls = pkgs.writeScriptBin "send-testmail-starttls" ''
120 #!${pkgs.python3.interpreter}
124 ctx = ssl.create_default_context()
126 with smtplib.SMTP('${mailerDomain}') as smtp:
128 smtp.starttls(context=ctx)
130 smtp.sendmail('matrix@${mailerDomain}', '${testEmail}', 'Subject: Test STARTTLS\n\nTest data.')
134 obtainTokenAndRegisterEmail = let
135 # adding the email through the API is quite complicated as it involves more than one step and some
136 # client-side calculation
137 insertEmailForAlice = pkgs.writeText "alice-email.sql" ''
138 INSERT INTO user_threepids (user_id, medium, address, validated_at, added_at) VALUES ('${testUser}@serverpostgres', 'email', '${testEmail}', '1629149927271', '1629149927270');
141 pkgs.writeScriptBin "obtain-token-and-register-email" ''
142 #!${pkgs.runtimeShell}
146 su postgres -c "psql -d matrix-synapse -f ${insertEmailForAlice}"
147 curl --fail -XPOST 'https://localhost:8448/_matrix/client/r0/account/password/email/requestToken' -d '{"email":"${testEmail}","client_secret":"foobar","send_attempt":1}' -v
149 in [ sendTestMailStarttls pkgs.matrix-synapse obtainTokenAndRegisterEmail ];
153 mailserver = args: let
156 security.pki.certificateFiles = [
160 networking.firewall.enable = false;
164 hostname = "${mailerDomain}";
165 # open relay for subnet
166 networksStyle = "subnet";
167 enableSubmission = true;
168 tlsTrustedAuthorities = "${mailerCerts.ca.cert}";
169 sslCert = "${mailerCerts.${mailerDomain}.cert}";
170 sslKey = "${mailerCerts.${mailerDomain}.key}";
172 # blackhole transport
173 transport = "example.com discard:silently";
176 debug_peer_level = "10";
177 smtpd_relay_restrictions = [
178 "permit_mynetworks" "reject_unauth_destination"
181 # disable obsolete protocols, something old versions of twisted are still using
182 smtpd_tls_protocols = "TLSv1.3, TLSv1.2, !TLSv1.1, !TLSv1, !SSLv2, !SSLv3";
183 smtp_tls_protocols = "TLSv1.3, TLSv1.2, !TLSv1.1, !TLSv1, !SSLv2, !SSLv3";
184 smtpd_tls_mandatory_protocols = "TLSv1.3, TLSv1.2, !TLSv1.1, !TLSv1, !SSLv2, !SSLv3";
185 smtp_tls_mandatory_protocols = "TLSv1.3, TLSv1.2, !TLSv1.1, !TLSv1, !SSLv2, !SSLv3";
190 serversqlite = args: {
191 services.matrix-synapse = {
195 database.name = "sqlite3";
196 tls_certificate_path = "${cert}";
197 tls_private_key_path = "${key}";
205 mailserver.wait_for_unit("postfix.service")
206 serverpostgres.succeed("send-testmail-starttls")
207 serverpostgres.wait_for_unit("matrix-synapse.service")
208 serverpostgres.wait_until_succeeds(
209 "curl --fail -L --cacert ${ca_pem} https://localhost:8448/"
211 serverpostgres.require_unit_state("postgresql.service")
212 serverpostgres.succeed("register_new_matrix_user -u ${testUser} -p ${testPassword} -a -k ${registrationSharedSecret} https://localhost:8448/")
213 serverpostgres.succeed("obtain-token-and-register-email")
214 serversqlite.wait_for_unit("matrix-synapse.service")
215 serversqlite.wait_until_succeeds(
216 "curl --fail -L --cacert ${ca_pem} https://localhost:8448/"
218 serversqlite.succeed("[ -e /var/lib/matrix-synapse/homeserver.db ]")