1 # This module defines global configuration for the Bash shell, in
2 # particular /etc/bashrc and /etc/profile.
4 { config, lib, pkgs, ... }:
8 cfge = config.environment;
10 cfg = config.programs.bash;
12 bashAliases = builtins.concatStringsSep "\n" (
13 lib.mapAttrsToList (k: v: "alias -- ${k}=${lib.escapeShellArg v}")
14 (lib.filterAttrs (k: v: v != null) cfg.shellAliases)
21 (lib.mkRemovedOptionModule [ "programs" "bash" "enable" ] "")
29 enable = lib.mkOption {
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
39 type = lib.types.bool;
43 shellAliases = lib.mkOption {
46 Set of aliases for bash shell, which overrides {option}`environment.shellAliases`.
47 See {option}`environment.shellAliases` for an option format description.
49 type = with lib.types; attrsOf (nullOr (either str path));
52 shellInit = lib.mkOption {
55 Shell script code called during bash shell initialisation.
57 type = lib.types.lines;
60 loginShellInit = lib.mkOption {
63 Shell script code called during login bash shell initialisation.
65 type = lib.types.lines;
68 interactiveShellInit = lib.mkOption {
71 Shell script code called during interactive bash shell initialisation.
73 type = lib.types.lines;
76 promptInit = lib.mkOption {
78 # Provide a nice prompt if the terminal supports it.
79 if [ "$TERM" != "dumb" ] || [ -n "$INSIDE_EMACS" ]; then
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\] "
86 PS1="\n\[\033[$PROMPT_COLOR\][\[\e]0;\u@\h: \w\a\]\u@\h:\w]\\$\[\033[0m\] "
88 if test "$TERM" = "xterm"; then
89 PS1="\[\033]2;\h:\u:\w\007\]$PS1"
94 Shell script code used to initialise the bash prompt.
96 type = lib.types.lines;
99 promptPluginInit = lib.mkOption {
102 Shell script code used to initialise bash prompt plugins.
104 type = lib.types.lines;
112 config = /* lib.mkIf cfg.enable */ {
116 shellAliases = builtins.mapAttrs (name: lib.mkDefault) cfge.shellAliases;
119 if [ -z "$__NIXOS_SET_ENVIRONMENT_DONE" ]; then
120 . ${config.system.build.setEnvironment}
126 loginShellInit = cfge.loginShellInit;
128 interactiveShellInit = ''
129 # Check the window size after every command.
130 shopt -s checkwinsize
132 # Disable hashing (i.e. caching) of command lookups.
136 ${cfg.promptPluginInit}
139 ${cfge.interactiveShellInit}
144 environment.etc.profile.text =
146 # /etc/profile: DO NOT EDIT -- this file has been generated automatically.
147 # This file is read for login shells.
149 # Only execute this file once per shell.
150 if [ -n "$__ETC_PROFILE_SOURCED" ]; then return; fi
151 __ETC_PROFILE_SOURCED=1
153 # Prevent this file from being sourced by interactive non-login child shells.
154 export __ETC_PROFILE_DONE=1
157 ${cfg.loginShellInit}
159 # Read system-wide modifications.
160 if test -f /etc/profile.local; then
164 if [ -n "''${BASH_VERSION:-}" ]; then
169 environment.etc.bashrc.text =
171 # /etc/bashrc: DO NOT EDIT -- this file has been generated automatically.
173 # Only execute this file once per shell.
174 if [ -n "$__ETC_BASHRC_SOURCED" ] || [ -n "$NOSYSBASHRC" ]; then return; fi
175 __ETC_BASHRC_SOURCED=1
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
184 # We are not always an interactive shell.
185 if [ -n "$PS1" ]; then
186 ${cfg.interactiveShellInit}
189 # Read system-wide modifications.
190 if test -f /etc/bashrc.local; then
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;
199 users.defaultUserShell = lib.mkDefault pkgs.bashInteractive;
201 environment.pathsToLink = lib.optionals cfg.completion.enable [
202 "/etc/bash_completion.d"
203 "/share/bash-completion"
207 [ "/run/current-system/sw/bin/bash"
208 "/run/current-system/sw/bin/sh"
209 "${pkgs.bashInteractive}/bin/bash"
210 "${pkgs.bashInteractive}/bin/sh"