diff --git a/doc/nix-prefetch.1.asciidoc b/doc/nix-prefetch.1.asciidoc index 44b3e17..0b6500a 100644 --- a/doc/nix-prefetch.1.asciidoc +++ b/doc/nix-prefetch.1.asciidoc @@ -18,6 +18,7 @@ nix-prefetch - Prefetch any fetcher function call, e.g. package sources [*--input* ] [*--output* ] [*--print-urls*] [*--print-path*] [*--compute-hash*] [*--check-store*] [*-s* | *--silent*] [*-q* | *--quiet*] [*-v* | *--verbose*] [*-vv* | *--debug*] ... ([*-f* | *--file*] | [*-A* | *--attr*] | [*-E* | *--expr*] | ) [] + [ *--experimental-features* | *--extra-experimental-features* ] [*--help* | *--autocomplete* | *--eval* ] [*--*] [*--* ((*-f* | *--file*) | (*-A* | *--attr*) | (*-E* | *--expr*) | )] ... *nix-prefetch* [(*-f* | *--file*) ] [*--deep*] [*-s* | *--silent*] [*-v* | *--verbose*] [*-vv* | *--debug*] ... (*-l* | *--list*) @@ -133,6 +134,12 @@ and can placed both before and after the parameters. *--deep*:: Rather than only listing the top-level fetchers, deep search Nixpkgs for fetchers (slow). +*--experimental-features*:: + Set the Nix experimental-features setting. + +*--extra-experimental-features*:: + Append to the Nix experimental-features setting. + *-s*, *--silent*:: No output to 'stderr'. diff --git a/src/main.sh b/src/main.sh index 680e4c7..5a898d4 100755 --- a/src/main.sh +++ b/src/main.sh @@ -157,12 +157,39 @@ EOF fi } -# The version of Nix with Flakes support requires the expression to be passed through flags, -# which are not present in previous versions, so to be backwards compatible, we conditionally pass them. -# The `nix-command` feature is not enabled by default, so enable it explicitly just in case. -nix flake --help &>/dev/null && nix_eval_expr_args=( --experimental-features nix-command --impure --expr ) || nix_eval_expr_args=() +declare -A experimental_features_status=() +concat_experimental_features() { + local -a experimental_features_array=() + for feature in "${!experimental_features_status[@]}"; do + (( "${experimental_features_status[$feature]}" )) && experimental_features_array+=( "$feature" ); + done + echo "${experimental_features_array[@]}" +} + +declare -i support_flakes +nix flake --help &>/dev/null && support_flakes=1 || support_flakes=0 nix_eval_args=() +# Use --extra-experimental-features by default +force_experimental_features=0 nix_eval() { + # The version of Nix with Flakes support requires the expression to be passed through flags, + # which are not present in previous versions, so to be backwards compatible, we conditionally pass them. + # The `nix-command` feature is not enabled by default, so enable it explicitly just in case. + local -a nix_eval_expr_args=() + if (( support_flakes )); then + for feature in "nix-command"; do + if (( force_experimental_features )); then + (( "${experimental_features_status[$feature]}" )) || die "nix-prefetch expects experimental feature $feature" + else + experimental_features_status[$feature]=1 + fi + done + (( force_experimental_features )) \ + && nix_eval_expr_args+=( --experimental-features ) \ + || nix_eval_expr_args+=( --extra-experimental-features ) + nix_eval_expr_args+=( "$(concat_experimental_features())" ) + nix_eval_expr_args+=( --impure --expr ) + fi local output_type=$1; shift local nix=$1; shift nix eval "$output_type" "${nix_eval_expr_args[@]}" "( @@ -262,6 +289,7 @@ handle_common() { export NIX_PREFETCH=1 } + # Each command should be handled differently and to prevent issues like determinig their priorities, # we do not allow them to be mixed, so e.g. calling adding --version while also having other arguments, # will just result in the help message being shown with an error code. @@ -356,6 +384,20 @@ while (( $# >= 1 )); do (( $# >= 2 )) || die_option_name_value nix_eval_args+=( --option "$1" "$2" ); shift; shift ;; + --extra-experimental-features) + (( support_flakes )) || die "The Nix executable $(nix --version) doesn't support specifying experimental features" + force_experimental_features=0 + while read -r -d " " feature; do + experimental_features_status[$feature]=1 + done <<< "$1"; shift + ;; + --experimental-features) + (( support_flakes )) || die "The Nix executable $(nix --version) doesn't support specifying experimental features" + force_experimental_features=1 + while read -r -d " " feature; do + experimental_features_status[$feature]=1 + done <<< "$1"; shift + ;; -s|--silent) silent=1; quiet=1; verbose=0; debug=0;; -q|--quiet) silent=0; quiet=1; verbose=0; debug=0;; -v|--verbose) silent=0; quiet=0; verbose=1;;