Skip to content

Commit

Permalink
src/main.sh: Add --[extra-]experimental-features support
Browse files Browse the repository at this point in the history
*   Default to using --extra-experimental-features
    instead of --experimental-features
    to show respect to the environment configuration.

*   Add flags --extra-experimental-features and --experimental-features
    to allow manual specification to the experimental-features setting
    (e.g. use `builtins.getFlake` when not globally enabled).
  • Loading branch information
ShamrockLee committed Feb 10, 2022
1 parent a4773bf commit 00ed107
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 4 deletions.
7 changes: 7 additions & 0 deletions doc/nix-prefetch.1.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ nix-prefetch - Prefetch any fetcher function call, e.g. package sources
[*--input* <input-type>] [*--output* <output-type>] [*--print-urls*] [*--print-path*]
[*--compute-hash*] [*--check-store*] [*-s* | *--silent*] [*-q* | *--quiet*] [*-v* | *--verbose*] [*-vv* | *--debug*] ...
([*-f* | *--file*] <file> | [*-A* | *--attr*] <attr> | [*-E* | *--expr*] <expr> | <url>) [<hash>]
[ *--experimental-features* | *--extra-experimental-features* ]
[*--help* | *--autocomplete* | *--eval* <expr>]
[*--*] [*--<name>* ((*-f* | *--file*) <file> | (*-A* | *--attr*) <attr> | (*-E* | *--expr*) <expr> | <str>)] ...
*nix-prefetch* [(*-f* | *--file*) <file>] [*--deep*] [*-s* | *--silent*] [*-v* | *--verbose*] [*-vv* | *--debug*] ... (*-l* | *--list*)
Expand Down Expand Up @@ -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'.

Expand Down
50 changes: 46 additions & 4 deletions src/main.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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[@]}" "(
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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;;
Expand Down

0 comments on commit 00ed107

Please sign in to comment.