Skip to content

Instantly share code, notes, and snippets.

@tarnacious
Last active September 25, 2023 11:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tarnacious/4fa4c449cb6f186b27fb01b2953696cc to your computer and use it in GitHub Desktop.
Save tarnacious/4fa4c449cb6f186b27fb01b2953696cc to your computer and use it in GitHub Desktop.
Building NixOS qcow2 images with flakes

Building NixOS qcow2 images with flakes

Nix expects configuration to tracked in git, so init a repository and add them if they're not already.

git init
git add .

Build the qcow2 image.

nix build .#nixosConfigurations.build-qcow2.config.system.build.qcow2

Copy it out of the store and make the copy writable.

cp result/nixos.qcow2 .
chmod 644 nixos.qcow2

Start a qemu VM.

virt-install \
     --name example \
     --ram 1536 \
     --disk nixos.qcow2 \
     --os-variant nixos-unstable \
     --import

Stop and delete virtual machine configuration

virsh destroy example
virsh undefine example
{ config, lib, pkgs, ... }: {
boot.kernelPackages = pkgs.linuxPackages_5_15;
users.users = {
tarn = {
isNormalUser = true;
extraGroups = [ "wheel" ];
password = "";
};
};
environment.systemPackages = with pkgs; [
python310
];
system.stateVersion = "23.05";
}
{
description = "Example Virtual Machine Configuration";
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixos-23.05";
};
outputs = { self, nixpkgs }: {
nixosConfigurations = {
# configuration for builidng qcow2 images
build-qcow2 = nixpkgs.lib.nixosSystem {
system = "x86_64-linux";
modules = [
./configuration.nix
./qcow.nix
];
};
};
};
}
{ config, lib, pkgs, modulesPath, ... }: {
imports = [
"${toString modulesPath}/profiles/qemu-guest.nix"
];
fileSystems."/" = {
device = "/dev/disk/by-label/nixos";
autoResize = true;
fsType = "ext4";
};
boot.kernelParams = ["console=ttyS0"];
boot.loader.grub.device = lib.mkDefault "/dev/vda";
system.build.qcow2 = import "${modulesPath}/../lib/make-disk-image.nix" {
inherit lib config pkgs;
diskSize = 10240;
format = "qcow2";
partitionTableType = "hybrid";
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment