bash: support bash completion

This commit is contained in:
Robert Helgesson 2022-08-07 13:10:13 +02:00
parent d9e03b7f8c
commit f5e9879e74
No known key found for this signature in database
GPG key ID: 36BDAA14C2797E89
6 changed files with 60 additions and 1 deletions

View file

@ -30,6 +30,27 @@ in
programs.bash = {
enable = mkEnableOption "GNU Bourne-Again SHell";
enableCompletion = mkOption {
type = types.bool;
default = true;
description = ''
Whether to enable Bash completion for all interactive Bash shells.
</para><para>
Note, if you use NixOS or nix-darwin and do not have Bash completion
enabled in the system configuration, then make sure to add
<programlisting language="nix">
environment.pathsToLink = [ "/share/bash-completion" ];
</programlisting>
to your system configuration to get completion for system packages.
Note, the legacy <filename>/etc/bash_completion.d</filename> path is
not supported by Home Manager.
'';
};
historySize = mkOption {
type = types.int;
default = 10000;
@ -193,6 +214,15 @@ in
[[ -f ~/.bashrc ]] && . ~/.bashrc
'';
# If completion is enabled then make sure it is sourced very early. This
# is to avoid problems if any other initialization code attempts to set up
# completion.
programs.bash.initExtra = mkIf cfg.enableCompletion (mkOrder 100 ''
if [[ ! -v BASH_COMPLETION_VERSINFO ]]; then
. "${pkgs.bash-completion}/etc/profile.d/bash_completion.sh"
fi
'');
home.file.".profile".source = writeBashScript "profile" ''
. "${config.home.profileDirectory}/etc/profile.d/hm-session-vars.sh"

View file

@ -3,7 +3,10 @@
{
programs = {
atuin.enable = true;
bash.enable = true;
bash = {
enable = true;
enableCompletion = false;
};
};
test.stubs = {

View file

@ -0,0 +1,23 @@
{ config, lib, pkgs, ... }:
with lib;
{
programs.bash.enable = true;
test.stubs.bash-completion = { };
nmt.script = ''
assertFileExists home-files/.bashrc
assertFileContains \
home-files/.bashrc \
'if [[ ! -v BASH_COMPLETION_VERSINFO ]]; then'
assertFileContains \
home-files/.bashrc \
' . "@bash-completion@/etc/profile.d/bash_completion.sh"'
assertFileContains \
home-files/.bashrc \
'fi'
'';
}

View file

@ -1,4 +1,5 @@
{
bash-completion = ./completion.nix;
bash-logout = ./logout.nix;
bash-session-variables = ./session-variables.nix;
}

View file

@ -6,6 +6,7 @@ with lib;
config = {
programs.bash = {
enable = true;
enableCompletion = false;
logoutExtra = ''
clear-console

View file

@ -6,6 +6,7 @@ with lib;
config = {
programs.bash = {
enable = true;
enableCompletion = false;
sessionVariables = {
V1 = "v1";