1# This module defines global configuration for the Bash shell, in
2# particular /etc/bashrc and /etc/profile.
3
4{ config, lib, pkgs, ... }:
5
6let
7
8 cfge = config.environment;
9
10 cfg = config.programs.bash;
11
12 bashAliases = builtins.concatStringsSep "\n" (
13 lib.mapAttrsFlatten (k: v: "alias -- ${k}=${lib.escapeShellArg v}")
14 (lib.filterAttrs (k: v: v != null) cfg.shellAliases)
15 );
16
17in
18
19{
20 imports = [
21 (lib.mkRemovedOptionModule [ "programs" "bash" "enable" ] "")
22 ];
23
24 options = {
25
26 programs.bash = {
27
28 /*
29 enable = lib.mkOption {
30 default = true;
31 description = ''
32 Whenever to configure Bash as an interactive shell.
33 Note that this tries to make Bash the default
34 {option}`users.defaultUserShell`,
35 which in turn means that you might need to explicitly
36 set this variable if you have another shell configured
37 with NixOS.
38 '';
39 type = lib.types.bool;
40 };
41 */
42
43 shellAliases = lib.mkOption {
44 default = {};
45 description = ''
46 Set of aliases for bash shell, which overrides {option}`environment.shellAliases`.
47 See {option}`environment.shellAliases` for an option format description.
48 '';
49 type = with lib.types; attrsOf (nullOr (either str path));
50 };
51
52 shellInit = lib.mkOption {
53 default = "";
54 description = ''
55 Shell script code called during bash shell initialisation.
56 '';
57 type = lib.types.lines;
58 };
59
60 loginShellInit = lib.mkOption {
61 default = "";
62 description = ''
63 Shell script code called during login bash shell initialisation.
64 '';
65 type = lib.types.lines;
66 };
67
68 interactiveShellInit = lib.mkOption {
69 default = "";
70 description = ''
71 Shell script code called during interactive bash shell initialisation.
72 '';
73 type = lib.types.lines;
74 };
75
76 promptInit = lib.mkOption {
77 default = ''
78 # Provide a nice prompt if the terminal supports it.
79 if [ "$TERM" != "dumb" ] || [ -n "$INSIDE_EMACS" ]; then
80 PROMPT_COLOR="1;31m"
81 ((UID)) && PROMPT_COLOR="1;32m"
82 if [ -n "$INSIDE_EMACS" ]; then
83 # Emacs term mode doesn't support xterm title escape sequence (\e]0;)
84 PS1="\n\[\033[$PROMPT_COLOR\][\u@\h:\w]\\$\[\033[0m\] "
85 else
86 PS1="\n\[\033[$PROMPT_COLOR\][\[\e]0;\u@\h: \w\a\]\u@\h:\w]\\$\[\033[0m\] "
87 fi
88 if test "$TERM" = "xterm"; then
89 PS1="\[\033]2;\h:\u:\w\007\]$PS1"
90 fi
91 fi
92 '';
93 description = ''
94 Shell script code used to initialise the bash prompt.
95 '';
96 type = lib.types.lines;
97 };
98
99 promptPluginInit = lib.mkOption {
100 default = "";
101 description = ''
102 Shell script code used to initialise bash prompt plugins.
103 '';
104 type = lib.types.lines;
105 internal = true;
106 };
107
108 };
109
110 };
111
112 config = /* lib.mkIf cfg.enable */ {
113
114 programs.bash = {
115
116 shellAliases = builtins.mapAttrs (name: lib.mkDefault) cfge.shellAliases;
117
118 shellInit = ''
119 if [ -z "$__NIXOS_SET_ENVIRONMENT_DONE" ]; then
120 . ${config.system.build.setEnvironment}
121 fi
122
123 ${cfge.shellInit}
124 '';
125
126 loginShellInit = cfge.loginShellInit;
127
128 interactiveShellInit = ''
129 # Check the window size after every command.
130 shopt -s checkwinsize
131
132 # Disable hashing (i.e. caching) of command lookups.
133 set +h
134
135 ${cfg.promptInit}
136 ${cfg.promptPluginInit}
137 ${bashAliases}
138
139 ${cfge.interactiveShellInit}
140 '';
141
142 };
143
144 environment.etc.profile.text =
145 ''
146 # /etc/profile: DO NOT EDIT -- this file has been generated automatically.
147 # This file is read for login shells.
148
149 # Only execute this file once per shell.
150 if [ -n "$__ETC_PROFILE_SOURCED" ]; then return; fi
151 __ETC_PROFILE_SOURCED=1
152
153 # Prevent this file from being sourced by interactive non-login child shells.
154 export __ETC_PROFILE_DONE=1
155
156 ${cfg.shellInit}
157 ${cfg.loginShellInit}
158
159 # Read system-wide modifications.
160 if test -f /etc/profile.local; then
161 . /etc/profile.local
162 fi
163
164 if [ -n "''${BASH_VERSION:-}" ]; then
165 . /etc/bashrc
166 fi
167 '';
168
169 environment.etc.bashrc.text =
170 ''
171 # /etc/bashrc: DO NOT EDIT -- this file has been generated automatically.
172
173 # Only execute this file once per shell.
174 if [ -n "$__ETC_BASHRC_SOURCED" ] || [ -n "$NOSYSBASHRC" ]; then return; fi
175 __ETC_BASHRC_SOURCED=1
176
177 # If the profile was not loaded in a parent process, source
178 # it. But otherwise don't do it because we don't want to
179 # clobber overridden values of $PATH, etc.
180 if [ -z "$__ETC_PROFILE_DONE" ]; then
181 . /etc/profile
182 fi
183
184 # We are not always an interactive shell.
185 if [ -n "$PS1" ]; then
186 ${cfg.interactiveShellInit}
187 fi
188
189 # Read system-wide modifications.
190 if test -f /etc/bashrc.local; then
191 . /etc/bashrc.local
192 fi
193 '';
194
195 # Configuration for readline in bash. We use "option default"
196 # priority to allow user override using both .text and .source.
197 environment.etc.inputrc.source = lib.mkOptionDefault ./inputrc;
198
199 users.defaultUserShell = lib.mkDefault pkgs.bashInteractive;
200
201 environment.pathsToLink = lib.optionals cfg.enableCompletion [
202 "/etc/bash_completion.d"
203 "/share/bash-completion"
204 ];
205
206 environment.shells =
207 [ "/run/current-system/sw/bin/bash"
208 "/run/current-system/sw/bin/sh"
209 "${pkgs.bashInteractive}/bin/bash"
210 "${pkgs.bashInteractive}/bin/sh"
211 ];
212
213 };
214
215}