about summary refs log tree commit diff
path: root/nixos/modules/services/networking/netbird/coturn.nix
blob: dd032abb2d75e39b6e8bb5dde5f85ab582b75485 (plain) (blame)
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
149
150
151
152
153
154
155
156
157
158
159
160
{
  config,
  lib,
  pkgs,
  ...
}:

let
  inherit (lib)
    getExe
    literalExpression
    mkAfter
    mkEnableOption
    mkIf
    mkMerge
    mkOption
    optionalAttrs
    optionalString
    ;

  inherit (lib.types)
    bool
    listOf
    nullOr
    path
    port
    str
    ;

  cfg = config.services.netbird.server.coturn;
in

{
  options.services.netbird.server.coturn = {
    enable = mkEnableOption "a Coturn server for Netbird, will also open the firewall on the configured range";

    useAcmeCertificates = mkOption {
      type = bool;
      default = false;
      description = ''
        Whether to use ACME certificates corresponding to the given domain for the server.
      '';
    };

    domain = mkOption {
      type = str;
      description = "The domain under which the coturn server runs.";
    };

    user = mkOption {
      type = str;
      default = "netbird";
      description = ''
        The username used by netbird to connect to the coturn server.
      '';
    };

    password = mkOption {
      type = nullOr str;
      default = null;
      description = ''
        The password of the user used by netbird to connect to the coturn server.
      '';
    };

    passwordFile = mkOption {
      type = nullOr path;
      default = null;
      description = ''
        The path to a file containing the password of the user used by netbird to connect to the coturn server.
      '';
    };

    openPorts = mkOption {
      type = listOf port;
      default = with config.services.coturn; [
        listening-port
        alt-listening-port
        tls-listening-port
        alt-tls-listening-port
      ];
      defaultText = literalExpression ''
        with config.services.coturn; [
          listening-port
          alt-listening-port
          tls-listening-port
          alt-tls-listening-port
        ];
      '';

      description = ''
        The list of ports used by coturn for listening to open in the firewall.
      '';
    };
  };

  config = mkIf cfg.enable (mkMerge [
    {
      assertions = [
        {
          assertion = (cfg.password == null) != (cfg.passwordFile == null);
          message = "Exactly one of `password` or `passwordFile` must be given for the coturn setup.";
        }
      ];

      services.coturn =
        {
          enable = true;

          realm = cfg.domain;
          lt-cred-mech = true;
          no-cli = true;

          extraConfig = ''
            fingerprint
            user=${cfg.user}:${if cfg.password != null then cfg.password else "@password@"}
            no-software-attribute
          '';
        }
        // (optionalAttrs cfg.useAcmeCertificates {
          cert = "@cert@";
          pkey = "@pkey@";
        });

      systemd.services.coturn =
        let
          dir = config.security.acme.certs.${cfg.domain}.directory;
          preStart' =
            (optionalString (cfg.passwordFile != null) ''
              ${getExe pkgs.replace-secret} @password@ ${cfg.passwordFile} /run/coturn/turnserver.cfg
            '')
            + (optionalString cfg.useAcmeCertificates ''
              ${getExe pkgs.replace-secret} @cert@ "$CREDENTIALS_DIRECTORY/cert.pem" /run/coturn/turnserver.cfg
              ${getExe pkgs.replace-secret} @pkey@ "$CREDENTIALS_DIRECTORY/pkey.pem" /run/coturn/turnserver.cfg
            '');
        in
        (optionalAttrs (preStart' != "") { preStart = mkAfter preStart'; })
        // (optionalAttrs cfg.useAcmeCertificates {
          serviceConfig.LoadCredential = [
            "cert.pem:${dir}/fullchain.pem"
            "pkey.pem:${dir}/key.pem"
          ];
        });

      security.acme.certs.${cfg.domain}.postRun = optionalString cfg.useAcmeCertificates "systemctl restart coturn.service";

      networking.firewall = {
        allowedUDPPorts = cfg.openPorts;
        allowedTCPPorts = cfg.openPorts;

        allowedUDPPortRanges = [
          {
            from = cfg.minPort;
            to = cfg.maxPort;
          }
        ];
      };
    }
  ]);
}