1{
2 config,
3 lib,
4 pkgs,
5 ...
6}:
7
8with lib;
9
10let
11 cfg = config.services.networking.websockify;
12in
13{
14 options = {
15 services.networking.websockify = {
16 enable = mkOption {
17 description = "Whether to enable websockify to forward websocket connections to TCP connections.";
18
19 default = false;
20
21 type = types.bool;
22 };
23
24 sslCert = mkOption {
25 description = "Path to the SSL certificate.";
26 type = types.path;
27 };
28
29 sslKey = mkOption {
30 description = "Path to the SSL key.";
31 default = cfg.sslCert;
32 defaultText = literalExpression "config.services.networking.websockify.sslCert";
33 type = types.path;
34 };
35
36 portMap = mkOption {
37 description = "Ports to map by default.";
38 default = { };
39 type = types.attrsOf types.port;
40 };
41 };
42 };
43
44 config = mkIf cfg.enable {
45 systemd.services."websockify@" = {
46 description = "Service to forward websocket connections to TCP connections (from port:to port %I)";
47 script = ''
48 IFS=':' read -a array <<< "$1"
49 ${pkgs.python3Packages.websockify}/bin/websockify --ssl-only \
50 --cert=${cfg.sslCert} --key=${cfg.sslKey} 0.0.0.0:''${array[0]} 0.0.0.0:''${array[1]}
51 '';
52 scriptArgs = "%i";
53 };
54
55 systemd.targets.default-websockify = {
56 description = "Target to start all default websockify@ services";
57 unitConfig.X-StopOnReconfiguration = true;
58 wants = mapAttrsToList (name: value: "websockify@${name}:${toString value}.service") cfg.portMap;
59 wantedBy = [ "multi-user.target" ];
60 };
61 };
62}