How to package a script with bash completion?


I am new to nixOS and I have just successfully added an executable bash script using home-manager with the following:

  home.packages = [
    (pkgs.writeShellApplication {
      name = "HelloWorld";
      text = builtins.readFile ./;

I want to add another script I have that uses git completion like so:

if [ -f "/usr/share/bash-completion/completions/git" ]; then
	source /usr/share/bash-completion/completions/git
	__git_complete git_delete_branch _git_branch
	echo "Error loading git completions"

However because bash-completion lives in my nix store this logic doesn’t work on nix.

What is the nix way to successfully add bash-git-completion?
My current understanding is that I can a) resolve the bash git completion path in my script, or to b) add a copy of bash git completion as a dependency file and package it with my script with mkDerivation.

I am unsure how to do both, and would really appreciate any advice.


I haven’t tried this so I’m just shooting from the hip.

If you have a GH account you may want to look at what others are doing in this GH code search, since they seem to be in the ballpark.

If you want to keep the generic standalone script, and assuming this is isn’t a custom completion script, you can probably package your script and do something like:

prePatch = ''
  substituteInPlace <your-script> --replace-fail '/usr/share/bash-completion/completions/git' '${git}/share/bash-completion/completions/git'
1 Like

Thank you, that’s really helpful.
Today I learnt the power of GH code search!

1 Like