summaryrefslogtreecommitdiff
path: root/modules/nixos/matrix/synapse.nix
diff options
context:
space:
mode:
authorAzat Bahawi <azat@bahawi.net>2022-12-17 16:39:09 +0300
committerAzat Bahawi <azat@bahawi.net>2022-12-17 16:39:09 +0300
commit8f137c28230623259a964484adcf31fe00756594 (patch)
tree82bce6a13fda125087cf6d9dc80aa91d9230d6c4 /modules/nixos/matrix/synapse.nix
parent3229e56e0d3620ddc735edcfbbefb167efa3b23f (diff)
2022-12-17
Diffstat (limited to 'modules/nixos/matrix/synapse.nix')
-rw-r--r--modules/nixos/matrix/synapse.nix93
1 files changed, 93 insertions, 0 deletions
diff --git a/modules/nixos/matrix/synapse.nix b/modules/nixos/matrix/synapse.nix
new file mode 100644
index 0000000..6ff5e0d
--- /dev/null
+++ b/modules/nixos/matrix/synapse.nix
@@ -0,0 +1,93 @@
+{
+ config,
+ lib,
+ pkgs,
+ ...
+}:
+with lib; let
+ cfg = config.nixfiles.modules.matrix.synapse;
+in {
+ options.nixfiles.modules.matrix.synapse = {
+ enable = mkEnableOption "Synapse Matrix server";
+
+ domain = mkOption {
+ description = "Domain name sans protocol scheme.";
+ type = with types; str;
+ default = config.networking.domain;
+ };
+ };
+
+ config = let
+ bind_address = "127.0.0.1";
+ port = 8448;
+ in
+ mkIf cfg.enable {
+ nixfiles.modules = {
+ nginx = {
+ enable = true;
+ upstreams.synapse.servers."${bind_address}:${toString port}" = {};
+ virtualHosts.${cfg.domain}.locations = {
+ "~ ^(/_matrix|/_synapse/client)".proxyPass = "http://synapse";
+ "= /.well-known/matrix/server" = {
+ extraConfig = ''
+ add_header Content-Type application/json;
+ '';
+ return = "200 '${
+ generators.toJSON {} {"m.server" = "${cfg.domain}:443";}
+ }'";
+ };
+ "= /.well-known/matrix/client" = {
+ extraConfig = ''
+ add_header Content-Type application/json;
+ add_header Access-Control-Allow-Origin *;
+ '';
+ return = "200 '${
+ generators.toJSON {} {
+ "m.homeserver".base_url = "https://${cfg.domain}";
+ }
+ }'";
+ };
+ };
+ };
+ postgresql.enable = true;
+ };
+
+ services = let
+ db = "synapse";
+ in {
+ matrix-synapse = {
+ enable = true;
+ server_name = config.networking.domain;
+
+ database_type = "psycopg2";
+ database_name = db;
+ database_user = db;
+
+ listeners = [
+ {
+ inherit bind_address port;
+ type = "http";
+ tls = false;
+ x_forwarded = true;
+ resources = [
+ {
+ names = ["client" "federation"];
+ compress = false;
+ }
+ ];
+ }
+ ];
+ };
+
+ postgresql = {
+ ensureDatabases = [db];
+ ensureUsers = [
+ {
+ name = db;
+ ensurePermissions."DATABASE \"${db}\"" = "ALL";
+ }
+ ];
+ };
+ };
+ };
+}