set up terranix matrix again

feature/clan.lol
Ingolf Wagner 2024-04-04 21:26:10 +02:00
parent 9f7b31fe57
commit 24618c8041
Signed by: palo
GPG Key ID: 76BF5F1928B9618B
7 changed files with 212 additions and 140 deletions

View File

@ -34,7 +34,8 @@
#./sync-torrent.nix
#./social-jitsi.nix
./social-matrix.nix
./social-matrix-terranix.nix
#./social-matrix-ingolf-wagner.nix
# matrix
# ------
@ -52,7 +53,6 @@
networking.hostName = "orbi";
components.terminal.enable = true;
components.mainUser.enable = true;
components.gui.enable = false;

View File

@ -1,8 +1,8 @@
{ config, pkgs, ... }:
let
# 1. create DNS entry `_matrix._tcp.ingolf-wagner.de SRV - 10 8448 matrix.ingolf-wagner.de` (8448 for federation)
# 2. create DNS entry `matrix.ingolf-wagner.de A - 95.216.66.212`
# 3. test with : https://federationtester.matrix.org/#ingolf-wagner.de
# 1. create DNS entry `matrix.ingolf-wagner.de A - 95.216.66.212`
# 2. test with : https://federationtester.matrix.org/#ingolf-wagner.de
# 3. info at : https://silvio.github.io/docker-matrix/Example.configs.html
domain = "ingolf-wagner.de";
baseUrl = "https://matrix.${domain}";
@ -39,8 +39,8 @@ let
in
{
networking.firewall.allowedTCPPorts = [ 80 443 8448 ];
networking.firewall.allowedUDPPorts = [ 80 443 8448 ];
networking.firewall.allowedTCPPorts = [ 80 443 ];
networking.firewall.allowedUDPPorts = [ 80 443 ];
environment.systemPackages = [ matrix_create_user ];
@ -53,7 +53,7 @@ in
users.groups.matrix-synapse.gid = config.ids.gids.matrix-synapse;
# todo : mount postgresql folder in a dedicated zfs pool
containers.matrix-ingolf-wagner-de = {
containers.matrix-ingolf-wagner = {
autoStart = true;
privateNetwork = false;
@ -109,7 +109,6 @@ in
];
};
};
};
@ -144,24 +143,6 @@ in
# };
"matrix.${domain}" = {
#listen = [
# {
# addr = "0.0.0.0";
# port = 80;
# }
# {
# addr = "0.0.0.0";
# port = 443;
# ssl = true;
# }
# # for federation
# {
# addr = "0.0.0.0";
# port = 8448;
# ssl = true;
# }
#];
#serverAliases = [ "ingolf-wagner.de" ];
enableACME = true;
forceSSL = true;
# It's also possible to do a redirect here or something else, this vhost is not

View File

@ -0,0 +1,199 @@
{ config, pkgs, ... }:
let
# 1. create DNS entry `matrix.terranix.org A - 95.216.66.212`
# 2. test with : https://federationtester.matrix.org/#terranix.org
# 3. info at : https://silvio.github.io/docker-matrix/Example.configs.html
domain = "terranix.org";
baseUrl = "https://matrix.${domain}";
synapse_port = 8008;
federation_port = 8448;
matrix_create_user = pkgs.writers.writeBashBin "matrix-create-user-${domain}" ''
${pkgs.matrix-synapse}/bin/register_new_matrix_user \
-k $( ${pkgs.gojq}/bin/gojq \
--yaml-input \
--raw-output \
'.registration_shared_secret' \
${config.sops.secrets.matrix_shared_secret.path} ) \
http://localhost:${toString synapse_port}
'';
clientConfig."m.homeserver".base_url = baseUrl;
serverConfig."m.server" = "matrix.${domain}:443";
mkWellKnown = data: ''
default_type application/json;
add_header Access-Control-Allow-Origin *;
return 200 '${builtins.toJSON data}';
'';
element-web =
pkgs.runCommand "element-web-with-config"
{
nativeBuildInputs = [ pkgs.buildPackages.jq ];
} ''
cp -r ${pkgs.element-web} $out
chmod -R u+w $out
jq '."default_server_config"."m.homeserver" = { "base_url": "https://matrix.${domain}", "server_name": "${domain}" }' \
> $out/config.json < ${pkgs.element-web}/config.json
ln -s $out/config.json $out/config.matrix.${domain}.json
'';
in
{
networking.firewall.allowedTCPPorts = [ 80 443 federation_port ];
networking.firewall.allowedUDPPorts = [ 80 443 federation_port ];
environment.systemPackages = [ matrix_create_user ];
sops.secrets.matrix_shared_secret.owner = "matrix-synapse";
users.users.matrix-synapse = {
isSystemUser = true;
uid = config.ids.uids.matrix-synapse;
group = "matrix-synapse";
};
users.groups.matrix-synapse.gid = config.ids.gids.matrix-synapse;
# todo : mount postgresql folder in a dedicated zfs pool
containers.matrix-terranix = {
autoStart = true;
privateNetwork = false;
bindMounts = {
rootpassword = {
hostPath = config.sops.secrets.matrix_shared_secret.path;
mountPoint = "/run/secrets/matrix-shared-secret";
isReadOnly = true;
};
};
config = { config, pkgs, lib, ... }: {
system.stateVersion = "23.11";
services.postgresql = {
enable = true;
initialScript = pkgs.writeText "synapse-init.sql" ''
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.matrix-synapse = {
enable = true;
settings.server_name = domain;
# The public base URL value must match the `base_url` value set in `clientConfig` above.
# The default value here is based on `server_name`, so if your `server_name` is different
# from the value of `matrix.<domain>` above, you will likely run into some mismatched domain names
# in client applications.
settings.public_baseurl = baseUrl;
extraConfigFiles = [ "/run/secrets/matrix-shared-secret" ];
settings.listeners = [
{
port = synapse_port;
bind_addresses = [ "::1" ];
type = "http";
tls = false;
x_forwarded = true;
resources = [
{
names = [ "client" ];
compress = true;
}
{
names = [ "federation" ];
compress = false;
}
];
}
];
};
};
};
services.nginx = {
enable = true;
recommendedTlsSettings = true;
recommendedOptimisation = true;
recommendedGzipSettings = true;
recommendedProxySettings = true;
virtualHosts = {
# lives on another host
# # If the A and AAAA DNS records on example.org do not point on the same host as the
# # records for myhostname.example.org, you can easily move the /.well-known
# # virtualHost section of the code to the host that is serving example.org, while
# # the rest stays on myhostname.example.org with no other changes required.
# # This pattern also allows to seamlessly move the homeserver from
# # myhostname.example.org to myotherhost.example.org by only changing the
# # /.well-known redirection target.
# "${domain}" = {
# enableACME = true;
# forceSSL = true;
# # This section is not needed if the server_name of matrix-synapse is equal to
# # the domain (i.e. example.org from @foo:example.org) and the federation port
# # is federation_port.
# # Further reference can be found in the docs about delegation under
# # https://element-hq.github.io/synapse/latest/delegate.html
# locations."= /.well-known/matrix/server".extraConfig = mkWellKnown serverConfig;
# # This is usually needed for homeserver discovery (from e.g. other Matrix clients).
# # Further reference can be found in the upstream docs at
# # https://spec.matrix.org/latest/client-server-api/#getwell-knownmatrixclient
# locations."= /.well-known/matrix/client".extraConfig = mkWellKnown clientConfig;
# };
"matrix.${domain}" = {
listen = [
{
addr = "0.0.0.0";
port = 80;
}
{
addr = "0.0.0.0";
port = 443;
ssl = true;
}
# for federation
{
addr = "0.0.0.0";
port = federation_port;
ssl = true;
}
];
enableACME = true;
forceSSL = true;
# It's also possible to do a redirect here or something else, this vhost is not
# needed for Matrix. It's recommended though to *not put* element
# here, see also the section about Element.
locations."/".extraConfig = ''
return 404;
'';
# Forward all Matrix API calls to the synapse Matrix homeserver. A trailing slash
# *must not* be used here.
locations."/_matrix".proxyPass = "http://[::1]:${toString synapse_port}";
# Forward requests for e.g. SSO and password-resets.
#locations."/_synapse/client".proxyPass = "http://[::1]:${toString synapse_port}";
};
"element.${domain}" = {
forceSSL = true;
enableACME = true;
extraConfig = ''
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_read_timeout 600;
'';
locations."/".root = element-web;
};
};
};
}

View File

@ -1,110 +0,0 @@
{ config, pkgs, ... }:
let
inherit (config.services.dendrite.settings.global) server_name;
nginx-vhost = "matrix.terranix.org";
element-web-terranix.org =
pkgs.runCommand "element-web-with-config"
{
nativeBuildInputs = [ pkgs.buildPackages.jq ];
} ''
cp -r ${pkgs.element-web} $out
chmod -R u+w $out
jq '."default_server_config"."m.homeserver" = { "base_url": "https://${nginx-vhost}:443", "server_name": "${server_name}" }' \
> $out/config.json < ${pkgs.element-web}/config.json
ln -s $out/config.json $out/config.${nginx-vhost}.json
'';
in
{
# $ nix-shell -p dendrite --run 'generate-keys --private-key /tmp/key'
sops.secrets.matrix-server-key = { };
services.dendrite = {
enable = true;
httpPort = 8448;
settings = {
global = {
server_name = "terranix.org";
# `private_key` has the type `path`
# prefix a `/` to make `path` happy
private_key = "/$CREDENTIALS_DIRECTORY/matrix-server-key";
trusted_third_party_id_servers = [
"matrix.org"
"vector.im"
"xaos.space"
"lassul.us"
"thalheim.io"
"nixos.org"
];
metrics.enabled = false;
};
logging = [
{
type = "std";
level = "warn";
}
];
client_api = {
registration_disabled = true;
rate_limiting.enabled = false;
# set only for the first admin account, than remove.
#registration_shared_secret = ""; # disable once first admin account is created
};
media_api = {
dynamic_thumbnails = true;
};
mscs = {
mscs = [ "msc2836" "msc2946" ];
};
sync_api = {
real_ip_header = "X-Real-IP";
};
federation_api = {
key_perspectives = [
{
server_name = "matrix.org";
keys = [
{
key_id = "ed25519:auto";
public_key = "Noi6WqcDj0QmPxCNQqgezwTlBKrfqehY1u2FyWP9uYw";
}
{
key_id = "ed25519:a_RXGa";
public_key = "l8Hft5qXKn1vfHrg3p4+W8gELQVo8N13JkluMfmn2sQ";
}
];
}
];
prefer_direct_fetch = false;
};
};
};
systemd.services.dendrite.serviceConfig.LoadCredential = [
"matrix-server-key:${config.sops.secrets.matrix-server-key.path}"
];
services.nginx.virtualHosts.${nginx-vhost} = {
forceSSL = true;
enableACME = true;
extraConfig = ''
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_read_timeout 600;
'';
locations."/_matrix".proxyPass = "http://127.0.0.1:${toString config.services.dendrite.httpPort}";
# for remote admin access
locations."/_synapse".proxyPass = "http://127.0.0.1:${toString config.services.dendrite.httpPort}";
locations."/".root = element-web-terranix.org;
};
#services.nginx.virtualHosts.${server_name} = {
# locations."= /.well-known/matrix/server".alias =
# pkgs.writeText "matrix-server" (builtins.toJSON { "m.server" = "${nginx-vhost}:443"; });
# locations."= /.well-known/matrix/client".alias =
# pkgs.writeText "matrix-client" (builtins.toJSON { "m.homeserver".base_url = "https://${nginx-vhost}"; });
#};
}

View File

@ -32,7 +32,6 @@
./social-jitsi.nix
# matrix
./terranix-dendrite.nix
./social-matrix.nix
# logging

View File

@ -6,6 +6,9 @@ locals {
normal_ttl = 12 * local.hours
short_ttl = 30 * local.minutes
very_short_ttl = 2 * local.minutes
orbi = "95.216.66.212"
robi = "144.76.13.147"
}
resource "namecheap_domain_records" "terranix" {
@ -31,14 +34,14 @@ resource "namecheap_domain_records" "terranix" {
# matrix.terranix.org
record {
address = "144.76.13.147"
address = local.orbi
hostname = "matrix"
mx_pref = 10
ttl = local.short_ttl
type = "A"
}
record {
address = "144.76.13.147"
address = local.orbi
hostname = "element"
mx_pref = 10
ttl = local.normal_ttl