2023-03-16 13:37:04 -04:00
(nixos-vms)=
# 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.
2024-01-30 17:13:45 -05:00
Virtual machines are a practical tool for experimenting with or debugging NixOS configurations.
2023-03-16 13:37:04 -04:00
## What do you need?
2024-06-28 16:27:17 -04:00
- A working [Nix installation ](https://nix.dev/manual/nix/stable/installation/installation.html ) on Linux, or [NixOS ](https://nixos.org/manual/nixos/stable/index.html#sec-installation ), with a graphical environment
2023-03-16 13:37:04 -04:00
- Basic knowledge of the [Nix language ](reading-nix-language )
2024-01-30 17:13:45 -05:00
:::{important}
A NixOS configuration is a Nix language function following the [NixOS module ](https://nixos.org/manual/nixos/stable/index.html#sec-writing-modules ) convention.
For a thorough treatment of the module system, check the [](module-system-deep-dive) tutorial.
:::
2023-11-22 11:43:38 -05:00
## Starting from a default NixOS configuration
2023-03-16 13:37:04 -04:00
2023-11-22 11:43:38 -05:00
In this tutorial you will use a default configuration that is shipped with NixOS.
2024-02-08 08:57:26 -05:00
You can also skip this section and copy the [sample configuration ](sample-nixos-config ) for this tutorial into a file `configuration.nix` in the current directory.
2023-03-16 13:37:04 -04:00
2023-11-27 10:39:04 -05:00
::::{admonition} NixOS
2023-03-16 13:37:04 -04:00
On NixOS, use the `nixos-generate-config` command to create a configuration file that contains some useful defaults and configuration suggestions.
2023-11-22 11:43:38 -05:00
Beware that the result of this command depends on your current NixOS configuration.
2024-04-22 13:28:58 -04:00
The output of `nixos-generate-config` can be made reproducible in a `nix-shell` environment.
2024-02-08 08:57:26 -05:00
Here we provide a configuration that is used for the [NixOS minimal ISO image ](https://nixos.org/download#nixos-iso ):
2023-11-27 10:39:04 -05:00
```shell-session
nix-shell -I nixpkgs=channel:nixos-23.11 -p "$(cat < < EOF
2024-04-22 13:28:58 -04:00
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 nixos.config.system.build.nixos-generate-config
2023-11-22 11:43:38 -05:00
EOF
)"
```
2023-11-27 10:39:04 -05:00
It does the following:
- Provide Nixpkgs from a [channel ](ref-pinning-nixpkgs )
2024-02-08 08:57:26 -05:00
- Take the configuration file for the minimal ISO image from the obtained version of the Nixpkgs repository
2023-11-27 10:39:04 -05:00
- Evaluate that NixOS configuration with `pkgs.nixos`
- Return the derivation which produces the `nixos-generate-config` executable from the evaluated configuration
2023-11-22 11:43:38 -05:00
2023-11-27 10:39:04 -05:00
By default, the generated configuration file is written to `/etc/nixos/configuration.nix` .
2023-03-16 13:37:04 -04:00
To avoid overwriting this file you have to specify the output directory.
Create a NixOS configuration in your working directory:
```shell-session
2024-04-22 13:28:58 -04:00
$ nixos-generate-config --dir ./
2023-03-16 13:37:04 -04:00
```
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.
2023-11-27 10:39:04 -05:00
::::
2023-03-16 13:37:04 -04:00
2023-10-06 17:20:19 -04:00
The default NixOS configuration without comments is:
2023-03-16 13:37:04 -04:00
```nix
{ config, pkgs, ... }:
{
imports = [ ./hardware-configuration.nix ];
boot.loader.systemd-boot.enable = true;
boot.loader.efi.canTouchEfiVariables = true;
2023-11-27 10:39:04 -05:00
system.stateVersion = "23.11";
2023-03-16 13:37:04 -04:00
}
```
2024-01-30 17:13:45 -05:00
To be able to log in, add the following lines to the returned attribute set:
2023-03-16 13:37:04 -04:00
```nix
users.users.alice = {
isNormalUser = true;
extraGroups = [ "wheel" ];
};
```
2024-05-04 08:45:14 -04:00
We add two lightweight programs as an example:
2024-04-22 13:28:58 -04:00
```nix
environment.systemPackages = with pkgs; [
cowsay
lolcat
];
```
2023-03-16 13:37:04 -04:00
:::{admonition} NixOS
2023-12-26 08:12:53 -05:00
On NixOS your configuration generated with `nixos-generate-config` contains this user configuration commented out.
2023-03-16 13:37:04 -04:00
:::
Additionally, you need to specify a password for this user.
2023-10-06 17:23:50 -04:00
For the purpose of demonstration only, you specify an insecure, plain text password by adding the `initialPassword` option to the user configuration:
2023-03-16 13:37:04 -04:00
```nix
2024-02-08 08:57:26 -05:00
initialPassword = "test";
2023-03-16 13:37:04 -04:00
```
2023-10-06 17:23:50 -04:00
:::{warning}
Do not use plain text passwords outside of this example unless you know what you are doing. See [`initialHashedPassword` ](https://nixos.org/manual/nixos/stable/options.html#opt-users.extraUsers._name_.initialHashedPassword ) or [`ssh.authorizedKeys` ](https://nixos.org/manual/nixos/stable/options.html#opt-users.extraUsers._name_.openssh.authorizedKeys.keys ) for more secure alternatives.
:::
2023-03-16 13:37:04 -04:00
This tutorial focuses on testing NixOS configurations on a virtual machine.
Therefore you will remove the reference to `hardware-configuration.nix` :
```diff
- imports = [ ./hardware-configuration.nix ];
```
2024-02-08 08:57:26 -05:00
(sample-nixos-config)=
### Sample configuration
The complete `configuration.nix` file looks like this:
2023-03-16 13:37:04 -04:00
```nix
{ 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.
2024-02-08 08:57:26 -05:00
initialPassword = "test";
2023-03-16 13:37:04 -04:00
};
2024-04-22 13:28:58 -04:00
environment.systemPackages = with pkgs; [
cowsay
lolcat
];
2023-11-27 10:39:04 -05:00
system.stateVersion = "23.11";
2023-03-16 13:37:04 -04:00
}
```
2023-10-06 18:37:27 -04:00
## Creating a QEMU based virtual machine from a NixOS configuration
2023-03-16 13:37:04 -04:00
2023-10-06 18:37:42 -04:00
A NixOS virtual machine is created with the `nix-build` command:
2023-03-16 13:37:04 -04:00
```shell-session
2024-04-22 13:28:58 -04:00
$ nix-build '< nixpkgs / nixos > ' -A vm -I nixpkgs=channel:nixos-23.11 -I nixos-config=./configuration.nix
2023-03-16 13:37:04 -04:00
```
2023-11-27 10:39:04 -05:00
This command builds the attribute `vm` from the `nixos-23.11` release of NixOS, using the NixOS configuration as specified in the relative path.
2023-03-16 13:37:04 -04:00
2024-04-21 17:20:51 -04:00
::::{dropdown} Detailed explanation
2023-03-16 13:37:04 -04:00
2024-06-28 16:27:17 -04:00
- The positional argument to [`nix-build` ](https://nix.dev/manual/nix/stable/command-ref/nix-build.html ) is a path to the derivation to be built.
2023-10-06 18:37:51 -04:00
That path can be obtained from [a Nix expression that evaluates to a derivation ](derivations ).
The virtual machine build helper is defined in NixOS, which is part of the [`nixpkgs` repository ](https://github.com/NixOS/nixpkgs ).
2023-11-13 04:30:42 -05:00
Therefore we use the [lookup path ](lookup-path-tutorial ) `<nixpkgs/nixos>` .
2023-10-06 18:37:51 -04:00
2024-06-28 16:27:17 -04:00
- The [`-A` option ](https://nix.dev/manual/nix/stable/command-ref/opt-common.html#opt-attr ) specifies the attribute to pick from the provided Nix expression `<nixpkgs/nixos>` .
2023-10-06 18:37:51 -04:00
To build the virtual machine, we choose the `vm` attribute as defined in [`nixos/default.nix` ](https://github.com/NixOS/nixpkgs/blob/7c164f4bea71d74d98780ab7be4f9105630a2eba/nixos/default.nix#L19 ).
2024-06-28 16:27:17 -04:00
- The [`-I` option ](https://nix.dev/manual/nix/stable/command-ref/opt-common.html#opt-I ) prepends entries to the search path.
2023-10-06 18:37:51 -04:00
Here we set `nixpkgs` to refer to a [specific version of Nixpkgs ](ref-pinning-nixpkgs ) and set `nix-config` to the `configuration.nix` file in the current directory.
2023-03-16 13:37:04 -04:00
:::{admonition} NixOS
On NixOS the `$NIX_PATH` environment variable is usually set up automatically, and there is also [a convenience command for building virtual machines ](https://nixos.org/manual/nixos/stable/#sec-changing-config ).
2024-04-22 13:28:58 -04:00
To use the current version of `nixpkgs` to build the virtual machine:
2023-03-16 13:37:04 -04:00
```shell-session
nixos-rebuild build-vm -I nixos-config=./configuration.nix
```
:::
2024-04-21 17:20:51 -04:00
::::
2023-03-16 13:37:04 -04:00
## 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.
```shell-session
2024-04-22 13:28:58 -04:00
$ ls -R ./result
result:
bin system
2023-03-16 13:37:04 -04:00
2024-04-22 13:28:58 -04:00
result/bin:
run-nixos-vm
2023-03-16 13:37:04 -04:00
```
Run the virtual machine:
```shell-session
2024-04-22 13:28:58 -04:00
$ QEMU_KERNEL_PARAMS=console=ttyS0 ./result/bin/run-nixos-vm -nographic; reset
2024-02-08 08:57:26 -05:00
```
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:
```shell-session
2024-04-22 13:28:58 -04:00
$ cowsay hello | lolcat
2024-02-08 08:57:26 -05:00
```
Exit the virtual machine by shutting it down:
```shell-session
2024-04-22 13:28:58 -04:00
$ sudo poweroff
2023-03-16 13:37:04 -04:00
```
2024-02-08 08:57:26 -05:00
:::{note}
If you forgot to add the user to `wheel` or didn't set a password, stop the virtual machine from a different terminal:
```shell-session
2024-04-22 13:28:58 -04:00
$ sudo pkill qemu
2024-02-08 08:57:26 -05:00
```
:::
2023-03-16 13:37:04 -04:00
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.
2023-10-22 20:15:50 -04:00
Delete this file when you change the configuration:
2023-03-16 13:37:04 -04:00
```shell-session
2024-04-22 13:28:58 -04:00
$ rm nixos.qcow2
```
## Running GNOME on a graphical VM
To create a virtual machine with a graphical user interface, add the following lines to the configuration:
```nix
# Enable the X11 windowing system.
services.xserver.enable = true;
# Enable the GNOME Desktop Environment.
services.xserver.displayManager.gdm.enable = true;
services.xserver.desktopManager.gnome.enable = true;
```
These three lines activate X11, the GDM display manager (to be able to login) and Gnome as desktop manager.
::::{admonition} NixOS
On NixOS, use `installation-cd-graphical-gnome.nix` to generate the configuration file:
```shell-session
nix-shell -I nixpkgs=channel:nixos-23.11 -p "$(cat < < EOF
let
pkgs = import < nixpkgs > { config = {}; overlays = []; };
iso-config = pkgs.path + /nixos/modules/installer/cd-dvd/installation-cd-graphical-gnome.nix;
nixos = pkgs.nixos iso-config;
in nixos.config.system.build.nixos-generate-config
EOF
)"
```
```shell-session
$ nixos-generate-config --dir ./
2023-03-16 13:37:04 -04:00
```
2024-04-22 13:28:58 -04:00
::::
The complete `configuration.nix` file looks like this:
```nix
{ config, pkgs, ... }:
{
boot.loader.systemd-boot.enable = true;
boot.loader.efi.canTouchEfiVariables = true;
services.xserver.enable = true;
services.xserver.displayManager.gdm.enable = true;
services.xserver.desktopManager.gnome.enable = true;
users.users.alice = {
isNormalUser = true;
extraGroups = [ "wheel" ];
initialPassword = "test";
};
system.stateVersion = "23.11";
}
```
To get graphical output, run the virtual machine without special options:
```shell-session
$ nix-build '< nixpkgs / nixos > ' -A vm -I nixpkgs=channel:nixos-23.11 -I nixos-config=./configuration.nix
$ ./result/bin/run-nixos-vm
```
## Running Sway as Wayland compositor on a VM
To change to a Wayland compositor, disable `services.xserver.desktopManager.gnome` and enable `programs.sway` :
```{code-block} diff
:caption: configuration.nix
- services.xserver.desktopManager.gnome.enable = true;
+ programs.sway.enable = true;
```
:::{note}
Running Wayland compositors in a virtual machine might lead to complications with the display drivers used by QEMU.
You need to choose from the available drivers one that is compatible with Sway.
See [QEMU User Documentation ](https://www.qemu.org/docs/master/system/qemu-manpage.html ) for options.
One possibility is the `virtio-vga` driver:
```shell-session
$ ./result/bin/run-nixos-vm -device virtio-vga
```
Arguments to QEMU can also be added to the configuration file:
```nix
{ config, pkgs, ... }:
{
boot.loader.systemd-boot.enable = true;
boot.loader.efi.canTouchEfiVariables = true;
services.xserver.enable = true;
services.xserver.displayManager.gdm.enable = true;
programs.sway.enable = true;
imports = [ < nixpkgs / nixos / modules / virtualisation / qemu-vm . nix > ];
virtualisation.qemu.options = [
"-device virtio-vga"
];
users.users.alice = {
isNormalUser = true;
extraGroups = [ "wheel" ];
initialPassword = "test";
};
system.stateVersion = "23.11";
}
```
:::
The NixOS manual has chapters on [X11 ](https://nixos.org/manual/nixos/stable/#sec-x11 ) and [Wayland ](https://nixos.org/manual/nixos/stable/#sec-wayland ) listing alternative window managers.
2023-09-11 04:59:13 -04:00
## References
2023-03-16 13:37:04 -04:00
- [NixOS Manual: NixOS Configuration ](https://nixos.org/manual/nixos/stable/index.html#ch-configuration ).
- [NixOS Manual: Modules ](https://nixos.org/manual/nixos/stable/index.html#sec-writing-modules ).
2023-10-22 20:12:12 -04:00
- [NixOS Manual Options reference ](https://nixos.org/manual/nixos/stable/options.html ).
- [NixOS Manual: Changing the configuration ](https://nixos.org/manual/nixos/stable/#sec-changing-config ).
2023-03-16 13:37:04 -04:00
- [NixOS source code: `configuration template` in `tools.nix` ](https://github.com/NixOS/nixpkgs/blob/4e0525a8cdb370d31c1e1ba2641ad2a91fded57d/nixos/modules/installer/tools/tools.nix#L122-L226 ).
2023-10-06 04:59:27 -04:00
- [NixOS source code: `vm` attribute in `default.nix` ](https://github.com/NixOS/nixpkgs/blob/master/nixos/default.nix ).
2024-06-28 16:27:17 -04:00
- [Nix manual: `nix-build` ](https://nix.dev/manual/nix/stable/command-ref/nix-build.html ).
- [Nix manual: common command-line options ](https://nix.dev/manual/nix/stable/command-ref/opt-common.html ).
- [Nix manual: `NIX_PATH` environment variable ](https://nix.dev/manual/nix/stable/command-ref/env-common.html#env-NIX_PATH ).
2024-02-09 06:51:42 -05:00
- [QEMU User Documentation ](https://www.qemu.org/docs/master/system/qemu-manpage.html ) for more runtime options
- [NixOS option search: `virtualisation.qemu` ](https://search.nixos.org/options?query=virtualisation.qemu ) for declarative virtual machine configuration
2024-01-30 17:13:45 -05:00
## Next steps
- [](module-system-deep-dive)
- [](integration-testing-vms)
- [](bootable-iso-image)