From 8e5ac55fb1dea52932f38bbc4502133a4dcc2a09 Mon Sep 17 00:00:00 2001 From: phaer Date: Tue, 1 Apr 2025 11:52:18 +0200 Subject: [PATCH] nixos-rebuild: don't eval closure before validating variant Because we want to be able to list variants even if one of them might not eval correctly. The eager evaluation was caused by us querying for the resulting image file name to early and is fixed by calling nix-instantiate/nix eval twice now, once for the variants, once for the image file name. fixes #394626 --- .../linux/nixos-rebuild/nixos-rebuild.sh | 34 +++++++++++++++---- 1 file changed, 28 insertions(+), 6 deletions(-) diff --git a/pkgs/os-specific/linux/nixos-rebuild/nixos-rebuild.sh b/pkgs/os-specific/linux/nixos-rebuild/nixos-rebuild.sh index c4766a7e685d..6c61b3c7a503 100755 --- a/pkgs/os-specific/linux/nixos-rebuild/nixos-rebuild.sh +++ b/pkgs/os-specific/linux/nixos-rebuild/nixos-rebuild.sh @@ -835,28 +835,50 @@ if [ -z "$rollback" ]; then "let value = import \"$(realpath $buildFile)\"; set = if builtins.isFunction value then value {} else value; - in builtins.mapAttrs (n: v: v.passthru.filePath) set.${attr:+$attr.}config.system.build.images" \ + in builtins.attrNames set.${attr:+$attr.}config.system.build.images" \ "${extraBuildFlags[@]}" )" elif [[ -z $flake ]]; then variants="$( runCmd nix-instantiate --eval --strict --json --expr \ - "with import {}; builtins.mapAttrs (n: v: v.passthru.filePath) config.system.build.images" \ + "with import {}; builtins.attrNames config.system.build.images" \ "${extraBuildFlags[@]}" )" else variants="$( runCmd nix "${flakeFlags[@]}" eval --json \ "$flake#$flakeAttr.config.system.build.images" \ - --apply "builtins.mapAttrs (n: v: v.passthru.filePath)" "${evalArgs[@]}" "${extraBuildFlags[@]}" + --apply "builtins.attrNames" "${evalArgs[@]}" "${extraBuildFlags[@]}" )" fi - if ! echo "$variants" | jq -e --arg variant "$imageVariant" "keys | any(. == \$variant)" > /dev/null; then + if ! echo "$variants" | jq -e --arg variant "$imageVariant" "any(. == \$variant)" > /dev/null; then echo -e "Please specify one of the following supported image variants via --image-variant:\n" >&2 - echo "$variants" | jq -r '. | keys | join ("\n")' + echo "$variants" | jq -r 'join ("\n")' exit 1 fi - imageName="$(echo "$variants" | jq -r --arg variant "$imageVariant" ".[\$variant]")" + + if [[ -z $buildingAttribute ]]; then + imageName="$( + runCmd nix-instantiate --eval --strict --json --expr \ + "let + value = import \"$(realpath $buildFile)\"; + set = if builtins.isFunction value then value {} else value; + in set.${attr:+$attr.}config.system.build.images.$imageVariant.v.passthru.filePath" \ + "${extraBuildFlags[@]}" + )" + elif [[ -z $flake ]]; then + imageName="$( + runCmd nix-instantiate --eval --strict --json --expr \ + "with import {}; config.system.build.images.$imageVariant.passthru.filePath" \ + "${extraBuildFlags[@]}" + )" + else + imageName="$( + runCmd nix "${flakeFlags[@]}" eval --json \ + "$flake#$flakeAttr.config.system.build.images.$imageVariant.passthru.filePath" \ + "${evalArgs[@]}" "${extraBuildFlags[@]}" + )" + fi if [[ -z $buildingAttribute ]]; then pathToConfig="$(nixBuild $buildFile -A "${attr:+$attr.}config.system.build.images.${imageVariant}" "${extraBuildFlags[@]}")"