9 cfg = config.virtualbox;
13 ./disk-size-option.nix
14 ../image/file-options.nix
15 (lib.mkRenamedOptionModuleWith {
26 (lib.mkRenamedOptionModuleWith {
42 baseImageFreeSpace = lib.mkOption {
46 Free space in the VirtualBox base image in MiB.
49 memorySize = lib.mkOption {
53 The amount of RAM the VirtualBox appliance can use in MiB.
56 vmDerivationName = lib.mkOption {
58 default = "nixos-ova-${config.system.nixos.label}-${pkgs.stdenv.hostPlatform.system}";
60 The name of the derivation for the VirtualBox appliance.
63 vmName = lib.mkOption {
65 default = "${config.system.nixos.distroName} ${config.system.nixos.label} (${pkgs.stdenv.hostPlatform.system})";
67 The name of the VirtualBox appliance.
70 params = lib.mkOption {
85 Parameters passed to the Virtualbox appliance.
87 Run `VBoxManage modifyvm --help` to see more options.
90 exportParams = lib.mkOption {
107 Parameters passed to the Virtualbox export command.
109 Run `VBoxManage export --help` to see more options.
112 extraDisk = lib.mkOption {
114 Optional extra disk/hdd configuration.
115 The disk will be an 'ext4' partition on a separate file.
120 mountPoint = "/home/demo/storage";
123 type = lib.types.nullOr (
124 lib.types.submodule {
126 size = lib.mkOption {
127 type = lib.types.int;
128 description = "Size in MiB";
130 label = lib.mkOption {
131 type = lib.types.str;
132 default = "vm-extra-storage";
133 description = "Label for the disk partition";
135 mountPoint = lib.mkOption {
136 type = lib.types.str;
137 description = "Path where to mount this disk.";
143 postExportCommands = lib.mkOption {
144 type = lib.types.lines;
147 ${pkgs.cot}/bin/cot edit-hardware "$fn" \
150 --nic-types VMXNET3 \
151 --nic-names 'Nic name' \
152 --nic-networks 'Nic match' \
153 --network-descriptions 'Nic description' \
154 --scsi-subtypes VirtualSCSI
157 Extra commands to run after exporting the OVA to `$fn`.
160 storageController = lib.mkOption {
184 Parameters passed to the VirtualBox appliance. Must have at least
187 Run `VBoxManage storagectl --help` to see more options.
194 # Use a priority just below mkOptionDefault (1500) instead of lib.mkDefault
195 # to avoid breaking existing configs using that.
196 virtualisation.diskSize = lib.mkOverride 1490 (50 * 1024);
198 virtualbox.params = lib.mkMerge [
199 (lib.mapAttrs (name: lib.mkDefault) {
204 audiocontroller = "ac97";
207 graphicscontroller = "vmsvga";
213 (lib.mkIf (pkgs.stdenv.hostPlatform.system == "i686-linux") { pae = "on"; })
216 system.nixos.tags = [ "virtualbox" ];
217 image.extension = "ova";
218 system.build.image = lib.mkDefault config.system.build.virtualBoxOVA;
219 system.build.virtualBoxOVA = import ../../lib/make-disk-image.nix {
220 name = cfg.vmDerivationName;
221 baseName = config.image.baseName;
223 inherit pkgs lib config;
224 partitionTableType = "legacy";
225 inherit (config.virtualisation) diskSize;
226 additionalSpace = "${toString cfg.baseImageFreeSpace}M";
230 export PATH=${pkgs.virtualbox}/bin:$PATH
232 echo "converting image to VirtualBox format..."
233 VBoxManage convertfromraw $diskImage disk.vdi
235 ${lib.optionalString (cfg.extraDisk != null) ''
236 echo "creating extra disk: data-disk.raw"
237 dataDiskImage=data-disk.raw
238 truncate -s ${toString cfg.extraDisk.size}M $dataDiskImage
240 parted --script $dataDiskImage -- \
242 mkpart primary ext4 1MiB -1
243 eval $(partx $dataDiskImage -o START,SECTORS --nr 1 --pairs)
244 mkfs.ext4 -F -L ${cfg.extraDisk.label} $dataDiskImage -E offset=$(sectorsToBytes $START) $(sectorsToKilobytes $SECTORS)K
245 echo "creating extra disk: data-disk.vdi"
246 VBoxManage convertfromraw $dataDiskImage data-disk.vdi
249 echo "creating VirtualBox VM..."
250 vmName="${cfg.vmName}";
251 VBoxManage createvm --name "$vmName" --register \
252 --ostype ${if pkgs.stdenv.hostPlatform.system == "x86_64-linux" then "Linux26_64" else "Linux26"}
253 VBoxManage modifyvm "$vmName" \
254 --memory ${toString cfg.memorySize} \
255 ${lib.cli.toGNUCommandLineShell { } cfg.params}
256 VBoxManage storagectl "$vmName" ${lib.cli.toGNUCommandLineShell { } cfg.storageController}
257 VBoxManage storageattach "$vmName" --storagectl ${cfg.storageController.name} --port 0 --device 0 --type hdd \
259 ${lib.optionalString (cfg.extraDisk != null) ''
260 VBoxManage storageattach "$vmName" --storagectl ${cfg.storageController.name} --port 1 --device 0 --type hdd \
261 --medium data-disk.vdi
264 echo "exporting VirtualBox VM..."
266 fn="$out/${config.image.fileName}"
267 VBoxManage export "$vmName" --output "$fn" --options manifest ${lib.escapeShellArgs cfg.exportParams}
268 ${cfg.postExportCommands}
272 mkdir -p $out/nix-support
273 echo "file ova $fn" >> $out/nix-support/hydra-build-products
280 device = "/dev/disk/by-label/nixos";
285 // (lib.optionalAttrs (cfg.extraDisk != null) {
286 ${cfg.extraDisk.mountPoint} = {
287 device = "/dev/disk/by-label/" + cfg.extraDisk.label;
293 boot.growPartition = true;
294 boot.loader.grub.device = "/dev/sda";
298 device = "/var/swap";
303 virtualisation.virtualbox.guest.enable = true;