depot/ops/nixos/bvm-matrix/default.nix

184 lines
5.4 KiB
Nix
Raw Normal View History

2021-05-22 21:48:13 +00:00
# SPDX-FileCopyrightText: 2020 Luke Granger-Brown <depot@lukegb.com>
#
# SPDX-License-Identifier: Apache-2.0
{ config, depot, pkgs, lib, ... }:
let
inherit (depot.ops) secrets;
machineSecrets = secrets.machineSpecific.bvm-matrix;
in {
imports = [
../lib/bvm.nix
];
# Networking!
networking = {
hostName = "bvm-matrix";
hostId = "1c2786ad";
interfaces.enp1s0 = {
ipv4.addresses = [{ address = "10.100.0.205"; prefixLength = 23; }];
};
interfaces.enp2s0 = {
2021-05-22 21:48:13 +00:00
ipv4.addresses = [{ address = "92.118.28.6"; prefixLength = 24; }];
ipv6.addresses = [{ address = "2a09:a441::6"; prefixLength = 32; }];
};
defaultGateway = { address = "92.118.28.1"; interface = "enp2s0"; };
defaultGateway6 = { address = "2a09:a441::1"; interface = "enp2s0"; };
2021-05-22 21:48:13 +00:00
firewall.allowedUDPPorts = [
3478 # TURN
];
firewall.allowedTCPPorts = [
80 443 # HTTP/S
3478 # TURN
];
};
2021-05-22 22:48:03 +00:00
my.ip.tailscale = "100.74.197.67";
2021-05-22 21:48:13 +00:00
services.postfix = {
enable = true;
hostname = "matrix.zxcvbnm.ninja";
origin = "zxcvbnm.ninja";
domain = "zxcvbnm.ninja";
enableSubmission = true;
};
2021-05-22 21:48:13 +00:00
services.postgresql = {
enable = true;
ensureDatabases = [ "matrix-synapse" ];
ensureUsers = [{
name = "matrix-synapse";
ensurePermissions = {
2021-05-22 22:52:24 +00:00
"DATABASE \"matrix-synapse\"" = "ALL PRIVILEGES";
2021-05-22 21:48:13 +00:00
};
}];
};
services.coturn = {
enable = true;
use-auth-secret = true;
realm = "matrix.zxcvbnm.ninja";
static-auth-secret = machineSecrets.turnSecret;
cert = "${config.security.acme.certs."matrix.zxcvbnm.ninja".directory}/fullchain.pem";
pkey = "${config.security.acme.certs."matrix.zxcvbnm.ninja".directory}/key.pem";
};
services.nginx = {
enable = true;
recommendedTlsSettings = true;
recommendedOptimisation = true;
recommendedGzipSettings = true;
recommendedProxySettings = true;
virtualHosts = {
"zxcvbnm.ninja" = {
forceSSL = true;
useACMEHost = "matrix.zxcvbnm.ninja";
locations = let
inherit (lib) mapAttrs' nameValuePair;
wellKnown = {
"matrix/server" = { "m.server" = "matrix.zxcvbnm.ninja:443"; };
"matrix/client" = {
"m.homeserver" = { "base_url" = "https://matrix.zxcvbnm.ninja"; };
"m.identity_server" = { "base_url" = "https://vector.im"; };
};
};
in
mapAttrs' (name: value: nameValuePair "= /.well-known/${name}" { extraConfig = ''
add_header Content-Type application/json;
add_header Access-Control-Allow-Origin *;
return 200 '${builtins.toJSON value}';
''; }) wellKnown;
};
"matrix.zxcvbnm.ninja" = {
forceSSL = true;
useACMEHost = "matrix.zxcvbnm.ninja";
locations."/".return = "301 https://element.zxcvbnm.ninja$request_uri";
locations."/_matrix".proxyPass = "http://[::1]:8008";
};
2021-05-22 23:17:27 +00:00
"element.zxcvbnm.ninja" = {
forceSSL = true;
useACMEHost = "matrix.zxcvbnm.ninja";
extraConfig = ''
add_header X-Frame-Options SAMEORIGIN;
add_header X-Content-Type-Options nosniff;
add_header X-XSS-Protection "1; mode=block";
add_header Content-Security-Policy "frame-ancestors 'none'";
'';
root = pkgs.element-web.override {
conf = {
default_server_config."m.homeserver" = {
2021-05-22 23:22:58 +00:00
base_url = "https://matrix.zxcvbnm.ninja";
2021-05-22 23:17:27 +00:00
server_name = "matrix.zxcvbnm.ninja";
};
disable_guests = true;
2021-05-22 23:26:02 +00:00
disable_custom_urls = true;
settingDefaults = {
"UIFeature.registration" = false;
};
2021-05-22 23:17:27 +00:00
};
};
};
2021-05-22 21:48:13 +00:00
};
};
services.matrix-synapse = {
enable = true;
server_name = "zxcvbnm.ninja";
2021-05-23 01:35:10 +00:00
macaroon_secret_key = machineSecrets.matrix.macaroonSecretKey;
registration_shared_secret = machineSecrets.matrix.registrationSecret;
turn_uris = [
2021-05-24 13:53:47 +00:00
"turns:matrix.zxcvbnm.ninja:3478?transport=udp"
"turns:matrix.zxcvbnm.ninja:3478?transport=tcp"
"turn:matrix.zxcvbnm.ninja:3478?transport=udp"
"turn:matrix.zxcvbnm.ninja:3478?transport=tcp"
];
turn_shared_secret = machineSecrets.turnSecret;
public_baseurl = "https://matrix.zxcvbnm.ninja/";
url_preview_enabled = true;
url_preview_ip_range_blacklist = [
"127.0.0.0/8"
"10.0.0.0/8"
"172.16.0.0/12"
"192.168.0.0/16"
"100.64.0.0/10"
"169.254.0.0/16"
"::1/128"
"fe80::/64"
"fc00::/7"
];
2021-05-22 21:48:13 +00:00
listeners = [{
port = 8008;
bind_address = "::1";
type = "http";
tls = false;
x_forwarded = true;
resources = [{
names = [ "client" "federation" ];
compress = false;
}];
}];
2021-05-23 01:35:10 +00:00
extraConfig = ''
form_secret: "${machineSecrets.matrix.formSecret}"
experimental_features:
spaces_enabled: true
2021-05-23 01:35:10 +00:00
'';
2021-05-22 21:48:13 +00:00
};
# Users allowed to use SSL certificate for matrix.zxcvbnm.ninja.
users.groups.matrixcert = {
members = [ "turnserver" "nginx" ];
};
security.acme = {
acceptTerms = true;
email = "letsencrypt@lukegb.com";
certs."matrix.zxcvbnm.ninja" = {
group = "matrixcert";
dnsProvider = "cloudflare";
credentialsFile = secrets.cloudflareCredentials;
extraDomainNames = [ "element.zxcvbnm.ninja" "zxcvbnm.ninja" ];
};
};
system.stateVersion = "21.05";
}