From 91f1f59969bc76082fab733105fa339041aa1d76 Mon Sep 17 00:00:00 2001 From: matthewcroughan Date: Tue, 12 Dec 2023 17:53:56 +0000 Subject: [PATCH] make-disk-image: use nix path-info to automatically set the imageSize --- lib/make-disk-image.nix | 9 ++++++++- lib/types/disk.nix | 10 +++++----- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/lib/make-disk-image.nix b/lib/make-disk-image.nix index 4a926e8c..2e53d544 100644 --- a/lib/make-disk-image.nix +++ b/lib/make-disk-image.nix @@ -24,7 +24,14 @@ let util-linux ]; preVM = '' - ${lib.concatMapStringsSep "\n" (disk: "truncate -s ${disk.imageSize} ${disk.name}.raw") (lib.attrValues nixosConfig.config.disko.devices.disk)} + export PATH=$PATH:${lib.makeBinPath (with pkgs; [ nix jq ])} + export NIX_STATE_DIR=$TMPDIR/state + export NIX_CONFIG="experimental-features = nix-command" + nix-store --load-db < ${pkgs.closureInfo { + rootPaths = [ systemToInstall.config.system.build.toplevel ]; + }}/registration + + ${lib.concatMapStringsSep "\n" (disk: "truncate -s $(($(set -x; nix path-info --json ${systemToInstall.config.system.build.toplevel} | jq .[].closureSize) + ${toString disk.imageSizeExtraBytes})) ${disk.name}.raw") (lib.attrValues nixosConfig.config.disko.devices.disk)} ''; postVM = '' # shellcheck disable=SC2154 diff --git a/lib/types/disk.nix b/lib/types/disk.nix index 60f31b50..aafe88e9 100644 --- a/lib/types/disk.nix +++ b/lib/types/disk.nix @@ -16,13 +16,13 @@ type = diskoLib.optionTypes.absolute-pathname; # TODO check if subpath of /dev ? - No! eg: /.swapfile description = "Device path"; }; - imageSize = lib.mkOption { - type = lib.types.strMatching "[0-9]+[KMGTP]?"; + imageSizeExtraBytes = lib.mkOption { + type = lib.types.int; description = '' - size of the image if the makeDiskImages function from diksoLib is used. - is used as an argument to truncate -s + extra size in bytes to add to the end of the disk image if the makeDiskImages function from diksoLib is used. + This is used as an argument to truncate -s ''; - default = "2G"; + default = 2000000000; }; content = diskoLib.deviceType { parent = config; device = config.device; }; _meta = lib.mkOption {