From 30c548dbffe5ed489b0603c4d388a26a7aba4969 Mon Sep 17 00:00:00 2001 From: Kleidi Bujari Date: Sat, 6 Jun 2026 20:52:09 -0700 Subject: put depotImage along other generators and use naming conventions --- modules/disk.nix | 20 ++++++++++++----- packages/make-zfs-image.nix | 54 ++++++++++++++++++++++----------------------- 2 files changed, 42 insertions(+), 32 deletions(-) diff --git a/modules/disk.nix b/modules/disk.nix index 79885cd..071c9ea 100644 --- a/modules/disk.nix +++ b/modules/disk.nix @@ -15,7 +15,10 @@ let ; in { - imports = [ (modulesPath + "/installer/scan/not-detected.nix") ]; + imports = [ + (modulesPath + "/installer/scan/not-detected.nix") + (modulesPath + "/image/file-options.nix") + ]; options.depot.disk = { enable = lib.mkEnableOption "Apply depot-standard ZFS disk layout."; @@ -112,10 +115,17 @@ in }; }; - system.build.image = pkgs.depot.make-zfs-image.override { - inherit pkgs lib config; - format = "qcow2"; - }; + image.modules.depot = + { config, modulesPath, ... }: + { + imports = [ (modulesPath + "/profiles/qemu-guest.nix") ]; + + image.extension = "qcow2"; + system.build.image = pkgs.depot.make-zfs-image.override { + inherit pkgs lib config; + inherit (config.image) fileName extension; + }; + }; # datasets = # let diff --git a/packages/make-zfs-image.nix b/packages/make-zfs-image.nix index 90ce63f..5a7500b 100644 --- a/packages/make-zfs-image.nix +++ b/packages/make-zfs-image.nix @@ -18,27 +18,16 @@ # Shell code executed after the VM has finished. postVM ? "", - name ? "nixos-disk-image", + fileName ? "nixos-disk-image", # Disk image format, one of qcow2, qcow2-compressed, vdi, vpc, raw. - format ? "raw", + extension ? "raw", + + # Whether to provision ZFS dataset for /home, since /home is generally omitted + # for server installations. By default, checks whether my user is defined. + makeHome ? builtins.hasAttr "kle" config.users.users, }: let - formatOpt = if format == "qcow2-compressed" then "qcow2" else format; - - compress = lib.optionalString (format == "qcow2-compressed") "-c"; - - filenameSuffix = - "." - + { - qcow2 = "qcow2"; - vdi = "vdi"; - vpc = "vhd"; - raw = "img"; - } - .${formatOpt} or formatOpt; - rootFilename = "nixos.root${filenameSuffix}"; - closureInfo = pkgs.closureInfo { rootPaths = [ config.system.build.toplevel ]; }; @@ -74,7 +63,7 @@ let ); image = vmTools.runInLinuxVM ( - pkgs.runCommand name + pkgs.runCommand fileName { inherit memSize; QEMU_OPTS = "-drive file=$rootDiskImage,if=virtio,cache=unsafe,werror=report"; @@ -88,16 +77,16 @@ let postVM = '' # truncate --size ${toString (bootSize + rootSize)} $rootDiskImage ${ - if formatOpt == "raw" then + if extension == "raw" then '' - mv $rootDiskImage $out/${rootFilename} + mv $rootDiskImage $out/${fileName} '' else '' - ${pkgs.qemu-utils}/bin/qemu-img convert -f raw -O ${formatOpt} ${compress} $rootDiskImage $out/${rootFilename} + ${pkgs.qemu-utils}/bin/qemu-img convert -f raw -O ${extension} $rootDiskImage $out/${fileName} '' } - rootDiskImage=$out/${rootFilename} + rootDiskImage=$out/${fileName} set -x ${postVM} ''; @@ -122,6 +111,8 @@ let zroot /dev/vda2 zfs create zroot/local + ${lib.optionalString makeHome "zfs create zroot/local/home -o mountpoint=/home -u"} + zfs create zroot/local/root -o mountpoint=/root -u zfs create zroot/local/var -o mountpoint=/var -u zfs create zroot/local/nix -o mountpoint=legacy @@ -150,16 +141,25 @@ let zpool export zroot '' ); - script = pkgs.writeShellScriptBin "runvm" '' - cp ${image}/nixos.root.qcow2 nixos.root.qcow2 - ${pkgs.qemu-utils}/bin/qemu-img resize nixos.root.qcow2 +10G + script = pkgs.writeShellScriptBin "run-${config.networking.hostName}-vm" '' + cp -f ${image}/${fileName} . + chmod +rwx ${fileName} + ${pkgs.qemu-utils}/bin/qemu-img resize ${fileName} +10G + ${pkgs.qemu}/bin/qemu-img check ${fileName} ${pkgs.qemu}/bin/qemu-system-x86_64 \ -enable-kvm \ -m 4G \ -cpu host \ -bios ${pkgs.OVMF.fd}/FV/OVMF.fd \ - -drive file=nixos.root.qcow2,format=qcow2,if=virtio \ + -drive file=${fileName},format=qcow2,if=virtio \ -boot order=c ''; in -script +pkgs.symlinkJoin { + name = "${config.networking.hostName}-image-outputs"; + paths = [ + image + script + ]; + meta.mainProgram = script.name; +} -- cgit v1.3.1