summary refs log tree commit diff
path: root/nixos
diff options
context:
space:
mode:
authorIan-Woo Kim <ianwookim@gmail.com>2016-12-03 20:57:24 -0800
committerRobin Gloster <mail@glob.in>2017-02-15 05:12:46 +0100
commit4f0b663c2e3939981d2e254a3f1d93ea2901599b (patch)
tree19ef7478f8970ae884c349539aa0573ebec07cea /nixos
parent0bfc631de20a351b23af571e80347df1a58a298a (diff)
downloadnixlib-4f0b663c2e3939981d2e254a3f1d93ea2901599b.tar
nixlib-4f0b663c2e3939981d2e254a3f1d93ea2901599b.tar.gz
nixlib-4f0b663c2e3939981d2e254a3f1d93ea2901599b.tar.bz2
nixlib-4f0b663c2e3939981d2e254a3f1d93ea2901599b.tar.lz
nixlib-4f0b663c2e3939981d2e254a3f1d93ea2901599b.tar.xz
nixlib-4f0b663c2e3939981d2e254a3f1d93ea2901599b.tar.zst
nixlib-4f0b663c2e3939981d2e254a3f1d93ea2901599b.zip
nixos-container: hostPort -> forwardPort and forwardPort is now a list of (protocol,hostPort,containerPort).
Diffstat (limited to 'nixos')
-rw-r--r--nixos/modules/virtualisation/containers.nix45
1 files changed, 32 insertions, 13 deletions
diff --git a/nixos/modules/virtualisation/containers.nix b/nixos/modules/virtualisation/containers.nix
index 83b7a2fdecdd..5c867cbc2c81 100644
--- a/nixos/modules/virtualisation/containers.nix
+++ b/nixos/modules/virtualisation/containers.nix
@@ -90,13 +90,13 @@ let
           extraFlags+=" --network-bridge=$HOST_BRIDGE"
         fi
         if [ -n "$HOST_PORT" ]; then
-	  OIFS=$IFS
-	  IFS=","
+          OIFS=$IFS
+          IFS=","
           for i in $HOST_PORT
-	  do
+          do
               extraFlags+=" --port=$i"
-	  done
-	  IFS=$OIFS
+          done
+          IFS=$OIFS
         fi
       fi
 
@@ -325,12 +325,29 @@ let
       '';
     };
 
-    hostPort = mkOption {
-      type = types.listOf types.str;
-      default = null;
-      example = [ "8080" ];
+    forwardPorts = mkOption {
+      type = types.listOf (types.submodule {
+        options = {
+          protocol = mkOption {
+            type = types.str;
+            default = "tcp";
+            description = "The protocol specifier for port forwarding between host and container";
+          };
+          hostPort = mkOption {
+            type = types.int;
+            description = "Source port of the external interface on host";
+          };
+          containerPort = mkOption {
+            type = types.nullOr types.int;
+            default = null;
+            description = "Target port of container";
+          };
+        };
+      });
+      default = [];
+      example = [ { protocol = "tcp"; hostPort = 8080; containerPort = 80; } ];
       description = ''
-        List of forwarded ports from the host to the container. 
+        List of forwarded ports from host to container. Each forwarded port is specified by protocol, hostPort and containerPort. By default, protocol is tcp and hostPort and containerPort are assumed to be the same if containerPort is not explicitly given. 
       '';
     };
 
@@ -662,7 +679,9 @@ in
     # Generate a configuration file in /etc/containers for each
     # container so that container@.target can get the container
     # configuration.
-    environment.etc = mapAttrs' (name: cfg: nameValuePair "containers/${name}.conf"
+    environment.etc =
+      let mkPortStr = p: p.protocol + ":" + (toString p.hostPort) + ":" + (if p.containerPort == null then toString p.hostPort else toString p.containerPort); 
+      in mapAttrs' (name: cfg: nameValuePair "containers/${name}.conf"
       { text =
           ''
             SYSTEM_PATH=${cfg.path}
@@ -671,8 +690,8 @@ in
               ${optionalString (cfg.hostBridge != null) ''
                 HOST_BRIDGE=${cfg.hostBridge}
               ''}
-              ${optionalString (length cfg.hostPort > 0) ''
-                HOST_PORT=${concatStringsSep "," cfg.hostPort}
+              ${optionalString (length cfg.forwardPorts > 0) ''
+                HOST_PORT=${concatStringsSep "," (map mkPortStr cfg.forwardPorts)}
               ''}
               ${optionalString (cfg.hostAddress != null) ''
                 HOST_ADDRESS=${cfg.hostAddress}