NixOS:nixos-rebuild build-vm: Difference between revisions

imported>Ibizaman
Add some info about build-vm-with-bootloader
Raboof (talk | contribs)
how to expose a qemu VM port to the host
Line 49: Line 49:
When running a virtual machine a file called <code>$hostname.qcow2</code> is created in your current working directory. After changing your <code>/etc/nixos/configuration.nix</code> delete this file, rebuild and then start the new virtual machine. Now you should be able to login.
When running a virtual machine a file called <code>$hostname.qcow2</code> is created in your current working directory. After changing your <code>/etc/nixos/configuration.nix</code> delete this file, rebuild and then start the new virtual machine. Now you should be able to login.


== Networking ==


=== qemu ===
To enable connecting from your host to your virtual machine, you'll need to forcefully override the default networking settings to apply those from https://wiki.qemu.org/Documentation/Networking#How_to_get_SSH_access_to_a_guest instead. For example, to expose the VM's port 80 on the (unprivileged) port 8009 of the 'localhost' of the host:
<syntaxhighlight lang="nix">
{ config, pkgs, lib, modulesPath, ... }:
{
  imports = [
    (modulesPath + "/virtualisation/qemu-vm.nix")
  ];
  virtualisation.qemu.networkingOptions = lib.mkForce [
    "-device e1000,netdev=net0"
    "-netdev user,id=net0,hostfwd=tcp:127.0.0.1:8009-:80,\${QEMU_NET_OPTS:+,$QEMU_NET_OPTS}"
  ];
  networking.firewall.allowedTCPPorts = [
    80
  ];
}
</syntaxhighlight>


== Alternatives ==
== Alternatives ==