nix

configuration files that power my machines
Log | Files | Refs | README | LICENSE

matrix.nix (2232B)


      1 {
      2   config,
      3   lib,
      4   ...
      5 }: let
      6   cfg = config.services.haravara.matrix;
      7 in {
      8   options.services.haravara.matrix = {
      9     enable = lib.mkEnableOption "Matrix Conduit server";
     10     port = lib.mkOption {
     11       type = lib.types.port;
     12       default = 6167;
     13       description = "Internal port for Conduit";
     14     };
     15     serverName = lib.mkOption {
     16       type = lib.types.str;
     17       description = "The server name for Matrix";
     18     };
     19     redirTarget = lib.mkOption {
     20       type = lib.types.str;
     21       description = "The target domain for the root redirect";
     22     };
     23     dataDir = lib.mkOption {
     24       type = lib.types.str;
     25       default = "/var/lib/matrix-conduit";
     26       description = "Directory for Conduit state data";
     27     };
     28     registrationTokenFile = lib.mkOption {
     29       type = lib.types.nullOr lib.types.path;
     30       default = null;
     31       description = "Path to a file containing the registration token";
     32     };
     33   };
     34 
     35   config = lib.mkIf cfg.enable {
     36     services.matrix-conduit = {
     37       enable = true;
     38       settings.global =
     39         {
     40           server_name = cfg.serverName;
     41           database_backend = "rocksdb";
     42           inherit (cfg) port;
     43           max_request_size = 20000000;
     44           allow_registration = true;
     45           allow_federation = true;
     46           trusted_servers = ["matrix.org"];
     47           address = "127.0.0.1";
     48         }
     49         // lib.optionalAttrs (cfg.registrationTokenFile != null) {
     50           registration_token = {_secret = cfg.registrationTokenFile;};
     51         };
     52     };
     53 
     54     # Basic directory setup for Conduit
     55     systemd.tmpfiles.rules = [
     56       "d ${cfg.dataDir}/appservices 0700 conduit conduit -"
     57     ];
     58 
     59     services.caddy.virtualHosts."${cfg.serverName}" = {
     60       extraConfig = ''
     61         reverse_proxy /_matrix/* 127.0.0.1:${toString cfg.port}
     62 
     63         redir / https://${cfg.redirTarget}
     64 
     65         header /.well-known/matrix/* Content-Type application/json
     66         header /.well-known/matrix/* Access-Control-Allow-Origin *
     67 
     68         respond /.well-known/matrix/server `{"m.server": "${cfg.serverName}:443"}`
     69         respond /.well-known/matrix/client `{"m.homeserver":{"base_url":"https://${cfg.serverName}"},"m.identity_server":{"base_url":"https://${cfg.serverName}"}}`
     70       '';
     71     };
     72   };
     73 }