[Nix-dev] Why Bash and Nix-Shell have no completion?

Marc Weber marco-oweber at gmx.de
Mon Jan 26 07:36:55 CET 2015

It got implemented somewhen in the past, some people told about
bash completion being annoying in some cases (-> [1]) .. which means the perfect
solutions means conditional opt-in/out eventually.

bash.nix has:

      interactiveShellInit = ''

I personally have patches putting a

  source /etc/bash/setup-all

in ~/.bashrc as default which means:
  1) user can edit .bashrc (because its not a symlink)
  2) user can opt-out easily
  3) user can look at setup-all which allows him/her to opt-in/out from
    individual features or completion scripts, such as individual
    completion scripts..
  -> [1]

those patches change quite a lot - and I'm unsure whether its worth
merging them into master.

I want to say: Builders should be as clean as possible eventually.
Thus opt-in makes sense - look at ~/.bashrc and source that script
to enable default behaviour ?

Marc Weber

[1] This is the sample code of my /etc/bash/nix-bash-lib which gets
sourced by /etc/bash/setup-all, it opts-out from gnu utils which was
reported to be annoying long time ago - so it might be outdated.

You could use such in your .bashrc

  NIX_COMPL_SCRIPT_SOURCED[the-individual-compl-script] = anything 
  source /etc/bash/setup-all

to opt-out.


  # potential problems (-rev 20179)
  #  - It doesn't support filenames with spaces.
  #  - It inserts a space after the filename when tab-completing in an
  #    "svn" command.
  #  - Many people find it annoying that tab-completion on commands like
  #    "tar" only matches filenames with the "right" extension.
  #  - Lluís reported bash apparently crashing on some tab completions.
  # comment: Does this apply to complete.gnu-longopt or also to bash_completion?

  if shopt -q progcomp &>/dev/null; then
    # bash supports completion:
      local profile="$1"

      # origin: bash_completion, slightly adopted
      # source script only once - allow user to use NIX_COMPL_SCRIPT_SOURCED to
      # opt out from bad scripts. If a user wants to reload all he can clear

      local nullglobStatus=$(shopt -p nullglob)
      shopt -s nullglob
      for s in "$profile"/etc/bash_completion.d/* "$p/share/bash-completion/completions/"*; do
        local base="${s/*\//}"
          -z "${NIX_COMPL_SCRIPT_SOURCED[$base]}" &&
        ]] && { . "$s"; NIX_COMPL_SCRIPT_SOURCED[$base]=1; }

      eval "$nullglobStatus"
    nix_add_profile_completion(){ :; }

More information about the nix-dev mailing list