make-disk-image: use nix path-info to automatically set the imageSize

This commit is contained in:
matthewcroughan 2023-12-12 17:53:56 +00:00 committed by Jörg Thalheim
parent 23d308f005
commit 8ceff1b731
2 changed files with 18 additions and 6 deletions

View file

@ -30,7 +30,19 @@ let
util-linux
] ++ nixosConfig.config.disko.extraDependencies;
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: ''
closureSize=$(nix path-info --closure-size --json ${systemToInstall.config.system.build.toplevel} | jq .[].closureSize)
size=$((closureSize + ${toString disk.imageSizeExtraBytes}))
echo "Truncating ${disk.name}.raw to $size bytes..."
truncate -s "$size" ${disk.name}.raw
'') (lib.attrValues nixosConfig.config.disko.devices.disk)}
'';
postVM = ''
# shellcheck disable=SC2154

View file

@ -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 diskoLib is used.
This is used as an argument to truncate -s. Defaults to 512MiB.
'';
default = "2G";
default = 536870912;
};
content = diskoLib.deviceType { parent = config; device = config.device; };
_meta = lib.mkOption {