summaryrefslogtreecommitdiff
path: root/modules/nixos/matrix-conduit.nix
blob: 726f377c23a12048153e93f609d9f647001643cd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
{
  config,
  pkgs,
  ...
}: let
  well_known_server = pkgs.writeText "well-known-matrix-server" ''
    {
      "m.server": "matrix.${config.services.matrix-conduit.settings.global.server_name}"
    }
  '';
  well_known_client = pkgs.writeText "well-known-matrix-client" ''
    {
      "m.homeserver": {
        "base_url": "https://matrix.${config.services.matrix-conduit.settings.global.server_name}"
      }
  '';
in {
  services.matrix-conduit = {
    enable = true;
    settings.global = {
      server_name = "${config.networking.domain}";
    };
  };
  services.nginx = {
    enable = true;
    virtualHosts = {
      "matrix.${config.services.matrix-conduit.settings.global.server_name}" = {
        forceSSL = true;
        enableACME = true;
        listen = [
          {
            addr = "0.0.0.0";
            port = 443;
            ssl = true;
          }
          {
            addr = "0.0.0.0";
            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;
        '';
      };
      "${config.services.matrix-conduit.settings.global.server_name}" = {
        forceSSL = true;
        enableACME = true;
        locations."/.well-known/matrix/server" = {
          alias = "${well_known_server}";
          extraConfig = ''
            default_type application/json;
          '';
        };
        locations."/.well-known/matrix/client" = {
          alias = "${well_known_client}";
          extraConfig = ''
            default_type application/json;
            add_header Access-Control-Allow-Origin "";
          '';
        };
      };
    };
    upstreams = {
      backend-conduit = {
        servers = {
          "localhost:${builtins.toString config.services.matrix-conduit.settings.global.port}" = {};
        };
      };
    };
  };
  networking.firewall.allowedTCPPorts = [8448];
  networking.firewall.allowedUDPPorts = [8448];
}