coturn.nix 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. { config, lib, ... }:
  2. with lib;
  3. let
  4. cfg = config.roles.server.coturn;
  5. in {
  6. options.roles.server.coturn = {
  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. extraConfig = mkOption {
  16. default = "";
  17. type = types.str;
  18. };
  19. sharedSecretFile = mkOption {
  20. type = types.str;
  21. };
  22. };
  23. config = mkIf cfg.enable {
  24. systemd.services.matrix-synapse.preStart = ''
  25. synapse_coturn=${config.roles.server.synapse.dataDir}/coturn.yaml
  26. cat << EOF > $synapse_coturn
  27. turn_shared_secret: $(cat ${cfg.sharedSecretFile})
  28. EOF
  29. '';
  30. networking.firewall = {
  31. allowedTCPPorts = [ 3487 5349 ];
  32. allowedUDPPorts = [ 3487 5349 ];
  33. allowedUDPPortRanges = [
  34. {
  35. from = 49152;
  36. to = 65535;
  37. }
  38. ];
  39. };
  40. users.users = {
  41. matrix-synapse.extraGroups = [ "turnserver" ];
  42. turnserver.extraGroups = [ "acme" ];
  43. };
  44. services = {
  45. coturn = {
  46. enable = true;
  47. cert = config.security.acme.certs.${cfg.domain}.directory + "/fullchain.pem";
  48. inherit (cfg) extraConfig;
  49. no-tcp-relay = true;
  50. pkey = config.security.acme.certs.${cfg.domain}.directory + "/key.pem";
  51. realm = cfg.domain;
  52. static-auth-secret-file = cfg.sharedSecretFile;
  53. use-auth-secret = true;
  54. };
  55. matrix-synapse = {
  56. extraConfigFiles = [
  57. "${config.roles.server.synapse.dataDir}/coturn.yaml"
  58. ];
  59. settings.turn_uris = [
  60. "turn:${cfg.domain}:3487?transport=udp"
  61. "turn:${cfg.domain}:3487?transport=tcp"
  62. "turns:${cfg.domain}:5349?transport=udp"
  63. "turns:${cfg.domain}:5349?transport=tcp"
  64. ];
  65. };
  66. };
  67. };
  68. }