jb55
· 17w
I asked how it worked. i didn't even know you could do this. this is badass
"The clever bit: It mounts your host's /nix/store directly into the VM via virtfs. The VM doesn't need its own copy of all ...
this thing is a bigger nerd than me, jfc:
QEMU has a protocol called 9P (from Plan 9) that lets the guest mount a host directory as a
filesystem. The virtio transport makes it fast - no network overhead, just shared memory.
The flag from the run script:
-virtfs local,path=/nix/store,security_model=none,mount_tag=nix-store
The guest kernel mounts it via:
mount -t 9p -o trans=virtio nix-store /nix/.ro-store
How NixOS layers it
Look at your df output again:
nix-store 461G 423G 15G 97% /nix/.ro-store # host's store (read-only)
tmpfs 2.0G 0 2.0G 0% /nix/.rw-store # writable tmpfs layer
overlay 2.0G 0 2.0G 0% /nix/store # combined view
It's an overlay filesystem:
- Lower layer: Your host's /nix/store mounted read-only
- Upper layer: A tmpfs for any new packages the VM builds/installs
- Merged view: The VM sees a unified /nix/store
Why it's brilliant for NixOS
Since Nix store paths are content-addressed and immutable, sharing them is safe. The VM
boots with instant access to your entire host store (423GB of packages) without copying a
single byte. It only uses disk space for genuinely new stuff the VM creates.
That's why the VM boots so fast and the disk image stays tiny.