30 cfg = config.services.netbird.server.coturn;
34 options.services.netbird.server.coturn = {
35 enable = mkEnableOption "a Coturn server for Netbird, will also open the firewall on the configured range";
37 useAcmeCertificates = mkOption {
41 Whether to use ACME certificates corresponding to the given domain for the server.
47 description = "The domain under which the coturn server runs.";
54 The username used by netbird to connect to the coturn server.
62 The password of the user used by netbird to connect to the coturn server.
63 Be advised this will be world readable in the nix store.
67 passwordFile = mkOption {
71 The path to a file containing the password of the user used by netbird to connect to the coturn server.
75 openPorts = mkOption {
77 default = with config.services.coturn; [
81 alt-tls-listening-port
83 defaultText = literalExpression ''
84 with config.services.coturn; [
88 alt-tls-listening-port
93 The list of ports used by coturn for listening to open in the firewall.
98 config = mkIf cfg.enable (mkMerge [
102 assertion = (cfg.password == null) != (cfg.passwordFile == null);
103 message = "Exactly one of `password` or `passwordFile` must be given for the coturn setup.";
117 user=${cfg.user}:${if cfg.password != null then cfg.password else "@password@"}
118 no-software-attribute
121 // (optionalAttrs cfg.useAcmeCertificates {
126 systemd.services.coturn =
128 dir = config.security.acme.certs.${cfg.domain}.directory;
130 (optionalString (cfg.passwordFile != null) ''
131 ${getExe pkgs.replace-secret} @password@ ${cfg.passwordFile} /run/coturn/turnserver.cfg
133 + (optionalString cfg.useAcmeCertificates ''
134 ${getExe pkgs.replace-secret} @cert@ <(echo -n "$CREDENTIALS_DIRECTORY/cert.pem") /run/coturn/turnserver.cfg
135 ${getExe pkgs.replace-secret} @pkey@ <(echo -n "$CREDENTIALS_DIRECTORY/pkey.pem") /run/coturn/turnserver.cfg
138 (optionalAttrs (preStart' != "") { preStart = mkAfter preStart'; })
139 // (optionalAttrs cfg.useAcmeCertificates {
140 serviceConfig.LoadCredential = [
141 "cert.pem:${dir}/fullchain.pem"
142 "pkey.pem:${dir}/key.pem"
146 security.acme.certs = mkIf cfg.useAcmeCertificates {
147 ${cfg.domain}.postRun = ''
148 systemctl restart coturn.service
152 networking.firewall = {
153 allowedUDPPorts = cfg.openPorts;
154 allowedTCPPorts = cfg.openPorts;
156 allowedUDPPortRanges = with config.services.coturn; [