2020-04-24 23:36:52 +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 >
<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.
</para>
<para >
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-18 16:06:22 +00:00
copy of the Element web client. See the
2020-04-24 23:36:52 +00:00
<link xlink:href= "https://matrix.org/docs/projects/try-matrix-now.html" > Try
2020-07-18 16:06:22 +00:00
Matrix Now!</link> overview page for links to Element Apps for Android and iOS,
2020-04-24 23:36:52 +00:00
desktop clients, as well as bridges to other networks and other projects
around Matrix.
</para>
<section xml:id= "module-services-matrix-synapse" >
<title > Synapse Homeserver</title>
<para >
<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> .
<programlisting >
2022-07-14 12:49:19 +00:00
{ pkgs, lib, config, ... }:
2020-04-24 23:36:52 +00:00
let
2022-07-14 12:49:19 +00:00
fqdn = "${config.networking.hostName}.${config.networking.domain}";
clientConfig = {
"m.homeserver".base_url = "https://${fqdn}";
"m.identity_server" = {};
2020-04-24 23:36:52 +00:00
};
2022-07-14 12:49:19 +00:00
serverConfig."m.server" = "${config.services.matrix-synapse.settings.server_name}:443";
mkWellKnown = data: ''
add_header Content-Type application/json;
add_header Access-Control-Allow-Origin *;
return 200 '${builtins.toJSON data}';
'';
in {
<xref linkend= "opt-networking.hostName" /> = "myhostname";
<xref linkend= "opt-networking.domain" /> = "example.org";
<xref linkend= "opt-networking.firewall.allowedTCPPorts" /> = [ 80 443 ];
2020-04-24 23:36:52 +00:00
2022-07-14 12:49:19 +00:00
<xref linkend= "opt-services.postgresql.enable" /> = true;
<xref linkend= "opt-services.postgresql.initialScript" /> = pkgs.writeText "synapse-init.sql" ''
2020-04-24 23:36:52 +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";
'';
services.nginx = {
<link linkend= "opt-services.nginx.enable" > enable</link> = true;
<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;
<link linkend= "opt-services.nginx.virtualHosts" > virtualHosts</link> = {
2022-07-14 12:49:19 +00:00
"${config.networking.domain}" = { <co xml:id= 'ex-matrix-synapse-dns' />
2021-01-15 22:18:51 +00:00
<link linkend= "opt-services.nginx.virtualHosts._name_.enableACME" > enableACME</link> = true;
<link linkend= "opt-services.nginx.virtualHosts._name_.forceSSL" > forceSSL</link> = true;
2022-07-14 12:49:19 +00:00
<link linkend= "opt-services.nginx.virtualHosts._name_.locations._name_.extraConfig" > locations."= /.well-known/matrix/server".extraConfig</link> = mkWellKnown serverConfig; <co xml:id= 'ex-matrix-synapse-well-known-server' />
<link linkend= "opt-services.nginx.virtualHosts._name_.locations._name_.extraConfig" > locations."= /.well-known/matrix/client".extraConfig</link> = mkWellKnown clientConfig; <co xml:id= 'ex-matrix-synapse-well-known-client' />
2020-04-24 23:36:52 +00:00
};
2022-07-14 12:49:19 +00:00
"${fqdn}" = {
2020-04-24 23:36:52 +00:00
<link linkend= "opt-services.nginx.virtualHosts._name_.enableACME" > enableACME</link> = true;
<link linkend= "opt-services.nginx.virtualHosts._name_.forceSSL" > forceSSL</link> = true;
2022-07-14 12:49:19 +00:00
<link linkend= "opt-services.nginx.virtualHosts._name_.locations._name_.extraConfig" > locations."/".extraConfig</link> = '' <co xml:id= 'ex-matrix-synapse-rev-default' />
2020-04-24 23:36:52 +00:00
return 404;
'';
2022-07-14 12:49:19 +00:00
<link linkend= "opt-services.nginx.virtualHosts._name_.locations._name_.proxyPass" > locations."/_matrix".proxyPass</link> = "http://[::1]:8008"; <co xml:id= 'ex-matrix-synapse-rev-proxy-pass' />
<link linkend= "opt-services.nginx.virtualHosts._name_.locations._name_.proxyPass" > locations."/_synapse/client".proxyPass</link> = "http://[::1]:8008"; <co xml:id= 'ex-matrix-synapse-rev-client' />
2020-04-24 23:36:52 +00:00
};
};
};
2022-07-14 12:49:19 +00:00
2020-04-24 23:36:52 +00:00
services.matrix-synapse = {
<link linkend= "opt-services.matrix-synapse.enable" > enable</link> = true;
2022-07-14 12:49:19 +00:00
<link linkend= "opt-services.matrix-synapse.settings.server_name" > settings.server_name</link> = config.networking.domain;
<link linkend= "opt-services.matrix-synapse.settings.listeners" > settings.listeners</link> = [
{ <link linkend= "opt-services.matrix-synapse.settings.listeners._.port" > port</link> = 8008;
2022-03-30 09:31:56 +00:00
<link linkend= "opt-services.matrix-synapse.settings.listeners._.bind_addresses" > bind_addresses</link> = [ "::1" ];
2022-03-05 16:20: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> = [ {
2022-07-14 12:49:19 +00:00
<link linkend= "opt-services.matrix-synapse.settings.listeners._.resources._.names" > names</link> = [ "client" "federation" ];
2022-03-05 16:20:37 +00:00
<link linkend= "opt-services.matrix-synapse.settings.listeners._.resources._.compress" > compress</link> = true;
} ];
2020-04-24 23:36:52 +00:00
}
];
};
2021-04-17 00:35:05 +00:00
}
2020-04-24 23:36:52 +00:00
</programlisting>
</para>
2022-07-14 12:49:19 +00:00
<calloutlist >
<callout arearefs= 'ex-matrix-synapse-dns' >
<para >
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.
</para>
</callout>
<callout arearefs= 'ex-matrix-synapse-well-known-server' >
<para >
This section is not needed if the <link linkend= "opt-services.matrix-synapse.settings.server_name" > server_name</link>
of <package > matrix-synapse</package> is equal to the domain (i.e.
<literal > example.org</literal> from <literal > @foo:example.org</literal> )
and the federation port is 8448.
Further reference can be found in the <link xlink:href= "https://matrix-org.github.io/synapse/latest/delegate.html" > docs
about delegation</link> .
</para>
</callout>
<callout arearefs= 'ex-matrix-synapse-well-known-client' >
<para >
This is usually needed for homeserver discovery (from e.g. other Matrix clients).
Further reference can be found in the <link xlink:href= "https://spec.matrix.org/latest/client-server-api/#getwell-knownmatrixclient" > upstream docs</link>
</para>
</callout>
<callout arearefs= 'ex-matrix-synapse-rev-default' >
<para >
It's also possible to do a redirect here or something else, this vhost is not
needed for Matrix. It's recommended though to <emphasis > not put</emphasis> element
here, see also the <link linkend= 'ex-matrix-synapse-rev-default' > section about Element</link> .
</para>
</callout>
<callout arearefs= 'ex-matrix-synapse-rev-proxy-pass' >
<para >
Forward all Matrix API calls to the synapse Matrix homeserver. A trailing slash
<emphasis > must not</emphasis> be used here.
</para>
</callout>
<callout arearefs= 'ex-matrix-synapse-rev-client' >
<para >
Forward requests for e.g. SSO and password-resets.
</para>
</callout>
</calloutlist>
</section>
<section xml:id= "module-services-matrix-register-users" >
<title > Registering Matrix users</title>
2020-04-24 23:36:52 +00:00
<para >
If you want to run a server with public registration by anybody, you can
2022-03-30 09:31:56 +00:00
then enable <literal > <link linkend= "opt-services.matrix-synapse.settings.enable_registration" > services.matrix-synapse.settings.enable_registration</link> =
2020-04-24 23:36:52 +00:00
true;</literal> . Otherwise, or you can generate a registration secret with
<command > pwgen -s 64 1</command> and set it with
2022-03-30 09:31:56 +00:00
<option > <link linkend= "opt-services.matrix-synapse.settings.registration_shared_secret" > services.matrix-synapse.settings.registration_shared_secret</link> </option> .
2022-03-05 16:20:37 +00:00
To create a new user or admin, run the following after you have set the secret
2020-04-24 23:36:52 +00:00
and have rebuilt NixOS:
<screen >
2022-07-14 12:49:19 +00:00
<prompt > $ </prompt> nix-shell -p matrix-synapse
2020-04-24 23:36:52 +00:00
<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>
Success!
</screen>
In the example, this would create a user with the Matrix Identifier
2022-07-14 12:49:19 +00:00
<literal > @your-username:example.org</literal> .
<warning >
<para >
When using <xref linkend= "opt-services.matrix-synapse.settings.registration_shared_secret" /> , the secret
will end up in the world-readable store. Instead it's recommended to deploy the secret
in an additional file like this:
<itemizedlist >
<listitem >
<para >
Create a file with the following contents:
<programlisting > registration_shared_secret: your-very-secret-secret</programlisting>
</para>
</listitem>
<listitem >
<para >
Deploy the file with a secret-manager such as <link xlink:href= "https://nixops.readthedocs.io/en/latest/overview.html#managing-keys" > <option > deployment.keys</option> </link>
from <citerefentry > <refentrytitle > nixops</refentrytitle> <manvolnum > 1</manvolnum> </citerefentry>
or <link xlink:href= "https://github.com/Mic92/sops-nix/" > sops-nix</link> to
e.g. <filename > /run/secrets/matrix-shared-secret</filename> and ensure that it's readable
by <package > matrix-synapse</package> .
</para>
</listitem>
<listitem >
<para >
Include the file like this in your configuration:
<programlisting >
{
<xref linkend= "opt-services.matrix-synapse.extraConfigFiles" /> = [
"/run/secrets/matrix-shared-secret"
];
}
</programlisting>
</para>
</listitem>
</itemizedlist>
</para>
</warning>
2020-04-24 23:36:52 +00:00
</para>
2022-07-14 12:49:19 +00:00
<note >
<para >
It's also possible to user alternative authentication mechanism such as
<link xlink:href= "https://github.com/matrix-org/matrix-synapse-ldap3" > LDAP (via <literal > matrix-synapse-ldap3</literal> )</link>
or <link xlink:href= "https://matrix-org.github.io/synapse/latest/openid.html" > OpenID</link> .
</para>
</note>
2020-04-24 23:36:52 +00:00
</section>
2020-07-18 16:06:22 +00:00
<section xml:id= "module-services-matrix-element-web" >
<title > Element (formerly known as Riot) Web Client</title>
2020-04-24 23:36:52 +00:00
<para >
2020-07-18 16:06:22 +00:00
<link xlink:href= "https://github.com/vector-im/riot-web/" > Element Web</link> is
2020-04-24 23:36:52 +00:00
the reference web client for Matrix and developed by the core team at
2020-07-18 16:06:22 +00: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
2020-04-24 23:36:52 +00:00
to complete the synapse installation with a web client served at
2020-07-18 16:06:22 +00: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> ,
2020-04-24 23:36:52 +00: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.
<programlisting >
{
2020-07-18 16:06:22 +00:00
services.nginx.virtualHosts."element.${fqdn}" = {
2020-04-24 23:36:52 +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-18 16:06:22 +00:00
"element.${config.networking.domain}"
2020-04-24 23:36:52 +00:00
];
2020-07-18 16:06:22 +00:00
<link linkend= "opt-services.nginx.virtualHosts._name_.root" > root</link> = pkgs.element-web.override {
2020-04-24 23:36:52 +00:00
conf = {
2022-07-14 12:49:19 +00:00
default_server_config = clientConfig; # see `clientConfig` from the snippet above.
2020-04-24 23:36:52 +00:00
};
};
};
}
</programlisting>
</para>
2022-07-14 12:49:19 +00:00
<note >
<para >
The Element developers do not recommend running Element and your Matrix
homeserver on the same fully-qualified domain name for security reasons. In
the example, this means that you should not reuse the
<literal > myhostname.example.org</literal> virtualHost to also serve Element,
but instead serve it on a different subdomain, like
<literal > element.example.org</literal> in the example. See the
<link xlink:href= "https://github.com/vector-im/element-web/tree/v1.10.0#important-security-notes" > Element
Important Security Notes</link> for more information on this subject.
</para>
</note>
2020-04-24 23:36:52 +00:00
</section>
</chapter>