From c6da9ef32d63db850220e67dd52564aca6f470c9 Mon Sep 17 00:00:00 2001 From: phaer Date: Tue, 10 Sep 2024 19:14:43 +0200 Subject: [PATCH] modules/virtualisation: add unified diskSize opt See https://github.com/NixOS/nixpkgs/pull/339535 and https://github.com/NixOS/nixpkgs/pull/341058 --- .../maintainers/scripts/ec2/amazon-image.nix | 34 +++++++++++------ .../scripts/openstack/openstack-image-zfs.nix | 27 +++++++++---- nixos/modules/virtualisation/azure-image.nix | 29 ++++++++------ .../virtualisation/digital-ocean-image.nix | 29 ++++++++------ .../virtualisation/disk-size-option.nix | 38 +++++++++++++++++++ .../virtualisation/google-compute-image.nix | 28 ++++++++------ nixos/modules/virtualisation/hyperv-image.nix | 31 ++++++++++----- nixos/modules/virtualisation/linode-image.nix | 27 ++++++++----- nixos/modules/virtualisation/oci-image.nix | 7 +++- nixos/modules/virtualisation/oci-options.nix | 23 +++++++---- .../modules/virtualisation/proxmox-image.nix | 30 +++++++++------ nixos/modules/virtualisation/qemu-vm.nix | 13 ++----- .../virtualisation/virtualbox-image.nix | 30 +++++++++------ nixos/release.nix | 2 +- 14 files changed, 231 insertions(+), 117 deletions(-) create mode 100644 nixos/modules/virtualisation/disk-size-option.nix diff --git a/nixos/maintainers/scripts/ec2/amazon-image.nix b/nixos/maintainers/scripts/ec2/amazon-image.nix index 9d0e7f5883e0..e08cf572f7f9 100644 --- a/nixos/maintainers/scripts/ec2/amazon-image.nix +++ b/nixos/maintainers/scripts/ec2/amazon-image.nix @@ -15,11 +15,23 @@ let inherit (lib.options) literalExpression; cfg = config.amazonImage; amiBootMode = if config.ec2.efi then "uefi" else "legacy-bios"; - in { - - imports = [ ../../../modules/virtualisation/amazon-image.nix ]; + imports = [ + ../../../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 # 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 { type = types.enum [ "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 = let configFile = pkgs.writeText "configuration.nix" '' @@ -98,7 +108,7 @@ in bootSize = 1000; # 1G is the minimum EBS volume - rootSize = cfg.sizeMB; + rootSize = config.virtualisation.diskSize; rootPoolProperties = { ashift = 12; autoexpand = "on"; @@ -151,7 +161,7 @@ in fsType = "ext4"; partitionTableType = if config.ec2.efi then "efi" else "legacy+gpt"; - diskSize = cfg.sizeMB; + inherit (config.virtualisation) diskSize; postVM = '' extension=''${diskImage##*.} diff --git a/nixos/maintainers/scripts/openstack/openstack-image-zfs.nix b/nixos/maintainers/scripts/openstack/openstack-image-zfs.nix index 72f123b16229..57022bd2f784 100644 --- a/nixos/maintainers/scripts/openstack/openstack-image-zfs.nix +++ b/nixos/maintainers/scripts/openstack/openstack-image-zfs.nix @@ -15,6 +15,18 @@ in { imports = [ ../../../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); options.openstackImage = { @@ -26,16 +38,10 @@ in ramMB = mkOption { type = types.int; - default = 1024; + default = (3 * 1024); description = "RAM allocation for build VM"; }; - sizeMB = mkOption { - type = types.int; - default = 8192; - description = "The size in MB of the image"; - }; - format = mkOption { type = types.enum [ "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 { inherit lib config; inherit (cfg) contents format name; @@ -77,7 +88,7 @@ in bootSize = 1000; memSize = cfg.ramMB; - rootSize = cfg.sizeMB; + rootSize = config.virtualisation.diskSize; rootPoolProperties = { ashift = 12; autoexpand = "on"; diff --git a/nixos/modules/virtualisation/azure-image.nix b/nixos/modules/virtualisation/azure-image.nix index 1f6b2bd52c04..76d8a3bb365b 100644 --- a/nixos/modules/virtualisation/azure-image.nix +++ b/nixos/modules/virtualisation/azure-image.nix @@ -10,18 +10,24 @@ let cfg = config.virtualisation.azureImage; 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 = { - diskSize = mkOption { - type = with types; either (enum [ "auto" ]) int; - default = "auto"; - example = 2048; - description = '' - Size of disk image. Unit is MB. - ''; - }; - bootSize = mkOption { type = types.int; default = 256; @@ -67,7 +73,8 @@ in bootSize = "${toString cfg.bootSize}M"; partitionTableType = if cfg.vmGeneration == "v2" then "efi" else "legacy"; - inherit (cfg) diskSize contents; + inherit (cfg) contents; + inherit (config.virtualisation) diskSize; inherit config lib pkgs; }; }; diff --git a/nixos/modules/virtualisation/digital-ocean-image.nix b/nixos/modules/virtualisation/digital-ocean-image.nix index 2d06c4c38fa1..b6ef01516e34 100644 --- a/nixos/modules/virtualisation/digital-ocean-image.nix +++ b/nixos/modules/virtualisation/digital-ocean-image.nix @@ -11,18 +11,24 @@ let 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 = { - 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 { type = with types; nullOr path; default = null; @@ -52,7 +58,6 @@ in #### implementation config = { - system.build.digitalOceanImage = import ../../lib/make-disk-image.nix { name = "digital-ocean-image"; format = "qcow2"; @@ -73,7 +78,7 @@ in config.virtualisation.digitalOcean.defaultConfigFile else cfg.configFile; - inherit (cfg) diskSize; + inherit (config.virtualisation) diskSize; inherit config lib pkgs; }; diff --git a/nixos/modules/virtualisation/disk-size-option.nix b/nixos/modules/virtualisation/disk-size-option.nix new file mode 100644 index 000000000000..487c3adfe2b7 --- /dev/null +++ b/nixos/modules/virtualisation/disk-size-option.nix @@ -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."; + } + ]; + }; +} diff --git a/nixos/modules/virtualisation/google-compute-image.nix b/nixos/modules/virtualisation/google-compute-image.nix index 416b47b768d9..c2529bb3db3f 100644 --- a/nixos/modules/virtualisation/google-compute-image.nix +++ b/nixos/modules/virtualisation/google-compute-image.nix @@ -19,18 +19,24 @@ let 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 = { - 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 { type = with types; nullOr str; default = null; @@ -86,7 +92,7 @@ in format = "raw"; configFile = if cfg.configFile == null then defaultConfigFile else cfg.configFile; partitionTableType = if cfg.efi then "efi" else "legacy"; - inherit (cfg) diskSize; + inherit (config.virtualisation) diskSize; inherit config lib pkgs; }; diff --git a/nixos/modules/virtualisation/hyperv-image.nix b/nixos/modules/virtualisation/hyperv-image.nix index d4ed256d0d91..ea0603fa6ae5 100644 --- a/nixos/modules/virtualisation/hyperv-image.nix +++ b/nixos/modules/virtualisation/hyperv-image.nix @@ -9,19 +9,26 @@ with lib; let cfg = config.hyperv; - in { + + imports = [ + ./disk-size-option.nix + (lib.mkRenamedOptionModuleWith { + sinceRelease = 2411; + from = [ + "hyperv" + "baseImageSize" + ]; + to = [ + "virtualisation" + "diskSize" + ]; + }) + ]; + options = { 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 { type = types.str; default = "nixos-hyperv-${config.system.nixos.label}-${pkgs.stdenv.hostPlatform.system}"; @@ -40,6 +47,10 @@ in }; 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 { name = cfg.vmDerivationName; postVM = '' @@ -47,7 +58,7 @@ in rm $diskImage ''; format = "raw"; - diskSize = cfg.baseImageSize; + inherit (config.virtualisation) diskSize; partitionTableType = "efi"; inherit config lib pkgs; }; diff --git a/nixos/modules/virtualisation/linode-image.nix b/nixos/modules/virtualisation/linode-image.nix index 48d7f98e6abe..ff61c5f5d1db 100644 --- a/nixos/modules/virtualisation/linode-image.nix +++ b/nixos/modules/virtualisation/linode-image.nix @@ -17,17 +17,24 @@ let ''; 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 = { - 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 { type = with types; nullOr str; @@ -62,7 +69,7 @@ in format = "raw"; partitionTableType = "none"; configFile = if cfg.configFile == null then defaultConfigFile else cfg.configFile; - inherit (cfg) diskSize; + inherit (config.virtualisation) diskSize; inherit config lib pkgs; }; }; diff --git a/nixos/modules/virtualisation/oci-image.nix b/nixos/modules/virtualisation/oci-image.nix index b867e7ae30e7..fe286853de81 100644 --- a/nixos/modules/virtualisation/oci-image.nix +++ b/nixos/modules/virtualisation/oci-image.nix @@ -12,9 +12,14 @@ in imports = [ ./oci-common.nix ]; 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 { inherit config lib pkgs; - inherit (cfg) diskSize; + inherit (config.virtualisation) diskSize; name = "oci-image"; configFile = ./oci-config-user.nix; format = "qcow2"; diff --git a/nixos/modules/virtualisation/oci-options.nix b/nixos/modules/virtualisation/oci-options.nix index 629b651ca5ac..b8d66c0290b3 100644 --- a/nixos/modules/virtualisation/oci-options.nix +++ b/nixos/modules/virtualisation/oci-options.nix @@ -1,10 +1,23 @@ { - config, lib, - pkgs, ... }: { + imports = [ + ./disk-size-option.nix + (lib.mkRenamedOptionModuleWith { + sinceRelease = 2411; + from = [ + "oci" + "diskSize" + ]; + to = [ + "virtualisation" + "diskSize" + ]; + }) + ]; + options = { oci = { efi = lib.mkOption { @@ -14,12 +27,6 @@ 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"; - }; }; }; } diff --git a/nixos/modules/virtualisation/proxmox-image.nix b/nixos/modules/virtualisation/proxmox-image.nix index 4364fbff2a83..9bbe7a596f07 100644 --- a/nixos/modules/virtualisation/proxmox-image.nix +++ b/nixos/modules/virtualisation/proxmox-image.nix @@ -6,8 +6,23 @@ }: with lib; - { + imports = [ + ./disk-size-option.nix + (lib.mkRenamedOptionModuleWith { + sinceRelease = 2411; + from = [ + "proxmox" + "qemuConf" + "diskSize" + ]; + to = [ + "virtualisation" + "diskSize" + ]; + }) + ]; + options.proxmox = { qemuConf = { # essential configs @@ -95,16 +110,6 @@ with lib; 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 { type = types.commas; default = "virtio=00:00:00:00:00:00,bridge=vmbr0,firewall=1"; @@ -305,7 +310,8 @@ with lib; mkdir -p $out/nix-support 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"; inherit config lib pkgs; }; diff --git a/nixos/modules/virtualisation/qemu-vm.nix b/nixos/modules/virtualisation/qemu-vm.nix index fe803c1971ca..af2f13b003a4 100644 --- a/nixos/modules/virtualisation/qemu-vm.nix +++ b/nixos/modules/virtualisation/qemu-vm.nix @@ -317,12 +317,11 @@ let copyChannel = false; OVMF = cfg.efi.OVMF; }; - in - { imports = [ ../profiles/qemu-guest.nix + ./disk-size-option.nix (mkRenamedOptionModule [ "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 { type = types.nullOr types.str; default = "./${config.system.name}.qcow2"; @@ -1250,6 +1241,8 @@ in # override by setting `virtualisation.fileSystems = lib.mkForce { };`. fileSystems = lib.mkIf (cfg.fileSystems != { }) (mkVMOverride cfg.fileSystems); + virtualisation.diskSizeAutoSupported = false; + virtualisation.fileSystems = let mkSharedDir = tag: share: { diff --git a/nixos/modules/virtualisation/virtualbox-image.nix b/nixos/modules/virtualisation/virtualbox-image.nix index 968f5b766e87..22646011d33d 100644 --- a/nixos/modules/virtualisation/virtualbox-image.nix +++ b/nixos/modules/virtualisation/virtualbox-image.nix @@ -7,22 +7,27 @@ let cfg = config.virtualbox; - in { + imports = [ + ./disk-size-option.nix + (lib.mkRenamedOptionModuleWith { + sinceRelease = 2411; + from = [ + "virtualbox" + "baseImageSize" + ]; + to = [ + "virtualisation" + "diskSize" + ]; + }) + ]; options = { 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 { - type = with lib.types; int; + type = lib.types.int; default = 30 * 1024; description = '' Free space in the VirtualBox base image in MiB. @@ -180,6 +185,9 @@ in }; 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 [ (lib.mapAttrs (name: lib.mkDefault) { @@ -204,7 +212,7 @@ in inherit pkgs lib config; partitionTableType = "legacy"; - diskSize = cfg.baseImageSize; + inherit (config.virtualisation) diskSize; additionalSpace = "${toString cfg.baseImageFreeSpace}M"; postVM = '' diff --git a/nixos/release.nix b/nixos/release.nix index 8a8c45f8f842..f77b9983e496 100644 --- a/nixos/release.nix +++ b/nixos/release.nix @@ -312,7 +312,7 @@ in rec { [ configuration versionModule ./maintainers/scripts/ec2/amazon-image.nix - ({ ... }: { amazonImage.sizeMB = "auto"; }) + ({ ... }: { amazonImage.virtualisation.diskSize = "auto"; }) ]; }).config.system.build.amazonImage)