modules/virtualisation: add unified diskSize opt

See https://github.com/NixOS/nixpkgs/pull/339535 and https://github.com/NixOS/nixpkgs/pull/341058
This commit is contained in:
phaer 2024-09-10 19:14:43 +02:00
parent 6071ee8e31
commit c6da9ef32d
14 changed files with 231 additions and 117 deletions

View File

@ -15,11 +15,23 @@ let
inherit (lib.options) literalExpression; inherit (lib.options) literalExpression;
cfg = config.amazonImage; cfg = config.amazonImage;
amiBootMode = if config.ec2.efi then "uefi" else "legacy-bios"; amiBootMode = if config.ec2.efi then "uefi" else "legacy-bios";
in in
{ {
imports = [
imports = [ ../../../modules/virtualisation/amazon-image.nix ]; ../../../modules/virtualisation/amazon-image.nix
../../../modules/virtualisation/disk-size-option.nix
(lib.mkRenamedOptionModuleWith {
sinceRelease = 2411;
from = [
"amazonImage"
"sizeMB"
];
to = [
"virtualisation"
"diskSize"
];
})
];
# Amazon recommends setting this to the highest possible value for a good EBS # Amazon recommends setting this to the highest possible value for a good EBS
# experience, which prior to 4.15 was 255. # experience, which prior to 4.15 was 255.
@ -52,13 +64,6 @@ in
''; '';
}; };
sizeMB = mkOption {
type = with types; either (enum [ "auto" ]) int;
default = 3072;
example = 8192;
description = "The size in MB of the image";
};
format = mkOption { format = mkOption {
type = types.enum [ type = types.enum [
"raw" "raw"
@ -70,6 +75,11 @@ in
}; };
}; };
# Use a priority just below mkOptionDefault (1500) instead of lib.mkDefault
# to avoid breaking existing configs using that.
config.virtualisation.diskSize = lib.mkOverride 1490 (3 * 1024);
config.virtualisation.diskSizeAutoSupported = !config.ec2.zfs.enable;
config.system.build.amazonImage = config.system.build.amazonImage =
let let
configFile = pkgs.writeText "configuration.nix" '' configFile = pkgs.writeText "configuration.nix" ''
@ -98,7 +108,7 @@ in
bootSize = 1000; # 1G is the minimum EBS volume bootSize = 1000; # 1G is the minimum EBS volume
rootSize = cfg.sizeMB; rootSize = config.virtualisation.diskSize;
rootPoolProperties = { rootPoolProperties = {
ashift = 12; ashift = 12;
autoexpand = "on"; autoexpand = "on";
@ -151,7 +161,7 @@ in
fsType = "ext4"; fsType = "ext4";
partitionTableType = if config.ec2.efi then "efi" else "legacy+gpt"; partitionTableType = if config.ec2.efi then "efi" else "legacy+gpt";
diskSize = cfg.sizeMB; inherit (config.virtualisation) diskSize;
postVM = '' postVM = ''
extension=''${diskImage##*.} extension=''${diskImage##*.}

View File

@ -15,6 +15,18 @@ in
{ {
imports = [ imports = [
../../../modules/virtualisation/openstack-config.nix ../../../modules/virtualisation/openstack-config.nix
../../../modules/virtualisation/disk-size-option.nix
(lib.mkRenamedOptionModuleWith {
sinceRelease = 2411;
from = [
"openstackImage"
"sizeMB"
];
to = [
"virtualisation"
"diskSize"
];
})
] ++ (lib.optional copyChannel ../../../modules/installer/cd-dvd/channel.nix); ] ++ (lib.optional copyChannel ../../../modules/installer/cd-dvd/channel.nix);
options.openstackImage = { options.openstackImage = {
@ -26,16 +38,10 @@ in
ramMB = mkOption { ramMB = mkOption {
type = types.int; type = types.int;
default = 1024; default = (3 * 1024);
description = "RAM allocation for build VM"; description = "RAM allocation for build VM";
}; };
sizeMB = mkOption {
type = types.int;
default = 8192;
description = "The size in MB of the image";
};
format = mkOption { format = mkOption {
type = types.enum [ type = types.enum [
"raw" "raw"
@ -61,6 +67,11 @@ in
}; };
}; };
# Use a priority just below mkOptionDefault (1500) instead of lib.mkDefault
# to avoid breaking existing configs using that.
virtualisation.diskSize = lib.mkOverride 1490 (8 * 1024);
virtualisation.diskSizeAutoSupported = false;
system.build.openstackImage = import ../../../lib/make-single-disk-zfs-image.nix { system.build.openstackImage = import ../../../lib/make-single-disk-zfs-image.nix {
inherit lib config; inherit lib config;
inherit (cfg) contents format name; inherit (cfg) contents format name;
@ -77,7 +88,7 @@ in
bootSize = 1000; bootSize = 1000;
memSize = cfg.ramMB; memSize = cfg.ramMB;
rootSize = cfg.sizeMB; rootSize = config.virtualisation.diskSize;
rootPoolProperties = { rootPoolProperties = {
ashift = 12; ashift = 12;
autoexpand = "on"; autoexpand = "on";

View File

@ -10,18 +10,24 @@ let
cfg = config.virtualisation.azureImage; cfg = config.virtualisation.azureImage;
in in
{ {
imports = [ ./azure-common.nix ]; imports = [
./azure-common.nix
./disk-size-option.nix
(lib.mkRenamedOptionModuleWith {
sinceRelease = 2411;
from = [
"virtualisation"
"azureImage"
"diskSize"
];
to = [
"virtualisation"
"diskSize"
];
})
];
options.virtualisation.azureImage = { options.virtualisation.azureImage = {
diskSize = mkOption {
type = with types; either (enum [ "auto" ]) int;
default = "auto";
example = 2048;
description = ''
Size of disk image. Unit is MB.
'';
};
bootSize = mkOption { bootSize = mkOption {
type = types.int; type = types.int;
default = 256; default = 256;
@ -67,7 +73,8 @@ in
bootSize = "${toString cfg.bootSize}M"; bootSize = "${toString cfg.bootSize}M";
partitionTableType = if cfg.vmGeneration == "v2" then "efi" else "legacy"; partitionTableType = if cfg.vmGeneration == "v2" then "efi" else "legacy";
inherit (cfg) diskSize contents; inherit (cfg) contents;
inherit (config.virtualisation) diskSize;
inherit config lib pkgs; inherit config lib pkgs;
}; };
}; };

View File

@ -11,18 +11,24 @@ let
in in
{ {
imports = [ ./digital-ocean-config.nix ]; imports = [
./digital-ocean-config.nix
./disk-size-option.nix
(lib.mkRenamedOptionModuleWith {
sinceRelease = 2411;
from = [
"virtualisation"
"digitalOceanImage"
"diskSize"
];
to = [
"virtualisation"
"diskSize"
];
})
];
options = { options = {
virtualisation.digitalOceanImage.diskSize = mkOption {
type = with types; either (enum [ "auto" ]) int;
default = "auto";
example = 4096;
description = ''
Size of disk image. Unit is MB.
'';
};
virtualisation.digitalOceanImage.configFile = mkOption { virtualisation.digitalOceanImage.configFile = mkOption {
type = with types; nullOr path; type = with types; nullOr path;
default = null; default = null;
@ -52,7 +58,6 @@ in
#### implementation #### implementation
config = { config = {
system.build.digitalOceanImage = import ../../lib/make-disk-image.nix { system.build.digitalOceanImage = import ../../lib/make-disk-image.nix {
name = "digital-ocean-image"; name = "digital-ocean-image";
format = "qcow2"; format = "qcow2";
@ -73,7 +78,7 @@ in
config.virtualisation.digitalOcean.defaultConfigFile config.virtualisation.digitalOcean.defaultConfigFile
else else
cfg.configFile; cfg.configFile;
inherit (cfg) diskSize; inherit (config.virtualisation) diskSize;
inherit config lib pkgs; inherit config lib pkgs;
}; };

View File

@ -0,0 +1,38 @@
{ lib, config, ... }:
let
t = lib.types;
in
{
options = {
virtualisation.diskSizeAutoSupported = lib.mkOption {
type = t.bool;
default = true;
description = ''
Whether the current image builder or vm runner supports `virtualisation.diskSize = "auto".`
'';
internal = true;
};
virtualisation.diskSize = lib.mkOption {
type = t.either (t.enum [ "auto" ]) t.ints.positive;
default = if config.virtualisation.diskSizeAutoSupported then "auto" else 1024;
defaultText = "\"auto\" if diskSizeAutoSupported, else 1024";
description = ''
The disk size in megabytes of the virtual machine.
'';
};
};
config =
let
inherit (config.virtualisation) diskSize diskSizeAutoSupported;
in
{
assertions = [
{
assertion = diskSize != "auto" || diskSizeAutoSupported;
message = "Setting virtualisation.diskSize to `auto` is not supported by the current image build or vm runner; use an explicit size.";
}
];
};
}

View File

@ -19,18 +19,24 @@ let
in in
{ {
imports = [ ./google-compute-config.nix ]; imports = [
./google-compute-config.nix
./disk-size-option.nix
(lib.mkRenamedOptionModuleWith {
sinceRelease = 2411;
from = [
"virtualisation"
"googleComputeImage"
"diskSize"
];
to = [
"virtualisation"
"diskSize"
];
})
];
options = { options = {
virtualisation.googleComputeImage.diskSize = mkOption {
type = with types; either (enum [ "auto" ]) int;
default = "auto";
example = 1536;
description = ''
Size of disk image. Unit is MB.
'';
};
virtualisation.googleComputeImage.configFile = mkOption { virtualisation.googleComputeImage.configFile = mkOption {
type = with types; nullOr str; type = with types; nullOr str;
default = null; default = null;
@ -86,7 +92,7 @@ in
format = "raw"; format = "raw";
configFile = if cfg.configFile == null then defaultConfigFile else cfg.configFile; configFile = if cfg.configFile == null then defaultConfigFile else cfg.configFile;
partitionTableType = if cfg.efi then "efi" else "legacy"; partitionTableType = if cfg.efi then "efi" else "legacy";
inherit (cfg) diskSize; inherit (config.virtualisation) diskSize;
inherit config lib pkgs; inherit config lib pkgs;
}; };

View File

@ -9,19 +9,26 @@ with lib;
let let
cfg = config.hyperv; cfg = config.hyperv;
in in
{ {
imports = [
./disk-size-option.nix
(lib.mkRenamedOptionModuleWith {
sinceRelease = 2411;
from = [
"hyperv"
"baseImageSize"
];
to = [
"virtualisation"
"diskSize"
];
})
];
options = { options = {
hyperv = { hyperv = {
baseImageSize = mkOption {
type = with types; either (enum [ "auto" ]) int;
default = "auto";
example = 2048;
description = ''
The size of the hyper-v base image in MiB.
'';
};
vmDerivationName = mkOption { vmDerivationName = mkOption {
type = types.str; type = types.str;
default = "nixos-hyperv-${config.system.nixos.label}-${pkgs.stdenv.hostPlatform.system}"; default = "nixos-hyperv-${config.system.nixos.label}-${pkgs.stdenv.hostPlatform.system}";
@ -40,6 +47,10 @@ in
}; };
config = { config = {
# Use a priority just below mkOptionDefault (1500) instead of lib.mkDefault
# to avoid breaking existing configs using that.
virtualisation.diskSize = lib.mkOverride 1490 (4 * 1024);
system.build.hypervImage = import ../../lib/make-disk-image.nix { system.build.hypervImage = import ../../lib/make-disk-image.nix {
name = cfg.vmDerivationName; name = cfg.vmDerivationName;
postVM = '' postVM = ''
@ -47,7 +58,7 @@ in
rm $diskImage rm $diskImage
''; '';
format = "raw"; format = "raw";
diskSize = cfg.baseImageSize; inherit (config.virtualisation) diskSize;
partitionTableType = "efi"; partitionTableType = "efi";
inherit config lib pkgs; inherit config lib pkgs;
}; };

View File

@ -17,17 +17,24 @@ let
''; '';
in in
{ {
imports = [ ./linode-config.nix ]; imports = [
./linode-config.nix
./disk-size-option.nix
(lib.mkRenamedOptionModuleWith {
sinceRelease = 2411;
from = [
"virtualisation"
"linodeImage"
"diskSize"
];
to = [
"virtualisation"
"diskSize"
];
})
];
options = { options = {
virtualisation.linodeImage.diskSize = mkOption {
type = with types; either (enum (singleton "auto")) ints.positive;
default = "auto";
example = 1536;
description = ''
Size of disk image in MB.
'';
};
virtualisation.linodeImage.configFile = mkOption { virtualisation.linodeImage.configFile = mkOption {
type = with types; nullOr str; type = with types; nullOr str;
@ -62,7 +69,7 @@ in
format = "raw"; format = "raw";
partitionTableType = "none"; partitionTableType = "none";
configFile = if cfg.configFile == null then defaultConfigFile else cfg.configFile; configFile = if cfg.configFile == null then defaultConfigFile else cfg.configFile;
inherit (cfg) diskSize; inherit (config.virtualisation) diskSize;
inherit config lib pkgs; inherit config lib pkgs;
}; };
}; };

View File

@ -12,9 +12,14 @@ in
imports = [ ./oci-common.nix ]; imports = [ ./oci-common.nix ];
config = { config = {
# Use a priority just below mkOptionDefault (1500) instead of lib.mkDefault
# to avoid breaking existing configs using that.
virtualisation.diskSize = lib.mkOverride 1490 (8 * 1024);
virtualisation.diskSizeAutoSupported = false;
system.build.OCIImage = import ../../lib/make-disk-image.nix { system.build.OCIImage = import ../../lib/make-disk-image.nix {
inherit config lib pkgs; inherit config lib pkgs;
inherit (cfg) diskSize; inherit (config.virtualisation) diskSize;
name = "oci-image"; name = "oci-image";
configFile = ./oci-config-user.nix; configFile = ./oci-config-user.nix;
format = "qcow2"; format = "qcow2";

View File

@ -1,10 +1,23 @@
{ {
config,
lib, lib,
pkgs,
... ...
}: }:
{ {
imports = [
./disk-size-option.nix
(lib.mkRenamedOptionModuleWith {
sinceRelease = 2411;
from = [
"oci"
"diskSize"
];
to = [
"virtualisation"
"diskSize"
];
})
];
options = { options = {
oci = { oci = {
efi = lib.mkOption { efi = lib.mkOption {
@ -14,12 +27,6 @@
Whether the OCI instance is using EFI. Whether the OCI instance is using EFI.
''; '';
}; };
diskSize = lib.mkOption {
type = lib.types.int;
default = 8192;
description = "Size of the disk image created in MB.";
example = "diskSize = 12 * 1024; # 12GiB";
};
}; };
}; };
} }

View File

@ -6,8 +6,23 @@
}: }:
with lib; with lib;
{ {
imports = [
./disk-size-option.nix
(lib.mkRenamedOptionModuleWith {
sinceRelease = 2411;
from = [
"proxmox"
"qemuConf"
"diskSize"
];
to = [
"virtualisation"
"diskSize"
];
})
];
options.proxmox = { options.proxmox = {
qemuConf = { qemuConf = {
# essential configs # essential configs
@ -95,16 +110,6 @@ with lib;
either "efi" or "hybrid". either "efi" or "hybrid".
''; '';
}; };
diskSize = mkOption {
type = types.str;
default = "auto";
example = "20480";
description = ''
The size of the disk, in megabytes.
if "auto" size is calculated based on the contents copied to it and
additionalSpace is taken into account.
'';
};
net0 = mkOption { net0 = mkOption {
type = types.commas; type = types.commas;
default = "virtio=00:00:00:00:00:00,bridge=vmbr0,firewall=1"; default = "virtio=00:00:00:00:00:00,bridge=vmbr0,firewall=1";
@ -305,7 +310,8 @@ with lib;
mkdir -p $out/nix-support mkdir -p $out/nix-support
echo "file vma $out/vzdump-qemu-${cfg.filenameSuffix}.vma.zst" > $out/nix-support/hydra-build-products echo "file vma $out/vzdump-qemu-${cfg.filenameSuffix}.vma.zst" > $out/nix-support/hydra-build-products
''; '';
inherit (cfg.qemuConf) additionalSpace diskSize bootSize; inherit (cfg.qemuConf) additionalSpace bootSize;
inherit (config.virtualisation) diskSize;
format = "raw"; format = "raw";
inherit config lib pkgs; inherit config lib pkgs;
}; };

View File

@ -317,12 +317,11 @@ let
copyChannel = false; copyChannel = false;
OVMF = cfg.efi.OVMF; OVMF = cfg.efi.OVMF;
}; };
in in
{ {
imports = [ imports = [
../profiles/qemu-guest.nix ../profiles/qemu-guest.nix
./disk-size-option.nix
(mkRenamedOptionModule (mkRenamedOptionModule
[ [
"virtualisation" "virtualisation"
@ -378,14 +377,6 @@ in
''; '';
}; };
virtualisation.diskSize = mkOption {
type = types.ints.positive;
default = 1024;
description = ''
The disk size in megabytes of the virtual machine.
'';
};
virtualisation.diskImage = mkOption { virtualisation.diskImage = mkOption {
type = types.nullOr types.str; type = types.nullOr types.str;
default = "./${config.system.name}.qcow2"; default = "./${config.system.name}.qcow2";
@ -1250,6 +1241,8 @@ in
# override by setting `virtualisation.fileSystems = lib.mkForce { };`. # override by setting `virtualisation.fileSystems = lib.mkForce { };`.
fileSystems = lib.mkIf (cfg.fileSystems != { }) (mkVMOverride cfg.fileSystems); fileSystems = lib.mkIf (cfg.fileSystems != { }) (mkVMOverride cfg.fileSystems);
virtualisation.diskSizeAutoSupported = false;
virtualisation.fileSystems = virtualisation.fileSystems =
let let
mkSharedDir = tag: share: { mkSharedDir = tag: share: {

View File

@ -7,22 +7,27 @@
let let
cfg = config.virtualbox; cfg = config.virtualbox;
in in
{ {
imports = [
./disk-size-option.nix
(lib.mkRenamedOptionModuleWith {
sinceRelease = 2411;
from = [
"virtualbox"
"baseImageSize"
];
to = [
"virtualisation"
"diskSize"
];
})
];
options = { options = {
virtualbox = { virtualbox = {
baseImageSize = lib.mkOption {
type = with lib.types; either (enum [ "auto" ]) int;
default = "auto";
example = 50 * 1024;
description = ''
The size of the VirtualBox base image in MiB.
'';
};
baseImageFreeSpace = lib.mkOption { baseImageFreeSpace = lib.mkOption {
type = with lib.types; int; type = lib.types.int;
default = 30 * 1024; default = 30 * 1024;
description = '' description = ''
Free space in the VirtualBox base image in MiB. Free space in the VirtualBox base image in MiB.
@ -180,6 +185,9 @@ in
}; };
config = { config = {
# 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);
virtualbox.params = lib.mkMerge [ virtualbox.params = lib.mkMerge [
(lib.mapAttrs (name: lib.mkDefault) { (lib.mapAttrs (name: lib.mkDefault) {
@ -204,7 +212,7 @@ in
inherit pkgs lib config; inherit pkgs lib config;
partitionTableType = "legacy"; partitionTableType = "legacy";
diskSize = cfg.baseImageSize; inherit (config.virtualisation) diskSize;
additionalSpace = "${toString cfg.baseImageFreeSpace}M"; additionalSpace = "${toString cfg.baseImageFreeSpace}M";
postVM = '' postVM = ''

View File

@ -312,7 +312,7 @@ in rec {
[ configuration [ configuration
versionModule versionModule
./maintainers/scripts/ec2/amazon-image.nix ./maintainers/scripts/ec2/amazon-image.nix
({ ... }: { amazonImage.sizeMB = "auto"; }) ({ ... }: { amazonImage.virtualisation.diskSize = "auto"; })
]; ];
}).config.system.build.amazonImage) }).config.system.build.amazonImage)