nix-community / nixops-vbox

NixOps VirtualBox backend [maintainer=@AmineChikhaoui]
GNU Lesser General Public License v3.0
22 stars 15 forks source link

feature request: virtualbox port fowarding #17

Open MasseGuillaume opened 9 years ago

MasseGuillaume commented 9 years ago

I would like to add NAT port forwarding and maybe bridged at some point.

I would look like:

virtualbox.portFowarding = [
  { host: 8080, guest: 8080 },
  { host: 8443, guest: 8443 }
]

template: https://github.com/NixOS/nixops/blob/master/nix/virtualbox.nix#L19 implementation: https://github.com/NixOS/nixops/blob/master/nixops/backends/virtualbox.py

VBoxManage list vms gives nixops-f39b835c-0bc9-11e5-b0cc-60f81dc7aec6-development

VBoxManage modifyvm "nixops-f39b835c-0bc9-11e5-b0cc-60f81dc7aec6-development" --natpf1 "udp-port8080,udp,,8080,,8080" VBoxManage modifyvm "nixops-f39b835c-0bc9-11e5-b0cc-60f81dc7aec6-development" --natpf1 "udp-port8443,udp,,8443,,8443"

Ref. Oracle VM VirtualBox® User Manual Chapter 6. Virtual networking https://www.virtualbox.org/manual/ch06.html Chapter 8. VBoxManage - (8.8.2. Networking settings) https://www.virtualbox.org/manual/ch08.html#idp96527440

3noch commented 7 years ago

I'm guessing the port-forwarding could be achieved with something like nginx on the host. Have you tried that?

Widdershin commented 5 years ago

Not sure if I'll have time to finish this off but this patch is a start:

diff --git a/nix/virtualbox.nix b/nix/virtualbox.nix
index 1e0aa9c..fabfe85 100644
--- a/nix/virtualbox.nix
+++ b/nix/virtualbox.nix
@@ -129,6 +129,15 @@ in
       });
     };

+    deployment.virtualbox.portForwarding = mkOption {
+      default = [];
+      type = types.listOf types.attrs;
+      description = ''
+        Ports to forward to the host system
+      '';
+
+    };
+
   };

diff --git a/nixops/backends/virtualbox.py b/nixops/backends/virtualbox.py
index ed6ce8b..0272d99 100644
--- a/nixops/backends/virtualbox.py
+++ b/nixops/backends/virtualbox.py
@@ -343,6 +343,15 @@ class VirtualBoxState(MachineState):

                 self._update_disk(disk_name, None)

+        # Create port mappings
+        if not self.started:
+            for port_mapping in defn.config["virtualbox"]["portForwarding"]:
+                self._logged_exec(
+                    ["VBoxManage", "modifyvm", self.vm_id, "--natpf1", "tcp-port{host},tcp,127.0.0.1,{host},,{guest}".format(**port_mapping)])
+        else:
+            self.warn("skipping port forwarding config since VirtualBox machine is running")
+
+
         if not self._client_private_key:
             (self._client_private_key, self._client_public_key) = nixops.util.create_key_pair()

This does actually work, for the record, but there's probably a bunch more to think about here, namely:

Bsami commented 5 years ago

This is actually already supported in Nixops. You may want to check this comment on NixOS/nixops#734