Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

src/main.sh: Add --experimental-features and --extra-experimental-features support #34

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 0.4.2

- The main script now defaults to pass to Nix `--extra-experimental-features` instead of `--experimental-features` to pay respect to the environment configuration.
- Add `--experimental-features` and `--extra-experimental-features` flags to allow temporary configuration overriding. One use case is to enable `builtins.getFlake` when not globally enabled.

## 0.4.1

- More compatible empty check in regex.
Expand Down
3 changes: 2 additions & 1 deletion contrib/nix-prefetch-completion.bash
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ _nix_prefetch() {
# Indenting with spaces is required to still make " $prev_word " work.
local params='
-f --file -A --attr -E --expr -i --index -F --fetcher --arg --argstr -I --option
-t --type --hash-algo -h --hash --input --output --eval '
-t --type --hash-algo -h --hash --input --output --eval
--experimental-features --extra-experimental-features '
local flags=' -s --silent -q --quiet -v --verbose -vv --debug -l --list --version ' flag
for flag in --fetchurl --force-https --print-urls --print-path --compute-hash --check-store --autocomplete --help --deep; do
flags+=" --no-${flag#--} $flag "
Expand Down
1 change: 1 addition & 0 deletions contrib/nix-prefetch-completion.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ _nix_prefetch() {
local params=(
'-f' '--file' '-A' '--attr' '-E' '--expr' '-i' '--index' '-F' '--fetcher' '--arg' '--argstr' '-I' '--option'
'-t' '--type' '--hash-algo' '-h' '--hash' '--input' '--output' '--eval'
'--experimental-features' '--extra-experimental-features'
)
local flags=( -s --silent -q --quiet -v --verbose -vv --debug -l --list --version ) flag
for flag in --fetchurl --force-https --print-urls --print-path --compute-hash --check-store --autocomplete --help --deep; do
Expand Down
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
51 changes: 47 additions & 4 deletions src/main.sh
Original file line number Diff line number Diff line change
Expand Up @@ -157,12 +157,40 @@ 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
(( debug )) && echo "Experimnetal Nix features" "${experimental_features_array[@]}" "are enabled" >&2 || true
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 Nix 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 +290,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 +385,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
[[ -n "$feature" ]] && experimental_features_status[$feature]=1 || true
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
[[ -n "$feature" ]] && experimental_features_status[$feature]=1 || true
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