summaryrefslogtreecommitdiffstats
path: root/nixos/modules/services/misc/turn-rs.nix
blob: b591205417fb8bfec4f75ab1cc216b0f2c0948a6 (plain)
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
{
  config,
  pkgs,
  lib,
  ...
}:

let
  cfg = config.services.turn-rs;
  format = pkgs.formats.toml { };
in
{
  options.services.turn-rs = {
    enable = lib.mkEnableOption "turn-rs server";
    package = lib.mkPackageOption pkgs "turn-rs" { };

    secretFile = lib.mkOption {
      type = lib.types.nullOr lib.types.path;
      default = null;
      example = "/run/keys/turn-rs.env";
      description = ''
        Environment variables from this file will be interpolated into the
        final config file using envsubst with this syntax: `$ENVIRONMENT` or
        `''${VARIABLE}`.
        The file should contain lines formatted as `SECRET_VAR=SECRET_VALUE`.
        This is useful to avoid putting secrets into the nix store.
      '';
    };

    settings = lib.mkOption {
      type = lib.types.submodule {
        freeformType = format.type;
      };
      description = "Turn-rs server config file";
      default = { };
      example = {
        turn = {
          realm = "localhost";
          interfaces = [
            {
              transport = "udp";
              bind = "127.0.0.1:3478";
              external = "127.0.0.1:3478";
            }
            {
              transport = "tcp";
              bind = "127.0.0.1:3478";
              external = "127.0.0.1:3478";
            }
          ];
        };

        auth.static_credentials = {
          user1 = "test";
          user2 = "test";
        };
      };
    };
  };

  config = lib.mkIf cfg.enable {
    services.turn-rs.settings = {
      api.bind = lib.mkDefault "127.0.0.1:3000";
      log.level = lib.mkDefault "info";
    };

    systemd.services.turn-rs = {
      enable = true;
      wantedBy = [ "multi-user.target" ];
      description = "Turn-rs Server Daemon";
      preStart =
        let
          configFile = format.generate "turn-rs-config.toml" cfg.settings;
        in
        ''
          ${lib.getExe pkgs.envsubst} -i "${configFile}" -o /run/turn-rs/config.toml
        '';
      serviceConfig = {
        RuntimeDirectory = "turn-rs";
        EnvironmentFile = lib.optional (cfg.secretFile != null) cfg.secretFile;
        ExecStart = "${lib.getExe cfg.package} --config=/run/turn-rs/config.toml";
        DynamicUser = true;
      };
    };
  };
}