fork/home-manager
git clone https://git.julia.blue/fork/home-manager
75790441
master
1{ config , lib , pkgs , ...} : 2 3with lib ; 4 5let 6 7cfg = config . programs . bash ; 8 9writeBashScript = name :text : 10pkgs . writeTextFile { 11inherit name text ; 12checkPhase = '' 13${ pkgs . stdenv . shellDryRun } " $ target " 14'' ; 15}; 16 17in { 18meta . maintainers = [ maintainers . rycee ]; 19 20imports = [ 21( mkRenamedOptionModule [ "programs" "bash" "enableAutojump" ] [ 22"programs" 23"autojump" 24"enable" 25]) 26]; 27 28options = { 29programs . bash = { 30enable = mkEnableOption "GNU Bourne-Again SHell" ; 31 32enableCompletion = mkOption { 33type = types . bool ; 34default = true ; 35description = '' 36Whether to enable Bash completion for all interactive Bash shells. 37 38Note, if you use NixOS or nix-darwin and do not have Bash completion 39enabled in the system configuration, then make sure to add 40 41```nix 42environment.pathsToLink = [ "/share/bash-completion" ]; 43``` 44 45to your system configuration to get completion for system packages. 46Note, the legacy {file}`/etc/bash_completion.d` path is 47not supported by Home Manager. 48'' ; 49}; 50 51historySize = mkOption { 52type = types . int ; 53default = 10000 ; 54description = "Number of history lines to keep in memory." ; 55}; 56 57historyFile = mkOption { 58type = types . nullOr types . str ; 59default = null ; 60description = "Location of the bash history file." ; 61}; 62 63historyFileSize = mkOption { 64type = types . int ; 65default = 100000 ; 66description = "Number of history lines to keep on file." ; 67}; 68 69historyControl = mkOption { 70type = 71types . listOf ( types . enum [ "erasedups" "ignoredups" "ignorespace" ]); 72default = [ ]; 73description = "Controlling how commands are saved on the history list." ; 74}; 75 76historyIgnore = mkOption { 77type = types . listOf types . str ; 78default = [ ]; 79example = [ "ls" "cd" "exit" ]; 80description = 81"List of commands that should not be saved to the history list." ; 82}; 83 84shellOptions = mkOption { 85type = types . listOf types . str ; 86default = [ 87# Append to history file rather than replacing it. 88"histappend" 89 90# check the window size after each command and, if 91# necessary, update the values of LINES and COLUMNS. 92"checkwinsize" 93 94# Extended globbing. 95"extglob" 96"globstar" 97 98# Warn if closing shell with running jobs. 99"checkjobs" 100]; 101example = [ "extglob" "-cdspell" ]; 102description = '' 103Shell options to set. Prefix an option with 104"`-`" to unset. 105'' ; 106}; 107 108sessionVariables = mkOption { 109default = { }; 110type = types . attrs ; 111example = { MAILCHECK = 30 ; }; 112description = '' 113Environment variables that will be set for the Bash session. 114'' ; 115}; 116 117shellAliases = mkOption { 118default = { }; 119type = types . attrsOf types . str ; 120example = literalExpression '' 121{ 122ll = "ls -l"; 123".." = "cd .."; 124} 125'' ; 126description = '' 127An attribute set that maps aliases (the top level attribute names in 128this option) to command strings or directly to build outputs. 129'' ; 130}; 131 132profileExtra = mkOption { 133default = "" ; 134type = types . lines ; 135description = '' 136Extra commands that should be run when initializing a login 137shell. 138'' ; 139}; 140 141initExtra = mkOption { 142default = "" ; 143type = types . lines ; 144description = '' 145Extra commands that should be run when initializing an 146interactive shell. 147'' ; 148}; 149 150bashrcExtra = mkOption { 151default = "" ; 152type = types . lines ; 153description = '' 154Extra commands that should be placed in {file}`~/.bashrc`. 155Note that these commands will be run even in non-interactive shells. 156'' ; 157}; 158 159logoutExtra = mkOption { 160default = "" ; 161type = types . lines ; 162description = '' 163Extra commands that should be run when logging out of an 164interactive shell. 165'' ; 166}; 167}; 168}; 169 170config = let 171aliasesStr = concatStringsSep "\n" 172( mapAttrsToList ( k :v :"alias ${ k } = ${ escapeShellArg v } " ) 173cfg . shellAliases ); 174 175shoptsStr = let switch = v :if hasPrefix "-" v then "-u" else "-s" ; 176in concatStringsSep "\n" 177( map ( v :"shopt ${ switch v } ${ removePrefix "-" v } " ) cfg . shellOptions ); 178 179sessionVarsStr = config . lib . shell . exportAll cfg . sessionVariables ; 180 181historyControlStr = concatStringsSep "\n" 182( mapAttrsToList ( n :v :" ${ n } = ${ v } " ) ({ 183HISTFILESIZE = toString cfg . historyFileSize ; 184HISTSIZE = toString cfg . historySize ; 185} // optionalAttrs ( cfg . historyFile != null ) { 186HISTFILE = ''" ${ cfg . historyFile } "'' ; 187} // optionalAttrs ( cfg . historyControl != [ ]) { 188HISTCONTROL = concatStringsSep ":" cfg . historyControl ; 189} // optionalAttrs ( cfg . historyIgnore != [ ]) { 190HISTIGNORE = escapeShellArg ( concatStringsSep ":" cfg . historyIgnore ); 191})); 192in mkIf cfg . enable { 193home . file . ".bash_profile" . source = writeBashScript "bash_profile" '' 194# include .profile if it exists 195[[ -f ~/.profile ]] && . ~/.profile 196 197# include .bashrc if it exists 198[[ -f ~/.bashrc ]] && . ~/.bashrc 199'' ; 200 201# If completion is enabled then make sure it is sourced very early. This 202# is to avoid problems if any other initialization code attempts to set up 203# completion. 204programs . bash . initExtra = mkIf cfg . enableCompletion ( mkOrder 100 '' 205if [[ ! -v BASH_COMPLETION_VERSINFO ]]; then 206. " ${ pkgs . bash-completion } /etc/profile.d/bash_completion.sh" 207fi 208'' ); 209 210home . file . ".profile" . source = writeBashScript "profile" '' 211. " ${ config . home . profileDirectory } /etc/profile.d/hm-session-vars.sh" 212 213${ sessionVarsStr } 214 215${ cfg . profileExtra } 216'' ; 217 218home . file . ".bashrc" . source = writeBashScript "bashrc" '' 219${ cfg . bashrcExtra } 220 221# Commands that should be applied only for interactive shells. 222[[ $- == *i* ]] || return 223 224${ historyControlStr } 225 226${ shoptsStr } 227 228${ aliasesStr } 229 230${ cfg . initExtra } 231'' ; 232 233home . file . ".bash_logout" = mkIf ( cfg . logoutExtra != "" ) { 234source = writeBashScript "bash_logout" cfg . logoutExtra ; 235}; 236}; 237}