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

Install git’s bash completion so that it is loaded on demand #68366

Merged
merged 1 commit into from Sep 18, 2019
Merged

Install git’s bash completion so that it is loaded on demand #68366

merged 1 commit into from Sep 18, 2019

Conversation

sorbits
Copy link
Contributor

@sorbits sorbits commented Sep 9, 2019

Motivation for this change

Putting the bash completion file in $out/share/bash-completion/completions means that it will be loaded on demand by bash-completion and is also consistent with most other packages.

With the old location, the user would either have to explicitly source the file during bash startup, or set BASH_COMPLETION_COMPAT_DIR before sourcing bash_completion.sh, which will eagerly load everything in that directory.

Things done
  • Tested using sandboxing (nix.useSandbox on NixOS, or option sandbox in nix.conf on non-NixOS)
  • Built on platform(s)
    • NixOS
    • macOS
    • other Linux distributions
  • Tested via one or more NixOS test(s) if existing and applicable for the change (look inside nixos/tests)
  • Tested compilation of all pkgs that depend on this change using nix-shell -p nix-review --run "nix-review wip"
  • Tested execution of all binary files (usually in ./result/bin/)
  • Determined the impact on package closure size (by running nix path-info -S before and after)
  • Ensured that relevant documentation is up to date
  • Fits CONTRIBUTING.md.
Notify maintainers

cc @peti @the-kenny @wmertens @globin

Putting the file in $out/share/bash-completion/completions means that it will be loaded on demand by nixpkgs.bash-completion.

With the old location, the user would either have to explicitly source the file during bash startup, or set BASH_COMPLETION_COMPAT_DIR before sourcing bash_completion.sh, which will eagerly load everything in that directory.
@teto
Copy link
Member

teto commented Sep 10, 2019

we have a new way to install these completion files #68329 that you could try here.

@sorbits
Copy link
Contributor Author

sorbits commented Sep 10, 2019

we have a new way to install these completion files #68329 that you could try here.

I wanted it to be simple to follow the change (for the reviewer) and to stay with the existing style of the postInstall phase. Bringing in a shell helper function for one of several files installed would look a little out of place.

But I can revise the commit if desired. In that case, I would also suggest using install -D instead of mkdir followed by ln and to not install git-prompt.sh into bash_completion.d. I believe the latter is done to automatically source it, but that cannot be relied upon, nor should it really be automatically sourced, and the file is already copied to share/git.

@lheckemann lheckemann added this to the 20.03 milestone Sep 10, 2019
@matthewbauer matthewbauer changed the base branch from master to staging September 17, 2019 21:44
mkdir -p $out/etc/bash_completion.d
ln -s $out/share/git/contrib/completion/git-completion.bash $out/etc/bash_completion.d/
ln -s $out/share/git/contrib/completion/git-prompt.sh $out/etc/bash_completion.d/
Copy link
Member

Choose a reason for hiding this comment

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

Shouldn't git-prompt.sh also be linked?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The git-prompt.sh file contains support functions for setting up a custom prompt. It has nothing to do with bash completion (despite its location).

The old bash completion system eagerly loaded all files in etc/bash_completion.d.

I imagine someone took advantage of this eager loading and installed git-prompt.sh into etc/bash_completion.d to have the functions automatically available without requiring the user to manually having to source the file.

With the new location (share/bash_completion/completions) files are loaded on demand, that is, one would have to type «command» and press tab, before «command».sh gets loaded from share/bash_completion/completions.

Therefore it doesn’t make sense to move git-prompt.sh to the new location.

It should probably never have been installed in the old location, and with bash completion v1.90 or later, it is not automatically sourced anymore, so if anything should be done, it would probably be to not touch the file at all, but have users source it from share/git/contrib.

@matthewbauer matthewbauer merged commit 0c1cef2 into NixOS:staging Sep 18, 2019
@RumataEstor
Copy link
Contributor

In my case (by default?) __load_completion does not include the profile-based directory ~/.nix-profile/share/bash-completion/completions where git completion is put. So I had to add BASH_COMPLETION_USER_DIR=~/.nix-profile/share/bash-completion to my ~/.profile.

@sorbits
Copy link
Contributor Author

sorbits commented Nov 6, 2019

In my case (by default?) __load_completion does not include the profile-based directory ~/.nix-profile/share/bash-completion/completions where git completion is put

Try add this to your shell startup (prior to sourcing bash_completion.sh):

if [[ -d "$HOME/.nix-profile/share" ]]; then
    XDG_DATA_DIRS="$HOME/.nix-profile/share${XDG_DATA_DIRS:+:$XDG_DATA_DIRS}"
fi

@RumataEstor
Copy link
Contributor

@sorbits Yes, that works and I thought to use that variable but it was not defined in my environment and that's probably the default state. As __load_completion uses for dir in ${XDG_DATA_DIRS:-/usr/local/share:/usr/share}; if I set it to only my value, I'll lose completions loaded from the base OS (I'm on MacOS) if there are any.

There might be some other use cases for that variable I'm not aware of, so I think it would be better to either populate XDG_DATA_DIRS fully in ~/.nix-profile/etc/profile.d/nix.sh, or to not touch it at all and to use some other variable only for completions.

@sorbits
Copy link
Contributor Author

sorbits commented Nov 7, 2019

if I set it to only my value, I'll lose completions loaded from the base OS (I'm on MacOS) if there are any.

You can include the default values like this:

if [[ -d "$HOME/.nix-profile/share" ]]; then
    XDG_DATA_DIRS="$HOME/.nix-profile/share:${XDG_DATA_DIRS:-/usr/local/share:/usr/share}"
fi

These defaults are from the XDG Base Directory Specification so they will not change in bash_completion.sh (i.e. it is safe to copy/paste them to your profile).

However, I do not think it makes sense to source completions from those locations when you use nixpkgs, only directories controlled by the nix package manager should be referenced by your system to ensure everything is in a known state / reproductible.

There might be some other use cases for that variable I'm not aware of, so I think it would be better to either populate XDG_DATA_DIRS fully in ~/.nix-profile/etc/profile.d/nix.sh, or to not touch it at all and to use some other variable only for completions.

I think it would be ideal if it was setup in ~/.nix-profile/etc/profile.d/nix.sh so that completions would work out-of-the-box.

But lack of default setup just means that we must do it manually in our custom shell startup scripts, not that we should avoid touching the variable.

I strongly encourage you to look into this and possibly submit a pull request to change this or at least get a conversation started somewhere where those more knowledgeable about this can weigh in on the pros and cons.

If you google for XDG_DATA_DIRS and NixOS, this is not the only issue caused by lack of useful default value, e.g. the Nix Cookbook also mentions that users must setup XDG_DATA_DIRS for the Desktop Environment to find .desktop files.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants