2019-03-12 10:01:36 +00:00
<chapter xmlns= "http://docbook.org/ns/docbook"
xmlns:xlink="http://www.w3.org/1999/xlink"
xmlns:xi="http://www.w3.org/2001/XInclude"
version="5.0"
xml:id="module-services-matrix">
<title > Matrix</title>
<para >
2019-09-19 18:17:30 +01:00
<link xlink:href= "https://matrix.org/" > Matrix</link> is an open standard for
interoperable, decentralised, real-time communication over IP. It can be used
to power Instant Messaging, VoIP/WebRTC signalling, Internet of Things
communication - or anywhere you need a standard HTTP API for publishing and
subscribing to data whilst tracking the conversation history.
2019-03-12 10:01:36 +00:00
</para>
<para >
2019-09-19 18:17:30 +01:00
This chapter will show you how to set up your own, self-hosted Matrix
homeserver using the Synapse reference homeserver, and how to serve your own
2020-07-16 22:59:16 +01:00
copy of the Element web client. See the
2019-09-19 18:17:30 +01:00
<link xlink:href= "https://matrix.org/docs/projects/try-matrix-now.html" > Try
2020-07-16 22:59:16 +01:00
Matrix Now!</link> overview page for links to Element Apps for Android and iOS,
2019-09-19 18:17:30 +01:00
desktop clients, as well as bridges to other networks and other projects
around Matrix.
2019-03-12 10:01:36 +00:00
</para>
<section xml:id= "module-services-matrix-synapse" >
<title > Synapse Homeserver</title>
2019-03-22 13:44:11 +00:00
2019-03-12 10:01:36 +00:00
<para >
2019-09-19 18:17:30 +01:00
<link xlink:href= "https://github.com/matrix-org/synapse" > Synapse</link> is
the reference homeserver implementation of Matrix from the core development
team at matrix.org. The following configuration example will set up a
synapse server for the <literal > example.org</literal> domain, served from
the host <literal > myhostname.example.org</literal> . For more information,
please refer to the
<link xlink:href= "https://github.com/matrix-org/synapse#synapse-installation" >
installation instructions of Synapse </link> .
2019-03-22 13:44:11 +00:00
<programlisting >
2021-04-16 17:13:25 +01:00
{ pkgs, lib, ... }:
2019-06-17 11:01:51 +01:00
let
fqdn =
2019-03-12 10:01:36 +00:00
let
2021-04-16 17:13:25 +01:00
join = hostName: domain: hostName + lib.optionalString (domain != null) ".${domain}";
2019-06-17 11:01:51 +01:00
in join config.networking.hostName config.networking.domain;
in {
networking = {
2020-03-15 15:58:50 +00:00
<link linkend= "opt-networking.hostName" > hostName</link> = "myhostname";
<link linkend= "opt-networking.domain" > domain</link> = "example.org";
2019-06-17 11:01:51 +01:00
};
2020-03-15 15:58:50 +00:00
<link linkend= "opt-networking.firewall.allowedTCPPorts" > networking.firewall.allowedTCPPorts</link> = [ 80 443 ];
<link linkend= "opt-services.postgresql.enable" > services.postgresql.enable</link> = true;
2020-04-16 10:37:36 +01:00
<link linkend= "opt-services.postgresql.initialScript" > services.postgresql.initialScript</link> = pkgs.writeText "synapse-init.sql" ''
2020-03-15 15:58:50 +00:00
CREATE ROLE "matrix-synapse" WITH LOGIN PASSWORD 'synapse';
CREATE DATABASE "matrix-synapse" WITH OWNER "matrix-synapse"
TEMPLATE template0
LC_COLLATE = "C"
LC_CTYPE = "C";
'';
2019-03-12 10:01:36 +00:00
2019-06-17 11:01:51 +01:00
services.nginx = {
2020-03-15 15:58:50 +00:00
<link linkend= "opt-services.nginx.enable" > enable</link> = true;
2019-06-17 11:01:51 +01:00
# only recommendedProxySettings and recommendedGzipSettings are strictly required,
# but the rest make sense as well
2020-03-15 15:58:50 +00:00
<link linkend= "opt-services.nginx.recommendedTlsSettings" > recommendedTlsSettings</link> = true;
<link linkend= "opt-services.nginx.recommendedOptimisation" > recommendedOptimisation</link> = true;
<link linkend= "opt-services.nginx.recommendedGzipSettings" > recommendedGzipSettings</link> = true;
<link linkend= "opt-services.nginx.recommendedProxySettings" > recommendedProxySettings</link> = true;
2019-03-12 10:01:36 +00:00
2020-03-15 15:58:50 +00:00
<link linkend= "opt-services.nginx.virtualHosts" > virtualHosts</link> = {
2019-06-17 11:01:51 +01:00
# This host section can be placed on a different host than the rest,
# i.e. to delegate from the host being accessible as ${config.networking.domain}
# to another host actually running the Matrix homeserver.
"${config.networking.domain}" = {
2021-01-13 21:20:17 +00:00
<link linkend= "opt-services.nginx.virtualHosts._name_.enableACME" > enableACME</link> = true;
<link linkend= "opt-services.nginx.virtualHosts._name_.forceSSL" > forceSSL</link> = true;
2020-03-15 15:58:50 +00:00
<link linkend= "opt-services.nginx.virtualHosts._name_.locations._name_.extraConfig" > locations."= /.well-known/matrix/server".extraConfig</link> =
2019-06-17 11:01:51 +01:00
let
# use 443 instead of the default 8448 port to unite
# the client-server and server-server port for simplicity
server = { "m.server" = "${fqdn}:443"; };
in ''
add_header Content-Type application/json;
return 200 '${builtins.toJSON server}';
'';
2020-03-15 15:58:50 +00:00
<link linkend= "opt-services.nginx.virtualHosts._name_.locations._name_.extraConfig" > locations."= /.well-known/matrix/client".extraConfig</link> =
2019-06-17 11:01:51 +01:00
let
client = {
"m.homeserver" = { "base_url" = "https://${fqdn}"; };
"m.identity_server" = { "base_url" = "https://vector.im"; };
};
2020-07-16 22:59:16 +01:00
# ACAO required to allow element-web on any URL to request this json file
2019-06-17 11:01:51 +01:00
in ''
add_header Content-Type application/json;
add_header Access-Control-Allow-Origin *;
return 200 '${builtins.toJSON client}';
'';
};
2019-03-12 10:01:36 +00:00
2019-06-17 11:01:51 +01:00
# Reverse proxy for Matrix client-server and server-server communication
${fqdn} = {
2020-03-15 15:58:50 +00:00
<link linkend= "opt-services.nginx.virtualHosts._name_.enableACME" > enableACME</link> = true;
<link linkend= "opt-services.nginx.virtualHosts._name_.forceSSL" > forceSSL</link> = true;
2019-03-12 10:01:36 +00:00
2019-06-17 11:01:51 +01:00
# Or do a redirect instead of the 404, or whatever is appropriate for you.
2020-07-16 22:59:16 +01:00
# But do not put a Matrix Web client here! See the Element web section below.
2020-03-15 15:58:50 +00:00
<link linkend= "opt-services.nginx.virtualHosts._name_.locations._name_.extraConfig" > locations."/".extraConfig</link> = ''
2019-06-17 11:01:51 +01:00
return 404;
'';
2019-03-12 10:01:36 +00:00
2019-06-17 11:01:51 +01:00
# forward all Matrix API calls to the synapse Matrix homeserver
locations."/_matrix" = {
2020-03-15 15:58:50 +00:00
<link linkend= "opt-services.nginx.virtualHosts._name_.locations._name_.proxyPass" > proxyPass</link> = "http://[::1]:8008"; # without a trailing /
2019-03-12 10:01:36 +00:00
};
};
};
2019-06-17 11:01:51 +01:00
};
services.matrix-synapse = {
2020-03-15 15:58:50 +00:00
<link linkend= "opt-services.matrix-synapse.enable" > enable</link> = true;
2022-02-08 14:03:37 +00:00
<link linkend= "opt-services.matrix-synapse.settings.server_name" > server_name</link> = config.networking.domain;
<link linkend= "opt-services.matrix-synapse.settings.listeners" > listeners</link> = [
2019-06-17 11:01:51 +01:00
{
2022-02-08 14:03:37 +00:00
<link linkend= "opt-services.matrix-synapse.settings.listeners._.port" > port</link> = 8008;
2022-03-12 15:33:46 +00:00
<link linkend= "opt-services.matrix-synapse.settings.listeners._.bind_addresses" > bind_addresses</link> = [ "::1" ];
2022-02-08 14:03:37 +00:00
<link linkend= "opt-services.matrix-synapse.settings.listeners._.type" > type</link> = "http";
<link linkend= "opt-services.matrix-synapse.settings.listeners._.tls" > tls</link> = false;
<link linkend= "opt-services.matrix-synapse.settings.listeners._.x_forwarded" > x_forwarded</link> = true;
<link linkend= "opt-services.matrix-synapse.settings.listeners._.resources" > resources</link> = [ {
<link linkend= "opt-services.matrix-synapse.settings.listeners._.resources._.names" > names</link> = [ "client" ];
<link linkend= "opt-services.matrix-synapse.settings.listeners._.resources._.compress" > compress</link> = true;
} {
<link linkend= "opt-services.matrix-synapse.settings.listeners._.resources._.names" > names</link> = [ "federation" ];
<link linkend= "opt-services.matrix-synapse.settings.listeners._.resources._.compress" > compress</link> = false;
} ];
2019-06-17 11:01:51 +01:00
}
];
};
2021-04-16 17:13:25 +01:00
}
2019-06-17 11:01:51 +01:00
</programlisting>
2019-03-12 10:01:36 +00:00
</para>
2019-03-22 13:44:11 +00:00
2019-03-12 10:01:36 +00:00
<para >
2019-09-19 18:17:30 +01:00
If the <code > A</code> and <code > AAAA</code> DNS records on
<literal > example.org</literal> do not point on the same host as the records
for <code > myhostname.example.org</code> , you can easily move the
<code > /.well-known</code> virtualHost section of the code to the host that
is serving <literal > example.org</literal> , while the rest stays on
<literal > myhostname.example.org</literal> with no other changes required.
This pattern also allows to seamlessly move the homeserver from
<literal > myhostname.example.org</literal> to
<literal > myotherhost.example.org</literal> by only changing the
<code > /.well-known</code> redirection target.
2019-03-12 10:01:36 +00:00
</para>
<para >
2019-09-19 18:17:30 +01:00
If you want to run a server with public registration by anybody, you can
2022-03-12 15:33:46 +00:00
then enable <literal > <link linkend= "opt-services.matrix-synapse.settings.enable_registration" > services.matrix-synapse.settings.enable_registration</link> =
2020-03-15 15:58:50 +00:00
true;</literal> . Otherwise, or you can generate a registration secret with
2019-09-19 18:17:30 +01:00
<command > pwgen -s 64 1</command> and set it with
2022-03-12 15:33:46 +00:00
<option > <link linkend= "opt-services.matrix-synapse.settings.registration_shared_secret" > services.matrix-synapse.settings.registration_shared_secret</link> </option> .
2022-02-08 14:03:37 +00:00
To create a new user or admin, run the following after you have set the secret
2019-09-19 18:17:30 +01:00
and have rebuilt NixOS:
2019-06-17 12:25:50 +01:00
<screen >
<prompt > $ </prompt> nix run nixpkgs.matrix-synapse
<prompt > $ </prompt> register_new_matrix_user -k <replaceable > your-registration-shared-secret</replaceable> http://localhost:8008
<prompt > New user localpart: </prompt> <replaceable > your-username</replaceable>
<prompt > Password:</prompt>
<prompt > Confirm password:</prompt>
<prompt > Make admin [no]:</prompt>
2019-06-17 11:01:51 +01:00
Success!
2019-06-17 12:25:50 +01:00
</screen>
2019-09-19 18:17:30 +01:00
In the example, this would create a user with the Matrix Identifier
<literal > @your-username:example.org</literal> . Note that the registration
secret ends up in the nix store and therefore is world-readable by any user
on your machine, so it makes sense to only temporarily activate the
2022-02-08 14:03:37 +00:00
<link linkend= "opt-services.matrix-synapse.settings.registration_shared_secret" > registration_shared_secret</link>
2020-03-15 15:58:50 +00:00
option until a better solution for NixOS is in place.
2019-03-12 10:01:36 +00:00
</para>
</section>
2020-07-16 22:59:16 +01:00
<section xml:id= "module-services-matrix-element-web" >
<title > Element (formerly known as Riot) Web Client</title>
2019-03-12 10:01:36 +00:00
2019-03-22 13:44:11 +00:00
<para >
2020-07-16 22:59:16 +01:00
<link xlink:href= "https://github.com/vector-im/riot-web/" > Element Web</link> is
2019-09-19 18:17:30 +01:00
the reference web client for Matrix and developed by the core team at
2020-07-16 22:59:16 +01:00
matrix.org. Element was formerly known as Riot.im, see the
<link xlink:href= "https://element.io/blog/welcome-to-element/" > Element introductory blog post</link>
for more information. The following snippet can be optionally added to the code before
2019-09-19 18:17:30 +01:00
to complete the synapse installation with a web client served at
2020-07-16 22:59:16 +01:00
<code > https://element.myhostname.example.org</code> and
<code > https://element.example.org</code> . Alternatively, you can use the hosted
copy at <link xlink:href= "https://app.element.io/" > https://app.element.io/</link> ,
2019-09-19 18:17:30 +01:00
or use other web clients or native client applications. Due to the
<literal > /.well-known</literal> urls set up done above, many clients should
fill in the required connection details automatically when you enter your
Matrix Identifier. See
<link xlink:href= "https://matrix.org/docs/projects/try-matrix-now.html" > Try
Matrix Now!</link> for a list of existing clients and their supported
featureset.
2019-03-22 13:44:11 +00:00
<programlisting >
2020-03-15 15:58:50 +00:00
{
2020-07-16 22:59:16 +01:00
services.nginx.virtualHosts."element.${fqdn}" = {
2020-03-15 15:58:50 +00:00
<link linkend= "opt-services.nginx.virtualHosts._name_.enableACME" > enableACME</link> = true;
<link linkend= "opt-services.nginx.virtualHosts._name_.forceSSL" > forceSSL</link> = true;
<link linkend= "opt-services.nginx.virtualHosts._name_.serverAliases" > serverAliases</link> = [
2020-07-16 22:59:16 +01:00
"element.${config.networking.domain}"
2020-03-15 15:58:50 +00:00
];
2019-03-12 10:01:36 +00:00
2020-07-16 22:59:16 +01:00
<link linkend= "opt-services.nginx.virtualHosts._name_.root" > root</link> = pkgs.element-web.override {
2020-03-15 15:58:50 +00:00
conf = {
default_server_config."m.homeserver" = {
2021-01-18 03:35:29 +00:00
"base_url" = "https://${fqdn}";
2020-03-15 15:58:50 +00:00
"server_name" = "${fqdn}";
};
};
};
};
}
2019-06-17 11:01:51 +01:00
</programlisting>
2019-03-12 10:01:36 +00:00
</para>
2019-03-22 13:44:11 +00:00
2019-03-12 10:01:36 +00:00
<para >
2020-07-16 22:59:16 +01:00
Note that the Element developers do not recommend running Element and your Matrix
2019-09-19 18:17:30 +01:00
homeserver on the same fully-qualified domain name for security reasons. In
the example, this means that you should not reuse the
2020-07-16 22:59:16 +01:00
<literal > myhostname.example.org</literal> virtualHost to also serve Element,
2019-09-19 18:17:30 +01:00
but instead serve it on a different subdomain, like
2020-07-16 22:59:16 +01:00
<literal > element.example.org</literal> in the example. See the
<link xlink:href= "https://github.com/vector-im/riot-web#important-security-note" > Element
2019-09-19 18:17:30 +01:00
Important Security Notes</link> for more information on this subject.
2019-03-12 10:01:36 +00:00
</para>
</section>
</chapter>