From bb4b25b302dbf0f527f190461b080b5262871756 Mon Sep 17 00:00:00 2001 From: Lin Jian Date: Sat, 21 Jan 2023 18:27:05 +0800 Subject: [PATCH] bash: format using nixfmt PR #3609 --- format | 1 - modules/programs/bash.nix | 153 +++++++++++++++++--------------------- 2 files changed, 69 insertions(+), 85 deletions(-) diff --git a/format b/format index 68751a3f7..ef5528b76 100755 --- a/format +++ b/format @@ -21,7 +21,6 @@ find . -name '*.nix' \ ! -path ./modules/lib/default.nix \ ! -path ./modules/lib/file-type.nix \ ! -path ./modules/misc/news.nix \ - ! -path ./modules/programs/bash.nix \ ! -path ./modules/programs/ssh.nix \ ! -path ./modules/programs/zsh.nix \ ! -path ./tests/default.nix \ diff --git a/modules/programs/bash.nix b/modules/programs/bash.nix index 742087b7d..0c0643da8 100644 --- a/modules/programs/bash.nix +++ b/modules/programs/bash.nix @@ -6,16 +6,15 @@ let cfg = config.programs.bash; - writeBashScript = name: text: pkgs.writeTextFile { - inherit name text; - checkPhase = '' - ${pkgs.stdenv.shellDryRun} "$target" - ''; - }; + writeBashScript = name: text: + pkgs.writeTextFile { + inherit name text; + checkPhase = '' + ${pkgs.stdenv.shellDryRun} "$target" + ''; + }; -in - -{ +in { meta.maintainers = [ maintainers.rycee ]; imports = [ @@ -70,20 +69,18 @@ in }; historyControl = mkOption { - type = types.listOf (types.enum [ - "erasedups" - "ignoredups" - "ignorespace" - ]); - default = []; + 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 = []; + default = [ ]; 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 { @@ -103,10 +100,7 @@ in # Warn if closing shell with running jobs. "checkjobs" ]; - example = [ - "extglob" - "-cdspell" - ]; + example = [ "extglob" "-cdspell" ]; description = '' Shell options to set. Prefix an option with - to unset. @@ -114,7 +108,7 @@ in }; sessionVariables = mkOption { - default = {}; + default = { }; type = types.attrs; example = { MAILCHECK = 30; }; description = '' @@ -123,7 +117,7 @@ in }; shellAliases = mkOption { - default = {}; + default = { }; type = types.attrsOf types.str; example = literalExpression '' { @@ -175,80 +169,71 @@ in }; }; - config = ( - let - aliasesStr = concatStringsSep "\n" ( - mapAttrsToList (k: v: "alias ${k}=${escapeShellArg v}") cfg.shellAliases - ); + 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 - ); + 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; + 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 { - home.file.".bash_profile".source = writeBashScript "bash_profile" '' - # include .profile if it exists - [[ -f ~/.profile ]] && . ~/.profile + 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 { + home.file.".bash_profile".source = writeBashScript "bash_profile" '' + # include .profile if it exists + [[ -f ~/.profile ]] && . ~/.profile - # include .bashrc if it exists - [[ -f ~/.bashrc ]] && . ~/.bashrc - ''; + # include .bashrc if it exists + [[ -f ~/.bashrc ]] && . ~/.bashrc + ''; - # 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 - ''); + # 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" '' - . "${config.home.profileDirectory}/etc/profile.d/hm-session-vars.sh" + home.file.".profile".source = writeBashScript "profile" '' + . "${config.home.profileDirectory}/etc/profile.d/hm-session-vars.sh" - ${sessionVarsStr} + ${sessionVarsStr} - ${cfg.profileExtra} - ''; + ${cfg.profileExtra} + ''; - home.file.".bashrc".source = writeBashScript "bashrc" '' - ${cfg.bashrcExtra} + home.file.".bashrc".source = writeBashScript "bashrc" '' + ${cfg.bashrcExtra} - # Commands that should be applied only for interactive shells. - [[ $- == *i* ]] || return + # Commands that should be applied only for interactive shells. + [[ $- == *i* ]] || return - ${historyControlStr} + ${historyControlStr} - ${shoptsStr} + ${shoptsStr} - ${aliasesStr} + ${aliasesStr} - ${cfg.initExtra} - ''; + ${cfg.initExtra} + ''; - home.file.".bash_logout" = mkIf (cfg.logoutExtra != "") { - source = writeBashScript "bash_logout" cfg.logoutExtra; - }; - } - ); + home.file.".bash_logout" = mkIf (cfg.logoutExtra != "") { + source = writeBashScript "bash_logout" cfg.logoutExtra; + }; + }; }