How to source

Git package includes a script, which provides some functions for shell prompt customisation. The file is not included into PATH but is present on the system:

$ fd --glob * /nix

What would be the proper portable way to source the file?

Two approaches seem to be common from searching GitHub: either configuring bashrc with Nix and referring to the file as ${pkgs.git}/share/git/contrib/completion/ or simply bundling it with the configuration. I would prefer keeping the same bashrc across NixOS and non-Nix systems for now.

This should already happen by default, assuming you don’t set programs.bash.enableCompletion to false, see this comment:

Normally nixos should add the git package’s share directory to XDG_DATA_DIRS, at least, by adding it to /run/current-system/sw/share. Maybe you override this?

1 Like

programs.bash.enableCompletion is set on my system. XDG_DATA_DIRS does not include git’s share folder but it does include /run/current-system/sw/share, which has the git’s completions linked correctly:

$ ls /run/current-system/sw/share/bash-completion/completions/

However the comment explicitly says that the completions from XDG_DATA_DIRS are not sourced as this is done by bash itself. Here’s what I found in the bash-completion docs:

The completion files of the name <cmd> or <cmd>.bash, where <cmd> is
the name of the target command, are searched in the above completion
directories in order. The file that is found first is used. When no
completion file is found in any completion directories in this process, the
completion files of the name _<cmd> is next searched in the completion
directories in order.

Which explains why /run/current-system/sw/share/bash-completion/completions/git is sourced (whenever a completion is requested for a git command) but the freestanding at the same directory is not.

I think I’ll just conditionally source /run/current-system/sw/share/bash-completion/completions/ in my bashrc.

Thanks for the hints!

Interesting! Given the file is deliberately added to the completions directory, I don’t think this is intentional. It should probably be considered a bug, and an option added for it. Mind raising an issue upstream so we pick the maintainer’s brain about it too?

1 Like

Done, thanks for the suggestion and for helping to debug this.

1 Like

awesome! this is now an option git: add prompt.enable by ProminentRetail · Pull Request #265532 · NixOS/nixpkgs · GitHub

works great for bash but doesn’t source when I use zsh…hmmmm