Custom Bash Completions using Home-Manager

I want to create a custom bash completion for my user using home-manager.

While I know I can create file in my home-dir and source it via the bashrc, this feel klunky. Is there a more indiomatic way to go about this? Maybe adding it to ~/.nix-profile/share/bash-completion/completion ?

Check home.file documentation

Usage example form @Misterio

Good search for home-manager

Thanks - I was alreaady using home.file but just had the target dir wrong.

For any poor future soul trying to do the same, here is how I worked it out.

Reading further into the /etc/bashrc section:

# Check whether we're running a version of Bash that has support for
# programmable completion. If we do, enable all modules installed in
# the system and user profile in obsolete /etc/bash_completion.d/
# directories. Bash loads completions in all
# $XDG_DATA_DIRS/bash-completion/completions/
# on demand, so they do not need to be sourced here.
if shopt -q progcomp &>/dev/null; then
  . "/nix/store/fnbf9g79mngi1sxdncizsvdr6xa8dmqc-bash-completion-2.11/etc/profile.d/bash_completion.sh"
  nullglobStatus=$(shopt -p nullglob)
  shopt -s nullglob
  for p in $NIX_PROFILES; do
    for m in "$p/etc/bash_completion.d/"*; do
      . "$m"
    done
  done
  eval "$nullglobStatus"
  unset nullglobStatus p m
fi

It pointed me at /nix/store/fnbf9g79mngi1sxdncizsvdr6xa8dmqc-bash-completion-2.11/etc/profile.d/bash_completion.sh which contained:

# shellcheck shell=sh disable=SC1091,SC2039,SC2166
# Check for interactive bash and that we haven't already been sourced.
if [ "x${BASH_VERSION-}" != x -a "x${PS1-}" != x -a "x${BASH_COMPLETION_VERSINFO-}" = x ]; then

    # Check for recent enough version of bash.
    if [ "${BASH_VERSINFO[0]}" -gt 4 ] ||
        [ "${BASH_VERSINFO[0]}" -eq 4 -a "${BASH_VERSINFO[1]}" -ge 2 ]; then
        [ -r "${XDG_CONFIG_HOME:-$HOME/.config}/bash_completion" ] &&
            . "${XDG_CONFIG_HOME:-$HOME/.config}/bash_completion"
        if shopt -q progcomp && [ -r /nix/store/fnbf9g79mngi1sxdncizsvdr6xa8dmqc-bash-completion-2.11/share/bash-completion/bash_completion ]; then
            # Source completion code.
            . /nix/store/fnbf9g79mngi1sxdncizsvdr6xa8dmqc-bash-completion-2.11/share/bash-completion/bash_completion
        fi
    fi

fi

so it looked like "${XDG_CONFIG_HOME:-$HOME/.config}/bash_completion" was the correct place to throw things.

Adding this to my home-manager config did the trick:
home.file.".config/bash_completion/customcompletion.bash".source = config.lib.file.mkOutOfStoreSymlink "${config.home.homeDirectory}/nixcfg/dotfiles/${config.home.username}/customcompletion.bash";

1 Like