synapse.nix 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. { config, lib, pkgs, ... }:
  2. with lib;
  3. let
  4. cfg = config.roles.server.synapse;
  5. in {
  6. options.roles.server.synapse = {
  7. enable = mkOption {
  8. default = false;
  9. type = types.bool;
  10. };
  11. domain = mkOption {
  12. default = config.roles.server.domain;
  13. type = types.str;
  14. };
  15. dataDir = mkOption {
  16. default = "/var/lib/matrix-synapse";
  17. type = types.str;
  18. };
  19. element = mkOption {
  20. default = false;
  21. type = types.bool;
  22. };
  23. registrationSharedSecretFile = mkOption {
  24. type = types.str;
  25. };
  26. };
  27. config = mkIf cfg.enable {
  28. systemd.services.matrix-synapse = {
  29. requires = [ "postgresql.service" ];
  30. after = [ "postgresql.service" ];
  31. };
  32. networking.firewall.allowedTCPPorts = [ 8448 ];
  33. services = {
  34. postgresql = {
  35. enable = mkForce true;
  36. ensureDatabases = [ config.services.matrix-synapse.settings.database.args.database ];
  37. ensureUsers = [{
  38. name = config.services.matrix-synapse.settings.database.args.user;
  39. ensureDBOwnership = true;
  40. }];
  41. };
  42. matrix-synapse = {
  43. enable = true;
  44. inherit (cfg) dataDir;
  45. enableRegistrationScript = false;
  46. settings = rec {
  47. database.allow_unsafe_locale = true;
  48. max_upload_size = "500M";
  49. media_store_path = cfg.dataDir + "/media_store";
  50. public_baseurl = "https://" + server_name;
  51. registration_shared_secret_path = cfg.registrationSharedSecretFile;
  52. server_name = "matrix." + cfg.domain;
  53. };
  54. };
  55. nginx = let
  56. clientConfig."m.homeserver".base_url = config.services.matrix-synapse.settings.public_baseurl;
  57. serverConfig."m.server" = config.services.matrix-synapse.settings.server_name + ":443";
  58. mkWellKnown = data: ''
  59. default_type application/json;
  60. add_header Access-Control-Allow-Origin *;
  61. return 200 '${builtins.toJSON data}';
  62. '';
  63. in {
  64. upstreams.synapse.servers = { "127.0.0.1:8008" = {}; };
  65. virtualHosts = {
  66. ${cfg.domain}.locations = {
  67. "= /.well-known/matrix/client".extraConfig = mkWellKnown clientConfig;
  68. "= /.well-known/matrix/server".extraConfig = mkWellKnown serverConfig;
  69. };
  70. ${config.services.matrix-synapse.settings.server_name} = {
  71. forceSSL = true;
  72. useACMEHost = cfg.domain;
  73. listen = [
  74. { addr = "0.0.0.0"; port = 80; }
  75. { addr = "[::0]"; port = 80; }
  76. { addr = "0.0.0.0"; port = 443; ssl = true; }
  77. { addr = "[::0]"; port = 443; ssl = true; }
  78. { addr = "0.0.0.0"; port = 8448; ssl = true; }
  79. { addr = "[::0]"; port = 8448; ssl = true; }
  80. ];
  81. locations = {
  82. "/".return = if cfg.element then
  83. "301 https://element.${cfg.domain}$request_uri" else
  84. "301 https://${cfg.domain}$request_uri";
  85. "/_matrix".proxyPass = "http://synapse";
  86. };
  87. };
  88. "element.${cfg.domain}" = {
  89. forceSSL = true;
  90. useACMEHost = cfg.domain;
  91. globalRedirect = if cfg.element then null else cfg.domain;
  92. root = if cfg.element then (pkgs.element-web.override {
  93. conf.default_server_config = clientConfig;
  94. }) else null;
  95. };
  96. };
  97. };
  98. };
  99. };
  100. }