{
  config,
  pkgs,
  lib,
  ...
}: let
  fqdn = "matrix.broccoli.town";
in {
  services.postgresql = {
    enable = true;
    ensureUsers = [
      {
        name = "matrix-synapse";
        ensurePermissions = {
          "DATABASE synapse" = "ALL PRIVILEGES";
        };
      }
    ];
    authentication = ''
      local all all trust
    '';
    ensureDatabases = [
      "synapse"
    ];
  };

  services.matrix-synapse = {
    enable = true;
    settings = {
      enable_metrics = true;
      server_name = "broccoli.town";
      database = {
        name = "psycopg2";
        args = {
          database = "synapse";
          user = "matrix-synapse";
        };
      };
      max_upload_size = "50M";
      listeners = [
        {
          port = 8008;
          type = "http";
          tls = false;
          x_forwarded = true;
          resources = [
            {
              names = ["client" "federation"];
              compress = false;
            }
          ];
        }
        {
          port = 9000;
          type = "metrics";
          tls = false;
          bind_addresses = [
            "0.0.0.0"
          ];
          resources = [
          ];
        }
      ];
    };
  };
}
