diff --git a/nixos/modules/virtualisation/qemu-vm.nix b/nixos/modules/virtualisation/qemu-vm.nix index d2a17664e9a1..7b9b5086dbb9 100644 --- a/nixos/modules/virtualisation/qemu-vm.nix +++ b/nixos/modules/virtualisation/qemu-vm.nix @@ -325,11 +325,15 @@ let OVMF = cfg.efi.OVMF; }; + virtualisationOptions = import ./virtualisation-options.nix; + in { imports = [ + ./virtualisation-options.nix ../profiles/qemu-guest.nix + virtualisationOptions.diskSize (mkRenamedOptionModule [ "virtualisation" @@ -385,14 +389,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"; @@ -1238,6 +1234,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/virtualisation-options.nix b/nixos/modules/virtualisation/virtualisation-options.nix new file mode 100644 index 000000000000..5040a7916d84 --- /dev/null +++ b/nixos/modules/virtualisation/virtualisation-options.nix @@ -0,0 +1,60 @@ +# This modules declares shared options for virtual machines, +# containers and anything else in `virtualisation`. +# +# This is useful to declare e.g. defaults for +# `virtualisation.diskSize` once, while building multiple +# different image formats of a NixOS configuration. +# +# Additional options can be migrated over time from +# `modules/virtualisation/qemu-vm.nix` and others. +# Please keep defaults and descriptions here generic +# and independent of i.e. hypervisor-specific notes +# and defaults where. +# Those can be added in the consuming modules where needed. +# needed. +let + _file = ./virtualisation-options.nix; + key = _file; +in +{ + diskSize = + { lib, config, ... }: + let + t = lib.types; + in + { + inherit _file key; + + 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 = "auto"; + 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."; + } + ]; + }; + }; +}