From de21486c39afb61b355baa3f2e4787ee7fe11c09 Mon Sep 17 00:00:00 2001 From: Lillian-Violet Date: Sun, 21 Jan 2024 22:58:18 +0100 Subject: [PATCH] Add conduit --- nixos/hosts/queen/configuration.nix | 2 + .../package-configs/conduit/configuration.nix | 143 ++++++++++++++++++ 2 files changed, 145 insertions(+) create mode 100644 nixos/server/package-configs/conduit/configuration.nix diff --git a/nixos/hosts/queen/configuration.nix b/nixos/hosts/queen/configuration.nix index d178c9b..c328563 100644 --- a/nixos/hosts/queen/configuration.nix +++ b/nixos/hosts/queen/configuration.nix @@ -32,6 +32,7 @@ ../../server/package-configs/jellyfin/configuration.nix ../../server/package-configs/ombi/configuration.nix ../../server/package-configs/aria2/configuration.nix + ../../server/package-configs/conduit/configuration.nix ]; boot.tmp.cleanOnBoot = true; @@ -89,6 +90,7 @@ akkoma age fzf + matrix-conduit docker docker-compose git diff --git a/nixos/server/package-configs/conduit/configuration.nix b/nixos/server/package-configs/conduit/configuration.nix new file mode 100644 index 0000000..60b25fc --- /dev/null +++ b/nixos/server/package-configs/conduit/configuration.nix @@ -0,0 +1,143 @@ +{ + inputs, + outputs, + lib, + config, + pkgs, + ... +}: let + # You'll need to edit these values + # The hostname that will appear in your user and room IDs + server_name = "matrix.gladtherescake.eu"; + + # The hostname that Conduit actually runs on + # + # This can be the same as `server_name` if you want. This is only necessary + # when Conduit is running on a different machine than the one hosting your + # root domain. This configuration also assumes this is all running on a single + # machine, some tweaks will need to be made if this is not the case. + matrix_hostname = "${server_name}"; + + # Build a dervation that stores the content of `${server_name}/.well-known/matrix/server` + well_known_server = pkgs.writeText "well-known-matrix-server" '' + { + "m.server": "${matrix_hostname}" + } + ''; + + # Build a dervation that stores the content of `${server_name}/.well-known/matrix/client` + well_known_client = pkgs.writeText "well-known-matrix-client" '' + { + "m.homeserver": { + "base_url": "https://${matrix_hostname}" + } + } + ''; +in { + # Configure Conduit itself + services.matrix-conduit = { + enable = true; + + # This causes NixOS to use the flake defined in this repository instead of + # the build of Conduit built into nixpkgs. + package = flake-inputs.conduit.packages.${pkgs.system}.default; + + settings.global = { + inherit server_name; + }; + }; + + # ACME data must be readable by the NGINX user + users.users.nginx.extraGroups = [ + "acme" + ]; + + # Configure NGINX as a reverse proxy + services.nginx = { + enable = true; + recommendedProxySettings = true; + + virtualHosts = { + "${matrix_hostname}" = { + forceSSL = true; + enableACME = true; + + listen = [ + { + addr = "0.0.0.0"; + port = 443; + ssl = true; + } + { + addr = "[::]"; + port = 443; + ssl = true; + } + { + addr = "0.0.0.0"; + port = 8448; + ssl = true; + } + { + addr = "[::]"; + port = 8448; + ssl = true; + } + ]; + + locations."/_matrix/" = { + proxyPass = "http://backend_conduit$request_uri"; + proxyWebsockets = true; + extraConfig = '' + proxy_set_header Host $host; + proxy_buffering off; + ''; + }; + + extraConfig = '' + merge_slashes off; + ''; + }; + + "${server_name}" = { + forceSSL = true; + enableACME = true; + + locations."=/.well-known/matrix/server" = { + # Use the contents of the derivation built previously + alias = "${well_known_server}"; + + extraConfig = '' + # Set the header since by default NGINX thinks it's just bytes + default_type application/json; + ''; + }; + + locations."=/.well-known/matrix/client" = { + # Use the contents of the derivation built previously + alias = "${well_known_client}"; + + extraConfig = '' + # Set the header since by default NGINX thinks it's just bytes + default_type application/json; + + # https://matrix.org/docs/spec/client_server/r0.4.0#web-browser-clients + add_header Access-Control-Allow-Origin "*"; + ''; + }; + }; + }; + + upstreams = { + "backend_conduit" = { + servers = { + "[::1]:${toString config.services.matrix-conduit.settings.global.port}" = {}; + }; + }; + }; + }; + + # Open firewall ports for HTTP, HTTPS, and Matrix federation + networking.firewall.allowedTCPPorts = [80 443 8448]; + networking.firewall.allowedUDPPorts = [80 443 8448]; +}