Building QEMU/KVM VMs

Hi everybody,

I want to build a full QEMU/KVM VM that doesn’t share anything with the host, and therefore I can’t use nixos-rebuild build-vm.

I searched this question and I found a post that describes building virtualBoxOVA by importing <nixos/nixos/modules/virtualisation/virtualbox-image.nix>.

So I looked at https://github.com/NixOS/nixpkgs/blob/52c495b2d9fb8918f71be2646bc626804cda8a73/nixos/modules/virtualisation/

and imported:

modules =
    [
    <nixos/nixos/modules/virtualisation/qemu-vm.nix>
    ...
    ];

and in REPL I looked at

:lf .
builtins.attrNames    nixosConfigurations."mysystem".config.system.build

but I didn’t notice anything new.

Could you please tell what do I need to import?

Also is there a better way how to specify imports?

I use a flake for my NixOS config and importing <nixos/.../...nix seems like pre-flake configuration. For example HM is imported as home-manager.nixosModules.home-manager and not <home-manager/.../...nix.

Thank you.

You can use fallocate to create a disk image. Then, create partitions n the disk image using fdisk or parted. Next, format the partitions and mount them. Then you can use nixos-install to install NixOS into the disk image. You should be able to convert the raw image to qcow2, if you want.

The problem with nixos-rebuild build-vm is that it shares /nix store. That’s great for testing but not so much for a general use as a VM that can run on a different host.

You’re looking for either virtualisation.useNixStoreImage or virtualisation.useBootLoader then.

But are you sure this is actually what you want? If you build the VM, you’ll need to copy the entire derivation to whatever device you plan to use it on anyway, and the closure will always contain everything it needs.

Not speaking for the OP’s specific needs, but a common reason to need this is when the intended VM host is not running nix/nixos; indeed it may not be a host you control at all.

Yes, of course. I want to get qcow2 file with many GiB in size.

Exactly.

Is my question surprising / unusual? Isn’t this a common thing people do to use Nix to get Raspberry Pi images, containers, VirtualBox VMs, AWS AMIs and they all have all bits they need to run on any hardware / host?

First time I heard about Nix is when I researched for an alternative to Hashicorp Packer to build AWS, Azure, QEMU… images

It is surprising that people don’t “speak” more often about building topic. Most of questions here on the forum are about building executable and configuration (NixOS)…

Me too!

I’m thinking about hacking https://github.com/NixOS/nixpkgs/blob/52c495b2d9fb8918f71be2646bc626804cda8a73/nixos/modules/virtualisation/digital-ocean-image.nix but at this moment I have no experience in building other things then compiling software.

Would you recommend some blog / documentation how people build custom images like Raspberry Pi, qcow2, AMI?

Yes please. Anyone knowledgeable in building rpi4/vm images?

Could this help you?

Yes, that helps. Thank you!

If you’re building VM why not build and install the VM within say, virt-manager? That would keep it pretty separate aside from passing through your network config and such? Or am I misunderstanding the specific ask here? Sorry if I am!