2014-04-14 15:26:48 +01:00
|
|
|
{ config, lib, pkgs, ... }:
|
2012-02-25 21:36:00 +00:00
|
|
|
|
2014-04-14 15:26:48 +01:00
|
|
|
with lib;
|
2012-02-25 21:36:00 +00:00
|
|
|
|
2014-08-04 10:29:51 +01:00
|
|
|
let
|
|
|
|
|
|
|
|
cfg = config.virtualbox;
|
|
|
|
|
|
|
|
in {
|
|
|
|
|
|
|
|
options = {
|
|
|
|
virtualbox = {
|
|
|
|
baseImageSize = mkOption {
|
2020-12-19 18:41:11 +00:00
|
|
|
type = with types; either (enum [ "auto" ]) int;
|
|
|
|
default = "auto";
|
|
|
|
example = 50 * 1024;
|
2014-08-04 10:29:51 +01:00
|
|
|
description = ''
|
2015-09-28 13:31:19 +01:00
|
|
|
The size of the VirtualBox base image in MiB.
|
2014-08-04 10:29:51 +01:00
|
|
|
'';
|
|
|
|
};
|
2021-07-24 08:33:10 +01:00
|
|
|
baseImageFreeSpace = mkOption {
|
|
|
|
type = with types; int;
|
|
|
|
default = 30 * 1024;
|
|
|
|
description = ''
|
|
|
|
Free space in the VirtualBox base image in MiB.
|
|
|
|
'';
|
|
|
|
};
|
2018-07-11 18:45:10 +01:00
|
|
|
memorySize = mkOption {
|
|
|
|
type = types.int;
|
|
|
|
default = 1536;
|
|
|
|
description = ''
|
|
|
|
The amount of RAM the VirtualBox appliance can use in MiB.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
vmDerivationName = mkOption {
|
|
|
|
type = types.str;
|
2018-08-20 20:11:29 +01:00
|
|
|
default = "nixos-ova-${config.system.nixos.label}-${pkgs.stdenv.hostPlatform.system}";
|
2018-07-11 18:45:10 +01:00
|
|
|
description = ''
|
|
|
|
The name of the derivation for the VirtualBox appliance.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
vmName = mkOption {
|
|
|
|
type = types.str;
|
2018-08-20 20:11:29 +01:00
|
|
|
default = "NixOS ${config.system.nixos.label} (${pkgs.stdenv.hostPlatform.system})";
|
2018-07-11 18:45:10 +01:00
|
|
|
description = ''
|
|
|
|
The name of the VirtualBox appliance.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
vmFileName = mkOption {
|
|
|
|
type = types.str;
|
2018-08-20 20:11:29 +01:00
|
|
|
default = "nixos-${config.system.nixos.label}-${pkgs.stdenv.hostPlatform.system}.ova";
|
2018-07-11 18:45:10 +01:00
|
|
|
description = ''
|
|
|
|
The file name of the VirtualBox appliance.
|
|
|
|
'';
|
|
|
|
};
|
2020-02-21 00:02:47 +00:00
|
|
|
params = mkOption {
|
|
|
|
type = with types; attrsOf (oneOf [ str int bool (listOf str) ]);
|
|
|
|
example = {
|
|
|
|
audio = "alsa";
|
|
|
|
rtcuseutc = "on";
|
|
|
|
usb = "off";
|
|
|
|
};
|
|
|
|
description = ''
|
|
|
|
Parameters passed to the Virtualbox appliance.
|
|
|
|
|
|
|
|
Run <literal>VBoxManage modifyvm --help</literal> to see more options.
|
|
|
|
'';
|
2021-06-02 12:07:50 +01:00
|
|
|
};
|
|
|
|
exportParams = mkOption {
|
|
|
|
type = with types; listOf (oneOf [ str int bool (listOf str) ]);
|
|
|
|
example = [
|
|
|
|
"--vsys" "0" "--vendor" "ACME Inc."
|
|
|
|
];
|
|
|
|
default = [];
|
|
|
|
description = ''
|
|
|
|
Parameters passed to the Virtualbox export command.
|
|
|
|
|
|
|
|
Run <literal>VBoxManage export --help</literal> to see more options.
|
|
|
|
'';
|
|
|
|
};
|
2019-04-23 06:11:39 +01:00
|
|
|
extraDisk = mkOption {
|
|
|
|
description = ''
|
|
|
|
Optional extra disk/hdd configuration.
|
|
|
|
The disk will be an 'ext4' partition on a separate VMDK file.
|
|
|
|
'';
|
|
|
|
default = null;
|
|
|
|
example = {
|
|
|
|
label = "storage";
|
|
|
|
mountPoint = "/home/demo/storage";
|
|
|
|
size = 100 * 1024;
|
|
|
|
};
|
|
|
|
type = types.nullOr (types.submodule {
|
|
|
|
options = {
|
|
|
|
size = mkOption {
|
|
|
|
type = types.int;
|
|
|
|
description = "Size in MiB";
|
|
|
|
};
|
|
|
|
label = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
default = "vm-extra-storage";
|
|
|
|
description = "Label for the disk partition";
|
|
|
|
};
|
|
|
|
mountPoint = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
description = "Path where to mount this disk.";
|
|
|
|
};
|
|
|
|
};
|
|
|
|
});
|
|
|
|
};
|
2014-08-04 10:29:51 +01:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
config = {
|
2020-02-21 00:02:47 +00:00
|
|
|
|
|
|
|
virtualbox.params = mkMerge [
|
|
|
|
(mapAttrs (name: mkDefault) {
|
|
|
|
acpi = "on";
|
|
|
|
vram = 32;
|
|
|
|
nictype1 = "virtio";
|
|
|
|
nic1 = "nat";
|
|
|
|
audiocontroller = "ac97";
|
|
|
|
audio = "alsa";
|
|
|
|
audioout = "on";
|
2020-06-17 17:42:36 +01:00
|
|
|
graphicscontroller = "vmsvga";
|
2020-02-21 00:02:47 +00:00
|
|
|
rtcuseutc = "on";
|
|
|
|
usb = "on";
|
|
|
|
usbehci = "on";
|
|
|
|
mouse = "usbtablet";
|
|
|
|
})
|
|
|
|
(mkIf (pkgs.stdenv.hostPlatform.system == "i686-linux") { pae = "on"; })
|
|
|
|
];
|
|
|
|
|
2016-03-15 12:34:41 +00:00
|
|
|
system.build.virtualBoxOVA = import ../../lib/make-disk-image.nix {
|
2018-07-11 18:45:10 +01:00
|
|
|
name = cfg.vmDerivationName;
|
2016-03-15 12:34:41 +00:00
|
|
|
|
2015-09-28 13:31:19 +01:00
|
|
|
inherit pkgs lib config;
|
2017-12-07 00:00:21 +00:00
|
|
|
partitionTableType = "legacy";
|
2015-09-28 13:31:19 +01:00
|
|
|
diskSize = cfg.baseImageSize;
|
2021-07-24 08:33:10 +01:00
|
|
|
additionalSpace = cfg.baseImageFreeSpace;
|
2015-09-28 13:31:19 +01:00
|
|
|
|
|
|
|
postVM =
|
|
|
|
''
|
2016-03-15 12:34:41 +00:00
|
|
|
export HOME=$PWD
|
2016-09-13 06:24:58 +01:00
|
|
|
export PATH=${pkgs.virtualbox}/bin:$PATH
|
2016-08-27 03:01:51 +01:00
|
|
|
|
2018-10-27 23:53:54 +01:00
|
|
|
echo "creating VirtualBox pass-through disk wrapper (no copying involved)..."
|
2016-08-27 03:01:51 +01:00
|
|
|
VBoxManage internalcommands createrawvmdk -filename disk.vmdk -rawdisk $diskImage
|
|
|
|
|
2019-04-23 06:11:39 +01:00
|
|
|
${optionalString (cfg.extraDisk != null) ''
|
|
|
|
echo "creating extra disk: data-disk.raw"
|
|
|
|
dataDiskImage=data-disk.raw
|
|
|
|
truncate -s ${toString cfg.extraDisk.size}M $dataDiskImage
|
|
|
|
|
|
|
|
parted --script $dataDiskImage -- \
|
|
|
|
mklabel msdos \
|
|
|
|
mkpart primary ext4 1MiB -1
|
|
|
|
eval $(partx $dataDiskImage -o START,SECTORS --nr 1 --pairs)
|
|
|
|
mkfs.ext4 -F -L ${cfg.extraDisk.label} $dataDiskImage -E offset=$(sectorsToBytes $START) $(sectorsToKilobytes $SECTORS)K
|
|
|
|
echo "creating extra disk: data-disk.vmdk"
|
|
|
|
VBoxManage internalcommands createrawvmdk -filename data-disk.vmdk -rawdisk $dataDiskImage
|
|
|
|
''}
|
|
|
|
|
2016-08-27 03:01:51 +01:00
|
|
|
echo "creating VirtualBox VM..."
|
2018-07-11 18:45:10 +01:00
|
|
|
vmName="${cfg.vmName}";
|
2016-03-15 12:34:41 +00:00
|
|
|
VBoxManage createvm --name "$vmName" --register \
|
2018-08-20 20:11:29 +01:00
|
|
|
--ostype ${if pkgs.stdenv.hostPlatform.system == "x86_64-linux" then "Linux26_64" else "Linux26"}
|
2016-03-15 12:34:41 +00:00
|
|
|
VBoxManage modifyvm "$vmName" \
|
2020-02-21 00:02:47 +00:00
|
|
|
--memory ${toString cfg.memorySize} \
|
|
|
|
${lib.cli.toGNUCommandLineShell { } cfg.params}
|
2016-03-15 12:34:41 +00:00
|
|
|
VBoxManage storagectl "$vmName" --name SATA --add sata --portcount 4 --bootable on --hostiocache on
|
|
|
|
VBoxManage storageattach "$vmName" --storagectl SATA --port 0 --device 0 --type hdd \
|
2016-08-27 03:01:51 +01:00
|
|
|
--medium disk.vmdk
|
2019-04-23 06:11:39 +01:00
|
|
|
${optionalString (cfg.extraDisk != null) ''
|
|
|
|
VBoxManage storageattach "$vmName" --storagectl SATA --port 1 --device 0 --type hdd \
|
|
|
|
--medium data-disk.vmdk
|
|
|
|
''}
|
2016-03-15 12:34:41 +00:00
|
|
|
|
|
|
|
echo "exporting VirtualBox VM..."
|
|
|
|
mkdir -p $out
|
2018-07-11 18:45:10 +01:00
|
|
|
fn="$out/${cfg.vmFileName}"
|
2021-06-02 12:07:50 +01:00
|
|
|
VBoxManage export "$vmName" --output "$fn" --options manifest ${escapeShellArgs cfg.exportParams}
|
2016-03-15 12:34:41 +00:00
|
|
|
|
2016-08-28 11:31:55 +01:00
|
|
|
rm -v $diskImage
|
|
|
|
|
2016-03-15 12:34:41 +00:00
|
|
|
mkdir -p $out/nix-support
|
|
|
|
echo "file ova $fn" >> $out/nix-support/hydra-build-products
|
2015-09-28 13:31:19 +01:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2019-04-23 06:11:39 +01:00
|
|
|
fileSystems = {
|
|
|
|
"/" = {
|
|
|
|
device = "/dev/disk/by-label/nixos";
|
|
|
|
autoResize = true;
|
|
|
|
fsType = "ext4";
|
|
|
|
};
|
|
|
|
} // (lib.optionalAttrs (cfg.extraDisk != null) {
|
|
|
|
${cfg.extraDisk.mountPoint} = {
|
|
|
|
device = "/dev/disk/by-label/" + cfg.extraDisk.label;
|
|
|
|
autoResize = true;
|
|
|
|
fsType = "ext4";
|
|
|
|
};
|
|
|
|
});
|
2015-09-28 13:31:19 +01:00
|
|
|
|
2018-01-06 13:52:51 +00:00
|
|
|
boot.growPartition = true;
|
2014-08-04 10:29:51 +01:00
|
|
|
boot.loader.grub.device = "/dev/sda";
|
2015-09-28 13:31:19 +01:00
|
|
|
|
2019-04-21 04:03:22 +01:00
|
|
|
swapDevices = [{
|
2019-03-13 01:24:44 +00:00
|
|
|
device = "/var/swap";
|
|
|
|
size = 2048;
|
2019-04-21 04:03:22 +01:00
|
|
|
}];
|
2019-03-13 01:24:44 +00:00
|
|
|
|
2015-09-02 03:54:31 +01:00
|
|
|
virtualisation.virtualbox.guest.enable = true;
|
2015-09-28 13:31:19 +01:00
|
|
|
|
2014-08-04 10:29:51 +01:00
|
|
|
};
|
2012-02-25 21:36:00 +00:00
|
|
|
}
|