1
0
Fork 0
mirror of https://github.com/nix-community/home-manager synced 2024-12-26 03:39:47 +01:00
home-manager/modules/programs/bash.nix

255 lines
6.8 KiB
Nix
Raw Normal View History

2017-01-07 19:16:26 +01:00
{ config, lib, pkgs, ... }:
with lib;
let
cfg = config.programs.bash;
writeBashScript = name: text: pkgs.writeTextFile {
inherit name text;
checkPhase = ''
${pkgs.stdenv.shellDryRun} "$target"
'';
};
2017-01-07 19:16:26 +01:00
in
{
meta.maintainers = [ maintainers.rycee ];
imports = [
(mkRenamedOptionModule [ "programs" "bash" "enableAutojump" ] [
"programs"
"autojump"
"enable"
])
];
2017-01-07 19:16:26 +01:00
options = {
programs.bash = {
enable = mkEnableOption "GNU Bourne-Again SHell";
2022-08-07 13:10:13 +02:00
enableCompletion = mkOption {
type = types.bool;
default = true;
description = ''
Whether to enable Bash completion for all interactive Bash shells.
</para><para>
Note, if you use NixOS or nix-darwin and do not have Bash completion
enabled in the system configuration, then make sure to add
<programlisting language="nix">
environment.pathsToLink = [ "/share/bash-completion" ];
</programlisting>
to your system configuration to get completion for system packages.
Note, the legacy <filename>/etc/bash_completion.d</filename> path is
not supported by Home Manager.
'';
};
2017-01-07 19:16:26 +01:00
historySize = mkOption {
type = types.int;
default = 10000;
description = "Number of history lines to keep in memory.";
};
2018-01-06 10:10:20 +01:00
historyFile = mkOption {
type = types.nullOr types.str;
default = null;
2018-01-06 10:10:20 +01:00
description = "Location of the bash history file.";
};
2017-01-07 19:16:26 +01:00
historyFileSize = mkOption {
type = types.int;
default = 100000;
description = "Number of history lines to keep on file.";
};
historyControl = mkOption {
type = types.listOf (types.enum [
"erasedups"
"ignoredups"
"ignorespace"
]);
default = [];
description = "Controlling how commands are saved on the history list.";
};
historyIgnore = mkOption {
type = types.listOf types.str;
default = [];
2017-01-15 20:03:55 +01:00
example = [ "ls" "cd" "exit" ];
2017-01-07 19:16:26 +01:00
description = "List of commands that should not be saved to the history list.";
};
shellOptions = mkOption {
type = types.listOf types.str;
default = [
# Append to history file rather than replacing it.
"histappend"
# check the window size after each command and, if
# necessary, update the values of LINES and COLUMNS.
"checkwinsize"
# Extended globbing.
"extglob"
"globstar"
# Warn if closing shell with running jobs.
"checkjobs"
];
2021-08-23 07:28:21 +02:00
example = [
"extglob"
"-cdspell"
];
description = ''
Shell options to set. Prefix an option with
<quote><literal>-</literal></quote> to unset.
'';
2017-01-07 19:16:26 +01:00
};
2017-10-12 15:06:51 +02:00
sessionVariables = mkOption {
default = {};
type = types.attrs;
2017-10-12 15:06:51 +02:00
example = { MAILCHECK = 30; };
description = ''
Environment variables that will be set for the Bash session.
'';
};
2017-01-07 19:16:26 +01:00
shellAliases = mkOption {
default = {};
2019-03-31 13:00:02 +02:00
type = types.attrsOf types.str;
example = literalExpression ''
{
ll = "ls -l";
".." = "cd ..";
}
'';
2017-01-07 19:16:26 +01:00
description = ''
An attribute set that maps aliases (the top level attribute names in
this option) to command strings or directly to build outputs.
2017-01-07 19:16:26 +01:00
'';
};
profileExtra = mkOption {
default = "";
type = types.lines;
2018-01-07 15:04:57 +01:00
description = ''
Extra commands that should be run when initializing a login
shell.
'';
2017-01-07 19:16:26 +01:00
};
initExtra = mkOption {
default = "";
type = types.lines;
description = ''
Extra commands that should be run when initializing an
interactive shell.
'';
};
bashrcExtra = mkOption {
2017-01-07 19:16:26 +01:00
default = "";
type = types.lines;
2018-01-07 15:04:57 +01:00
description = ''
Extra commands that should be placed in <filename>~/.bashrc</filename>.
Note that these commands will be run even in non-interactive shells.
2018-01-07 15:04:57 +01:00
'';
2017-01-07 19:16:26 +01:00
};
2019-07-23 23:55:41 +02:00
logoutExtra = mkOption {
default = "";
type = types.lines;
description = ''
Extra commands that should be run when logging out of an
interactive shell.
'';
};
2017-01-07 19:16:26 +01:00
};
};
config = (
let
aliasesStr = concatStringsSep "\n" (
mapAttrsToList (k: v: "alias ${k}=${escapeShellArg v}") cfg.shellAliases
2017-01-07 19:16:26 +01:00
);
2021-08-23 07:28:21 +02:00
shoptsStr = let
switch = v: if hasPrefix "-" v then "-u" else "-s";
in concatStringsSep "\n" (
map (v: "shopt ${switch v} ${removePrefix "-" v}") cfg.shellOptions
2017-01-07 19:16:26 +01:00
);
sessionVarsStr = config.lib.shell.exportAll cfg.sessionVariables;
2018-01-04 12:22:17 +01:00
historyControlStr =
concatStringsSep "\n" (mapAttrsToList (n: v: "${n}=${v}") (
{
HISTFILESIZE = toString cfg.historyFileSize;
2018-01-06 10:10:20 +01:00
HISTSIZE = toString cfg.historySize;
2018-01-04 12:22:17 +01:00
}
// optionalAttrs (cfg.historyFile != null) {
HISTFILE = "\"${cfg.historyFile}\"";
}
2018-01-04 12:22:17 +01:00
// optionalAttrs (cfg.historyControl != []) {
HISTCONTROL = concatStringsSep ":" cfg.historyControl;
}
// optionalAttrs (cfg.historyIgnore != []) {
HISTIGNORE = concatStringsSep ":" cfg.historyIgnore;
}
));
2017-01-07 19:16:26 +01:00
in mkIf cfg.enable {
home.file.".bash_profile".source = writeBashScript "bash_profile" ''
2017-01-07 19:16:26 +01:00
# include .profile if it exists
[[ -f ~/.profile ]] && . ~/.profile
# include .bashrc if it exists
[[ -f ~/.bashrc ]] && . ~/.bashrc
'';
2022-08-07 13:10:13 +02:00
# If completion is enabled then make sure it is sourced very early. This
# is to avoid problems if any other initialization code attempts to set up
# completion.
programs.bash.initExtra = mkIf cfg.enableCompletion (mkOrder 100 ''
if [[ ! -v BASH_COMPLETION_VERSINFO ]]; then
. "${pkgs.bash-completion}/etc/profile.d/bash_completion.sh"
fi
'');
home.file.".profile".source = writeBashScript "profile" ''
2018-07-29 18:15:50 +02:00
. "${config.home.profileDirectory}/etc/profile.d/hm-session-vars.sh"
${sessionVarsStr}
2017-01-07 19:16:26 +01:00
${cfg.profileExtra}
'';
home.file.".bashrc".source = writeBashScript "bashrc" ''
${cfg.bashrcExtra}
# Commands that should be applied only for interactive shells.
[[ $- == *i* ]] || return
${historyControlStr}
${shoptsStr}
${aliasesStr}
${cfg.initExtra}
2017-01-07 19:16:26 +01:00
'';
2019-07-23 23:55:41 +02:00
home.file.".bash_logout" = mkIf (cfg.logoutExtra != "") {
source = writeBashScript "bash_logout" cfg.logoutExtra;
2019-07-23 23:55:41 +02:00
};
2017-01-07 19:16:26 +01:00
}
);
}