2020-10-12 00:22:58 +00:00
|
|
|
{ config, lib, pkgs, ... }:
|
|
|
|
|
|
|
|
with lib;
|
|
|
|
|
|
|
|
let
|
|
|
|
|
|
|
|
cfg = config.programs.bash;
|
|
|
|
|
2023-08-08 10:19:01 +00:00
|
|
|
writeBashScript = name: text:
|
|
|
|
pkgs.writeTextFile {
|
|
|
|
inherit name text;
|
|
|
|
checkPhase = ''
|
|
|
|
${pkgs.stdenv.shellDryRun} "$target"
|
|
|
|
'';
|
|
|
|
};
|
2020-10-12 00:22:58 +00:00
|
|
|
|
2023-08-08 10:19:01 +00:00
|
|
|
in {
|
2020-10-12 00:22:58 +00:00
|
|
|
meta.maintainers = [ maintainers.rycee ];
|
|
|
|
|
|
|
|
imports = [
|
|
|
|
(mkRenamedOptionModule [ "programs" "bash" "enableAutojump" ] [
|
|
|
|
"programs"
|
|
|
|
"autojump"
|
|
|
|
"enable"
|
|
|
|
])
|
|
|
|
];
|
|
|
|
|
|
|
|
options = {
|
|
|
|
programs.bash = {
|
|
|
|
enable = mkEnableOption "GNU Bourne-Again SHell";
|
|
|
|
|
2023-01-10 09:35:00 +00:00
|
|
|
enableCompletion = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = true;
|
|
|
|
description = ''
|
|
|
|
Whether to enable Bash completion for all interactive Bash shells.
|
|
|
|
|
|
|
|
Note, if you use NixOS or nix-darwin and do not have Bash completion
|
|
|
|
enabled in the system configuration, then make sure to add
|
|
|
|
|
2023-08-08 10:19:01 +00:00
|
|
|
```nix
|
2023-01-10 09:35:00 +00:00
|
|
|
environment.pathsToLink = [ "/share/bash-completion" ];
|
2023-08-08 10:19:01 +00:00
|
|
|
```
|
2023-01-10 09:35:00 +00:00
|
|
|
|
|
|
|
to your system configuration to get completion for system packages.
|
2023-08-08 10:19:01 +00:00
|
|
|
Note, the legacy {file}`/etc/bash_completion.d` path is
|
2023-01-10 09:35:00 +00:00
|
|
|
not supported by Home Manager.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2020-10-12 00:22:58 +00:00
|
|
|
historySize = mkOption {
|
|
|
|
type = types.int;
|
|
|
|
default = 10000;
|
|
|
|
description = "Number of history lines to keep in memory.";
|
|
|
|
};
|
|
|
|
|
|
|
|
historyFile = mkOption {
|
2021-07-02 22:36:30 +00:00
|
|
|
type = types.nullOr types.str;
|
|
|
|
default = null;
|
2020-10-12 00:22:58 +00:00
|
|
|
description = "Location of the bash history file.";
|
|
|
|
};
|
|
|
|
|
|
|
|
historyFileSize = mkOption {
|
|
|
|
type = types.int;
|
|
|
|
default = 100000;
|
|
|
|
description = "Number of history lines to keep on file.";
|
|
|
|
};
|
|
|
|
|
|
|
|
historyControl = mkOption {
|
2024-06-04 18:23:39 +00:00
|
|
|
type = types.listOf
|
|
|
|
(types.enum [ "erasedups" "ignoredups" "ignorespace" "ignoreboth" ]);
|
2023-08-08 10:19:01 +00:00
|
|
|
default = [ ];
|
2020-10-12 00:22:58 +00:00
|
|
|
description = "Controlling how commands are saved on the history list.";
|
|
|
|
};
|
|
|
|
|
|
|
|
historyIgnore = mkOption {
|
|
|
|
type = types.listOf types.str;
|
2023-08-08 10:19:01 +00:00
|
|
|
default = [ ];
|
2020-10-12 00:22:58 +00:00
|
|
|
example = [ "ls" "cd" "exit" ];
|
2023-08-08 10:19:01 +00:00
|
|
|
description =
|
|
|
|
"List of commands that should not be saved to the history list.";
|
2020-10-12 00:22:58 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
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"
|
|
|
|
];
|
2023-08-08 10:19:01 +00:00
|
|
|
example = [ "extglob" "-cdspell" ];
|
2021-11-04 16:42:44 +00:00
|
|
|
description = ''
|
|
|
|
Shell options to set. Prefix an option with
|
2023-08-08 10:19:01 +00:00
|
|
|
"`-`" to unset.
|
2021-11-04 16:42:44 +00:00
|
|
|
'';
|
2020-10-12 00:22:58 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
sessionVariables = mkOption {
|
2023-08-08 10:19:01 +00:00
|
|
|
default = { };
|
2020-10-12 00:22:58 +00:00
|
|
|
type = types.attrs;
|
|
|
|
example = { MAILCHECK = 30; };
|
|
|
|
description = ''
|
|
|
|
Environment variables that will be set for the Bash session.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
shellAliases = mkOption {
|
2023-08-08 10:19:01 +00:00
|
|
|
default = { };
|
2020-10-12 00:22:58 +00:00
|
|
|
type = types.attrsOf types.str;
|
2021-11-04 16:42:44 +00:00
|
|
|
example = literalExpression ''
|
2020-10-12 00:22:58 +00:00
|
|
|
{
|
|
|
|
ll = "ls -l";
|
|
|
|
".." = "cd ..";
|
|
|
|
}
|
|
|
|
'';
|
|
|
|
description = ''
|
|
|
|
An attribute set that maps aliases (the top level attribute names in
|
|
|
|
this option) to command strings or directly to build outputs.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
profileExtra = mkOption {
|
|
|
|
default = "";
|
|
|
|
type = types.lines;
|
|
|
|
description = ''
|
|
|
|
Extra commands that should be run when initializing a login
|
|
|
|
shell.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2021-07-02 22:36:30 +00:00
|
|
|
initExtra = mkOption {
|
2020-10-12 00:22:58 +00:00
|
|
|
default = "";
|
|
|
|
type = types.lines;
|
|
|
|
description = ''
|
2021-07-02 22:36:30 +00:00
|
|
|
Extra commands that should be run when initializing an
|
|
|
|
interactive shell.
|
2020-10-12 00:22:58 +00:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2021-07-02 22:36:30 +00:00
|
|
|
bashrcExtra = mkOption {
|
2020-10-12 00:22:58 +00:00
|
|
|
default = "";
|
|
|
|
type = types.lines;
|
|
|
|
description = ''
|
2023-08-08 10:19:01 +00:00
|
|
|
Extra commands that should be placed in {file}`~/.bashrc`.
|
2021-07-02 22:36:30 +00:00
|
|
|
Note that these commands will be run even in non-interactive shells.
|
2020-10-12 00:22:58 +00:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
logoutExtra = mkOption {
|
|
|
|
default = "";
|
|
|
|
type = types.lines;
|
|
|
|
description = ''
|
|
|
|
Extra commands that should be run when logging out of an
|
|
|
|
interactive shell.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2023-08-08 10:19:01 +00:00
|
|
|
config = let
|
|
|
|
aliasesStr = concatStringsSep "\n"
|
|
|
|
(mapAttrsToList (k: v: "alias ${k}=${escapeShellArg v}")
|
|
|
|
cfg.shellAliases);
|
|
|
|
|
|
|
|
shoptsStr = let switch = v: if hasPrefix "-" v then "-u" else "-s";
|
|
|
|
in concatStringsSep "\n"
|
|
|
|
(map (v: "shopt ${switch v} ${removePrefix "-" v}") cfg.shellOptions);
|
|
|
|
|
|
|
|
sessionVarsStr = config.lib.shell.exportAll cfg.sessionVariables;
|
|
|
|
|
|
|
|
historyControlStr = concatStringsSep "\n"
|
|
|
|
(mapAttrsToList (n: v: "${n}=${v}") ({
|
|
|
|
HISTFILESIZE = toString cfg.historyFileSize;
|
|
|
|
HISTSIZE = toString cfg.historySize;
|
|
|
|
} // optionalAttrs (cfg.historyFile != null) {
|
|
|
|
HISTFILE = ''"${cfg.historyFile}"'';
|
|
|
|
} // optionalAttrs (cfg.historyControl != [ ]) {
|
|
|
|
HISTCONTROL = concatStringsSep ":" cfg.historyControl;
|
|
|
|
} // optionalAttrs (cfg.historyIgnore != [ ]) {
|
|
|
|
HISTIGNORE = escapeShellArg (concatStringsSep ":" cfg.historyIgnore);
|
|
|
|
}));
|
|
|
|
in mkIf cfg.enable {
|
2024-06-04 18:23:39 +00:00
|
|
|
home.packages = [ pkgs.bashInteractive ];
|
|
|
|
|
2023-08-08 10:19:01 +00:00
|
|
|
home.file.".bash_profile".source = writeBashScript "bash_profile" ''
|
|
|
|
# include .profile if it exists
|
|
|
|
[[ -f ~/.profile ]] && . ~/.profile
|
|
|
|
|
|
|
|
# include .bashrc if it exists
|
|
|
|
[[ -f ~/.bashrc ]] && . ~/.bashrc
|
|
|
|
'';
|
2020-10-12 00:22:58 +00:00
|
|
|
|
2023-08-08 10:19:01 +00: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
|
|
|
|
'');
|
2023-01-10 09:35:00 +00:00
|
|
|
|
2023-08-08 10:19:01 +00:00
|
|
|
home.file.".profile".source = writeBashScript "profile" ''
|
|
|
|
. "${config.home.profileDirectory}/etc/profile.d/hm-session-vars.sh"
|
2020-10-12 00:22:58 +00:00
|
|
|
|
2023-08-08 10:19:01 +00:00
|
|
|
${sessionVarsStr}
|
2020-10-12 00:22:58 +00:00
|
|
|
|
2023-08-08 10:19:01 +00:00
|
|
|
${cfg.profileExtra}
|
|
|
|
'';
|
2020-10-12 00:22:58 +00:00
|
|
|
|
2023-08-08 10:19:01 +00:00
|
|
|
home.file.".bashrc".source = writeBashScript "bashrc" ''
|
|
|
|
${cfg.bashrcExtra}
|
2021-07-02 22:36:30 +00:00
|
|
|
|
2023-08-08 10:19:01 +00:00
|
|
|
# Commands that should be applied only for interactive shells.
|
|
|
|
[[ $- == *i* ]] || return
|
2021-07-02 22:36:30 +00:00
|
|
|
|
2023-08-08 10:19:01 +00:00
|
|
|
${historyControlStr}
|
2021-07-02 22:36:30 +00:00
|
|
|
|
2023-08-08 10:19:01 +00:00
|
|
|
${shoptsStr}
|
2021-07-02 22:36:30 +00:00
|
|
|
|
2023-08-08 10:19:01 +00:00
|
|
|
${aliasesStr}
|
2021-07-02 22:36:30 +00:00
|
|
|
|
2023-08-08 10:19:01 +00:00
|
|
|
${cfg.initExtra}
|
|
|
|
'';
|
2020-10-12 00:22:58 +00:00
|
|
|
|
2023-08-08 10:19:01 +00:00
|
|
|
home.file.".bash_logout" = mkIf (cfg.logoutExtra != "") {
|
|
|
|
source = writeBashScript "bash_logout" cfg.logoutExtra;
|
|
|
|
};
|
|
|
|
};
|
2020-10-12 00:22:58 +00:00
|
|
|
}
|