From 48ec455ae83f6255b312d71918b3434bcd4609b2 Mon Sep 17 00:00:00 2001 From: phaer Date: Fri, 22 Nov 2024 12:48:31 +0100 Subject: [PATCH] virtualisation/disk-image: init New module to support qemu, qemu-efi, raw, raw-efi outputs as known from nixos-generators in system.build.images. --- nixos/modules/virtualisation/disk-image.nix | 61 +++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 nixos/modules/virtualisation/disk-image.nix diff --git a/nixos/modules/virtualisation/disk-image.nix b/nixos/modules/virtualisation/disk-image.nix new file mode 100644 index 000000000000..c524b6627113 --- /dev/null +++ b/nixos/modules/virtualisation/disk-image.nix @@ -0,0 +1,61 @@ +{ + config, + lib, + pkgs, + ... +}: +let + cfg = config.image; +in +{ + imports = [ + ./disk-size-option.nix + ../image/file-options.nix + ]; + + options.image = { + format = lib.mkOption { + description = "Format of the disk image to generate: raw or qcow2"; + type = lib.types.enum [ + "raw" + "qcow2" + ]; + default = "qcow2"; + }; + efiSupport = lib.mkOption { + description = "Whether the disk image should support EFI boot or legacy boot"; + type = lib.types.bool; + default = true; + }; + }; + + config = { + boot.loader.grub = lib.mkIf (!cfg.efiSupport) { + enable = lib.mkOptionDefault true; + devices = lib.mkDefault [ "/dev/vda" ]; + }; + boot.loader.systemd-boot.enable = lib.mkDefault cfg.efiSupport; + boot.growPartition = lib.mkDefault true; + + fileSystems = { + "/" = { + device = "/dev/disk/by-label/nixos"; + autoResize = true; + fsType = "ext4"; + }; + "/boot" = lib.mkIf (cfg.efiSupport) { + device = "/dev/disk/by-label/ESP"; + fsType = "vfat"; + }; + }; + + system.nixos.tags = [ cfg.format ] ++ lib.optionals cfg.efiSupport [ "efi" ]; + image.extension = cfg.format; + system.build.image = import ../../lib/make-disk-image.nix { + inherit lib config pkgs; + inherit (config.virtualisation) diskSize; + inherit (cfg) baseName format; + partitionTableType = if cfg.efiSupport then "efi" else "legacy"; + }; + }; +}