summary refs log tree commit diff
path: root/hosts/vps/services/matrix.nix
blob: 66adaed711541adb190fa568b359dade7a66fe8b (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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
{
  pkgs,
  config,
  lib,
  inputs,
  ...
}: {
  config = {
    age.secrets.matrix = {
      file = "${inputs.self}/secrets/matrix.age";
      mode = "600";
      owner = "root";
      group = "root";
    };

    services.dendrite = let
      database_config = {
        connection_string = "postgresql:///dendrite?host=/run/postgresql";
        max_open_conns = 10;
        max_idle_conns = 5;
      };
    in {
      enable = true;

      loadCredential = ["private_key:${config.age.secrets.matrix.path}" "tlsCert:/var/lib/caddy/.local/share/caddy/certificates/acme-v02.api.letsencrypt.org-directory/tzlil.net/tzlil.net.crt" "tlsKey:/var/lib/caddy/.local/share/caddy/certificates/acme-v02.api.letsencrypt.org-directory/tzlil.net/tzlil.net.key"];

      # tlsCert = "$CREDENTIALS_DIRECTORY/tlsCert";
      # tlsKey = "$CREDENTIALS_DIRECTORY/tlsKey";

      # httpPort = null;
      # httpsPort = 8448;

      settings = {
        global = {
          server_name = "tzlil.net";
          private_key = "$CREDENTIALS_DIRECTORY/private_key";

          # preserve across restarts
          jetstream.storage_path = "/var/lib/dendrite/";

          dns_cache = {
            enabled = true;
            cache_size = 4096;
            cache_lifetime = "600s";
          };

          presence = {
            enable_inbound = true;
            enable_outbound = true;
          };

          cache.max_size_estimated = "16gb";
        };
        federation_api.key_perspectives = [
          {
            server_name = "matrix.org";
            keys = [
              {
                key_id = "ed25519:auto";
                public_key = "Noi6WqcDj0QmPxCNQqgezwTlBKrfqehY1u2FyWP9uYw";
              }
              {
                key_id = "ed25519:a_RXGa";
                public_key = "l8Hft5qXKn1vfHrg3p4+W8gELQVo8N13JkluMfmn2sQ";
              }
            ];
          }
        ];

        app_service_api.database = database_config;
        federation_api.database = database_config;
        key_server.database = database_config;
        media_api.database = database_config;
        mscs.database = database_config;
        room_server.database = database_config;
        sync_api.database = database_config;
        user_api.account_database = database_config;
        user_api.device_database = database_config;
      };
    };

    systemd.services.dendrite.serviceConfig.ExecStart = lib.mkForce (lib.strings.concatStringsSep " " [
      "${pkgs.dendrite}/bin/dendrite"
      "--config /run/dendrite/dendrite.yaml"
      "--http-bind-address :8008"
      "--https-bind-address :8448"
      "--tls-cert $CREDENTIALS_DIRECTORY/tlsCert"
      "--tls-key $CREDENTIALS_DIRECTORY/tlsKey"
    ]);

    services.postgresql = {
      enable = true;
      ensureUsers = [
        {
          name = "dendrite";
          # ensurePermissions = {
          # "DATABASE dendrite" = "ALL PRIVILEGES";
          # };
        }
      ];

      ensureDatabases = ["dendrite"];
    };

    users.groups.dendrite = {};
    users.users.dendrite = {
      isSystemUser = true;
      description = "dendrite";
      group = "dendrite";
    };
    # not needed if i use /var/lib/private , DynamicUser can remap the permissions for the service
    systemd.services.dendrite.serviceConfig.User = "dendrite";
    systemd.services.dendrite.serviceConfig.Group = "dendrite";
    systemd.services.dendrite.serviceConfig.DynamicUser = lib.mkForce "false";

    environment.persistence."/nix/persist".directories = [
      {
        directory = "/var/lib/postgresql/${config.services.postgresql.package.psqlSchema}";
        user = "postgres";
        group = "postgres";
      }

      {
        directory = "/var/lib/dendrite";
        user = "dendrite";
        group = "dendrite";
      }
    ];

    systemd.services.dendrite.after = ["postgresql.service"];

    services.caddy = {
      # for federation
      virtualHosts."tzlil.net:8448".extraConfig = ''
        reverse_proxy /_matrix/* localhost:8008
      '';
      # for clients
      virtualHosts."tzlil.net".extraConfig = ''
               reverse_proxy /_matrix/* localhost:8008
               header /.well-known/matrix/* Content-Type application/json
               header /.well-known/matrix/* Access-Control-Allow-Origin *
               respond /.well-known/matrix/server `{"m.server": "tzlil.net"}`
        respond /.well-known/matrix/client `{"m.homeserver": {"base_url": "https://tzlil.net"}}`
      '';
    };
    networking.firewall.allowedTCPPorts = [8448];
  };
}