aboutsummaryrefslogtreecommitdiff
path: root/nixpkgs/nixos/modules/services/networking/websockify.nix
blob: 27cb47be12f7e0335aa7776acc7703cdba2ea560 (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
{ config, lib, pkgs, ... }:

with lib;

let cfg = config.services.networking.websockify; in {
  options = {
    services.networking.websockify = {
      enable = mkOption {
        description = "Whether to enable websockify to forward websocket connections to TCP connections.";

        default = false;

        type = types.bool;
      };

      sslCert = mkOption {
        description = "Path to the SSL certificate.";
        type = types.path;
      };

      sslKey = mkOption {
        description = "Path to the SSL key.";
        default = cfg.sslCert;
        defaultText = "config.services.networking.websockify.sslCert";
        type = types.path;
      };

      portMap = mkOption {
        description = "Ports to map by default.";
        default = {};
        type = types.attrsOf types.int;
      };
    };
  };

  config = mkIf cfg.enable {
    systemd.services."websockify@" = {
      description = "Service to forward websocket connections to TCP connections (from port:to port %I)";
      script = ''
        IFS=':' read -a array <<< "$1"
        ${pkgs.pythonPackages.websockify}/bin/websockify --ssl-only \
          --cert=${cfg.sslCert} --key=${cfg.sslKey} 0.0.0.0:''${array[0]} 0.0.0.0:''${array[1]}
      '';
      scriptArgs = "%i";
    };

    systemd.targets.default-websockify = {
      description = "Target to start all default websockify@ services";
      unitConfig.X-StopOnReconfiguration = true;
      wants = mapAttrsToList (name: value: "websockify@${name}:${toString value}.service") cfg.portMap;
      wantedBy = [ "multi-user.target" ];
    };
  };
}