2020-04-24 23:36:52 +00:00
|
|
|
# This module defines global configuration for the Bash shell, in
|
|
|
|
# particular /etc/bashrc and /etc/profile.
|
|
|
|
|
|
|
|
{ config, lib, pkgs, ... }:
|
|
|
|
|
|
|
|
let
|
|
|
|
|
|
|
|
cfge = config.environment;
|
|
|
|
|
|
|
|
cfg = config.programs.bash;
|
|
|
|
|
2024-05-15 15:35:15 +00:00
|
|
|
bashAliases = builtins.concatStringsSep "\n" (
|
2024-07-27 06:49:29 +00:00
|
|
|
lib.mapAttrsToList (k: v: "alias -- ${k}=${lib.escapeShellArg v}")
|
2024-05-15 15:35:15 +00:00
|
|
|
(lib.filterAttrs (k: v: v != null) cfg.shellAliases)
|
2020-04-24 23:36:52 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
in
|
|
|
|
|
|
|
|
{
|
|
|
|
imports = [
|
2024-05-15 15:35:15 +00:00
|
|
|
(lib.mkRemovedOptionModule [ "programs" "bash" "enable" ] "")
|
2020-04-24 23:36:52 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
options = {
|
|
|
|
|
|
|
|
programs.bash = {
|
|
|
|
|
|
|
|
/*
|
2024-05-15 15:35:15 +00:00
|
|
|
enable = lib.mkOption {
|
2020-04-24 23:36:52 +00:00
|
|
|
default = true;
|
2024-04-21 15:54:59 +00:00
|
|
|
description = ''
|
2020-04-24 23:36:52 +00:00
|
|
|
Whenever to configure Bash as an interactive shell.
|
|
|
|
Note that this tries to make Bash the default
|
2022-08-12 12:06:08 +00:00
|
|
|
{option}`users.defaultUserShell`,
|
2020-04-24 23:36:52 +00:00
|
|
|
which in turn means that you might need to explicitly
|
|
|
|
set this variable if you have another shell configured
|
|
|
|
with NixOS.
|
|
|
|
'';
|
2024-05-15 15:35:15 +00:00
|
|
|
type = lib.types.bool;
|
2020-04-24 23:36:52 +00:00
|
|
|
};
|
|
|
|
*/
|
|
|
|
|
2024-05-15 15:35:15 +00:00
|
|
|
shellAliases = lib.mkOption {
|
2020-04-24 23:36:52 +00:00
|
|
|
default = {};
|
2024-04-21 15:54:59 +00:00
|
|
|
description = ''
|
2022-08-12 12:06:08 +00:00
|
|
|
Set of aliases for bash shell, which overrides {option}`environment.shellAliases`.
|
|
|
|
See {option}`environment.shellAliases` for an option format description.
|
2020-04-24 23:36:52 +00:00
|
|
|
'';
|
2024-05-15 15:35:15 +00:00
|
|
|
type = with lib.types; attrsOf (nullOr (either str path));
|
2020-04-24 23:36:52 +00:00
|
|
|
};
|
|
|
|
|
2024-05-15 15:35:15 +00:00
|
|
|
shellInit = lib.mkOption {
|
2020-04-24 23:36:52 +00:00
|
|
|
default = "";
|
2024-04-21 15:54:59 +00:00
|
|
|
description = ''
|
2020-04-24 23:36:52 +00:00
|
|
|
Shell script code called during bash shell initialisation.
|
|
|
|
'';
|
2024-05-15 15:35:15 +00:00
|
|
|
type = lib.types.lines;
|
2020-04-24 23:36:52 +00:00
|
|
|
};
|
|
|
|
|
2024-05-15 15:35:15 +00:00
|
|
|
loginShellInit = lib.mkOption {
|
2020-04-24 23:36:52 +00:00
|
|
|
default = "";
|
2024-04-21 15:54:59 +00:00
|
|
|
description = ''
|
2020-04-24 23:36:52 +00:00
|
|
|
Shell script code called during login bash shell initialisation.
|
|
|
|
'';
|
2024-05-15 15:35:15 +00:00
|
|
|
type = lib.types.lines;
|
2020-04-24 23:36:52 +00:00
|
|
|
};
|
|
|
|
|
2024-05-15 15:35:15 +00:00
|
|
|
interactiveShellInit = lib.mkOption {
|
2020-04-24 23:36:52 +00:00
|
|
|
default = "";
|
2024-04-21 15:54:59 +00:00
|
|
|
description = ''
|
2020-04-24 23:36:52 +00:00
|
|
|
Shell script code called during interactive bash shell initialisation.
|
|
|
|
'';
|
2024-05-15 15:35:15 +00:00
|
|
|
type = lib.types.lines;
|
2020-04-24 23:36:52 +00:00
|
|
|
};
|
|
|
|
|
2024-05-15 15:35:15 +00:00
|
|
|
promptInit = lib.mkOption {
|
2020-04-24 23:36:52 +00:00
|
|
|
default = ''
|
|
|
|
# Provide a nice prompt if the terminal supports it.
|
2021-09-18 10:52:07 +00:00
|
|
|
if [ "$TERM" != "dumb" ] || [ -n "$INSIDE_EMACS" ]; then
|
2020-04-24 23:36:52 +00:00
|
|
|
PROMPT_COLOR="1;31m"
|
2021-09-18 10:52:07 +00:00
|
|
|
((UID)) && PROMPT_COLOR="1;32m"
|
2023-10-09 19:29:22 +00:00
|
|
|
if [ -n "$INSIDE_EMACS" ]; then
|
2020-04-24 23:36:52 +00:00
|
|
|
# Emacs term mode doesn't support xterm title escape sequence (\e]0;)
|
|
|
|
PS1="\n\[\033[$PROMPT_COLOR\][\u@\h:\w]\\$\[\033[0m\] "
|
|
|
|
else
|
|
|
|
PS1="\n\[\033[$PROMPT_COLOR\][\[\e]0;\u@\h: \w\a\]\u@\h:\w]\\$\[\033[0m\] "
|
|
|
|
fi
|
|
|
|
if test "$TERM" = "xterm"; then
|
|
|
|
PS1="\[\033]2;\h:\u:\w\007\]$PS1"
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
'';
|
2024-04-21 15:54:59 +00:00
|
|
|
description = ''
|
2020-04-24 23:36:52 +00:00
|
|
|
Shell script code used to initialise the bash prompt.
|
|
|
|
'';
|
2024-05-15 15:35:15 +00:00
|
|
|
type = lib.types.lines;
|
2020-04-24 23:36:52 +00:00
|
|
|
};
|
|
|
|
|
2024-05-15 15:35:15 +00:00
|
|
|
promptPluginInit = lib.mkOption {
|
2021-05-03 20:48:10 +00:00
|
|
|
default = "";
|
2024-04-21 15:54:59 +00:00
|
|
|
description = ''
|
2021-05-03 20:48:10 +00:00
|
|
|
Shell script code used to initialise bash prompt plugins.
|
2020-04-24 23:36:52 +00:00
|
|
|
'';
|
2024-05-15 15:35:15 +00:00
|
|
|
type = lib.types.lines;
|
2021-05-03 20:48:10 +00:00
|
|
|
internal = true;
|
2020-04-24 23:36:52 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2024-05-15 15:35:15 +00:00
|
|
|
config = /* lib.mkIf cfg.enable */ {
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
programs.bash = {
|
|
|
|
|
2024-05-15 15:35:15 +00:00
|
|
|
shellAliases = builtins.mapAttrs (name: lib.mkDefault) cfge.shellAliases;
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
shellInit = ''
|
|
|
|
if [ -z "$__NIXOS_SET_ENVIRONMENT_DONE" ]; then
|
|
|
|
. ${config.system.build.setEnvironment}
|
|
|
|
fi
|
|
|
|
|
|
|
|
${cfge.shellInit}
|
|
|
|
'';
|
|
|
|
|
|
|
|
loginShellInit = cfge.loginShellInit;
|
|
|
|
|
|
|
|
interactiveShellInit = ''
|
|
|
|
# Check the window size after every command.
|
|
|
|
shopt -s checkwinsize
|
|
|
|
|
|
|
|
# Disable hashing (i.e. caching) of command lookups.
|
|
|
|
set +h
|
|
|
|
|
|
|
|
${cfg.promptInit}
|
2021-05-03 20:48:10 +00:00
|
|
|
${cfg.promptPluginInit}
|
2020-04-24 23:36:52 +00:00
|
|
|
${bashAliases}
|
|
|
|
|
|
|
|
${cfge.interactiveShellInit}
|
|
|
|
'';
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
environment.etc.profile.text =
|
|
|
|
''
|
|
|
|
# /etc/profile: DO NOT EDIT -- this file has been generated automatically.
|
|
|
|
# This file is read for login shells.
|
|
|
|
|
|
|
|
# Only execute this file once per shell.
|
|
|
|
if [ -n "$__ETC_PROFILE_SOURCED" ]; then return; fi
|
|
|
|
__ETC_PROFILE_SOURCED=1
|
|
|
|
|
|
|
|
# Prevent this file from being sourced by interactive non-login child shells.
|
|
|
|
export __ETC_PROFILE_DONE=1
|
|
|
|
|
|
|
|
${cfg.shellInit}
|
|
|
|
${cfg.loginShellInit}
|
|
|
|
|
|
|
|
# Read system-wide modifications.
|
|
|
|
if test -f /etc/profile.local; then
|
|
|
|
. /etc/profile.local
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [ -n "''${BASH_VERSION:-}" ]; then
|
|
|
|
. /etc/bashrc
|
|
|
|
fi
|
|
|
|
'';
|
|
|
|
|
|
|
|
environment.etc.bashrc.text =
|
|
|
|
''
|
|
|
|
# /etc/bashrc: DO NOT EDIT -- this file has been generated automatically.
|
|
|
|
|
|
|
|
# Only execute this file once per shell.
|
2021-09-18 10:52:07 +00:00
|
|
|
if [ -n "$__ETC_BASHRC_SOURCED" ] || [ -n "$NOSYSBASHRC" ]; then return; fi
|
2020-04-24 23:36:52 +00:00
|
|
|
__ETC_BASHRC_SOURCED=1
|
|
|
|
|
|
|
|
# If the profile was not loaded in a parent process, source
|
|
|
|
# it. But otherwise don't do it because we don't want to
|
|
|
|
# clobber overridden values of $PATH, etc.
|
|
|
|
if [ -z "$__ETC_PROFILE_DONE" ]; then
|
|
|
|
. /etc/profile
|
|
|
|
fi
|
|
|
|
|
|
|
|
# We are not always an interactive shell.
|
|
|
|
if [ -n "$PS1" ]; then
|
|
|
|
${cfg.interactiveShellInit}
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Read system-wide modifications.
|
|
|
|
if test -f /etc/bashrc.local; then
|
|
|
|
. /etc/bashrc.local
|
|
|
|
fi
|
|
|
|
'';
|
|
|
|
|
|
|
|
# Configuration for readline in bash. We use "option default"
|
|
|
|
# priority to allow user override using both .text and .source.
|
2024-05-15 15:35:15 +00:00
|
|
|
environment.etc.inputrc.source = lib.mkOptionDefault ./inputrc;
|
2020-04-24 23:36:52 +00:00
|
|
|
|
2024-05-15 15:35:15 +00:00
|
|
|
users.defaultUserShell = lib.mkDefault pkgs.bashInteractive;
|
2020-04-24 23:36:52 +00:00
|
|
|
|
2024-06-20 14:57:18 +00:00
|
|
|
environment.pathsToLink = lib.optionals cfg.completion.enable [
|
2020-04-24 23:36:52 +00:00
|
|
|
"/etc/bash_completion.d"
|
|
|
|
"/share/bash-completion"
|
|
|
|
];
|
|
|
|
|
|
|
|
environment.shells =
|
|
|
|
[ "/run/current-system/sw/bin/bash"
|
|
|
|
"/run/current-system/sw/bin/sh"
|
|
|
|
"${pkgs.bashInteractive}/bin/bash"
|
|
|
|
"${pkgs.bashInteractive}/bin/sh"
|
|
|
|
];
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|