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.
run the ./result/bin/run-nixos-vm script, which creates the my-vm.qcow2 virtual disk
Shutdown the VM gracefully
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.