2014-04-14 14:26:48 +00:00
|
|
|
{
|
|
|
|
config,
|
|
|
|
lib,
|
|
|
|
pkgs,
|
|
|
|
...
|
|
|
|
}:
|
2014-08-04 09:29:51 +00:00
|
|
|
let
|
|
|
|
|
|
|
|
cfg = config.virtualbox;
|
|
|
|
in
|
|
|
|
{
|
2024-09-10 17:14:43 +00:00
|
|
|
imports = [
|
|
|
|
./disk-size-option.nix
|
|
|
|
(lib.mkRenamedOptionModuleWith {
|
|
|
|
sinceRelease = 2411;
|
|
|
|
from = [
|
|
|
|
"virtualbox"
|
|
|
|
"baseImageSize"
|
|
|
|
];
|
|
|
|
to = [
|
|
|
|
"virtualisation"
|
|
|
|
"diskSize"
|
|
|
|
];
|
|
|
|
})
|
|
|
|
];
|
2014-08-04 09:29:51 +00:00
|
|
|
|
|
|
|
options = {
|
|
|
|
virtualbox = {
|
2024-08-28 19:19:13 +00:00
|
|
|
baseImageFreeSpace = lib.mkOption {
|
2024-09-10 17:14:43 +00:00
|
|
|
type = lib.types.int;
|
2021-07-24 07:33:10 +00:00
|
|
|
default = 30 * 1024;
|
2022-07-20 10:32:04 +00:00
|
|
|
description = ''
|
2021-07-24 07:33:10 +00:00
|
|
|
Free space in the VirtualBox base image in MiB.
|
|
|
|
'';
|
|
|
|
};
|
2024-08-28 19:19:13 +00:00
|
|
|
memorySize = lib.mkOption {
|
|
|
|
type = lib.types.int;
|
2018-07-11 17:45:10 +00:00
|
|
|
default = 1536;
|
2022-07-20 10:32:04 +00:00
|
|
|
description = ''
|
2018-07-11 17:45:10 +00:00
|
|
|
The amount of RAM the VirtualBox appliance can use in MiB.
|
|
|
|
'';
|
|
|
|
};
|
2024-08-28 19:19:13 +00:00
|
|
|
vmDerivationName = lib.mkOption {
|
|
|
|
type = lib.types.str;
|
2018-08-20 19:11:29 +00:00
|
|
|
default = "nixos-ova-${config.system.nixos.label}-${pkgs.stdenv.hostPlatform.system}";
|
2022-07-20 10:32:04 +00:00
|
|
|
description = ''
|
2018-07-11 17:45:10 +00:00
|
|
|
The name of the derivation for the VirtualBox appliance.
|
|
|
|
'';
|
|
|
|
};
|
2024-08-28 19:19:13 +00:00
|
|
|
vmName = lib.mkOption {
|
|
|
|
type = lib.types.str;
|
2022-12-17 23:00:58 +00:00
|
|
|
default = "${config.system.nixos.distroName} ${config.system.nixos.label} (${pkgs.stdenv.hostPlatform.system})";
|
2022-07-20 10:32:04 +00:00
|
|
|
description = ''
|
2018-07-11 17:45:10 +00:00
|
|
|
The name of the VirtualBox appliance.
|
|
|
|
'';
|
|
|
|
};
|
2024-08-28 19:19:13 +00:00
|
|
|
vmFileName = lib.mkOption {
|
|
|
|
type = lib.types.str;
|
2018-08-20 19:11:29 +00:00
|
|
|
default = "nixos-${config.system.nixos.label}-${pkgs.stdenv.hostPlatform.system}.ova";
|
2022-07-20 10:32:04 +00:00
|
|
|
description = ''
|
2018-07-11 17:45:10 +00:00
|
|
|
The file name of the VirtualBox appliance.
|
|
|
|
'';
|
|
|
|
};
|
2024-08-28 19:19:13 +00:00
|
|
|
params = lib.mkOption {
|
|
|
|
type =
|
|
|
|
with lib.types;
|
|
|
|
attrsOf (oneOf [
|
|
|
|
str
|
|
|
|
int
|
|
|
|
bool
|
|
|
|
(listOf str)
|
|
|
|
]);
|
2020-02-21 00:02:47 +00:00
|
|
|
example = {
|
|
|
|
audio = "alsa";
|
|
|
|
rtcuseutc = "on";
|
|
|
|
usb = "off";
|
|
|
|
};
|
2022-07-20 10:32:04 +00:00
|
|
|
description = ''
|
2020-02-21 00:02:47 +00:00
|
|
|
Parameters passed to the Virtualbox appliance.
|
|
|
|
|
2022-07-20 10:32:04 +00:00
|
|
|
Run `VBoxManage modifyvm --help` to see more options.
|
2020-02-21 00:02:47 +00:00
|
|
|
'';
|
2021-06-02 11:07:50 +00:00
|
|
|
};
|
2024-08-28 19:19:13 +00:00
|
|
|
exportParams = lib.mkOption {
|
|
|
|
type =
|
|
|
|
with lib.types;
|
|
|
|
listOf (oneOf [
|
|
|
|
str
|
|
|
|
int
|
|
|
|
bool
|
|
|
|
(listOf str)
|
|
|
|
]);
|
2021-06-02 11:07:50 +00:00
|
|
|
example = [
|
|
|
|
"--vsys"
|
|
|
|
"0"
|
|
|
|
"--vendor"
|
|
|
|
"ACME Inc."
|
|
|
|
];
|
|
|
|
default = [ ];
|
2022-07-20 10:32:04 +00:00
|
|
|
description = ''
|
2021-06-02 11:07:50 +00:00
|
|
|
Parameters passed to the Virtualbox export command.
|
|
|
|
|
2022-07-20 10:32:04 +00:00
|
|
|
Run `VBoxManage export --help` to see more options.
|
2021-06-02 11:07:50 +00:00
|
|
|
'';
|
|
|
|
};
|
2024-08-28 19:19:13 +00:00
|
|
|
extraDisk = lib.mkOption {
|
2022-07-20 10:32:04 +00:00
|
|
|
description = ''
|
2019-04-23 05:11:39 +00:00
|
|
|
Optional extra disk/hdd configuration.
|
2023-03-04 09:45:38 +00:00
|
|
|
The disk will be an 'ext4' partition on a separate file.
|
2019-04-23 05:11:39 +00:00
|
|
|
'';
|
|
|
|
default = null;
|
|
|
|
example = {
|
|
|
|
label = "storage";
|
|
|
|
mountPoint = "/home/demo/storage";
|
|
|
|
size = 100 * 1024;
|
|
|
|
};
|
2024-08-28 19:19:13 +00:00
|
|
|
type = lib.types.nullOr (
|
|
|
|
lib.types.submodule {
|
2019-04-23 05:11:39 +00:00
|
|
|
options = {
|
2024-08-28 19:19:13 +00:00
|
|
|
size = lib.mkOption {
|
|
|
|
type = lib.types.int;
|
2022-07-20 10:32:04 +00:00
|
|
|
description = "Size in MiB";
|
2024-10-17 16:27:37 +00:00
|
|
|
};
|
2024-08-28 19:19:13 +00:00
|
|
|
label = lib.mkOption {
|
|
|
|
type = lib.types.str;
|
2019-04-23 05:11:39 +00:00
|
|
|
default = "vm-extra-storage";
|
2022-07-20 10:32:04 +00:00
|
|
|
description = "Label for the disk partition";
|
2024-10-17 16:27:37 +00:00
|
|
|
};
|
2024-08-28 19:19:13 +00:00
|
|
|
mountPoint = lib.mkOption {
|
|
|
|
type = lib.types.str;
|
2022-07-20 10:32:04 +00:00
|
|
|
description = "Path where to mount this disk.";
|
2024-10-17 16:27:37 +00:00
|
|
|
};
|
2019-04-23 05:11:39 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
);
|
|
|
|
};
|
2024-08-28 19:19:13 +00:00
|
|
|
postExportCommands = lib.mkOption {
|
|
|
|
type = lib.types.lines;
|
2022-10-13 16:01:12 +00:00
|
|
|
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 = ''
|
|
|
|
Extra commands to run after exporting the OVA to `$fn`.
|
|
|
|
'';
|
|
|
|
};
|
2024-08-28 19:19:13 +00:00
|
|
|
storageController = lib.mkOption {
|
|
|
|
type =
|
|
|
|
with lib.types;
|
|
|
|
attrsOf (oneOf [
|
|
|
|
str
|
|
|
|
int
|
|
|
|
bool
|
|
|
|
(listOf str)
|
|
|
|
]);
|
2022-10-13 17:01:08 +00:00
|
|
|
example = {
|
|
|
|
name = "SCSI";
|
|
|
|
add = "scsi";
|
|
|
|
portcount = 16;
|
|
|
|
bootable = "on";
|
|
|
|
hostiocache = "on";
|
|
|
|
};
|
|
|
|
default = {
|
|
|
|
name = "SATA";
|
|
|
|
add = "sata";
|
|
|
|
portcount = 4;
|
|
|
|
bootable = "on";
|
|
|
|
hostiocache = "on";
|
|
|
|
};
|
|
|
|
description = ''
|
|
|
|
Parameters passed to the VirtualBox appliance. Must have at least
|
|
|
|
`name`.
|
|
|
|
|
|
|
|
Run `VBoxManage storagectl --help` to see more options.
|
|
|
|
'';
|
|
|
|
};
|
2014-08-04 09:29:51 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
config = {
|
2024-09-10 17:14:43 +00:00
|
|
|
# Use a priority just below mkOptionDefault (1500) instead of lib.mkDefault
|
|
|
|
# to avoid breaking existing configs using that.
|
|
|
|
virtualisation.diskSize = lib.mkOverride 1490 (50 * 1024);
|
2020-02-21 00:02:47 +00:00
|
|
|
|
2024-08-28 19:19:13 +00:00
|
|
|
virtualbox.params = lib.mkMerge [
|
|
|
|
(lib.mapAttrs (name: lib.mkDefault) {
|
2020-02-21 00:02:47 +00:00
|
|
|
acpi = "on";
|
|
|
|
vram = 32;
|
|
|
|
nictype1 = "virtio";
|
|
|
|
nic1 = "nat";
|
|
|
|
audiocontroller = "ac97";
|
|
|
|
audio = "alsa";
|
|
|
|
audioout = "on";
|
2020-06-17 16:42:36 +00:00
|
|
|
graphicscontroller = "vmsvga";
|
2020-02-21 00:02:47 +00:00
|
|
|
rtcuseutc = "on";
|
|
|
|
usb = "on";
|
|
|
|
usbehci = "on";
|
|
|
|
mouse = "usbtablet";
|
|
|
|
})
|
2024-08-28 19:19:13 +00:00
|
|
|
(lib.mkIf (pkgs.stdenv.hostPlatform.system == "i686-linux") { pae = "on"; })
|
2020-02-21 00:02:47 +00:00
|
|
|
];
|
|
|
|
|
2016-03-15 12:34:41 +00:00
|
|
|
system.build.virtualBoxOVA = import ../../lib/make-disk-image.nix {
|
2018-07-11 17:45:10 +00:00
|
|
|
name = cfg.vmDerivationName;
|
2016-03-15 12:34:41 +00:00
|
|
|
|
2015-09-28 12:31:19 +00:00
|
|
|
inherit pkgs lib config;
|
2017-12-07 00:00:21 +00:00
|
|
|
partitionTableType = "legacy";
|
2024-09-10 17:14:43 +00:00
|
|
|
inherit (config.virtualisation) diskSize;
|
2021-08-01 18:59:08 +00:00
|
|
|
additionalSpace = "${toString cfg.baseImageFreeSpace}M";
|
2015-09-28 12:31:19 +00:00
|
|
|
|
|
|
|
postVM = ''
|
2016-03-15 12:34:41 +00:00
|
|
|
export HOME=$PWD
|
2016-09-13 05:24:58 +00:00
|
|
|
export PATH=${pkgs.virtualbox}/bin:$PATH
|
2024-10-17 16:27:37 +00:00
|
|
|
|
2023-03-04 09:45:38 +00:00
|
|
|
echo "converting image to VirtualBox format..."
|
|
|
|
VBoxManage convertfromraw $diskImage disk.vdi
|
2024-10-17 16:27:37 +00:00
|
|
|
|
2024-08-28 19:19:13 +00:00
|
|
|
${lib.optionalString (cfg.extraDisk != null) ''
|
2019-04-23 05:11:39 +00:00
|
|
|
echo "creating extra disk: data-disk.raw"
|
|
|
|
dataDiskImage=data-disk.raw
|
|
|
|
truncate -s ${toString cfg.extraDisk.size}M $dataDiskImage
|
2024-10-17 16:27:37 +00:00
|
|
|
|
2019-04-23 05:11:39 +00:00
|
|
|
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 09:45:38 +00:00
|
|
|
echo "creating extra disk: data-disk.vdi"
|
|
|
|
VBoxManage convertfromraw $dataDiskImage data-disk.vdi
|
2019-04-23 05:11:39 +00:00
|
|
|
''}
|
2024-10-17 16:27:37 +00:00
|
|
|
|
2016-08-27 02:01:51 +00:00
|
|
|
echo "creating VirtualBox VM..."
|
2018-07-11 17:45:10 +00:00
|
|
|
vmName="${cfg.vmName}";
|
2016-03-15 12:34:41 +00:00
|
|
|
VBoxManage createvm --name "$vmName" --register \
|
2018-08-20 19:11:29 +00: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}
|
2022-10-13 17:01:08 +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 09:45:38 +00:00
|
|
|
--medium disk.vdi
|
2024-08-28 19:19:13 +00:00
|
|
|
${lib.optionalString (cfg.extraDisk != null) ''
|
2022-10-13 17:01:08 +00:00
|
|
|
VBoxManage storageattach "$vmName" --storagectl ${cfg.storageController.name} --port 1 --device 0 --type hdd \
|
2023-03-04 09:45:38 +00:00
|
|
|
--medium data-disk.vdi
|
2019-04-23 05:11:39 +00:00
|
|
|
''}
|
2024-10-17 16:27:37 +00:00
|
|
|
|
2016-03-15 12:34:41 +00:00
|
|
|
echo "exporting VirtualBox VM..."
|
|
|
|
mkdir -p $out
|
2018-07-11 17:45:10 +00:00
|
|
|
fn="$out/${cfg.vmFileName}"
|
2024-08-28 19:19:13 +00:00
|
|
|
VBoxManage export "$vmName" --output "$fn" --options manifest ${lib.escapeShellArgs cfg.exportParams}
|
2022-10-13 16:01:12 +00:00
|
|
|
${cfg.postExportCommands}
|
2024-10-17 16:27:37 +00:00
|
|
|
|
2016-08-28 10:31:55 +00:00
|
|
|
rm -v $diskImage
|
2024-10-17 16:27:37 +00:00
|
|
|
|
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 12:31:19 +00:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2019-04-23 05:11:39 +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";
|
|
|
|
};
|
|
|
|
});
|
2015-09-28 12:31:19 +00:00
|
|
|
|
2018-01-06 13:52:51 +00:00
|
|
|
boot.growPartition = true;
|
2014-08-04 09:29:51 +00:00
|
|
|
boot.loader.grub.device = "/dev/sda";
|
2015-09-28 12:31:19 +00:00
|
|
|
|
2019-04-21 03:03:22 +00:00
|
|
|
swapDevices = [
|
|
|
|
{
|
2019-03-13 01:24:44 +00:00
|
|
|
device = "/var/swap";
|
|
|
|
size = 2048;
|
2019-04-21 03:03:22 +00:00
|
|
|
}
|
|
|
|
];
|
2019-03-13 01:24:44 +00:00
|
|
|
|
2015-09-02 02:54:31 +00:00
|
|
|
virtualisation.virtualbox.guest.enable = true;
|
2015-09-28 12:31:19 +00:00
|
|
|
|
2014-08-04 09:29:51 +00:00
|
|
|
};
|
2012-02-25 21:36:00 +00:00
|
|
|
}
|