| 
									
										
										
										
											2014-04-14 16:26:48 +02:00
										 |  |  | { config, lib, pkgs, ... }: | 
					
						
							| 
									
										
										
										
											2013-02-14 21:50:41 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-04-14 16:26:48 +02:00
										 |  |  | with lib; | 
					
						
							| 
									
										
										
										
											2013-02-14 21:50:41 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | let cfg = config.services.networking.websockify; in { | 
					
						
							|  |  |  |   options = { | 
					
						
							|  |  |  |     services.networking.websockify = { | 
					
						
							|  |  |  |       enable = mkOption {   | 
					
						
							| 
									
										
										
										
											2013-02-18 11:51:33 -05:00
										 |  |  |         description = "Whether to enable websockify to forward websocket connections to TCP connections."; | 
					
						
							| 
									
										
										
										
											2013-02-14 21:50:41 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |         default = false;    | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         type = types.bool;  | 
					
						
							|  |  |  |       }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       sslCert = mkOption { | 
					
						
							| 
									
										
										
										
											2013-02-18 11:51:33 -05:00
										 |  |  |         description = "Path to the SSL certificate."; | 
					
						
							| 
									
										
										
										
											2013-02-14 21:50:41 -05:00
										 |  |  |         type = types.path; | 
					
						
							|  |  |  |       }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       sslKey = mkOption { | 
					
						
							| 
									
										
										
										
											2013-02-18 11:51:33 -05:00
										 |  |  |         description = "Path to the SSL key."; | 
					
						
							| 
									
										
										
										
											2013-02-14 21:50:41 -05:00
										 |  |  |         default = cfg.sslCert; | 
					
						
							| 
									
										
										
										
											2013-02-15 14:36:20 -05:00
										 |  |  |         defaultText = "config.services.networking.websockify.sslCert"; | 
					
						
							| 
									
										
										
										
											2013-02-14 21:50:41 -05:00
										 |  |  |         type = types.path; | 
					
						
							|  |  |  |       }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       portMap = mkOption { | 
					
						
							| 
									
										
										
										
											2013-02-18 11:51:33 -05:00
										 |  |  |         description = "Ports to map by default."; | 
					
						
							| 
									
										
										
										
											2013-02-14 21:50:41 -05:00
										 |  |  |         default = {}; | 
					
						
							|  |  |  |         type = types.attrsOf types.int; | 
					
						
							|  |  |  |       }; | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  |   }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   config = mkIf cfg.enable { | 
					
						
							|  |  |  |     systemd.services."websockify@" = { | 
					
						
							| 
									
										
										
										
											2013-02-18 11:55:10 -05:00
										 |  |  |       description = "Service to forward websocket connections to TCP connections (from port:to port %I)"; | 
					
						
							| 
									
										
										
										
											2013-02-14 21:50:41 -05:00
										 |  |  |       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" = { | 
					
						
							| 
									
										
										
										
											2013-02-18 11:55:10 -05:00
										 |  |  |       description = "Target to start all default websockify@ services"; | 
					
						
							| 
									
										
										
										
											2013-02-18 11:55:46 -05:00
										 |  |  |       unitConfig."X-StopOnReconfiguration" = true; | 
					
						
							| 
									
										
										
										
											2013-02-14 21:50:41 -05:00
										 |  |  |       wants = mapAttrsToList (name: value: "websockify@${name}:${toString value}.service") cfg.portMap; | 
					
						
							|  |  |  |       wantedBy = [ "multi-user.target" ]; | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  |   }; | 
					
						
							|  |  |  | } |