2020-11-09 15:20:09 +00:00
|
|
|
# adapted from https://nixos.org/manual/nixos/stable/index.html#module-services-matrix
|
|
|
|
{ modulesPath, config, lib, pkgs, ... }:
|
2021-01-11 02:10:33 +00:00
|
|
|
let
|
2020-11-10 07:15:13 +00:00
|
|
|
matrix-registration = import ../../matrix-registration/default.nix;
|
2020-11-09 15:20:09 +00:00
|
|
|
storage-device = "/dev/disk/by-id/scsi-0DO_Volume_matrix-storage";
|
2021-01-11 02:10:33 +00:00
|
|
|
storage-dir = "/srv/matrix-data";
|
2020-11-10 07:15:13 +00:00
|
|
|
matrix-reg-dir = "${storage-dir}/matrix-registration";
|
2021-03-11 15:01:01 +00:00
|
|
|
slackbridge-dir = "${storage-dir}/slackbridge";
|
2021-03-17 20:54:46 +00:00
|
|
|
secrets = builtins.fromJSON (builtins.readFile ./secrets.json);
|
2021-03-11 15:01:01 +00:00
|
|
|
slack-reg-source-yaml = (builtins.readFile ./slack-registration.yaml);
|
|
|
|
slack-reg-dest-yaml = pkgs.writeText "slack-registration.yaml" "${slack-reg-source-yaml}";
|
2021-01-11 02:10:33 +00:00
|
|
|
fqdn =
|
2020-11-09 15:20:09 +00:00
|
|
|
let
|
|
|
|
join = hostName: domain: hostName + lib.optionalString (domain != null) ".${domain}";
|
|
|
|
in join config.networking.hostName config.networking.domain;
|
|
|
|
in {
|
|
|
|
imports = [
|
|
|
|
"${toString modulesPath}/virtualisation/digital-ocean-image.nix"
|
|
|
|
];
|
|
|
|
|
2021-03-11 15:01:01 +00:00
|
|
|
environment.systemPackages = [ pkgs.jq matrix-registration pkgs.matrix-appservice-slack ];
|
2020-11-09 15:20:09 +00:00
|
|
|
services.openssh.enable = true;
|
|
|
|
networking.firewall.allowedTCPPorts = [ 22 80 443 ];
|
|
|
|
|
|
|
|
users.users.root.openssh.authorizedKeys.keys = [
|
|
|
|
(builtins.readFile "/home/mike/.ssh/id_mops.pub")
|
|
|
|
];
|
|
|
|
|
2021-01-11 02:10:33 +00:00
|
|
|
### app specific config
|
2020-11-09 15:20:09 +00:00
|
|
|
|
|
|
|
# mount DigitalOcean volume for use by postgres
|
|
|
|
fileSystems."${storage-dir}" = {
|
|
|
|
device = storage-device;
|
|
|
|
};
|
|
|
|
|
|
|
|
networking = {
|
|
|
|
hostName = "matrix";
|
|
|
|
domain = "waffle.farm";
|
|
|
|
};
|
|
|
|
|
2021-01-11 02:10:33 +00:00
|
|
|
services.postgresql = {
|
2020-11-09 15:20:09 +00:00
|
|
|
enable = true;
|
|
|
|
dataDir = "${storage-dir}/db";
|
|
|
|
|
|
|
|
initialScript = pkgs.writeText "synapse-init.sql" ''
|
2021-03-17 20:54:46 +00:00
|
|
|
CREATE ROLE "matrix-synapse" WITH LOGIN PASSWORD '${secrets.matrix.psql_password}';
|
2020-11-09 15:20:09 +00:00
|
|
|
CREATE DATABASE "matrix-synapse" WITH OWNER "matrix-synapse"
|
|
|
|
TEMPLATE template0
|
|
|
|
LC_COLLATE = "C"
|
|
|
|
LC_CTYPE = "C";
|
2021-03-11 15:01:01 +00:00
|
|
|
CREATE DATABASE slack_bridge;
|
2021-03-17 20:54:46 +00:00
|
|
|
CREATE USER slackbridge_user WITH PASSWORD '${secrets.matrix.slack_bridge.psql_password}';
|
2021-03-11 15:01:01 +00:00
|
|
|
GRANT ALL PRIVILEGES ON DATABASE slack_bridge to slackbridge_user;
|
2020-11-09 15:20:09 +00:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
services.nginx = {
|
|
|
|
enable = true;
|
|
|
|
recommendedTlsSettings = true;
|
|
|
|
recommendedOptimisation = true;
|
|
|
|
recommendedGzipSettings = true;
|
|
|
|
recommendedProxySettings = true;
|
|
|
|
|
|
|
|
virtualHosts = {
|
|
|
|
"${config.networking.domain}" = {
|
|
|
|
enableACME = true;
|
|
|
|
forceSSL = true;
|
|
|
|
|
|
|
|
locations."= /.well-known/matrix/server".extraConfig =
|
|
|
|
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}';
|
|
|
|
'';
|
|
|
|
locations."= /.well-known/matrix/client".extraConfig =
|
|
|
|
let
|
|
|
|
client = {
|
|
|
|
"m.homeserver" = { "base_url" = "https://${fqdn}"; };
|
|
|
|
"m.identity_server" = { "base_url" = "https://vector.im"; };
|
|
|
|
};
|
|
|
|
# ACAO required to allow element-web on any URL to request this json file
|
|
|
|
in ''
|
|
|
|
add_header Content-Type application/json;
|
|
|
|
add_header Access-Control-Allow-Origin *;
|
|
|
|
return 200 '${builtins.toJSON client}';
|
|
|
|
'';
|
|
|
|
locations."/".extraConfig = ''
|
2020-11-10 07:15:13 +00:00
|
|
|
return 301 https://waffle.farm/register;
|
2020-11-09 15:20:09 +00:00
|
|
|
'';
|
2020-11-10 07:15:13 +00:00
|
|
|
locations."~ ^/(static|register)" = {
|
|
|
|
proxyPass = "http://localhost:5000";
|
|
|
|
};
|
2021-03-11 15:01:01 +00:00
|
|
|
locations."~ ^/slackbridge" = {
|
|
|
|
proxyPass = "http://localhost:9898";
|
|
|
|
};
|
2020-11-09 15:20:09 +00:00
|
|
|
|
|
|
|
};
|
|
|
|
# Reverse proxy for Matrix client-server and server-server communication
|
|
|
|
${fqdn} = {
|
|
|
|
enableACME = true;
|
|
|
|
forceSSL = true;
|
|
|
|
|
|
|
|
# Or do a redirect instead of the 404, or whatever is appropriate for you.
|
|
|
|
# But do not put a Matrix Web client here! See the Element web section below.
|
|
|
|
locations."/".extraConfig = ''
|
|
|
|
return 301 https://chat.waffle.farm;
|
|
|
|
'';
|
|
|
|
|
|
|
|
# forward all Matrix API calls to the synapse Matrix homeserver
|
|
|
|
locations."/_matrix" = {
|
|
|
|
proxyPass = "http://[::1]:8008"; # without a trailing /
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
"chat.${config.networking.domain}" = {
|
|
|
|
enableACME = true;
|
|
|
|
forceSSL = true;
|
|
|
|
serverAliases = [
|
|
|
|
"chat.${config.networking.domain}"
|
|
|
|
];
|
|
|
|
root = pkgs.element-web.override {
|
|
|
|
conf = {
|
|
|
|
default_server_config."m.homeserver" = {
|
|
|
|
"base_url" = "https://${fqdn}";
|
|
|
|
"server_name" = "${config.networking.domain}";
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
services.matrix-synapse = {
|
|
|
|
enable = true;
|
|
|
|
server_name = config.networking.domain;
|
2021-03-17 20:54:46 +00:00
|
|
|
registration_shared_secret = secrets.matrix.registration_secret;
|
2020-11-10 07:15:13 +00:00
|
|
|
extraConfig = ''
|
2021-01-11 02:10:33 +00:00
|
|
|
allow_public_rooms_over_federation: true
|
2020-11-10 07:15:13 +00:00
|
|
|
auto_join_rooms:
|
|
|
|
- "#cryptic-chat:waffle.farm"
|
2021-03-11 15:01:01 +00:00
|
|
|
app_service_config_files:
|
|
|
|
- "${slack-reg-dest-yaml}"
|
2020-11-10 07:15:13 +00:00
|
|
|
'';
|
2020-11-09 15:20:09 +00:00
|
|
|
|
|
|
|
listeners = [
|
|
|
|
{
|
|
|
|
port = 8008;
|
|
|
|
bind_address = "::1";
|
|
|
|
type = "http";
|
|
|
|
tls = false;
|
|
|
|
x_forwarded = true;
|
|
|
|
resources = [
|
|
|
|
{
|
|
|
|
names = [ "client" "federation" ];
|
|
|
|
compress = false;
|
|
|
|
}
|
|
|
|
];
|
|
|
|
}
|
|
|
|
];
|
|
|
|
};
|
|
|
|
security.acme.acceptTerms = true;
|
|
|
|
security.acme.certs = {
|
|
|
|
"${fqdn}".email = "mike@betamike.com";
|
|
|
|
"${config.networking.domain}".email = "mike@betamike.com";
|
|
|
|
"chat.${config.networking.domain}".email = "mike@betamike.com";
|
|
|
|
};
|
2020-11-10 07:15:13 +00:00
|
|
|
users.users.matrix-registration = {
|
|
|
|
home = matrix-reg-dir;
|
|
|
|
createHome = true;
|
2021-10-16 02:16:03 +00:00
|
|
|
isNormalUser = true;
|
2020-11-10 07:15:13 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
systemd.services.matrix-registration = let
|
|
|
|
configFile = pkgs.writeText "matrix-reg-config.yaml" ''
|
|
|
|
server_location: 'https://matrix.waffle.farm:443'
|
2021-10-16 02:16:03 +00:00
|
|
|
base_url: ""
|
2020-11-10 07:15:13 +00:00
|
|
|
server_name: 'waffle.farm'
|
2021-10-16 02:16:03 +00:00
|
|
|
registration_shared_secret: '${secrets.matrix.registration_secret}'
|
|
|
|
admin_api_shared_secret: '${secrets.matrix.admin_api_secret}'
|
|
|
|
client_redirect: 'chat.waffle.farm'
|
|
|
|
client_logo: 'static/images/element-logo.png'
|
2020-11-10 07:15:13 +00:00
|
|
|
db: 'sqlite:///${matrix-reg-dir}/db.sqlite3'
|
|
|
|
host: 'localhost'
|
|
|
|
port: 5000
|
2021-10-16 02:16:03 +00:00
|
|
|
rate_limit: ["1000 per day", "100 per minute"]
|
2020-11-10 07:15:13 +00:00
|
|
|
allow_cors: false
|
2021-10-16 02:16:03 +00:00
|
|
|
ip_logging: false
|
2020-11-10 07:15:13 +00:00
|
|
|
logging:
|
2021-10-16 02:16:03 +00:00
|
|
|
disable_existing_loggers: false
|
2020-11-10 07:15:13 +00:00
|
|
|
version: 1
|
|
|
|
root:
|
|
|
|
level: DEBUG
|
|
|
|
handlers: [console]
|
|
|
|
formatters:
|
|
|
|
brief:
|
|
|
|
format: '%(name)s - %(levelname)s - %(message)s'
|
|
|
|
precise:
|
|
|
|
format: '%(asctime)s - %(name)s - %(levelname)s - %(message)s'
|
|
|
|
handlers:
|
|
|
|
console:
|
|
|
|
class: logging.StreamHandler
|
|
|
|
level: INFO
|
|
|
|
formatter: brief
|
|
|
|
stream: ext://sys.stdout
|
|
|
|
password:
|
|
|
|
min_length: 8
|
2021-10-16 02:16:03 +00:00
|
|
|
username:
|
|
|
|
validation_regex: [] #list of regexes that the selected username must match. Example: '[a-zA-Z]\.[a-zA-Z]'
|
|
|
|
invalidation_regex: [] #list of regexes that the selected username must NOT match. Example: '(admin|support)'
|
2020-11-10 07:15:13 +00:00
|
|
|
'';
|
|
|
|
in {
|
|
|
|
enable = true;
|
|
|
|
after = [ "matrix-synapse.service" ];
|
|
|
|
bindsTo = [ "matrix-synapse.service" ];
|
|
|
|
description = "Matrix-registration daemon";
|
|
|
|
wantedBy = [ "multi-user.target" ];
|
|
|
|
serviceConfig = {
|
|
|
|
Type = "simple";
|
|
|
|
WorkingDirectory = matrix-registration;
|
|
|
|
ExecStart = "${matrix-registration}/bin/matrix-registration --config-path ${configFile} serve";
|
|
|
|
User = "matrix-registration";
|
|
|
|
Restart = "always";
|
|
|
|
};
|
|
|
|
};
|
2021-03-11 15:01:01 +00:00
|
|
|
|
|
|
|
users.users.slackbridge = {
|
|
|
|
home = slackbridge-dir;
|
|
|
|
createHome = true;
|
2021-10-16 02:16:03 +00:00
|
|
|
isNormalUser = true;
|
2021-03-11 15:01:01 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
systemd.services.matrix-appservice-slack = let
|
|
|
|
slackbridge-config-file = pkgs.writeText "matrix-slack-bridge-config.yaml" ''
|
|
|
|
homeserver:
|
|
|
|
server_name: waffle.farm
|
|
|
|
url: http://[::1]:8008
|
|
|
|
media_url: "http://matrix.waffle.farm"
|
|
|
|
appservice_port: 8090
|
|
|
|
username_prefix: "slack_"
|
|
|
|
|
|
|
|
db:
|
|
|
|
engine: "postgres"
|
2021-03-17 20:54:46 +00:00
|
|
|
connectionString: "postgresql://slackbridge_user:${secrets.matrix.slack_bridge.psql_password}@localhost/slack_bridge"
|
2021-03-11 15:01:01 +00:00
|
|
|
|
|
|
|
matrix_admin_room: "!tuUJADDNODYliJTxYK:waffle.farm"
|
|
|
|
|
|
|
|
rtm:
|
|
|
|
enable: true
|
|
|
|
logging: "silent"
|
|
|
|
|
|
|
|
slack_hook_port: 9898
|
|
|
|
inbound_uri_prefix: "https://waffle.farm/slackbridge/"
|
|
|
|
|
|
|
|
# Optional. Allow users to add channels dynamically by using oauth, or puppet themselves.
|
|
|
|
#
|
|
|
|
oauth2:
|
|
|
|
client_id: "4494054004.1702274627236"
|
2021-03-17 20:54:46 +00:00
|
|
|
client_secret: "${secrets.matrix.slack_bridge.client_secret}"
|
2021-03-11 15:01:01 +00:00
|
|
|
#redirect_prefix: "https://waffle.farm/slackbridge/oauth"
|
|
|
|
|
|
|
|
# Optional. Enable metrics reporting on http://0.0.0.0:bridgePort/metrics which can be scraped by prometheus
|
|
|
|
enable_metrics: true
|
|
|
|
|
|
|
|
provisioning:
|
|
|
|
enabled: true
|
|
|
|
require_public_room: true
|
|
|
|
allow_private_channels: true
|
|
|
|
limits:
|
|
|
|
room_count: 20
|
|
|
|
team_count: 1
|
|
|
|
|
|
|
|
puppeting:
|
|
|
|
enabled: true
|
|
|
|
onboard_users: true
|
|
|
|
|
|
|
|
logging:
|
|
|
|
console: "debug"
|
|
|
|
|
|
|
|
bot_profile:
|
|
|
|
displayname: "Slack Bridger"
|
|
|
|
'';
|
|
|
|
in {
|
|
|
|
enable = true;
|
|
|
|
description = "matrix-appservice-slack daemon";
|
|
|
|
wantedBy = [ "multi-user.target" ];
|
|
|
|
serviceConfig = {
|
|
|
|
Type = "simple";
|
|
|
|
ExecStart = "${pkgs.matrix-appservice-slack}/bin/matrix-appservice-slack -c ${slackbridge-config-file} -f ${slack-reg-dest-yaml} -p 8090";
|
|
|
|
User = "slackbridge";
|
|
|
|
Restart = "always";
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2020-11-09 15:20:09 +00:00
|
|
|
}
|