summaryrefslogtreecommitdiff
path: root/Biz/Cloud/Chat.nix
blob: be856d89db9dbd739c1de497283949a815a50016 (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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
{ config, pkgs, ... }:
#
# a homeserver for matrix.org.
#
# this uses the config.networking.domain as the ACME host. be sure to add the
# fqdn and element subdomains to security.acme.certs.<name>.extraDomainNames
#
# - nixos manual: https://nixos.org/nixos/manual/index.html#module-services-matrix
#
# to create new users:
#
#     nix run nixpkgs.matrix-synapse
#     register_new_matrix_user -k <registration_shared_secret> http://localhost:<matrix_port>
#
let
  fqdn = "matrix.${config.networking.domain}";
  element = "chat.${config.networking.domain}";
  matrix_port = 8448;
in {
  # matrix-synapse server. for what the settings mean, see:
  # https://nixos.org/nixos/manual/index.html#module-services-matrix
  #
  services.matrix-synapse = {
    enable = false;
    settings.server_name = config.networking.domain;
    #registration_shared_secret = "AkGRWSQLga3RoKRFnHhKoeCEIeZzu31y4TRzMRkMyRbBnETkVTSxilf24qySLzQn";
    settings.listeners = [
      {
        port = matrix_port;
        bind_address = "::1";
        type = "http";
        tls = false;
        x_forwarded = true;
        resources = [
          {
            names = [ "client" "federation" ];
            compress = false;
          }
        ];
      }
    ];
  };
  # matrix needs a database
  #
  services.postgresql.enable = true;
  # web proxy for the matrix server
  #
  services.nginx = {
    enable = true;
    recommendedTlsSettings = true;
    recommendedOptimisation = true;
    recommendedGzipSettings = true;
    recommendedProxySettings = true;
    virtualHosts = {
      # route to matrix-synapse
      "${config.networking.domain}" = {
        locations."= /.well-known/matrix/server".extraConfig =
          let
            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"; };
            };
          in ''
            add_header Content-Type application/json;
            add_header Access-Control-Allow-Origin *;
            return 200 '${builtins.toJSON client}';
          '';
      };
      # reverse proxy for matrix client-server and server-server communication
      "${fqdn}" = {
        forceSSL = true;
        useACMEHost = config.networking.domain;
        locations."/".extraConfig = ''
          return 404;
        '';
        locations."/_matrix" = {
          proxyPass = "http://[::1]:${toString matrix_port}";
        };
      };
    };
  };
  # matrix client, available at chat.simatime.com
  #
  # note that element and matrix-synapse must be on separate fqdn's to
  # protect from XSS attacks:
  # https://github.com/vector-im/element-web#important-security-note
  #
  services.nginx.virtualHosts."${element}" = {
    useACMEHost = config.networking.domain;
    forceSSL = true;
    root = pkgs.element-web;
  };
}