2009-05-28 12:06:54 +00:00
|
|
|
# This module defines global configuration for the Bash shell, in
|
|
|
|
# particular /etc/bashrc and /etc/profile.
|
|
|
|
|
2014-04-14 14:26:48 +00:00
|
|
|
{ config, lib, pkgs, ... }:
|
2010-09-13 15:41:38 +00:00
|
|
|
|
2014-04-14 14:26:48 +00:00
|
|
|
with lib;
|
2009-05-28 12:06:54 +00:00
|
|
|
|
2009-05-28 13:17:56 +00:00
|
|
|
let
|
|
|
|
|
2013-09-18 03:18:34 +00:00
|
|
|
cfge = config.environment;
|
2013-01-30 13:05:53 +00:00
|
|
|
|
2013-09-18 03:18:34 +00:00
|
|
|
cfg = config.programs.bash;
|
|
|
|
|
|
|
|
bashCompletion = optionalString cfg.enableCompletion ''
|
2012-10-16 16:23:28 +00: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
|
|
|
|
# $XDG_DATA_DIRS/share/bash-completion/completions/
|
|
|
|
# on demand, so they do not need to be sourced here.
|
2012-10-16 16:23:28 +00:00
|
|
|
if shopt -q progcomp &>/dev/null; then
|
2016-09-28 15:46:29 +00:00
|
|
|
. "${pkgs.bash-completion}/etc/profile.d/bash_completion.sh"
|
2012-10-16 16:23:28 +00:00
|
|
|
nullglobStatus=$(shopt -p nullglob)
|
|
|
|
shopt -s nullglob
|
2012-10-16 17:07:19 +00: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 16:23:28 +00:00
|
|
|
. $m
|
|
|
|
done
|
|
|
|
done
|
|
|
|
eval "$nullglobStatus"
|
2012-10-16 16:41:20 +00:00
|
|
|
unset nullglobStatus p m
|
2012-10-16 16:23:28 +00:00
|
|
|
fi
|
|
|
|
'';
|
|
|
|
|
2013-09-18 03:18:34 +00:00
|
|
|
bashAliases = concatStringsSep "\n" (
|
2013-01-30 13:05:53 +00:00
|
|
|
mapAttrsFlatten (k: v: "alias ${k}='${v}'") cfg.shellAliases
|
2012-07-25 14:53:46 +00:00
|
|
|
);
|
2012-10-16 16:23:28 +00:00
|
|
|
|
2013-09-04 11:05:09 +00:00
|
|
|
in
|
|
|
|
|
|
|
|
{
|
2009-05-28 13:17:56 +00:00
|
|
|
options = {
|
|
|
|
|
2013-09-18 03:18:34 +00:00
|
|
|
programs.bash = {
|
|
|
|
|
2014-04-29 16:57:04 +00:00
|
|
|
/*
|
2013-09-18 03:18:34 +00: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 16:57:04 +00:00
|
|
|
*/
|
2012-10-30 12:33:37 +00:00
|
|
|
|
2013-09-18 03:18:34 +00:00
|
|
|
shellAliases = mkOption {
|
2016-04-18 12:19:38 +00:00
|
|
|
default = config.environment.shellAliases;
|
2013-09-18 03:18:34 +00:00
|
|
|
description = ''
|
|
|
|
Set of aliases for bash shell. See <option>environment.shellAliases</option>
|
|
|
|
for an option format description.
|
|
|
|
'';
|
|
|
|
type = types.attrs; # types.attrsOf types.stringOrPath;
|
|
|
|
};
|
2013-01-30 13:05:53 +00:00
|
|
|
|
2013-09-18 03:18:34 +00:00
|
|
|
shellInit = mkOption {
|
|
|
|
default = "";
|
|
|
|
description = ''
|
|
|
|
Shell script code called during bash shell initialisation.
|
|
|
|
'';
|
|
|
|
type = types.lines;
|
|
|
|
};
|
2009-05-28 13:17:56 +00:00
|
|
|
|
2013-09-18 03:18:34 +00: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 15:15:11 +00:00
|
|
|
PROMPT_COLOR="1;31m"
|
|
|
|
let $UID && PROMPT_COLOR="1;32m"
|
|
|
|
PS1="\n\[\033[$PROMPT_COLOR\][\u@\h:\w]\\$\[\033[0m\] "
|
|
|
|
if test "$TERM" = "xterm"; then
|
|
|
|
PS1="\[\033]2;\h:\u:\w\007\]$PS1"
|
|
|
|
fi
|
2013-09-18 03:18:34 +00:00
|
|
|
fi
|
|
|
|
'';
|
|
|
|
description = ''
|
|
|
|
Shell script code used to initialise the bash prompt.
|
|
|
|
'';
|
|
|
|
type = types.lines;
|
|
|
|
};
|
|
|
|
|
|
|
|
enableCompletion = mkOption {
|
2018-03-29 11:47:33 +00:00
|
|
|
default = true;
|
2013-09-18 03:18:34 +00:00
|
|
|
description = ''
|
|
|
|
Enable Bash completion for all interactive bash shells.
|
|
|
|
'';
|
|
|
|
type = types.bool;
|
|
|
|
};
|
2012-10-16 16:23:28 +00:00
|
|
|
|
2012-11-11 20:46:25 +00:00
|
|
|
};
|
|
|
|
|
2009-05-28 13:17:56 +00:00
|
|
|
};
|
|
|
|
|
2014-04-29 16:57:04 +00:00
|
|
|
config = /* mkIf cfg.enable */ {
|
2011-09-14 18:20:50 +00:00
|
|
|
|
2013-09-18 03:18:34 +00:00
|
|
|
programs.bash = {
|
2013-09-04 11:05:09 +00:00
|
|
|
|
2013-09-18 03:18:34 +00:00
|
|
|
shellInit = ''
|
2018-08-29 16:05:48 +00:00
|
|
|
if [ -z "$__NIXOS_SET_ENVIRONMENT_DONE" ]; then
|
|
|
|
. ${config.system.build.setEnvironment}
|
|
|
|
fi
|
2013-09-04 11:05:09 +00:00
|
|
|
|
2013-09-18 03:18:34 +00:00
|
|
|
${cfge.shellInit}
|
|
|
|
'';
|
2013-09-04 11:05:09 +00:00
|
|
|
|
2013-09-18 03:18:34 +00:00
|
|
|
loginShellInit = cfge.loginShellInit;
|
2013-09-04 11:05:09 +00:00
|
|
|
|
2013-09-18 03:18:34 +00:00
|
|
|
interactiveShellInit = ''
|
2013-09-04 11:05:09 +00:00
|
|
|
# Check the window size after every command.
|
|
|
|
shopt -s checkwinsize
|
|
|
|
|
|
|
|
# Disable hashing (i.e. caching) of command lookups.
|
|
|
|
set +h
|
2013-09-18 03:18:34 +00:00
|
|
|
|
|
|
|
${cfg.promptInit}
|
|
|
|
${bashCompletion}
|
|
|
|
${bashAliases}
|
2013-10-11 09:55:58 +00:00
|
|
|
|
|
|
|
${cfge.interactiveShellInit}
|
2013-09-04 11:05:09 +00:00
|
|
|
'';
|
|
|
|
|
2013-09-18 03:18:34 +00:00
|
|
|
};
|
2013-09-04 11:05:09 +00:00
|
|
|
|
2013-09-18 03:18:34 +00:00
|
|
|
environment.etc."profile".text =
|
2013-09-04 11:05:09 +00:00
|
|
|
''
|
2013-09-18 03:18:34 +00: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 22:25:22 +00:00
|
|
|
# Prevent this file from being sourced by interactive non-login child shells.
|
|
|
|
export __ETC_PROFILE_DONE=1
|
2013-09-18 03:18:34 +00:00
|
|
|
|
2013-09-24 22:25:22 +00:00
|
|
|
${cfg.shellInit}
|
2013-09-18 03:18:34 +00:00
|
|
|
${cfg.loginShellInit}
|
|
|
|
|
|
|
|
# Read system-wide modifications.
|
|
|
|
if test -f /etc/profile.local; then
|
2018-08-29 16:05:53 +00:00
|
|
|
. /etc/profile.local
|
2013-09-18 03:18:34 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
if [ -n "''${BASH_VERSION:-}" ]; then
|
2018-08-29 16:05:53 +00:00
|
|
|
. /etc/bashrc
|
2013-09-18 03:18:34 +00:00
|
|
|
fi
|
2013-09-04 11:05:09 +00:00
|
|
|
'';
|
|
|
|
|
2013-09-18 03:18:34 +00: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 22:25:22 +00: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 03:18:34 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
# We are not always an interactive shell.
|
|
|
|
if [ -n "$PS1" ]; then
|
2018-08-29 16:05:53 +00:00
|
|
|
${cfg.interactiveShellInit}
|
2013-09-18 03:18:34 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
# Read system-wide modifications.
|
|
|
|
if test -f /etc/bashrc.local; then
|
2018-08-29 16:05:53 +00:00
|
|
|
. /etc/bashrc.local
|
2013-09-18 03:18:34 +00: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 03:18:34 +00:00
|
|
|
|
2016-06-12 19:03:14 +00:00
|
|
|
users.defaultUserShell = mkDefault pkgs.bashInteractive;
|
2013-09-18 03:18:34 +00:00
|
|
|
|
2013-09-26 23:13:20 +00: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 03:18:34 +00:00
|
|
|
environment.shells =
|
|
|
|
[ "/run/current-system/sw/bin/bash"
|
|
|
|
"/var/run/current-system/sw/bin/bash"
|
|
|
|
"/run/current-system/sw/bin/sh"
|
|
|
|
"/var/run/current-system/sw/bin/sh"
|
|
|
|
"${pkgs.bashInteractive}/bin/bash"
|
|
|
|
"${pkgs.bashInteractive}/bin/sh"
|
|
|
|
];
|
2009-05-28 12:06:54 +00:00
|
|
|
|
2013-09-04 11:05:09 +00:00
|
|
|
};
|
2012-07-25 14:53:46 +00:00
|
|
|
|
2009-05-28 12:06:54 +00:00
|
|
|
}
|