Open brprice opened 3 years ago
The problem lies within qemu-vm.nix, specifically https://github.com/NixOS/nixpkgs/blob/cedcf2565c6b982d703d67455199b09a3d905d86/nixos/modules/virtualisation/qemu-vm.nix#L629-L633 we add a regInfo
kernel parameter when doing build-vm
(but not build-vm-with-bootloader
); this is then parsed on boot at
https://github.com/NixOS/nixpkgs/blob/cedcf2565c6b982d703d67455199b09a3d905d86/nixos/modules/virtualisation/qemu-vm.nix#L599-L611 in order to seed the vm's nix db.
However, this only works when booting directly, as it adds a kernel parameter directly to the qemu options.
It seems that if we want something similar for when running with a bootloader, we need to do some similar indirection via the bootdisk that is created https://github.com/NixOS/nixpkgs/blob/cedcf2565c6b982d703d67455199b09a3d905d86/nixos/modules/virtualisation/qemu-vm.nix#L176.
Since we have an actual filesystem we can use, I initially tried recording the regInfo
in a file in the boot disk and then reading it out in postBootCommands
. Unfortunately this runs too early, and the fs is not yet mounted.
I then did the analogue of the non-bootloader case by editing the /boot/loader/entries/...
file, which works but feels rather un-robust. (https://github.com/brprice/nixpkgs/tree/brprice/vm-nix-db-sed) I'm not particularly happy with this approach as it relies on guessing the correct bootloader file to edit, I have no idea if it would work with both grub and systemd-boot etc.
I marked this as stale due to inactivity. → More info
Describe the bug VMs created by
nixos-rebuild build-vm
andnixos-rebuild build-vm-with-bootloader
both share/nix/store
with their host (via an overlayfs), but onlybuild-vm
properly populates the vm's nix db. This leads to needless downloading of packages when doing anix shell nixpkgs#coreutils
, for instance.The underlying reason for this is that
build-vm
registers the closure of the vm in its nix db:nix-store --dump-db
will show lots of output; however,build-vm-with-bootloader
does not:nix-store --dump-db
will be blank. This leads nix to ignore everything in/nix/.ro-store
, and re-download into/nix/.rw-store/store
as needed.To Reproduce Steps to reproduce the behavior:
nixos-rebuild build-vm --flake <path/to/flake>#bare
(if you are using the linked flake; adjust if not)nix-store --dump-db
gives lots of output (this is the good case)rm vmhost.qcow2
-v
in the next command is vital for that)nixos-rebuild build-vm-with-bootloader -v --flake <path/to/flake>#bare
building '/nix/store/kpr3f65i050ia9qqd6cd3bzir6jw0qwk-run-nixos-vm.drv'...
; runnix-store -q --outputs <that /nix/store/....drv>
to find the script to run the vm.nix-store --dump-db
gives no output (this is the bad case)Expected behavior That the two vms should behave the same for this test. In particular, that the vm-with-bootloader should have a bunch of paths registered in its nix db.
Notify maintainers Last to touch
/nixos/modules/virtualisation/qemu-vm.nix
: @worldofpeace; The last people who touched relevant lines: @dezgeg, @edolstra, @eonpataponMetadata
Maintainer information: