1 # vmTools {#sec-vm-tools}
3 A set of VM related utilities, that help in building some packages in more advanced scenarios.
5 ## `vmTools.createEmptyImage` {#vm-tools-createEmptyImage}
7 A bash script fragment that produces a disk image at `destination`.
9 ### Attributes {#vm-tools-createEmptyImage-attributes}
11 * `size`. The disk size, in MiB.
12 * `fullName`. Name that will be written to `${destination}/nix-support/full-name`.
13 * `destination` (optional, default `$out`). Where to write the image files.
15 ## `vmTools.runInLinuxVM` {#vm-tools-runInLinuxVM}
17 Run a derivation in a Linux virtual machine (using Qemu/KVM).
18 By default, there is no disk image; the root filesystem is a `tmpfs`, and the Nix store is shared with the host (via the [9P protocol](https://wiki.qemu.org/Documentation/9p#9p_Protocol)).
19 Thus, any pure Nix derivation should run unmodified.
21 If the build fails and Nix is run with the `-K/--keep-failed` option, a script `run-vm` will be left behind in the temporary build directory that allows you to boot into the VM and debug it interactively.
23 ### Attributes {#vm-tools-runInLinuxVM-attributes}
25 * `preVM` (optional). Shell command to be evaluated *before* the VM is started (i.e., on the host).
26 * `memSize` (optional, default `512`). The memory size of the VM in MiB.
27 * `diskImage` (optional). A file system image to be attached to `/dev/sda`.
28 Note that currently we expect the image to contain a filesystem, not a full disk image with a partition table etc.
30 ### Examples {#vm-tools-runInLinuxVM-examples}
32 Build the derivation hello inside a VM:
34 { pkgs }: with pkgs; with vmTools;
38 Build inside a VM with extra memory:
40 { pkgs }: with pkgs; with vmTools;
41 runInLinuxVM (hello.overrideAttrs (_: { memSize = 1024; }))
44 Use VM with a disk image (implicitly sets `diskImage`, see [`vmTools.createEmptyImage`](#vm-tools-createEmptyImage)):
46 { pkgs }: with pkgs; with vmTools;
47 runInLinuxVM (hello.overrideAttrs (_: {
48 preVM = createEmptyImage {
50 fullName = "vm-image";
55 ## `vmTools.extractFs` {#vm-tools-extractFs}
57 Takes a file, such as an ISO, and extracts its contents into the store.
59 ### Attributes {#vm-tools-extractFs-attributes}
61 * `file`. Path to the file to be extracted.
62 Note that currently we expect the image to contain a filesystem, not a full disk image with a partition table etc.
63 * `fs` (optional). Filesystem of the contents of the file.
65 ### Examples {#vm-tools-extractFs-examples}
67 Extract the contents of an ISO file:
69 { pkgs }: with pkgs; with vmTools;
70 extractFs { file = ./image.iso; }
73 ## `vmTools.extractMTDfs` {#vm-tools-extractMTDfs}
75 Like [](#vm-tools-extractFs), but it makes use of a [Memory Technology Device (MTD)](https://en.wikipedia.org/wiki/Memory_Technology_Device).
77 ## `vmTools.runInLinuxImage` {#vm-tools-runInLinuxImage}
79 Like [](#vm-tools-runInLinuxVM), but instead of using `stdenv` from the Nix store, run the build using the tools provided by `/bin`, `/usr/bin`, etc. from the specified filesystem image, which typically is a filesystem containing a [FHS](https://en.wikipedia.org/wiki/Filesystem_Hierarchy_Standard)-based Linux distribution.
81 ## `vmTools.makeImageTestScript` {#vm-tools-makeImageTestScript}
83 Generate a script that can be used to run an interactive session in the given image.
85 ### Examples {#vm-tools-makeImageTestScript-examples}
87 Create a script for running a Fedora 27 VM:
89 { pkgs }: with pkgs; with vmTools;
90 makeImageTestScript diskImages.fedora27x86_64
93 Create a script for running an Ubuntu 20.04 VM:
95 { pkgs }: with pkgs; with vmTools;
96 makeImageTestScript diskImages.ubuntu2004x86_64
99 ## `vmTools.diskImageFuns` {#vm-tools-diskImageFuns}
101 A set of functions that build a predefined set of minimal Linux distributions images.
103 ### Images {#vm-tools-diskImageFuns-images}
131 ### Attributes {#vm-tools-diskImageFuns-attributes}
133 * `size` (optional, defaults to `4096`). The size of the image, in MiB.
134 * `extraPackages` (optional). A list names of additional packages from the distribution that should be included in the image.
136 ### Examples {#vm-tools-diskImageFuns-examples}
138 8GiB image containing Firefox in addition to the default packages:
140 { pkgs }: with pkgs; with vmTools;
141 diskImageFuns.ubuntu2004x86_64 { extraPackages = [ "firefox" ]; size = 8192; }
144 ## `vmTools.diskImageExtraFuns` {#vm-tools-diskImageExtraFuns}
146 Shorthand for `vmTools.diskImageFuns.<attr> { extraPackages = ... }`.
148 ## `vmTools.diskImages` {#vm-tools-diskImages}
150 Shorthand for `vmTools.diskImageFuns.<attr> { }`.