1{ config, lib, pkgs, ... }:
2
3with lib;
4
5let
6
7 cfg = config.virtualbox;
8
9in {
10
11 options = {
12 virtualbox = {
13 baseImageSize = mkOption {
14 type = types.str;
15 default = "10G";
16 description = ''
17 The size of the VirtualBox base image. The size string should be on
18 a format the qemu-img command accepts.
19 '';
20 };
21 };
22 };
23
24 config = {
25 system.build.virtualBoxImage =
26 pkgs.vmTools.runInLinuxVM (
27 pkgs.runCommand "virtualbox-image"
28 { memSize = 768;
29 preVM =
30 ''
31 mkdir $out
32 diskImage=$out/image
33 ${pkgs.vmTools.qemu}/bin/qemu-img create -f raw $diskImage "${cfg.baseImageSize}"
34 mv closure xchg/
35 '';
36 postVM =
37 ''
38 echo "creating VirtualBox disk image..."
39 ${pkgs.vmTools.qemu}/bin/qemu-img convert -f raw -O vdi $diskImage $out/disk.vdi
40 rm $diskImage
41 '';
42 buildInputs = [ pkgs.utillinux pkgs.perl ];
43 exportReferencesGraph =
44 [ "closure" config.system.build.toplevel ];
45 }
46 ''
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
52
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
56 mkdir /mnt
57 mount /dev/vda1 /mnt
58
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
64
65 # Copy all paths in the closure to the filesystem.
66 storePaths=$(perl ${pkgs.pathsFromGraph} /tmp/xchg/closure)
67
68 echo "filling Nix store..."
69 mkdir -p /mnt/nix/store
70 set -f
71 cp -prd $storePaths /mnt/nix/store/
72
73 mkdir -p /mnt/etc/nix
74 echo 'build-users-group = ' > /mnt/etc/nix/nix.conf
75
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
79
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}
83
84 # `nixos-rebuild' requires an /etc/NIXOS.
85 mkdir -p /mnt/etc/nixos
86 touch /mnt/etc/NIXOS
87
88 # `switch-to-configuration' requires a /bin/sh
89 mkdir -p /mnt/bin
90 ln -s ${config.system.build.binsh}/bin/sh /mnt/bin/sh
91
92 # Generate the GRUB menu.
93 ln -s vda /dev/sda
94 chroot /mnt ${config.system.build.toplevel}/bin/switch-to-configuration boot
95
96 umount /mnt/proc /mnt/dev /mnt/sys
97 umount /mnt
98 ''
99 );
100
101 system.build.virtualBoxOVA = pkgs.runCommand "virtualbox-ova"
102 { buildInputs = [ pkgs.linuxPackages.virtualbox ];
103 vmName = "NixOS ${config.system.nixosVersion} (${pkgs.stdenv.system})";
104 fileName = "nixos-${config.system.nixosVersion}-${pkgs.stdenv.system}.ova";
105 }
106 ''
107 echo "creating VirtualBox VM..."
108 export HOME=$PWD
109 VBoxManage createvm --name "$vmName" --register \
110 --ostype ${if pkgs.stdenv.system == "x86_64-linux" then "Linux26_64" else "Linux26"}
111 VBoxManage modifyvm "$vmName" \
112 --memory 1536 --acpi on --vram 10 \
113 --nictype1 virtio --nic1 nat \
114 --audiocontroller ac97 --audio alsa \
115 --rtcuseutc on \
116 --usb on --mouse usbtablet
117 VBoxManage storagectl "$vmName" --name SATA --add sata --portcount 4 --bootable on --hostiocache on
118 VBoxManage storageattach "$vmName" --storagectl SATA --port 0 --device 0 --type hdd \
119 --medium ${config.system.build.virtualBoxImage}/disk.vdi
120
121 echo "exporting VirtualBox VM..."
122 mkdir -p $out
123 VBoxManage export "$vmName" --output "$out/$fileName"
124 '';
125
126 fileSystems."/".device = "/dev/disk/by-label/nixos";
127
128 boot.loader.grub.version = 2;
129 boot.loader.grub.device = "/dev/sda";
130
131 services.virtualboxGuest.enable = true;
132 };
133}