Skip to content

Commit 3a239c8

Browse files
committed
add declarative port forwarding
1 parent e43e0ed commit 3a239c8

File tree

3 files changed

+17
-4
lines changed

3 files changed

+17
-4
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ test-resources:
1010
$(NIXOS_SHELL) examples/vm-resources.nix
1111

1212
test-forward:
13-
QEMU_NET_OPTS="hostfwd=tcp::2222-:22" $(NIXOS_SHELL) examples/vm.nix
13+
$(NIXOS_SHELL) examples/vm-forward.nix
1414

1515
test-graphics:
1616
$(NIXOS_SHELL) examples/vm-graphics.nix

README.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,12 @@ Or switch to qemu console with `Ctrl-a c` and type:
5252
## Port forwarding
5353

5454
To forward ports from the virtual machine to the host, override the
55-
`QEMU_NET_OPTS` environment variable.
56-
In this example the tcp port 2222 on the host is forwarded to port 22 in the virtual
57-
machine:
55+
`virtualisation.qemu.networkingOptions` NixOS option.
56+
See `examples/vm-forward.nix` where the ssh server running on port 22 in the
57+
virtual machine is made accessible through port 2222 on the host.
58+
59+
If `virtualisation.qemu.networkingOptions` is not overridden the same can be
60+
also achieved by using the `QEMU_NET_OPTS` environment variable.
5861

5962
```console
6063
$ QEMU_NET_OPTS="hostfwd=tcp::2222-:22" nixos-shell

examples/vm-forward.nix

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{ pkgs, ... }: {
2+
services.openssh.enable = true;
3+
virtualisation.qemu.networkingOptions = [
4+
# We need to re-define our usermode network driver
5+
# since we are overriding the default value.
6+
"-net nic,netdev=user.0,model=virtio"
7+
# Than we can use qemu's hostfwd option to forward ports.
8+
"-netdev user,id=user.0,hostfwd=tcp::2222-:22"
9+
];
10+
}

0 commit comments

Comments
 (0)