2020-04-24 23:36:52 +00:00
|
|
|
{ config, lib, pkgs, ... }:
|
|
|
|
|
|
|
|
with lib;
|
|
|
|
|
|
|
|
let
|
|
|
|
|
|
|
|
cfg = config.virtualbox;
|
|
|
|
|
|
|
|
in {
|
|
|
|
|
|
|
|
options = {
|
|
|
|
virtualbox = {
|
|
|
|
baseImageSize = mkOption {
|
2021-04-26 19:14:03 +00:00
|
|
|
type = with types; either (enum [ "auto" ]) int;
|
|
|
|
default = "auto";
|
|
|
|
example = 50 * 1024;
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc ''
|
2020-04-24 23:36:52 +00:00
|
|
|
The size of the VirtualBox base image in MiB.
|
|
|
|
'';
|
|
|
|
};
|
2021-08-05 21:33:18 +00:00
|
|
|
baseImageFreeSpace = mkOption {
|
|
|
|
type = with types; int;
|
|
|
|
default = 30 * 1024;
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc ''
|
2021-08-05 21:33:18 +00:00
|
|
|
Free space in the VirtualBox base image in MiB.
|
|
|
|
'';
|
|
|
|
};
|
2020-04-24 23:36:52 +00:00
|
|
|
memorySize = mkOption {
|
|
|
|
type = types.int;
|
|
|
|
default = 1536;
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc ''
|
2020-04-24 23:36:52 +00:00
|
|
|
The amount of RAM the VirtualBox appliance can use in MiB.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
vmDerivationName = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
default = "nixos-ova-${config.system.nixos.label}-${pkgs.stdenv.hostPlatform.system}";
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc ''
|
2020-04-24 23:36:52 +00:00
|
|
|
The name of the derivation for the VirtualBox appliance.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
vmName = mkOption {
|
|
|
|
type = types.str;
|
2023-01-20 10:41:00 +00:00
|
|
|
default = "${config.system.nixos.distroName} ${config.system.nixos.label} (${pkgs.stdenv.hostPlatform.system})";
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc ''
|
2020-04-24 23:36:52 +00:00
|
|
|
The name of the VirtualBox appliance.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
vmFileName = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
default = "nixos-${config.system.nixos.label}-${pkgs.stdenv.hostPlatform.system}.ova";
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc ''
|
2020-04-24 23:36:52 +00:00
|
|
|
The file name of the VirtualBox appliance.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
params = mkOption {
|
|
|
|
type = with types; attrsOf (oneOf [ str int bool (listOf str) ]);
|
|
|
|
example = {
|
|
|
|
audio = "alsa";
|
|
|
|
rtcuseutc = "on";
|
|
|
|
usb = "off";
|
|
|
|
};
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc ''
|
2020-04-24 23:36:52 +00:00
|
|
|
Parameters passed to the Virtualbox appliance.
|
|
|
|
|
2022-08-12 12:06:08 +00:00
|
|
|
Run `VBoxManage modifyvm --help` to see more options.
|
2020-04-24 23:36:52 +00:00
|
|
|
'';
|
2021-06-28 23:13:55 +00:00
|
|
|
};
|
|
|
|
exportParams = mkOption {
|
|
|
|
type = with types; listOf (oneOf [ str int bool (listOf str) ]);
|
|
|
|
example = [
|
|
|
|
"--vsys" "0" "--vendor" "ACME Inc."
|
|
|
|
];
|
|
|
|
default = [];
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc ''
|
2021-06-28 23:13:55 +00:00
|
|
|
Parameters passed to the Virtualbox export command.
|
|
|
|
|
2022-08-12 12:06:08 +00:00
|
|
|
Run `VBoxManage export --help` to see more options.
|
2021-06-28 23:13:55 +00:00
|
|
|
'';
|
|
|
|
};
|
2020-08-20 17:08:02 +00:00
|
|
|
extraDisk = mkOption {
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc ''
|
2020-08-20 17:08:02 +00:00
|
|
|
Optional extra disk/hdd configuration.
|
2023-03-04 12:14:45 +00:00
|
|
|
The disk will be an 'ext4' partition on a separate file.
|
2020-08-20 17:08:02 +00:00
|
|
|
'';
|
|
|
|
default = null;
|
|
|
|
example = {
|
|
|
|
label = "storage";
|
|
|
|
mountPoint = "/home/demo/storage";
|
|
|
|
size = 100 * 1024;
|
|
|
|
};
|
|
|
|
type = types.nullOr (types.submodule {
|
|
|
|
options = {
|
|
|
|
size = mkOption {
|
|
|
|
type = types.int;
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc "Size in MiB";
|
2020-08-20 17:08:02 +00:00
|
|
|
};
|
|
|
|
label = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
default = "vm-extra-storage";
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc "Label for the disk partition";
|
2020-08-20 17:08:02 +00:00
|
|
|
};
|
|
|
|
mountPoint = mkOption {
|
|
|
|
type = types.str;
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc "Path where to mount this disk.";
|
2020-08-20 17:08:02 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
});
|
|
|
|
};
|
2023-02-09 11:40:11 +00:00
|
|
|
postExportCommands = mkOption {
|
|
|
|
type = types.lines;
|
|
|
|
default = "";
|
|
|
|
example = ''
|
|
|
|
${pkgs.cot}/bin/cot edit-hardware "$fn" \
|
|
|
|
-v vmx-14 \
|
|
|
|
--nics 2 \
|
|
|
|
--nic-types VMXNET3 \
|
|
|
|
--nic-names 'Nic name' \
|
|
|
|
--nic-networks 'Nic match' \
|
|
|
|
--network-descriptions 'Nic description' \
|
|
|
|
--scsi-subtypes VirtualSCSI
|
|
|
|
'';
|
|
|
|
description = lib.mdDoc ''
|
|
|
|
Extra commands to run after exporting the OVA to `$fn`.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
storageController = mkOption {
|
|
|
|
type = with types; attrsOf (oneOf [ str int bool (listOf str) ]);
|
|
|
|
example = {
|
|
|
|
name = "SCSI";
|
|
|
|
add = "scsi";
|
|
|
|
portcount = 16;
|
|
|
|
bootable = "on";
|
|
|
|
hostiocache = "on";
|
|
|
|
};
|
|
|
|
default = {
|
|
|
|
name = "SATA";
|
|
|
|
add = "sata";
|
|
|
|
portcount = 4;
|
|
|
|
bootable = "on";
|
|
|
|
hostiocache = "on";
|
|
|
|
};
|
|
|
|
description = lib.mdDoc ''
|
|
|
|
Parameters passed to the VirtualBox appliance. Must have at least
|
|
|
|
`name`.
|
|
|
|
|
|
|
|
Run `VBoxManage storagectl --help` to see more options.
|
|
|
|
'';
|
|
|
|
};
|
2020-04-24 23:36:52 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
config = {
|
|
|
|
|
|
|
|
virtualbox.params = mkMerge [
|
|
|
|
(mapAttrs (name: mkDefault) {
|
|
|
|
acpi = "on";
|
|
|
|
vram = 32;
|
|
|
|
nictype1 = "virtio";
|
|
|
|
nic1 = "nat";
|
|
|
|
audiocontroller = "ac97";
|
|
|
|
audio = "alsa";
|
|
|
|
audioout = "on";
|
2020-07-18 16:06:22 +00:00
|
|
|
graphicscontroller = "vmsvga";
|
2020-04-24 23:36:52 +00:00
|
|
|
rtcuseutc = "on";
|
|
|
|
usb = "on";
|
|
|
|
usbehci = "on";
|
|
|
|
mouse = "usbtablet";
|
|
|
|
})
|
|
|
|
(mkIf (pkgs.stdenv.hostPlatform.system == "i686-linux") { pae = "on"; })
|
|
|
|
];
|
|
|
|
|
|
|
|
system.build.virtualBoxOVA = import ../../lib/make-disk-image.nix {
|
|
|
|
name = cfg.vmDerivationName;
|
|
|
|
|
|
|
|
inherit pkgs lib config;
|
|
|
|
partitionTableType = "legacy";
|
|
|
|
diskSize = cfg.baseImageSize;
|
2021-08-05 21:33:18 +00:00
|
|
|
additionalSpace = "${toString cfg.baseImageFreeSpace}M";
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
postVM =
|
|
|
|
''
|
|
|
|
export HOME=$PWD
|
|
|
|
export PATH=${pkgs.virtualbox}/bin:$PATH
|
|
|
|
|
2023-03-04 12:14:45 +00:00
|
|
|
echo "converting image to VirtualBox format..."
|
|
|
|
VBoxManage convertfromraw $diskImage disk.vdi
|
2020-04-24 23:36:52 +00:00
|
|
|
|
2020-08-20 17:08:02 +00: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
|
2023-03-04 12:14:45 +00:00
|
|
|
echo "creating extra disk: data-disk.vdi"
|
|
|
|
VBoxManage convertfromraw $dataDiskImage data-disk.vdi
|
2020-08-20 17:08:02 +00:00
|
|
|
''}
|
|
|
|
|
2020-04-24 23:36:52 +00:00
|
|
|
echo "creating VirtualBox VM..."
|
|
|
|
vmName="${cfg.vmName}";
|
|
|
|
VBoxManage createvm --name "$vmName" --register \
|
|
|
|
--ostype ${if pkgs.stdenv.hostPlatform.system == "x86_64-linux" then "Linux26_64" else "Linux26"}
|
|
|
|
VBoxManage modifyvm "$vmName" \
|
|
|
|
--memory ${toString cfg.memorySize} \
|
|
|
|
${lib.cli.toGNUCommandLineShell { } cfg.params}
|
2023-02-09 11:40:11 +00:00
|
|
|
VBoxManage storagectl "$vmName" ${lib.cli.toGNUCommandLineShell { } cfg.storageController}
|
|
|
|
VBoxManage storageattach "$vmName" --storagectl ${cfg.storageController.name} --port 0 --device 0 --type hdd \
|
2023-03-04 12:14:45 +00:00
|
|
|
--medium disk.vdi
|
2020-08-20 17:08:02 +00:00
|
|
|
${optionalString (cfg.extraDisk != null) ''
|
2023-02-09 11:40:11 +00:00
|
|
|
VBoxManage storageattach "$vmName" --storagectl ${cfg.storageController.name} --port 1 --device 0 --type hdd \
|
2023-03-04 12:14:45 +00:00
|
|
|
--medium data-disk.vdi
|
2020-08-20 17:08:02 +00:00
|
|
|
''}
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
echo "exporting VirtualBox VM..."
|
|
|
|
mkdir -p $out
|
|
|
|
fn="$out/${cfg.vmFileName}"
|
2021-06-28 23:13:55 +00:00
|
|
|
VBoxManage export "$vmName" --output "$fn" --options manifest ${escapeShellArgs cfg.exportParams}
|
2023-02-09 11:40:11 +00:00
|
|
|
${cfg.postExportCommands}
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
rm -v $diskImage
|
|
|
|
|
|
|
|
mkdir -p $out/nix-support
|
|
|
|
echo "file ova $fn" >> $out/nix-support/hydra-build-products
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2020-08-20 17:08:02 +00: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";
|
|
|
|
};
|
|
|
|
});
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
boot.growPartition = true;
|
|
|
|
boot.loader.grub.device = "/dev/sda";
|
|
|
|
|
|
|
|
swapDevices = [{
|
|
|
|
device = "/var/swap";
|
|
|
|
size = 2048;
|
|
|
|
}];
|
|
|
|
|
|
|
|
virtualisation.virtualbox.guest.enable = true;
|
|
|
|
|
|
|
|
};
|
|
|
|
}
|