fork/home-manager

git clone https://git.julia.blue/fork/home-manager

Robert HelgessonMerge PR #421575790441

master
6.5 KiB237 linesraw
1{ config, lib, pkgs, ... }:
2
3with lib;
4
5let
6
7  cfg = config.programs.bash;
8
9  writeBashScript = name: text:
10    pkgs.writeTextFile {
11      inherit name text;
12      checkPhase = ''
13        ${pkgs.stdenv.shellDryRun} "$target"
14      '';
15    };
16
17in {
18  meta.maintainers = [ maintainers.rycee ];
19
20  imports = [
21    (mkRenamedOptionModule [ "programs" "bash" "enableAutojump" ] [
22      "programs"
23      "autojump"
24      "enable"
25    ])
26  ];
27
28  options = {
29    programs.bash = {
30      enable = mkEnableOption "GNU Bourne-Again SHell";
31
32      enableCompletion = mkOption {
33        type = types.bool;
34        default = true;
35        description = ''
36          Whether to enable Bash completion for all interactive Bash shells.
37
38          Note, if you use NixOS or nix-darwin and do not have Bash completion
39          enabled in the system configuration, then make sure to add
40
41          ```nix
42            environment.pathsToLink = [ "/share/bash-completion" ];
43          ```
44
45          to your system configuration to get completion for system packages.
46          Note, the legacy {file}`/etc/bash_completion.d` path is
47          not supported by Home Manager.
48        '';
49      };
50
51      historySize = mkOption {
52        type = types.int;
53        default = 10000;
54        description = "Number of history lines to keep in memory.";
55      };
56
57      historyFile = mkOption {
58        type = types.nullOr types.str;
59        default = null;
60        description = "Location of the bash history file.";
61      };
62
63      historyFileSize = mkOption {
64        type = types.int;
65        default = 100000;
66        description = "Number of history lines to keep on file.";
67      };
68
69      historyControl = mkOption {
70        type =
71          types.listOf (types.enum [ "erasedups" "ignoredups" "ignorespace" ]);
72        default = [ ];
73        description = "Controlling how commands are saved on the history list.";
74      };
75
76      historyIgnore = mkOption {
77        type = types.listOf types.str;
78        default = [ ];
79        example = [ "ls" "cd" "exit" ];
80        description =
81          "List of commands that should not be saved to the history list.";
82      };
83
84      shellOptions = mkOption {
85        type = types.listOf types.str;
86        default = [
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        ];
101        example = [ "extglob" "-cdspell" ];
102        description = ''
103          Shell options to set. Prefix an option with
104          "`-`" to unset.
105        '';
106      };
107
108      sessionVariables = mkOption {
109        default = { };
110        type = types.attrs;
111        example = { MAILCHECK = 30; };
112        description = ''
113          Environment variables that will be set for the Bash session.
114        '';
115      };
116
117      shellAliases = mkOption {
118        default = { };
119        type = types.attrsOf types.str;
120        example = literalExpression ''
121          {
122            ll = "ls -l";
123            ".." = "cd ..";
124          }
125        '';
126        description = ''
127          An attribute set that maps aliases (the top level attribute names in
128          this option) to command strings or directly to build outputs.
129        '';
130      };
131
132      profileExtra = mkOption {
133        default = "";
134        type = types.lines;
135        description = ''
136          Extra commands that should be run when initializing a login
137          shell.
138        '';
139      };
140
141      initExtra = mkOption {
142        default = "";
143        type = types.lines;
144        description = ''
145          Extra commands that should be run when initializing an
146          interactive shell.
147        '';
148      };
149
150      bashrcExtra = mkOption {
151        default = "";
152        type = types.lines;
153        description = ''
154          Extra commands that should be placed in {file}`~/.bashrc`.
155          Note that these commands will be run even in non-interactive shells.
156        '';
157      };
158
159      logoutExtra = mkOption {
160        default = "";
161        type = types.lines;
162        description = ''
163          Extra commands that should be run when logging out of an
164          interactive shell.
165        '';
166      };
167    };
168  };
169
170  config = let
171    aliasesStr = concatStringsSep "\n"
172      (mapAttrsToList (k: v: "alias ${k}=${escapeShellArg v}")
173        cfg.shellAliases);
174
175    shoptsStr = let switch = v: if hasPrefix "-" v then "-u" else "-s";
176    in concatStringsSep "\n"
177    (map (v: "shopt ${switch v} ${removePrefix "-" v}") cfg.shellOptions);
178
179    sessionVarsStr = config.lib.shell.exportAll cfg.sessionVariables;
180
181    historyControlStr = concatStringsSep "\n"
182      (mapAttrsToList (n: v: "${n}=${v}") ({
183        HISTFILESIZE = toString cfg.historyFileSize;
184        HISTSIZE = toString cfg.historySize;
185      } // optionalAttrs (cfg.historyFile != null) {
186        HISTFILE = ''"${cfg.historyFile}"'';
187      } // optionalAttrs (cfg.historyControl != [ ]) {
188        HISTCONTROL = concatStringsSep ":" cfg.historyControl;
189      } // optionalAttrs (cfg.historyIgnore != [ ]) {
190        HISTIGNORE = escapeShellArg (concatStringsSep ":" cfg.historyIgnore);
191      }));
192  in mkIf cfg.enable {
193    home.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.
204    programs.bash.initExtra = mkIf cfg.enableCompletion (mkOrder 100 ''
205      if [[ ! -v BASH_COMPLETION_VERSINFO ]]; then
206        . "${pkgs.bash-completion}/etc/profile.d/bash_completion.sh"
207      fi
208    '');
209
210    home.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
218    home.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
233    home.file.".bash_logout" = mkIf (cfg.logoutExtra != "") {
234      source = writeBashScript "bash_logout" cfg.logoutExtra;
235    };
236  };
237}