How to export a “NixOS Virtual Machine” to Proxmox VM

How can I export a “NixOS Virtual Machine” after creating it with nixos-rebuild build-vm?

  • I really like that I can create a Qemu VM with the exact configuration I want. But I need to be able to move this VM to a more traditional VM solution like Proxmox, VBox or VMWare.

My naive approach was to

  1. Create the NixOS Qemu VM as instructed in NixOS virtual machines — nix.dev documentation

  2. run the ./result/bin/run-nixos-vm script, which creates the my-vm.qcow2 virtual disk

  3. Shutdown the VM gracefully

  4. Copy the my-vm.qcow2 virtual disk created in step 2 and import it as a bootable disk into a Proxmox VM.

However, the Proxmox VM never boots with this qcow2 virtual disk. And attaching the disk as a secondary hard drive in the Proxmox VM, shows it only has folders, but all of them empty (like /home/my_user and other.

Little I knew (and after digging a little bit I have learned) that NixOS does something very clever with qemu… it creates a VM that has most of not all its files linked to files in my laptop, which the common NixOS method of linking files to the /nix/store/ directory in your laptop.

From looking trough the virtualisation sub-options, it depends on what you want to achieve. By default nix VMs will use the hosts store instead of using a separate image inside the VM. As far as I know and how I understand it, virtualisation.useNixStoreImage = true; should do the trick.
However, in this case all writes to the store will be stored inside a temporary file system. Thus, they will not survive rebooting the VM.
From looking at the documentation of virtualisation.useNixStoreImage, a persistent nix store (et. al.) should be achieved by using virtualisation.useBootLoader.

I hope that this at least points you in the right direction. Tho I don’t have much experience using the virtualisation module, thus it’s quite possible that there are other, better suited, options.

Is the VM an EFI or BIOS install?

Thank you so much for the fast responses.

I just discovered and started using GitHub - nix-community/nixos-generators: Collection of image builders [maintainer=@Lassulus] as follows:

  1. Create the VM with nixos-generate -f proxmox -c configuration.nix - This creates a *.vma.zst file in my /nix/store/ (about 500MB)

  2. Copy the file to Proxmox with scp

  3. Uncompress the file with unzstd /var/lib/vz/dump/my-vm.vma.zst

  4. Import the VM into Proxmox with qmrestore /mnt/backup/my-vm.vma 601

1 Like