Skip to content

Commit 1c18b28

Browse files
committed
nixos/livekit: init
1 parent bbbf9da commit 1c18b28

File tree

1 file changed

+139
-0
lines changed

1 file changed

+139
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
{
2+
config,
3+
lib,
4+
pkgs,
5+
utils,
6+
...
7+
}: let
8+
cfg = config.services.livekit;
9+
format = pkgs.formats.json {};
10+
in {
11+
meta.maintainers = with lib.maintainers; [quadradical];
12+
options.services.livekit = {
13+
enable = lib.mkEnableOption "Enable the livekit server";
14+
package = lib.mkPackageOption pkgs "livekit" {};
15+
16+
keyFile = lib.mkOption {
17+
type = lib.types.path;
18+
description = ''
19+
LiveKit key file holding one or multiple application secrets. Use `livekit-server generate-keys` to generate a random key name and secret.
20+
21+
The file should have the format `<keyname>: <secret>`. Example:
22+
```
23+
lk-jwt-service: f6lQGaHtM5HfgZjIcec3cOCRfiDqIine4CpZZnqdT5cE
24+
```
25+
26+
Individual key/secret pairs need to be passed to clients to connect to this instance.
27+
'';
28+
};
29+
30+
openFirewall = lib.mkOption {
31+
type = lib.types.bool;
32+
default = false;
33+
description = "Opens port range for LiveKit on the firewall.";
34+
};
35+
36+
settings = lib.mkOption {
37+
type = lib.types.submodule {
38+
freeformType = format.type;
39+
options = {
40+
port = lib.mkOption {
41+
type = lib.types.port;
42+
default = 7880;
43+
description = "Main TCP port for RoomService and RTC endpoint.";
44+
};
45+
46+
rtc = {
47+
port_range_start = lib.mkOption {
48+
type = lib.types.int;
49+
default = 50000;
50+
description = "Start of UDP port range for WebRTC";
51+
};
52+
53+
port_range_end = lib.mkOption {
54+
type = lib.types.int;
55+
default = 51000;
56+
description = "End of UDP port range for WebRTC";
57+
};
58+
59+
use_external_ip = lib.mkOption {
60+
type = lib.types.bool;
61+
default = false;
62+
description = ''
63+
When set to true, attempts to discover the host's public IP via STUN.
64+
This is useful for cloud environments such as AWS & Google where hosts have an internal IP that maps to an external one.
65+
'';
66+
};
67+
};
68+
};
69+
};
70+
default = {};
71+
description = ''
72+
LiveKit configuration file expressed in nix.
73+
74+
For an example configuration, see <https://docs.livekit.io/home/self-hosting/deployment/#configuration>.
75+
For all possible values, see <https://github.com/livekit/livekit/blob/master/config-sample.yaml>.
76+
'';
77+
};
78+
};
79+
80+
config = lib.mkIf cfg.enable {
81+
networking.firewall = lib.mkIf cfg.openFirewall {
82+
allowedTCPPorts = [
83+
cfg.settings.port
84+
];
85+
allowedUDPPortRanges = [
86+
{
87+
from = cfg.settings.rtc.port_range_start;
88+
to = cfg.settings.rtc.port_range_end;
89+
}
90+
];
91+
};
92+
93+
systemd.services.livekit = {
94+
description = "LiveKit SFU server";
95+
documentation = ["https://docs.livekit.io"];
96+
wantedBy = ["multi-user.target"];
97+
wants = ["network-online.target"];
98+
after = ["network-online.target"];
99+
100+
serviceConfig = {
101+
LoadCredential = ["livekit-secrets:${cfg.keyFile}"];
102+
ExecStart = utils.escapeSystemdExecArgs [
103+
(lib.getExe cfg.package)
104+
"--config=${format.generate "livekit.json" cfg.settings}"
105+
"--key-file=/run/credentials/livekit.service/livekit-secrets"
106+
];
107+
DynamicUser = true;
108+
LockPersonality = true;
109+
MemoryDenyWriteExecute = true;
110+
ProtectClock = true;
111+
ProtectControlGroups = true;
112+
ProtectHostname = true;
113+
ProtectKernelLogs = true;
114+
ProtectKernelModules = true;
115+
ProtectKernelTunables = true;
116+
PrivateDevices = true;
117+
PrivateMounts = true;
118+
PrivateUsers = true;
119+
RestrictAddressFamilies = [
120+
"AF_INET"
121+
"AF_INET6"
122+
"AF_NETLINK"
123+
];
124+
RestrictNamespaces = true;
125+
RestrictRealtime = true;
126+
ProtectHome = true;
127+
SystemCallArchitectures = "native";
128+
SystemCallFilter = [
129+
"@system-service"
130+
"~@privileged"
131+
"~@resources"
132+
];
133+
Restart = "on-failure";
134+
RestartSec = 5;
135+
UMask = "077";
136+
};
137+
};
138+
};
139+
}

0 commit comments

Comments
 (0)