2009-05-28 13:06:54 +01:00
|
|
|
# This module defines global configuration for the Bash shell, in
|
|
|
|
# particular /etc/bashrc and /etc/profile.
|
|
|
|
|
2014-04-14 15:26:48 +01:00
|
|
|
{ config, lib, pkgs, ... }:
|
2010-09-13 16:41:38 +01:00
|
|
|
|
2014-04-14 15:26:48 +01:00
|
|
|
with lib;
|
2009-05-28 13:06:54 +01:00
|
|
|
|
2009-05-28 14:17:56 +01:00
|
|
|
let
|
|
|
|
|
2013-09-18 04:18:34 +01:00
|
|
|
cfge = config.environment;
|
2013-01-30 13:05:53 +00:00
|
|
|
|
2013-09-18 04:18:34 +01:00
|
|
|
cfg = config.programs.bash;
|
|
|
|
|
|
|
|
bashCompletion = optionalString cfg.enableCompletion ''
|
2012-10-16 17:23:28 +01:00
|
|
|
# Check whether we're running a version of Bash that has support for
|
|
|
|
# programmable completion. If we do, enable all modules installed in
|
2017-12-10 12:09:55 +00:00
|
|
|
# the system and user profile in obsolete /etc/bash_completion.d/
|
|
|
|
# directories. Bash loads completions in all
|
2018-11-04 12:06:13 +00:00
|
|
|
# $XDG_DATA_DIRS/bash-completion/completions/
|
2017-12-10 12:09:55 +00:00
|
|
|
# on demand, so they do not need to be sourced here.
|
2012-10-16 17:23:28 +01:00
|
|
|
if shopt -q progcomp &>/dev/null; then
|
2016-09-28 16:46:29 +01:00
|
|
|
. "${pkgs.bash-completion}/etc/profile.d/bash_completion.sh"
|
2012-10-16 17:23:28 +01:00
|
|
|
nullglobStatus=$(shopt -p nullglob)
|
|
|
|
shopt -s nullglob
|
2012-10-16 18:07:19 +01:00
|
|
|
for p in $NIX_PROFILES; do
|
2017-12-10 08:54:00 +00:00
|
|
|
for m in "$p/etc/bash_completion.d/"*; do
|
2012-10-16 17:23:28 +01:00
|
|
|
. $m
|
|
|
|
done
|
|
|
|
done
|
|
|
|
eval "$nullglobStatus"
|
2012-10-16 17:41:20 +01:00
|
|
|
unset nullglobStatus p m
|
2012-10-16 17:23:28 +01:00
|
|
|
fi
|
|
|
|
'';
|
|
|
|
|
2013-09-18 04:18:34 +01:00
|
|
|
bashAliases = concatStringsSep "\n" (
|
2018-10-12 14:58:35 +01:00
|
|
|
mapAttrsFlatten (k: v: "alias ${k}=${escapeShellArg v}")
|
2019-04-24 04:48:22 +01:00
|
|
|
(filterAttrs (k: v: v != null) cfg.shellAliases)
|
2012-07-25 15:53:46 +01:00
|
|
|
);
|
2012-10-16 17:23:28 +01:00
|
|
|
|
2013-09-04 12:05:09 +01:00
|
|
|
in
|
|
|
|
|
|
|
|
{
|
2009-05-28 14:17:56 +01:00
|
|
|
options = {
|
|
|
|
|
2013-09-18 04:18:34 +01:00
|
|
|
programs.bash = {
|
|
|
|
|
2014-04-29 17:57:04 +01:00
|
|
|
/*
|
2013-09-18 04:18:34 +01:00
|
|
|
enable = mkOption {
|
|
|
|
default = true;
|
|
|
|
description = ''
|
|
|
|
Whenever to configure Bash as an interactive shell.
|
|
|
|
Note that this tries to make Bash the default
|
|
|
|
<option>users.defaultUserShell</option>,
|
|
|
|
which in turn means that you might need to explicitly
|
|
|
|
set this variable if you have another shell configured
|
|
|
|
with NixOS.
|
|
|
|
'';
|
|
|
|
type = types.bool;
|
|
|
|
};
|
2014-04-29 17:57:04 +01:00
|
|
|
*/
|
2012-10-30 12:33:37 +00:00
|
|
|
|
2013-09-18 04:18:34 +01:00
|
|
|
shellAliases = mkOption {
|
2018-08-04 04:32:30 +01:00
|
|
|
default = {};
|
2013-09-18 04:18:34 +01:00
|
|
|
description = ''
|
2018-08-04 04:32:30 +01:00
|
|
|
Set of aliases for bash shell, which overrides <option>environment.shellAliases</option>.
|
|
|
|
See <option>environment.shellAliases</option> for an option format description.
|
2013-09-18 04:18:34 +01:00
|
|
|
'';
|
2018-10-12 14:58:35 +01:00
|
|
|
type = with types; attrsOf (nullOr (either str path));
|
2013-09-18 04:18:34 +01:00
|
|
|
};
|
2013-01-30 13:05:53 +00:00
|
|
|
|
2013-09-18 04:18:34 +01:00
|
|
|
shellInit = mkOption {
|
|
|
|
default = "";
|
|
|
|
description = ''
|
|
|
|
Shell script code called during bash shell initialisation.
|
|
|
|
'';
|
|
|
|
type = types.lines;
|
|
|
|
};
|
2009-05-28 14:17:56 +01:00
|
|
|
|
2013-09-18 04:18:34 +01:00
|
|
|
loginShellInit = mkOption {
|
|
|
|
default = "";
|
|
|
|
description = ''
|
|
|
|
Shell script code called during login bash shell initialisation.
|
|
|
|
'';
|
|
|
|
type = types.lines;
|
|
|
|
};
|
|
|
|
|
|
|
|
interactiveShellInit = mkOption {
|
|
|
|
default = "";
|
|
|
|
description = ''
|
|
|
|
Shell script code called during interactive bash shell initialisation.
|
|
|
|
'';
|
|
|
|
type = types.lines;
|
|
|
|
};
|
|
|
|
|
|
|
|
promptInit = mkOption {
|
|
|
|
default = ''
|
2015-11-18 15:24:14 +00:00
|
|
|
# Provide a nice prompt if the terminal supports it.
|
|
|
|
if [ "$TERM" != "dumb" -o -n "$INSIDE_EMACS" ]; then
|
2015-10-14 16:15:11 +01:00
|
|
|
PROMPT_COLOR="1;31m"
|
|
|
|
let $UID && PROMPT_COLOR="1;32m"
|
2018-12-17 23:42:41 +00:00
|
|
|
if [ -n "$INSIDE_EMACS" ]; then
|
|
|
|
# Emacs term mode doesn't support xterm title escape sequence (\e]0;)
|
|
|
|
PS1="\n\[\033[$PROMPT_COLOR\][\u@\h:\w]\\$\[\033[0m\] "
|
|
|
|
else
|
2019-03-05 12:27:32 +00:00
|
|
|
PS1="\n\[\033[$PROMPT_COLOR\][\[\e]0;\u@\h: \w\a\]\u@\h:\w]\\$\[\033[0m\] "
|
2018-12-17 23:42:41 +00:00
|
|
|
fi
|
2015-10-14 16:15:11 +01:00
|
|
|
if test "$TERM" = "xterm"; then
|
|
|
|
PS1="\[\033]2;\h:\u:\w\007\]$PS1"
|
|
|
|
fi
|
2013-09-18 04:18:34 +01:00
|
|
|
fi
|
|
|
|
'';
|
|
|
|
description = ''
|
|
|
|
Shell script code used to initialise the bash prompt.
|
|
|
|
'';
|
|
|
|
type = types.lines;
|
|
|
|
};
|
|
|
|
|
|
|
|
enableCompletion = mkOption {
|
2018-03-29 12:47:33 +01:00
|
|
|
default = true;
|
2013-09-18 04:18:34 +01:00
|
|
|
description = ''
|
|
|
|
Enable Bash completion for all interactive bash shells.
|
|
|
|
'';
|
|
|
|
type = types.bool;
|
|
|
|
};
|
2012-10-16 17:23:28 +01:00
|
|
|
|
2012-11-11 20:46:25 +00:00
|
|
|
};
|
|
|
|
|
2009-05-28 14:17:56 +01:00
|
|
|
};
|
|
|
|
|
2014-04-29 17:57:04 +01:00
|
|
|
config = /* mkIf cfg.enable */ {
|
2011-09-14 19:20:50 +01:00
|
|
|
|
2013-09-18 04:18:34 +01:00
|
|
|
programs.bash = {
|
2013-09-04 12:05:09 +01:00
|
|
|
|
2018-08-04 04:32:30 +01:00
|
|
|
shellAliases = mapAttrs (name: mkDefault) cfge.shellAliases;
|
|
|
|
|
2013-09-18 04:18:34 +01:00
|
|
|
shellInit = ''
|
2018-08-29 17:05:48 +01:00
|
|
|
if [ -z "$__NIXOS_SET_ENVIRONMENT_DONE" ]; then
|
|
|
|
. ${config.system.build.setEnvironment}
|
|
|
|
fi
|
2013-09-04 12:05:09 +01:00
|
|
|
|
2013-09-18 04:18:34 +01:00
|
|
|
${cfge.shellInit}
|
|
|
|
'';
|
2013-09-04 12:05:09 +01:00
|
|
|
|
2013-09-18 04:18:34 +01:00
|
|
|
loginShellInit = cfge.loginShellInit;
|
2013-09-04 12:05:09 +01:00
|
|
|
|
2013-09-18 04:18:34 +01:00
|
|
|
interactiveShellInit = ''
|
2013-09-04 12:05:09 +01:00
|
|
|
# Check the window size after every command.
|
|
|
|
shopt -s checkwinsize
|
|
|
|
|
|
|
|
# Disable hashing (i.e. caching) of command lookups.
|
|
|
|
set +h
|
2013-09-18 04:18:34 +01:00
|
|
|
|
|
|
|
${cfg.promptInit}
|
|
|
|
${bashCompletion}
|
|
|
|
${bashAliases}
|
2013-10-11 10:55:58 +01:00
|
|
|
|
|
|
|
${cfge.interactiveShellInit}
|
2013-09-04 12:05:09 +01:00
|
|
|
'';
|
|
|
|
|
2013-09-18 04:18:34 +01:00
|
|
|
};
|
2013-09-04 12:05:09 +01:00
|
|
|
|
2013-09-18 04:18:34 +01:00
|
|
|
environment.etc."profile".text =
|
2013-09-04 12:05:09 +01:00
|
|
|
''
|
2013-09-18 04:18:34 +01:00
|
|
|
# /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
|
|
|
|
|
2013-09-24 23:25:22 +01:00
|
|
|
# Prevent this file from being sourced by interactive non-login child shells.
|
|
|
|
export __ETC_PROFILE_DONE=1
|
2013-09-18 04:18:34 +01:00
|
|
|
|
2013-09-24 23:25:22 +01:00
|
|
|
${cfg.shellInit}
|
2013-09-18 04:18:34 +01:00
|
|
|
${cfg.loginShellInit}
|
|
|
|
|
|
|
|
# Read system-wide modifications.
|
|
|
|
if test -f /etc/profile.local; then
|
2018-08-29 17:05:53 +01:00
|
|
|
. /etc/profile.local
|
2013-09-18 04:18:34 +01:00
|
|
|
fi
|
|
|
|
|
|
|
|
if [ -n "''${BASH_VERSION:-}" ]; then
|
2018-08-29 17:05:53 +01:00
|
|
|
. /etc/bashrc
|
2013-09-18 04:18:34 +01:00
|
|
|
fi
|
2013-09-04 12:05:09 +01:00
|
|
|
'';
|
|
|
|
|
2013-09-18 04:18:34 +01:00
|
|
|
environment.etc."bashrc".text =
|
|
|
|
''
|
|
|
|
# /etc/bashrc: DO NOT EDIT -- this file has been generated automatically.
|
|
|
|
|
|
|
|
# Only execute this file once per shell.
|
|
|
|
if [ -n "$__ETC_BASHRC_SOURCED" -o -n "$NOSYSBASHRC" ]; then return; fi
|
|
|
|
__ETC_BASHRC_SOURCED=1
|
|
|
|
|
2013-09-24 23:25:22 +01:00
|
|
|
# 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
|
2013-09-18 04:18:34 +01:00
|
|
|
fi
|
|
|
|
|
|
|
|
# We are not always an interactive shell.
|
|
|
|
if [ -n "$PS1" ]; then
|
2018-08-29 17:05:53 +01:00
|
|
|
${cfg.interactiveShellInit}
|
2013-09-18 04:18:34 +01:00
|
|
|
fi
|
|
|
|
|
|
|
|
# Read system-wide modifications.
|
|
|
|
if test -f /etc/bashrc.local; then
|
2018-08-29 17:05:53 +01:00
|
|
|
. /etc/bashrc.local
|
2013-09-18 04:18:34 +01:00
|
|
|
fi
|
|
|
|
'';
|
|
|
|
|
2017-11-20 11:27:50 +00:00
|
|
|
# Configuration for readline in bash. We use "option default"
|
|
|
|
# priority to allow user override using both .text and .source.
|
|
|
|
environment.etc."inputrc".source = mkOptionDefault ./inputrc;
|
2013-09-18 04:18:34 +01:00
|
|
|
|
2016-06-12 20:03:14 +01:00
|
|
|
users.defaultUserShell = mkDefault pkgs.bashInteractive;
|
2013-09-18 04:18:34 +01:00
|
|
|
|
2013-09-27 00:13:20 +01:00
|
|
|
environment.pathsToLink = optionals cfg.enableCompletion [
|
|
|
|
"/etc/bash_completion.d"
|
|
|
|
"/share/bash-completion"
|
|
|
|
];
|
|
|
|
|
2018-02-28 13:54:52 +00:00
|
|
|
environment.systemPackages = optional cfg.enableCompletion
|
|
|
|
pkgs.nix-bash-completions;
|
|
|
|
|
2013-09-18 04:18:34 +01:00
|
|
|
environment.shells =
|
|
|
|
[ "/run/current-system/sw/bin/bash"
|
|
|
|
"/run/current-system/sw/bin/sh"
|
|
|
|
"${pkgs.bashInteractive}/bin/bash"
|
|
|
|
"${pkgs.bashInteractive}/bin/sh"
|
|
|
|
];
|
2009-05-28 13:06:54 +01:00
|
|
|
|
2013-09-04 12:05:09 +01:00
|
|
|
};
|
2012-07-25 15:53:46 +01:00
|
|
|
|
2009-05-28 13:06:54 +01:00
|
|
|
}
|