This project supports both flake-based workflows and non flake-based workflows.
Add the project to your flake.nix inputs to access the library from your flake outputs:
{
inputs = {
nix-vm-test = {
url = "github:numtide/nix-vm-test";
inputs.nixpkgs.follows = "nixpkgs";
};
};
outputs = { self, nixpkgs, nix-vm-test }: {
packages.x86_64-linux = let
test = nix-vm-test.lib.x86_64-linux.debian."12" {
(...)
};
in test.driver
};
}{ system ? "x86_64-linux", pkgs ? import <nixpkgs> { inherit system; }}:
let
nix-vm-test-src = pkgs.fetchFromGitHub {
owner = "numtide";
repo = "nix-vm-test";
# Replace this commit rev with the one pointed by the current main branch.
rev = "e9df86d6275c5cda31c8bf3e7103e6c7c9e6fdad";
sha256 = "sha256-NEpnmdhF5roYluocBmfpTjcg5giWtIXCMbsJFxTnNkM=";
};
nix-vm-test = import "${nix-vm-test-src}/lib.nix" {
inherit system;
nixpkgs = <nixpkgs>;
};
test = nix-vm-test.debian."12" {
(...)
};
in test.driverNote: For a real-world deployment, you would probably want to pin Nixpkgs to a particular version, instead of using the one coming from your host path.
There is one last option to use this library: via a Nixpkgs overlay.
The overlay function makes all linux distributions available under the attribute
pkgs.testers.nonNixOSDistros...:
{
inputs = {
nixpkgs.url = "github:nixos/nixpkgs?ref=nixos-unstable";
nix-vm-test.url = "github:numtide/nix-vm-test";
};
outputs = { self, nixpkgs, nix-vm-test }:
let
system = "x86_64-linux";
pkgs = import nixpkgs {
inherit system;
overlays = [
nix-vm-test.overlays.default
];
};
in
packages.x86_64-linux.mytest = pkgs.testers.nonNixOSDistros.debian."13" {
sharedDirs.debDir = {
source = "${./.}";
target = "/mnt/debdir";
};
testScript = ''
vm.wait_for_unit("multi-user.target")
vm.succeed("apt-get -yq install /mnt/debdir/hello.deb")
'';
};
}The API consists consists roughly of two parts:
$ROOT.$DISTRIBUTION {
$INPUTS
}Where $ROOT will be nix-vm-test.lib.<system> on a flake-based setup.
<system> is the host system running the driver. Supported hosts:
| Host system | Guest system | Acceleration | Notes |
|---|---|---|---|
x86_64-linux |
x86_64-linux |
KVM | |
aarch64-linux |
aarch64-linux |
KVM | |
aarch64-darwin |
aarch64-linux |
HVF | macOS 15+. Requires a Linux builder (see README). |
On macOS the VMs run as aarch64-linux guests, so only distributions that publish
an aarch64 image are available there. Arch Linux is x86_64-only and is therefore
not exposed on aarch64-darwin, and Fedora is only available as release 43 there
(older aarch64 releases are no longer on the release mirror). The Rocky 9.0
aarch64 image is skipped (it is damaged), and so are the 8.x ones on aarch64-darwin
(they need 64 KB pages, which Apple Silicon lacks).
Where $DISTRIBUTION is a $NAME.$VERSION couple. Here's the currently supported name/version couples:
| Name | Version |
|---|---|
| debian | "12" |
| debian | "13" |
| ubuntu | "20_04" |
| ubuntu | "22_04" |
| ubuntu | "22_10" |
| ubuntu | "23_04" |
| ubuntu | "23_10" |
| fedora | "38" |
| fedora | "39" |
A Nix attribute set, as described in the Nix manual, is used to configure the VM test. The API is consistent across all the supported distributions.
For instance:
{
diskSize = "+2M";
sharedDirs = {
shareName = {
source = "/home/numtide/share";
target = "/mnt";
};
};
testScript = ''
vm.wait_for_unit("multi-user.target")
vm.succeed("apt-get update")
'';
}Where:
| Attribute Name | Description | Example |
|---|---|---|
| diskSize | Resize the guest root partition to a new size. That size can be either absolute (EG. 2G, 800M) or relative to the original size (EG. +500M, +2G) |
+2G |
| sharedDirs | This attribute set describes the host directories that will be mounted to the guest filesystem. source being the host directory, target being the path we want to mount the directory on the VM. |
See above example |
| memorySize | Memory available to the guest VM, in MiB. Defaults to 1024. | 2048 |
| cpus | Number of virtual CPUs available to the guest VM. Defaults to 2. | 4 |
| selinuxEnforcing | Fedora and Rocky only. Boot with SELinux in enforcing mode instead of permissive. Defaults to true on Rocky and false on Fedora. |
false |
| testScript | A Python script used to orchestrate the integration test. This is compatible with NixOS tests. You can have a look at the relevant NixOS manual section to get the reference documentation of the available methods. | See above example |
let
vmTest = nix-vm-test.debian."12" {
diskSize = "+2M";
sharedDirs = {
shareName = {
source = "/home/numtide/share";
target = "/mnt";
};
};
testScript = ''
vm.wait_for_unit("multi-user.target")
vm.succeed("apt-get update")
'';
};
interactive = vmTest.driverInteractive;
driver = vmTest.driver;
driverInSandbox = vmTest.sandbox;
in ...| Output "Path" | Description |
|---|---|
| driver | Run the test non-interactively. The VM will be able to reach the internet via a NAT interface with the host. This is likely the mode you want to use on your CI. |
| driverInteractive | Run the test interactively. An interactive IPython console will open. You can interactively run the Python commands through this console. A qemu window also opens up, allowing you to directly interact with the VM shell. You can log in to the VM through the root account with an empty password. |
| sandbox | Run the test non-interactively in the Nix sandbox. |