Skip to content

Instantly share code, notes, and snippets.

@ibizaman
Last active January 9, 2024 22:58
Show Gist options
  • Save ibizaman/c7dc4ef51612338312e7118bb029d1d2 to your computer and use it in GitHub Desktop.
Save ibizaman/c7dc4ef51612338312e7118bb029d1d2 to your computer and use it in GitHub Desktop.
$ nixos-rebuild build-vm-with-bootloader --fast -I nixos-config=./configuration.nix -I nixpkgs=.
building the system configuration...
these 5 derivations will be built:
/nix/store/4sl01j0zzg0aplvzxpwy81gpaaz2i3v5-users-groups.json.drv
/nix/store/rpxd1rvfk9fih85hkvcqs6z0avw35hyy-nixos-system-nixos-22.11.3408.a575c243c23.drv
/nix/store/4wm1pi5hvhfpwic3x3g14zqrx5a25g0y-nixos-boot-disk.drv
/nix/store/r5vj647ln6spdlhzrj2n0hzl7kg8rk7n-run-nixos-vm.drv
/nix/store/xx2z3hz5c27k68si86k9wk4j4jdjrgih-nixos-vm.drv
building '/nix/store/4sl01j0zzg0aplvzxpwy81gpaaz2i3v5-users-groups.json.drv'...
building '/nix/store/rpxd1rvfk9fih85hkvcqs6z0avw35hyy-nixos-system-nixos-22.11.3408.a575c243c23.drv'...
building '/nix/store/4wm1pi5hvhfpwic3x3g14zqrx5a25g0y-nixos-boot-disk.drv'...
Formatting '/nix/store/96rq1541a8r0mazqg65ab89zi3a8w157-nixos-boot-disk/disk.img', fmt=qcow2 cluster_size=65536 extended_l2=off compression_type=zlib size=62914560 lazy_refcounts=off refcount_bits=16
cSeaBIOS (version rel-1.16.0-0-gd239552ce722-prebuilt.qemu.org)
iPXE (http://ipxe.org) 00:03.0 CA00 PCI2.10 PnP PMM+1FF90EC0+1FEF0EC0 CA00
Booting from ROM...
Probing EDD (edd=off to disable)... ok
cloading kernel modules...
[ 0.305667] Invalid ELF header magic: != ELF
[ 0.328776] Invalid ELF header magic: != ELF
[ 0.335248] Invalid ELF header magic: != ELF
[ 0.340363] Invalid ELF header magic: != ELF
[ 0.459875] Invalid ELF header magic: != ELF
[ 0.464003] Invalid ELF header magic: != ELF
[ 0.476038] Invalid ELF header magic: != ELF
[ 0.481008] Invalid ELF header magic: != ELF
[ 0.485137] Invalid ELF header magic: != ELF
[ 0.489075] Invalid ELF header magic: != ELF
[ 0.511848] Invalid ELF header magic: != ELF
[ 0.517496] Invalid ELF header magic: != ELF
[ 0.522842] Invalid ELF header magic: != ELF
[ 0.529019] Invalid ELF header magic: != ELF
[ 0.533889] Invalid ELF header magic: != ELF
[ 0.625150] Invalid ELF header magic: != ELF
[ 0.651211] Invalid ELF header magic: != ELF
[ 0.673125] Invalid ELF header magic: != ELF
[ 0.684963] Invalid ELF header magic: != ELF
[ 0.692906] Invalid ELF header magic: != ELF
mounting Nix store...
mounting host's temporary directory...
starting stage 2 (/nix/store/kjr8f1pbmx89d8wwz3s6sjnwdw5zzpib-vm-run-stage2)
Creating new GPT entries in memory.
The operation has completed successfully.
mkfs.fat 4.2 (2021-01-31)
Hidden (32) does not match sectors (63)
insmod: ERROR: could not insert module /nix/store/4lqix0wdg2pbn1xqr1qi416bk4xi60ni-linux-5.15.104/lib/modules/5.15.104/kernel/fs/efivarfs/efivarfs.ko.xz: No such device
updating GRUB 2 menu...
installing the GRUB 2 boot loader on /dev/vdb...
Installing for i386-pc platform.
Installation finished. No error reported.
[ 6.686267] reboot: Power down
building '/nix/store/r5vj647ln6spdlhzrj2n0hzl7kg8rk7n-run-nixos-vm.drv'...
building '/nix/store/xx2z3hz5c27k68si86k9wk4j4jdjrgih-nixos-vm.drv'...
Done. The virtual machine can be started by running /nix/store/s1fwy979nzjgsjsndk54gdnvwvcr6m5n-nixos-vm/bin/run-nixos-vm
#! /nix/store/96ky1zdkpq871h2dlk198fz0zvklr1dr-bash-5.1-p16/bin/bash
export PATH=/nix/store/wxb674h6dp7h63na8z6jwpagps811jl7-coreutils-9.1/bin${PATH:+:}$PATH
set -e
NIX_DISK_IMAGE=$(readlink -f "${NIX_DISK_IMAGE:-./nixos.qcow2}")
if ! test -e "$NIX_DISK_IMAGE"; then
/nix/store/x41ba9z7gp0621b6gz14qg8hqql7kzdq-qemu-host-cpu-only-7.1.0/bin/qemu-img create -f qcow2 "$NIX_DISK_IMAGE" \
1024M
fi
# Create a directory for storing temporary data of the running VM.
if [ -z "$TMPDIR" ] || [ -z "$USE_TMPDIR" ]; then
TMPDIR=$(mktemp -d nix-vm.XXXXXXXXXX --tmpdir)
fi
# Create a directory for exchanging data with the VM.
mkdir -p "$TMPDIR/xchg"
# Create a writable copy/snapshot of the boot disk.
# A writable boot disk can be booted from automatically.
/nix/store/x41ba9z7gp0621b6gz14qg8hqql7kzdq-qemu-host-cpu-only-7.1.0/bin/qemu-img create -f qcow2 -F qcow2 -b /nix/store/ys7yf1gl7mvq7yqk3ffdn2whssradrc1-nixos-boot-disk/disk.img "$TMPDIR/disk.img"
NIX_EFI_VARS=$(readlink -f "${NIX_EFI_VARS:-./nixos-efi-vars.fd}")
cd "$TMPDIR"
# Start QEMU.
exec /nix/store/x41ba9z7gp0621b6gz14qg8hqql7kzdq-qemu-host-cpu-only-7.1.0/bin/qemu-kvm -cpu max \
-name nixos \
-m 8192 \
-smp 1 \
-device virtio-rng-pci \
-net nic,netdev=user.0,model=virtio -netdev user,id=user.0,"$QEMU_NET_OPTS" \
-virtfs local,path=/nix/store,security_model=none,mount_tag=nix-store \
-virtfs local,path="${SHARED_DIR:-$TMPDIR/xchg}",security_model=none,mount_tag=shared \
-virtfs local,path="$TMPDIR"/xchg,security_model=none,mount_tag=xchg \
-drive cache=writeback,file="$NIX_DISK_IMAGE",id=drive1,if=none,index=1,werror=report -device virtio-blk-pci,drive=drive1\
-drive file="$TMPDIR"/disk.img,id=drive2,if=none,index=2,media=disk -device virtio-blk-pci,bootindex=1,drive=drive2 \
-device virtio-keyboard \
-usb \
-device usb-tablet,bus=usb-bus.0 \
$QEMU_OPTS \
"$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment