bash: format using nixfmt

PR #3609
This commit is contained in:
Lin Jian 2023-01-21 18:27:05 +08:00 committed by Robert Helgesson
parent 8f39c67c4c
commit bb4b25b302
No known key found for this signature in database
GPG key ID: 36BDAA14C2797E89
2 changed files with 69 additions and 85 deletions

1
format
View file

@ -21,7 +21,6 @@ find . -name '*.nix' \
! -path ./modules/lib/default.nix \ ! -path ./modules/lib/default.nix \
! -path ./modules/lib/file-type.nix \ ! -path ./modules/lib/file-type.nix \
! -path ./modules/misc/news.nix \ ! -path ./modules/misc/news.nix \
! -path ./modules/programs/bash.nix \
! -path ./modules/programs/ssh.nix \ ! -path ./modules/programs/ssh.nix \
! -path ./modules/programs/zsh.nix \ ! -path ./modules/programs/zsh.nix \
! -path ./tests/default.nix \ ! -path ./tests/default.nix \

View file

@ -6,16 +6,15 @@ let
cfg = config.programs.bash; cfg = config.programs.bash;
writeBashScript = name: text: pkgs.writeTextFile { writeBashScript = name: text:
inherit name text; pkgs.writeTextFile {
checkPhase = '' inherit name text;
${pkgs.stdenv.shellDryRun} "$target" checkPhase = ''
''; ${pkgs.stdenv.shellDryRun} "$target"
}; '';
};
in in {
{
meta.maintainers = [ maintainers.rycee ]; meta.maintainers = [ maintainers.rycee ];
imports = [ imports = [
@ -70,20 +69,18 @@ in
}; };
historyControl = mkOption { historyControl = mkOption {
type = types.listOf (types.enum [ type =
"erasedups" types.listOf (types.enum [ "erasedups" "ignoredups" "ignorespace" ]);
"ignoredups" default = [ ];
"ignorespace"
]);
default = [];
description = "Controlling how commands are saved on the history list."; description = "Controlling how commands are saved on the history list.";
}; };
historyIgnore = mkOption { historyIgnore = mkOption {
type = types.listOf types.str; type = types.listOf types.str;
default = []; default = [ ];
example = [ "ls" "cd" "exit" ]; example = [ "ls" "cd" "exit" ];
description = "List of commands that should not be saved to the history list."; description =
"List of commands that should not be saved to the history list.";
}; };
shellOptions = mkOption { shellOptions = mkOption {
@ -103,10 +100,7 @@ in
# Warn if closing shell with running jobs. # Warn if closing shell with running jobs.
"checkjobs" "checkjobs"
]; ];
example = [ example = [ "extglob" "-cdspell" ];
"extglob"
"-cdspell"
];
description = '' description = ''
Shell options to set. Prefix an option with Shell options to set. Prefix an option with
<quote><literal>-</literal></quote> to unset. <quote><literal>-</literal></quote> to unset.
@ -114,7 +108,7 @@ in
}; };
sessionVariables = mkOption { sessionVariables = mkOption {
default = {}; default = { };
type = types.attrs; type = types.attrs;
example = { MAILCHECK = 30; }; example = { MAILCHECK = 30; };
description = '' description = ''
@ -123,7 +117,7 @@ in
}; };
shellAliases = mkOption { shellAliases = mkOption {
default = {}; default = { };
type = types.attrsOf types.str; type = types.attrsOf types.str;
example = literalExpression '' example = literalExpression ''
{ {
@ -175,80 +169,71 @@ in
}; };
}; };
config = ( config = let
let aliasesStr = concatStringsSep "\n"
aliasesStr = concatStringsSep "\n" ( (mapAttrsToList (k: v: "alias ${k}=${escapeShellArg v}")
mapAttrsToList (k: v: "alias ${k}=${escapeShellArg v}") cfg.shellAliases cfg.shellAliases);
);
shoptsStr = let shoptsStr = let switch = v: if hasPrefix "-" v then "-u" else "-s";
switch = v: if hasPrefix "-" v then "-u" else "-s"; in concatStringsSep "\n"
in concatStringsSep "\n" ( (map (v: "shopt ${switch v} ${removePrefix "-" v}") cfg.shellOptions);
map (v: "shopt ${switch v} ${removePrefix "-" v}") cfg.shellOptions
);
sessionVarsStr = config.lib.shell.exportAll cfg.sessionVariables; sessionVarsStr = config.lib.shell.exportAll cfg.sessionVariables;
historyControlStr = historyControlStr = concatStringsSep "\n"
concatStringsSep "\n" (mapAttrsToList (n: v: "${n}=${v}") ( (mapAttrsToList (n: v: "${n}=${v}") ({
{ HISTFILESIZE = toString cfg.historyFileSize;
HISTFILESIZE = toString cfg.historyFileSize; HISTSIZE = toString cfg.historySize;
HISTSIZE = toString cfg.historySize; } // optionalAttrs (cfg.historyFile != null) {
} HISTFILE = ''"${cfg.historyFile}"'';
// optionalAttrs (cfg.historyFile != null) { } // optionalAttrs (cfg.historyControl != [ ]) {
HISTFILE = "\"${cfg.historyFile}\""; HISTCONTROL = concatStringsSep ":" cfg.historyControl;
} } // optionalAttrs (cfg.historyIgnore != [ ]) {
// optionalAttrs (cfg.historyControl != []) { HISTIGNORE = escapeShellArg (concatStringsSep ":" cfg.historyIgnore);
HISTCONTROL = concatStringsSep ":" cfg.historyControl; }));
} in mkIf cfg.enable {
// optionalAttrs (cfg.historyIgnore != []) { home.file.".bash_profile".source = writeBashScript "bash_profile" ''
HISTIGNORE = escapeShellArg (concatStringsSep ":" cfg.historyIgnore); # include .profile if it exists
} [[ -f ~/.profile ]] && . ~/.profile
));
in mkIf cfg.enable {
home.file.".bash_profile".source = writeBashScript "bash_profile" ''
# include .profile if it exists
[[ -f ~/.profile ]] && . ~/.profile
# include .bashrc if it exists # include .bashrc if it exists
[[ -f ~/.bashrc ]] && . ~/.bashrc [[ -f ~/.bashrc ]] && . ~/.bashrc
''; '';
# If completion is enabled then make sure it is sourced very early. This # 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 # is to avoid problems if any other initialization code attempts to set up
# completion. # completion.
programs.bash.initExtra = mkIf cfg.enableCompletion (mkOrder 100 '' programs.bash.initExtra = mkIf cfg.enableCompletion (mkOrder 100 ''
if [[ ! -v BASH_COMPLETION_VERSINFO ]]; then if [[ ! -v BASH_COMPLETION_VERSINFO ]]; then
. "${pkgs.bash-completion}/etc/profile.d/bash_completion.sh" . "${pkgs.bash-completion}/etc/profile.d/bash_completion.sh"
fi fi
''); '');
home.file.".profile".source = writeBashScript "profile" '' home.file.".profile".source = writeBashScript "profile" ''
. "${config.home.profileDirectory}/etc/profile.d/hm-session-vars.sh" . "${config.home.profileDirectory}/etc/profile.d/hm-session-vars.sh"
${sessionVarsStr} ${sessionVarsStr}
${cfg.profileExtra} ${cfg.profileExtra}
''; '';
home.file.".bashrc".source = writeBashScript "bashrc" '' home.file.".bashrc".source = writeBashScript "bashrc" ''
${cfg.bashrcExtra} ${cfg.bashrcExtra}
# Commands that should be applied only for interactive shells. # Commands that should be applied only for interactive shells.
[[ $- == *i* ]] || return [[ $- == *i* ]] || return
${historyControlStr} ${historyControlStr}
${shoptsStr} ${shoptsStr}
${aliasesStr} ${aliasesStr}
${cfg.initExtra} ${cfg.initExtra}
''; '';
home.file.".bash_logout" = mkIf (cfg.logoutExtra != "") { home.file.".bash_logout" = mkIf (cfg.logoutExtra != "") {
source = writeBashScript "bash_logout" cfg.logoutExtra; source = writeBashScript "bash_logout" cfg.logoutExtra;
}; };
} };
);
} }