NixOS virtual machines#

One of the most important features of NixOS is the ability to configure the entire system declaratively, including packages to be installed, services to be run, as well as other settings and options.

NixOS configurations can be used to test and use NixOS using a virtual machine, independent of an installation on a “bare metal” computer.

What will you learn?#

This tutorial serves as an introduction creating NixOS virtual machines. Virtual machines are a practical tool for experimenting with or debugging NixOS configurations.

What do you need?#


A NixOS configuration is a Nix language function following the NixOS module convention. For a thorough treatment of the module system, check the Module system deep dive tutorial.

Starting from a default NixOS configuration#

In this tutorial you will use a default configuration that is shipped with NixOS. You can also skip this section and copy the sample configuration for this tutorial into a file configuration.nix in the current directory.


On NixOS, use the nixos-generate-config command to create a configuration file that contains some useful defaults and configuration suggestions.

Beware that the result of this command depends on your current NixOS configuration. The output of ‘nixos-generate-config’ can be made reproducible in a nix-shell environment. Here we provide a configuration that is used for the NixOS minimal ISO image:

nix-shell -I nixpkgs=channel:nixos-23.11 -p 'let pkgs = import <nixpkgs> { config = {}; overlays = []; }; iso-config = pkgs.path + /nixos/modules/installer/cd-dvd/installation-cd-minimal.nix; nixos = pkgs.nixos iso-config; in'
Detailed explanation

The above shell command is a one-liner so it’s easier to copy and paste. This is the readable long form using a heredoc:

nix-shell -I nixpkgs=channel:nixos-23.11 -p "$(cat <<EOF
  pkgs = import <nixpkgs> { config = {}; overlays = []; };
  iso-config = pkgs.path + /nixos/modules/installer/cd-dvd/installation-cd-minimal.nix;
  nixos = pkgs.nixos iso-config;

It does the following:

  • Provide Nixpkgs from a channel

  • Take the configuration file for the minimal ISO image from the obtained version of the Nixpkgs repository

  • Evaluate that NixOS configuration with pkgs.nixos

  • Return the derivation which produces the nixos-generate-config executable from the evaluated configuration

By default, the generated configuration file is written to /etc/nixos/configuration.nix. To avoid overwriting this file you have to specify the output directory. Create a NixOS configuration in your working directory:

nixos-generate-config --dir ./

In the working directory you will then find two files:

  1. hardware-configuration.nix is specific to the hardware nix-generate-config is being run on. You can ignore that file for this tutorial because it has no effect inside a virtual machine.

  2. configuration.nix contains various suggestions and comments for the initial setup of a desktop computer.

The default NixOS configuration without comments is:

1{ config, pkgs, ... }:
3  imports =  [ ./hardware-configuration.nix ];
5  boot.loader.systemd-boot.enable = true;
6  boot.loader.efi.canTouchEfiVariables = true;
8  system.stateVersion = "23.11";

To be able to log in, add the following lines to the returned attribute set:

1  users.users.alice = {
2    isNormalUser = true;
3    extraGroups = [ "wheel" ];
4    packages = with pkgs; [
5      cowsay
6      lolcat
7    ];
8  };


On NixOS your configuration generated with nixos-generate-config contains this user configuration commented out.

Additionally, you need to specify a password for this user. For the purpose of demonstration only, you specify an insecure, plain text password by adding the initialPassword option to the user configuration:

1initialPassword = "test";


Do not use plain text passwords outside of this example unless you know what you are doing. See initialHashedPassword or ssh.authorizedKeys for more secure alternatives.

This tutorial focuses on testing NixOS configurations on a virtual machine. Therefore you will remove the reference to hardware-configuration.nix:

-  imports =  [ ./hardware-configuration.nix ];

Sample configuration#

The complete configuration.nix file looks like this:

 1{ config, pkgs, ... }:
 3  boot.loader.systemd-boot.enable = true;
 4  boot.loader.efi.canTouchEfiVariables = true;
 6  users.users.alice = {
 7    isNormalUser = true;
 8    extraGroups = [ "wheel" ]; # Enable ‘sudo’ for the user.
 9    packages = with pkgs; [
10      cowsay
11      lolcat
12    ];
13    initialPassword = "test";
14  };
16  system.stateVersion = "23.11";

Creating a QEMU based virtual machine from a NixOS configuration#

A NixOS virtual machine is created with the nix-build command:

nix-build '<nixpkgs/nixos>' -A vm \
-I nixpkgs=channel:nixos-23.11 \
-I nixos-config=./configuration.nix

This command builds the attribute vm from the nixos-23.11 release of NixOS, using the NixOS configuration as specified in the relative path.

Detailed explanation


On NixOS the $NIX_PATH environment variable is usually set up automatically, and there is also a convenience command for building virtual machines. You can use the current version of nixpkgs to build the virtual machine like this:

nixos-rebuild build-vm -I nixos-config=./configuration.nix

Running the virtual machine#

The previous command created a link with the name result in the working directory. It links to the directory that contains the virtual machine.

ls -R ./result
    bin  system


Run the virtual machine:

QEMU_KERNEL_PARAMS=console=ttyS0 ./result/bin/run-nixos-vm -nographic; reset

This command will run QEMU in the current terminal due to -nographic. console=ttyS0 will also show the boot process, which ends at the console login screen.

Log in as alice with the password test. Check that the programs are indeed available as specified:

cowsay hello | lolcat

Exit the virtual machine by shutting it down:

sudo poweroff


If you forgot to add the user to wheel or didn’t set a password, stop the virtual machine from a different terminal:

sudo pkill qemu

Running the virtual machine will create a nixos.qcow2 file in the current directory. This disk image file contains the dynamic state of the virtual machine. It can interfere with debugging as it keeps the state of previous runs, for example the user password.

Delete this file when you change the configuration:

rm nixos.qcow2


Next steps#