NixOS:nixos-rebuild build-vm: Difference between revisions
m Add subtitle and fix formatting |
m fix sectioning |
||
(One intermediate revision by the same user not shown) | |||
Line 1: | Line 1: | ||
The commands | |||
<code> | * <code>nixos-rebuild build-vm</code> | ||
nixos-rebuild build-vm | * <code>nixos-rebuild build-vm-with-bootloader</code> | ||
will build a virtual machine running NixOS with your system's configuration (usually <code>/etc/nixos/configuration.nix</code>) by using [[Qemu]]. | |||
One of its usages is for testing new configurations without needing to try them on the host. | |||
= Usage = | |||
== Create login user == | |||
Before calling one of the commands above, you will have to create a user with an initial password first which you can login to because your passwords of your current system are not carried over to the virtual machine. | |||
Here is an example for a default user which you can simply add to your system config: | |||
<syntaxhighlight lang="nix"> | <syntaxhighlight lang="nix"> | ||
users.users.nixosvmtest.isSystemUser = true ; | users.users.nixosvmtest.isSystemUser = true; | ||
users.users.nixosvmtest.initialPassword = "test"; | users.users.nixosvmtest.initialPassword = "test"; | ||
users.groups.nixosvmtest = {}; | |||
users.users.nixosvmtest.group = "nixosvmtest"; | |||
</syntaxhighlight> | </syntaxhighlight> | ||
See this [https://discourse.nixos.org/t/default-login-and-password-for-nixos/4683/2 discourse-answer] for more information. | |||
== Create and run virtual machine == | |||
Create the virtual machine by using of the commands above. Afterwards you will find an executable file in <code>./result/bin</code>. By executing the file qemu will open up and you can start testing the system. | |||
== Examples == | |||
<syntaxhighlight lang="bash"> | |||
# build vm of your system config | |||
nixos-rebuild build-vm | |||
# e.g. to specify the environment variables / cores used | |||
nixos-rebuild build-vm\ | |||
-I nixos-config=./configuration.nix\ | |||
-I nix_path='<nixpkgs/nixos>'\ | |||
--max-jobs 4\ | |||
--show-trace | |||
# From the man page: This boots using the regular boot loader of your configuration | |||
# rather than booting directly into the kernel and initial ramdisk of the system. | |||
nixos-rebuild build-vm-with-bootloader | |||
</syntaxhighlight> | |||
= Configure Virtual Machine = | |||
By default, the virtual machine is configured to have 1 CPU and 1024MiB memory. It may be too small for testing with desktop environment enabled inside. You can | By default, the virtual machine is configured to have 1 CPU and 1024MiB memory. It may be too small for testing with a desktop environment enabled inside. You can configure the allocated resources with either | ||
* [https://search.nixos.org/options?channel=25.05&show=virtualisation.vmVariant&query=virtualisation.vmVariant <code>virtualisation.vmVariant</code>] | |||
* [https://search.nixos.org/options?channel=25.05&show=virtualisation.vmVariantWithBootLoader&query=virtualisation.vmVariant <code>virtualisation.vmVariantWithBootloader</code>] | |||
by adding the following to your config: | |||
<syntaxhighlight lang="nix"> | <syntaxhighlight lang="nix"> | ||
# replace `vmVariant` with `vmVariantWithBootLoader` if you are going to use `build-vm-with-bootloder`. | |||
virtualisation.vmVariant = { | virtualisation.vmVariant = { | ||
# following configuration is added only when building VM with build-vm | # the following configuration is added only when building VM with `build-vm` | ||
virtualisation = { | virtualisation = { | ||
memorySize = | memorySize = 2048; # use 2048MiB memory | ||
cores = 3; | cores = 3; # use 3 cpu cores | ||
}; | }; | ||
} | } | ||
</syntaxhighlight> | </syntaxhighlight> | ||
= Troubleshooting = | |||
== Still can't login after updating configuration == | |||
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: | 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: | ||
Line 76: | Line 93: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
= Alternatives = | |||
== Bootable ISO == | |||
Build it as a [https://nix.dev/tutorials/nixos/building-bootable-iso-image bootable ISO image]: | Build it as a [https://nix.dev/tutorials/nixos/building-bootable-iso-image bootable ISO image]: | ||
== VM == | |||
<syntaxhighlight lang="nix"> | <syntaxhighlight lang="nix"> | ||
{ | { |
Latest revision as of 09:57, 24 September 2025
The commands
nixos-rebuild build-vm
nixos-rebuild build-vm-with-bootloader
will build a virtual machine running NixOS with your system's configuration (usually /etc/nixos/configuration.nix
) by using Qemu.
One of its usages is for testing new configurations without needing to try them on the host.
Usage
Create login user
Before calling one of the commands above, you will have to create a user with an initial password first which you can login to because your passwords of your current system are not carried over to the virtual machine.
Here is an example for a default user which you can simply add to your system config:
users.users.nixosvmtest.isSystemUser = true;
users.users.nixosvmtest.initialPassword = "test";
users.groups.nixosvmtest = {};
users.users.nixosvmtest.group = "nixosvmtest";
See this discourse-answer for more information.
Create and run virtual machine
Create the virtual machine by using of the commands above. Afterwards you will find an executable file in ./result/bin
. By executing the file qemu will open up and you can start testing the system.
Examples
# build vm of your system config
nixos-rebuild build-vm
# e.g. to specify the environment variables / cores used
nixos-rebuild build-vm\
-I nixos-config=./configuration.nix\
-I nix_path='<nixpkgs/nixos>'\
--max-jobs 4\
--show-trace
# From the man page: This boots using the regular boot loader of your configuration
# rather than booting directly into the kernel and initial ramdisk of the system.
nixos-rebuild build-vm-with-bootloader
Configure Virtual Machine
By default, the virtual machine is configured to have 1 CPU and 1024MiB memory. It may be too small for testing with a desktop environment enabled inside. You can configure the allocated resources with either
by adding the following to your config:
# replace `vmVariant` with `vmVariantWithBootLoader` if you are going to use `build-vm-with-bootloder`.
virtualisation.vmVariant = {
# the following configuration is added only when building VM with `build-vm`
virtualisation = {
memorySize = 2048; # use 2048MiB memory
cores = 3; # use 3 cpu cores
};
}
Troubleshooting
Still can't login after updating configuration
When running a virtual machine a file called $hostname.qcow2
is created in your current working directory. After changing your /etc/nixos/configuration.nix
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:
{ 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
];
}
Alternatives
Bootable ISO
Build it as a bootable ISO image:
VM
{
...
imports = [
<nixos/nixos/modules/virtualisation/virtualbox-image.nix> ]
...
nix build -f '<nixpkgs/nixos>' -I nixos-config=./configuration.nix config.system.build.virtualBoxOVA
Source