diff --git a/pkgs/shells/hooks/sourceInputCompletionHook/default.nix b/pkgs/shells/hooks/sourceInputCompletionHook/default.nix new file mode 100644 index 0000000000000..c21c752ad9d94 --- /dev/null +++ b/pkgs/shells/hooks/sourceInputCompletionHook/default.nix @@ -0,0 +1,8 @@ +{ makeSetupHook, findutils, substituteAll }: + +# See the header comment in ./source-input-completion-hook.sh for example usage. +makeSetupHook { name = "source-input-completion-hook"; } ( + substituteAll { + src = ./source-input-completion-hook.sh; + inherit findutils; + }) diff --git a/pkgs/shells/hooks/sourceInputCompletionHook/source-input-completion-hook.sh b/pkgs/shells/hooks/sourceInputCompletionHook/source-input-completion-hook.sh new file mode 100644 index 0000000000000..d8672f86594bd --- /dev/null +++ b/pkgs/shells/hooks/sourceInputCompletionHook/source-input-completion-hook.sh @@ -0,0 +1,40 @@ + +# This function is meant to inspect the inputs to a derivation and source the shell completion. +# This is intended to be only used with nix-shell to provide a better user experience, and is not +# intended to be used within nixpkgs as shell completion is only useful in an interactive shell. +sourceInputCompletion() { + local completionPath= + + case $(basename $SHELL) in + bash) + completionPath=share/bash-completion/completions/ + ;; + fish) + completionPath=share/fish/vendor_completions.d/ + ;; + zsh) + completionPath=share/zsh/site-functions/ + ;; + *) + echo "$(basename $SHELL) is not supported for shell completion. Skipping." + exit 0 + esac + + declare -A inputsSourced + for input in $nativeBuildInputs $buildInputs $propagatedNativeBuildInputs $propagatedBuildInputs; do + local completionDir=$input/$completionPath + if [ -d $completionDir ]; then + for script in $(@findutils@/bin/find $completionDir -type f); do + . $script + # grab derivation pname. E.g. /nix/store/...-kubectl-1.19.4 -> kubectl + inputsSourced[$(echo $input | sed -e 's/[^-]*-//' | sed -e 's/-.*$//')]=1 + done + fi + done + + echo "Completions added for: ${!inputsSourced[@]}" +} + +if [ -z "$dontUseSourceInputCompletion" ]; then + sourceInputCompletion +fi diff --git a/pkgs/top-level/all-packages.nix b/pkgs/top-level/all-packages.nix index 6ae76b9242d39..333029ea62f22 100644 --- a/pkgs/top-level/all-packages.nix +++ b/pkgs/top-level/all-packages.nix @@ -161,6 +161,8 @@ in deps = [ innoextract file-rename ]; } ../build-support/setup-hooks/gog-unpack.sh; + sourceInputCompletionHook = callPackage ../shells/hooks/sourceInputCompletionHook { }; + buildEnv = callPackage ../build-support/buildenv { }; # not actually a package # TODO: eventually migrate everything to buildFHSUserEnvBubblewrap