2017-01-07 19:16:26 +01:00
|
|
|
{ config, lib, pkgs, ... }:
|
|
|
|
|
|
|
|
with lib;
|
|
|
|
|
|
|
|
let
|
|
|
|
|
|
|
|
cfg = config.programs.bash;
|
|
|
|
|
2023-01-21 11:27:05 +01:00
|
|
|
writeBashScript = name: text:
|
|
|
|
pkgs.writeTextFile {
|
|
|
|
inherit name text;
|
|
|
|
checkPhase = ''
|
|
|
|
${pkgs.stdenv.shellDryRun} "$target"
|
|
|
|
'';
|
|
|
|
};
|
2017-01-07 19:16:26 +01:00
|
|
|
|
2023-01-21 11:27:05 +01:00
|
|
|
in {
|
2017-09-26 23:40:31 +02:00
|
|
|
meta.maintainers = [ maintainers.rycee ];
|
|
|
|
|
2020-09-18 15:07:40 +02:00
|
|
|
imports = [
|
|
|
|
(mkRenamedOptionModule [ "programs" "bash" "enableAutojump" ] [
|
|
|
|
"programs"
|
|
|
|
"autojump"
|
|
|
|
"enable"
|
|
|
|
])
|
|
|
|
];
|
|
|
|
|
2017-01-07 19:16:26 +01:00
|
|
|
options = {
|
|
|
|
programs.bash = {
|
2023-07-02 01:45:18 +02:00
|
|
|
enable = mkEnableOption "GNU Bourne-Again SHell";
|
2017-01-07 19:16:26 +01:00
|
|
|
|
2024-08-18 15:15:31 +02:00
|
|
|
package = mkPackageOption pkgs "bash" { default = "bashInteractive"; };
|
|
|
|
|
2022-08-07 13:10:13 +02:00
|
|
|
enableCompletion = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = true;
|
2023-07-02 01:45:18 +02:00
|
|
|
description = ''
|
2022-08-07 13:10:13 +02:00
|
|
|
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-07-01 01:30:13 +02:00
|
|
|
```nix
|
2022-08-07 13:10:13 +02:00
|
|
|
environment.pathsToLink = [ "/share/bash-completion" ];
|
2023-07-01 01:30:13 +02:00
|
|
|
```
|
2022-08-07 13:10:13 +02:00
|
|
|
|
|
|
|
to your system configuration to get completion for system packages.
|
2023-07-01 01:30:13 +02:00
|
|
|
Note, the legacy {file}`/etc/bash_completion.d` path is
|
2022-08-07 13:10:13 +02:00
|
|
|
not supported by Home Manager.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2017-01-07 19:16:26 +01:00
|
|
|
historySize = mkOption {
|
|
|
|
type = types.int;
|
|
|
|
default = 10000;
|
2023-07-02 01:45:18 +02:00
|
|
|
description = "Number of history lines to keep in memory.";
|
2017-01-07 19:16:26 +01:00
|
|
|
};
|
|
|
|
|
2018-01-06 10:10:20 +01:00
|
|
|
historyFile = mkOption {
|
2021-02-12 19:06:24 +01:00
|
|
|
type = types.nullOr types.str;
|
|
|
|
default = null;
|
2023-07-02 01:45:18 +02:00
|
|
|
description = "Location of the bash history file.";
|
2018-01-06 10:10:20 +01:00
|
|
|
};
|
|
|
|
|
2017-01-07 19:16:26 +01:00
|
|
|
historyFileSize = mkOption {
|
|
|
|
type = types.int;
|
|
|
|
default = 100000;
|
2023-07-02 01:45:18 +02:00
|
|
|
description = "Number of history lines to keep on file.";
|
2017-01-07 19:16:26 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
historyControl = mkOption {
|
2024-05-12 22:33:36 +02:00
|
|
|
type = types.listOf
|
|
|
|
(types.enum [ "erasedups" "ignoredups" "ignorespace" "ignoreboth" ]);
|
2023-01-21 11:27:05 +01:00
|
|
|
default = [ ];
|
2023-07-02 01:45:18 +02:00
|
|
|
description = "Controlling how commands are saved on the history list.";
|
2017-01-07 19:16:26 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
historyIgnore = mkOption {
|
|
|
|
type = types.listOf types.str;
|
2023-01-21 11:27:05 +01:00
|
|
|
default = [ ];
|
2017-01-15 20:03:55 +01:00
|
|
|
example = [ "ls" "cd" "exit" ];
|
2023-07-02 01:45:18 +02:00
|
|
|
description =
|
2023-01-21 11:27:05 +01:00
|
|
|
"List of commands that should not be saved to the history list.";
|
2017-01-07 19:16:26 +01: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-01-21 11:27:05 +01:00
|
|
|
example = [ "extglob" "-cdspell" ];
|
2023-07-02 01:45:18 +02:00
|
|
|
description = ''
|
2021-08-23 07:28:21 +02:00
|
|
|
Shell options to set. Prefix an option with
|
2023-07-01 01:30:13 +02:00
|
|
|
"`-`" to unset.
|
2021-08-23 07:28:21 +02:00
|
|
|
'';
|
2017-01-07 19:16:26 +01:00
|
|
|
};
|
|
|
|
|
2017-10-12 15:06:51 +02:00
|
|
|
sessionVariables = mkOption {
|
2023-01-21 11:27:05 +01:00
|
|
|
default = { };
|
2019-04-27 00:21:18 +02:00
|
|
|
type = types.attrs;
|
2017-10-12 15:06:51 +02:00
|
|
|
example = { MAILCHECK = 30; };
|
2023-07-02 01:45:18 +02:00
|
|
|
description = ''
|
2017-10-12 15:06:51 +02:00
|
|
|
Environment variables that will be set for the Bash session.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2017-01-07 19:16:26 +01:00
|
|
|
shellAliases = mkOption {
|
2023-01-21 11:27:05 +01:00
|
|
|
default = { };
|
2019-03-31 13:00:02 +02:00
|
|
|
type = types.attrsOf types.str;
|
2021-10-09 11:14:08 +02:00
|
|
|
example = literalExpression ''
|
2020-02-26 22:44:54 +01:00
|
|
|
{
|
|
|
|
ll = "ls -l";
|
|
|
|
".." = "cd ..";
|
|
|
|
}
|
|
|
|
'';
|
2023-07-02 01:45:18 +02:00
|
|
|
description = ''
|
2017-01-07 19:16:26 +01:00
|
|
|
An attribute set that maps aliases (the top level attribute names in
|
2018-06-11 04:41:27 +02:00
|
|
|
this option) to command strings or directly to build outputs.
|
2017-01-07 19:16:26 +01:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
profileExtra = mkOption {
|
|
|
|
default = "";
|
|
|
|
type = types.lines;
|
2023-07-02 01:45:18 +02:00
|
|
|
description = ''
|
2018-01-07 15:04:57 +01:00
|
|
|
Extra commands that should be run when initializing a login
|
|
|
|
shell.
|
|
|
|
'';
|
2017-01-07 19:16:26 +01:00
|
|
|
};
|
|
|
|
|
2021-03-31 20:08:24 +02:00
|
|
|
initExtra = mkOption {
|
2018-01-07 15:14:41 +01:00
|
|
|
default = "";
|
|
|
|
type = types.lines;
|
2023-07-02 01:45:18 +02:00
|
|
|
description = ''
|
2021-03-31 20:08:24 +02:00
|
|
|
Extra commands that should be run when initializing an
|
|
|
|
interactive shell.
|
2018-01-07 15:14:41 +01:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2021-03-31 20:08:24 +02:00
|
|
|
bashrcExtra = mkOption {
|
2017-01-07 19:16:26 +01:00
|
|
|
default = "";
|
|
|
|
type = types.lines;
|
2023-07-02 01:45:18 +02:00
|
|
|
description = ''
|
2023-07-01 01:30:13 +02:00
|
|
|
Extra commands that should be placed in {file}`~/.bashrc`.
|
2021-03-31 20:08:24 +02:00
|
|
|
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;
|
2023-07-02 01:45:18 +02:00
|
|
|
description = ''
|
2019-07-23 23:55:41 +02:00
|
|
|
Extra commands that should be run when logging out of an
|
|
|
|
interactive shell.
|
|
|
|
'';
|
|
|
|
};
|
2017-01-07 19:16:26 +01:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2023-01-21 11:27:05 +01: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-08-18 15:15:31 +02:00
|
|
|
home.packages = [ cfg.package ];
|
2024-04-19 09:50:22 +02:00
|
|
|
|
2023-01-21 11:27:05 +01: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
|
|
|
|
'';
|
2017-01-07 19:16:26 +01:00
|
|
|
|
2023-01-21 11:27:05 +01: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
|
|
|
|
'');
|
2022-08-07 13:10:13 +02:00
|
|
|
|
2023-01-21 11:27:05 +01:00
|
|
|
home.file.".profile".source = writeBashScript "profile" ''
|
|
|
|
. "${config.home.profileDirectory}/etc/profile.d/hm-session-vars.sh"
|
2018-01-04 13:29:45 +01:00
|
|
|
|
2023-01-21 11:27:05 +01:00
|
|
|
${sessionVarsStr}
|
2017-01-07 19:16:26 +01:00
|
|
|
|
2023-01-21 11:27:05 +01:00
|
|
|
${cfg.profileExtra}
|
|
|
|
'';
|
2017-01-07 19:16:26 +01:00
|
|
|
|
2023-01-21 11:27:05 +01:00
|
|
|
home.file.".bashrc".source = writeBashScript "bashrc" ''
|
|
|
|
${cfg.bashrcExtra}
|
2021-03-31 20:08:24 +02:00
|
|
|
|
2023-01-21 11:27:05 +01:00
|
|
|
# Commands that should be applied only for interactive shells.
|
|
|
|
[[ $- == *i* ]] || return
|
2021-03-31 20:08:24 +02:00
|
|
|
|
2023-01-21 11:27:05 +01:00
|
|
|
${historyControlStr}
|
2021-03-31 20:08:24 +02:00
|
|
|
|
2023-01-21 11:27:05 +01:00
|
|
|
${shoptsStr}
|
2021-03-31 20:08:24 +02:00
|
|
|
|
2023-01-21 11:27:05 +01:00
|
|
|
${aliasesStr}
|
2021-03-31 20:08:24 +02:00
|
|
|
|
2023-01-21 11:27:05 +01:00
|
|
|
${cfg.initExtra}
|
|
|
|
'';
|
2017-01-07 19:16:26 +01:00
|
|
|
|
2023-01-21 11:27:05 +01:00
|
|
|
home.file.".bash_logout" = mkIf (cfg.logoutExtra != "") {
|
|
|
|
source = writeBashScript "bash_logout" cfg.logoutExtra;
|
|
|
|
};
|
|
|
|
};
|
2017-01-07 19:16:26 +01:00
|
|
|
}
|