From f5e9879e74e6202e2dbb3628fad2d20eac0d8be4 Mon Sep 17 00:00:00 2001 From: Robert Helgesson Date: Sun, 7 Aug 2022 13:10:13 +0200 Subject: [PATCH] bash: support bash completion --- modules/programs/bash.nix | 30 +++++++++++++++++++ tests/modules/programs/atuin/bash.nix | 5 +++- tests/modules/programs/bash/completion.nix | 23 ++++++++++++++ tests/modules/programs/bash/default.nix | 1 + tests/modules/programs/bash/logout.nix | 1 + .../programs/bash/session-variables.nix | 1 + 6 files changed, 60 insertions(+), 1 deletion(-) create mode 100644 tests/modules/programs/bash/completion.nix diff --git a/modules/programs/bash.nix b/modules/programs/bash.nix index 53477bf9a..0f9fd922b 100644 --- a/modules/programs/bash.nix +++ b/modules/programs/bash.nix @@ -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. + + + + Note, if you use NixOS or nix-darwin and do not have Bash completion + enabled in the system configuration, then make sure to add + + + environment.pathsToLink = [ "/share/bash-completion" ]; + + + to your system configuration to get completion for system packages. + Note, the legacy /etc/bash_completion.d 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" diff --git a/tests/modules/programs/atuin/bash.nix b/tests/modules/programs/atuin/bash.nix index f8c314099..521886b2e 100644 --- a/tests/modules/programs/atuin/bash.nix +++ b/tests/modules/programs/atuin/bash.nix @@ -3,7 +3,10 @@ { programs = { atuin.enable = true; - bash.enable = true; + bash = { + enable = true; + enableCompletion = false; + }; }; test.stubs = { diff --git a/tests/modules/programs/bash/completion.nix b/tests/modules/programs/bash/completion.nix new file mode 100644 index 000000000..2e5e69dd1 --- /dev/null +++ b/tests/modules/programs/bash/completion.nix @@ -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' + ''; +} diff --git a/tests/modules/programs/bash/default.nix b/tests/modules/programs/bash/default.nix index e9f431cd2..1dbbee777 100644 --- a/tests/modules/programs/bash/default.nix +++ b/tests/modules/programs/bash/default.nix @@ -1,4 +1,5 @@ { + bash-completion = ./completion.nix; bash-logout = ./logout.nix; bash-session-variables = ./session-variables.nix; } diff --git a/tests/modules/programs/bash/logout.nix b/tests/modules/programs/bash/logout.nix index a34292858..c366c520e 100644 --- a/tests/modules/programs/bash/logout.nix +++ b/tests/modules/programs/bash/logout.nix @@ -6,6 +6,7 @@ with lib; config = { programs.bash = { enable = true; + enableCompletion = false; logoutExtra = '' clear-console diff --git a/tests/modules/programs/bash/session-variables.nix b/tests/modules/programs/bash/session-variables.nix index 09c69814d..bbf2b71ab 100644 --- a/tests/modules/programs/bash/session-variables.nix +++ b/tests/modules/programs/bash/session-variables.nix @@ -6,6 +6,7 @@ with lib; config = { programs.bash = { enable = true; + enableCompletion = false; sessionVariables = { V1 = "v1";