Simple Nix Virtual Machines

Created in: 1/09/2024

Related Tags: Nix SysAdmin Virtualization


If you want to create a simple VM using Nix, this is your guide. For more complicated setups look at:

As well as more information that can be found on nix.dev.

Most simple way of creating a Virtual Machine

  • Persistance
  • Has a separate terminal window
  • Typical adjustment of Fullscreen and I/O
  • Can have a graphical user interface (e.g. GNOME)

Sample Minimal Configuration

{ config, pkgs, ... }:
{
  boot.loader.systemd-boot.enable = true;
  boot.loader.efi.canTouchEfiVariables = true;

  users.users.alice = {
    isNormalUser = true;
    extraGroups = [ "wheel" ]; # Enable ‘sudo’ for the user.
    initialPassword = "test";
  };

  environment.systemPackages = with pkgs; [
    # some pkgs here
  ];

  system.stateVersion = "23.11";
}

Add GNOME or another graphical Desktop Environment

Just these two lines in your config to add GNOME:

services.xserver.enable = true;
services.xserver.displayManager.gdm.enable = true;
services.xserver.desktopManager.gnome.enable = true;

Or to run a wayland compositor such as sway:

services.xserver.enable = true;
services.xserver.displayManager.gdm.enable = true;
programs.sway.enable = true;

Run the VM

There is an specific command to run these VMs:

nixos-rebuild build-vm

By default it will make create the VM with a clone of your /etc/nixos/configuration.nix (if you are using a flake it would not be your actual config). NOT recommended, as there could be a problem with the passwords, and you will not be able to log in.

To run the VM with a diferent config do:

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

It will automatically open a new terminal window with your VM on it. The password should be the configuration option

users.users."<USER>".initialPasswords = "<STRING>";
Warning:

If the password is not added you will not be able to log in into the user.

To use your own /etc/nixos/configuration.nix, you can make a duplicate and add this config value.

After running the nixos-rebuild buil-vm run the executable that is at the end of the build.

To automatically do this you can use this command:

nixos-rebuild build-vm -I nixos-config=./configuration.nix |& grep 'The virtual machine can be started by running' | awk '{print $NF}' | sh

It will not output anything else from the nixos-rebuild build-vm command, the logs or traceback in case it fails. If it fails it will not output anything.

Warning:

If you are planning in using the same VM several times, do not use this command, since you will not be able to see the VM binary.

It should work without any dependencies or installed packages.

Persistance

This type of VM have persistance by default, you can run them, create files, reboot and the files are still there. This is because of a file named nixos.qcow2 created in the directory that contains the configuration.nix config.

If you delete it, then the next time the VM is booted, it will reset to its initial state.

ISO mages for VMs can be created using some tool like nix generators.