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

sourceInputCompletionHook: init #104225

Closed
Closed
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
8 changes: 8 additions & 0 deletions pkgs/shells/hooks/sourceInputCompletionHook/default.nix
Original file line number Diff line number Diff line change
@@ -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;
})
Original file line number Diff line number Diff line change
@@ -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
Comment on lines +20 to +21
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
exit 0
esac
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bash completions should be loaded lazily. This can be achieved with XDG_DATA_DIRS via #103501 or if that's too radical, a shell hook.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yea, I'm thinking of closing this in favor of #103501 . Made this PR before I was aware of it's existence

# 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
2 changes: 2 additions & 0 deletions pkgs/top-level/all-packages.nix
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,8 @@ in
deps = [ innoextract file-rename ]; }
../build-support/setup-hooks/gog-unpack.sh;

sourceInputCompletionHook = callPackage ../shells/hooks/sourceInputCompletionHook { };
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

explicitly state ShellHook?


buildEnv = callPackage ../build-support/buildenv { }; # not actually a package

# TODO: eventually migrate everything to buildFHSUserEnvBubblewrap
Expand Down