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.int;
15 default = 10 * 1024;
16 description = ''
17 The size of the VirtualBox base image in MiB.
18 '';
19 };
20 memorySize = mkOption {
21 type = types.int;
22 default = 1536;
23 description = ''
24 The amount of RAM the VirtualBox appliance can use in MiB.
25 '';
26 };
27 vmDerivationName = mkOption {
28 type = types.str;
29 default = "nixos-ova-${config.system.nixos.label}-${pkgs.stdenv.hostPlatform.system}";
30 description = ''
31 The name of the derivation for the VirtualBox appliance.
32 '';
33 };
34 vmName = mkOption {
35 type = types.str;
36 default = "NixOS ${config.system.nixos.label} (${pkgs.stdenv.hostPlatform.system})";
37 description = ''
38 The name of the VirtualBox appliance.
39 '';
40 };
41 vmFileName = mkOption {
42 type = types.str;
43 default = "nixos-${config.system.nixos.label}-${pkgs.stdenv.hostPlatform.system}.ova";
44 description = ''
45 The file name of the VirtualBox appliance.
46 '';
47 };
48 };
49 };
50
51 config = {
52 system.build.virtualBoxOVA = import ../../lib/make-disk-image.nix {
53 name = cfg.vmDerivationName;
54
55 inherit pkgs lib config;
56 partitionTableType = "legacy";
57 diskSize = cfg.baseImageSize;
58
59 postVM =
60 ''
61 export HOME=$PWD
62 export PATH=${pkgs.virtualbox}/bin:$PATH
63
64 echo "creating VirtualBox pass-through disk wrapper (no copying invovled)..."
65 VBoxManage internalcommands createrawvmdk -filename disk.vmdk -rawdisk $diskImage
66
67 echo "creating VirtualBox VM..."
68 vmName="${cfg.vmName}";
69 VBoxManage createvm --name "$vmName" --register \
70 --ostype ${if pkgs.stdenv.hostPlatform.system == "x86_64-linux" then "Linux26_64" else "Linux26"}
71 VBoxManage modifyvm "$vmName" \
72 --memory ${toString cfg.memorySize} --acpi on --vram 32 \
73 ${optionalString (pkgs.stdenv.hostPlatform.system == "i686-linux") "--pae on"} \
74 --nictype1 virtio --nic1 nat \
75 --audiocontroller ac97 --audio alsa \
76 --rtcuseutc on \
77 --usb on --mouse usbtablet
78 VBoxManage storagectl "$vmName" --name SATA --add sata --portcount 4 --bootable on --hostiocache on
79 VBoxManage storageattach "$vmName" --storagectl SATA --port 0 --device 0 --type hdd \
80 --medium disk.vmdk
81
82 echo "exporting VirtualBox VM..."
83 mkdir -p $out
84 fn="$out/${cfg.vmFileName}"
85 VBoxManage export "$vmName" --output "$fn"
86
87 rm -v $diskImage
88
89 mkdir -p $out/nix-support
90 echo "file ova $fn" >> $out/nix-support/hydra-build-products
91 '';
92 };
93
94 fileSystems."/" = {
95 device = "/dev/disk/by-label/nixos";
96 autoResize = true;
97 };
98
99 boot.growPartition = true;
100 boot.loader.grub.device = "/dev/sda";
101
102 virtualisation.virtualbox.guest.enable = true;
103
104 };
105}