Home Explore Blog Models CI



nixpkgs

2nd chunk of `doc/build-helpers/images/makediskimage.section.md`
9d881616edbc6ad2826f6927f5078bd5bad859e8a872396f0000000100000cf2
- arbitrary NixOS configuration
- automatic or bound disk size: `diskSize` parameter, `additionalSpace` can be set when `diskSize` is `auto` to add a constant of disk space
- multiple partition table layouts: EFI, legacy, legacy + GPT, hybrid, none through `partitionTableType` parameter
- OVMF or EFI firmwares and variables templates can be customized
- root filesystem `fsType` can be customized to whatever `mkfs.${fsType}` exists during operations
- root filesystem label can be customized, defaults to `nix-store` if it's a Nix store image, otherwise `nixpkgs/nixos`
- arbitrary code can be executed after disk image was produced with `postVM`
- the current nixpkgs can be realized as a channel in the disk image, which will change the hash of the image when the sources are updated
- additional store paths can be provided through `additionalPaths`

### Full NixOS image {#sec-make-disk-image-features-full-image}

- arbitrary contents with permissions can be placed in the target filesystem using `contents`
- a `/etc/nixpkgs/nixos/configuration.nix` can be provided through `configFile`
- bootloaders are supported
- EFI variables can be mutated during image production and the result is exposed in `$out`
- boot partition size when partition table is `efi` or `hybrid`

### On bit-to-bit reproducibility {#sec-make-disk-image-features-reproducibility}

Images are **NOT** deterministic, please do not hesitate to try to fix this, source of determinisms are (not exhaustive) :

- bootloader installation has timestamps
- SQLite Nix store database contains registration times
- `/etc/shadow` is in a non-deterministic order

A `deterministic` flag is available for best efforts determinism.

## Usage {#sec-make-disk-image-usage}

To produce a Nix-store only image:
```nix
let
  pkgs = import <nixpkgs> { };
  lib = pkgs.lib;
  make-disk-image = import <nixpkgs/nixos/lib/make-disk-image.nix>;
in
make-disk-image {
  inherit pkgs lib;
  config = { };
  additionalPaths = [ ];
  format = "qcow2";
  onlyNixStore = true;
  partitionTableType = "none";
  installBootLoader = false;
  touchEFIVars = false;
  diskSize = "auto";
  additionalSpace = "0M"; # Defaults to 512M.
  copyChannel = false;
}
```

Some arguments can be left out, they are shown explicitly for the sake of the example.

Building this derivation will provide a QCOW2 disk image containing only the Nix store and its registration information.

To produce a NixOS installation image disk with UEFI and bootloader installed:
```nix
let
  pkgs = import <nixpkgs> { };
  lib = pkgs.lib;
  make-disk-image = import <nixpkgs/nixos/lib/make-disk-image.nix>;
  evalConfig = import <nixpkgs/nixos/lib/eval-config.nix>;
in
make-disk-image {
  inherit pkgs lib;
  inherit
    (evalConfig {
      modules = [
        {
          fileSystems."/" = {
            device = "/dev/vda";
            fsType = "ext4";
            autoFormat = true;
          };
          boot.grub.device = "/dev/vda";
        }
      ];
    })
    config
    ;
  format = "qcow2";
  onlyNixStore = false;
  partitionTableType = "legacy+gpt";
  installBootLoader = true;
  touchEFIVars = true;
  diskSize = "auto";
  additionalSpace = "0M"; # Defaults to 512M.
  copyChannel = false;
  memSize = 2048; # Qemu VM memory size in MiB (1024*1024 bytes). Defaults to 1024M.
}
```

Title: NixOS Disk Image: Full Installation Features, Reproducibility, and Usage Examples
Summary
This section details additional features for creating full NixOS installation images, including support for custom `configFile`, bootloaders, EFI variable mutation, and configurable boot partition sizes. It also addresses image reproducibility, noting that images are *not* deterministic by default due to elements like timestamps and non-deterministic ordering in `/etc/shadow`, but a `deterministic` flag is available for best-effort reproducibility. Finally, the chunk provides practical usage examples: one for generating a Nix-store only QCOW2 image with specific settings like `onlyNixStore = true` and `partitionTableType = "none"`, and another for creating a full NixOS installation QCOW2 image with UEFI, a bootloader, `partitionTableType = "legacy+gpt"`, and custom QEMU VM memory.