···
baseImageSize = mkOption {
15
+
default = 10 * 1024;
17
-
The size of the VirtualBox base image. The size string should be on
18
-
a format the qemu-img command accepts.
17
+
The size of the VirtualBox base image in MiB.
25
-
system.build.virtualBoxImage =
26
-
pkgs.vmTools.runInLinuxVM (
27
-
pkgs.runCommand "virtualbox-image"
32
-
diskImage=$out/image
33
-
${pkgs.vmTools.qemu}/bin/qemu-img create -f raw $diskImage "${cfg.baseImageSize}"
38
-
echo "creating VirtualBox disk image..."
39
-
${pkgs.vmTools.qemu}/bin/qemu-img convert -f raw -O vdi $diskImage $out/disk.vdi
42
-
buildInputs = [ pkgs.utillinux pkgs.perl ];
43
-
exportReferencesGraph =
44
-
[ "closure" config.system.build.toplevel ];
25
+
system.build.virtualBoxImage = import ../../lib/make-disk-image.nix {
26
+
inherit pkgs lib config;
28
+
diskSize = cfg.baseImageSize;
30
+
configFile = pkgs.writeText "configuration.nix"
33
+
imports = [ <nixpkgs/nixos/modules/virtualisation/virtualbox-image.nix> ];
47
-
# Create a single / partition.
48
-
${pkgs.parted}/sbin/parted /dev/vda mklabel msdos
49
-
${pkgs.parted}/sbin/parted /dev/vda -- mkpart primary ext2 1M -1s
50
-
. /sys/class/block/vda1/uevent
51
-
mknod /dev/vda1 b $MAJOR $MINOR
53
-
# Create an empty filesystem and mount it.
54
-
${pkgs.e2fsprogs}/sbin/mkfs.ext4 -L nixos /dev/vda1
55
-
${pkgs.e2fsprogs}/sbin/tune2fs -c 0 -i 0 /dev/vda1
57
-
mount /dev/vda1 /mnt
59
-
# The initrd expects these directories to exist.
60
-
mkdir /mnt/dev /mnt/proc /mnt/sys
61
-
mount --bind /proc /mnt/proc
62
-
mount --bind /dev /mnt/dev
63
-
mount --bind /sys /mnt/sys
65
-
# Copy all paths in the closure to the filesystem.
66
-
storePaths=$(perl ${pkgs.pathsFromGraph} /tmp/xchg/closure)
68
-
echo "filling Nix store..."
69
-
mkdir -p /mnt/nix/store
71
-
cp -prd $storePaths /mnt/nix/store/
73
-
mkdir -p /mnt/etc/nix
74
-
echo 'build-users-group = ' > /mnt/etc/nix/nix.conf
76
-
# Register the paths in the Nix database.
77
-
printRegistration=1 perl ${pkgs.pathsFromGraph} /tmp/xchg/closure | \
78
-
chroot /mnt ${config.nix.package}/bin/nix-store --load-db
80
-
# Create the system profile to allow nixos-rebuild to work.
81
-
chroot /mnt ${config.nix.package}/bin/nix-env \
82
-
-p /nix/var/nix/profiles/system --set ${config.system.build.toplevel}
84
-
# `nixos-rebuild' requires an /etc/NIXOS.
85
-
mkdir -p /mnt/etc/nixos
86
-
touch /mnt/etc/NIXOS
88
-
# `switch-to-configuration' requires a /bin/sh
90
-
ln -s ${config.system.build.binsh}/bin/sh /mnt/bin/sh
92
-
# Generate the GRUB menu.
94
-
chroot /mnt ${config.system.build.toplevel}/bin/switch-to-configuration boot
96
-
umount /mnt/proc /mnt/dev /mnt/sys
39
+
echo "creating VirtualBox disk image..."
40
+
${pkgs.vmTools.qemu}/bin/qemu-img convert -f raw -O vdi $diskImage $out/disk.vdi
system.build.virtualBoxOVA = pkgs.runCommand "virtualbox-ova"
{ buildInputs = [ pkgs.linuxPackages.virtualbox ];
vmName = "NixOS ${config.system.nixosVersion} (${pkgs.stdenv.system})";
···
VBoxManage storagectl "$vmName" --name SATA --add sata --portcount 4 --bootable on --hostiocache on
VBoxManage storageattach "$vmName" --storagectl SATA --port 0 --device 0 --type hdd \
--medium ${config.system.build.virtualBoxImage}/disk.vdi
echo "exporting VirtualBox VM..."
VBoxManage export "$vmName" --output "$out/$fileName"
fileSystems."/".device = "/dev/disk/by-label/nixos";
129
-
boot.loader.grub.version = 2;
boot.loader.grub.device = "/dev/sda";
virtualisation.virtualbox.guest.enable = true;