From e3dc31303276525b867328f75111a23f0aa3674a Mon Sep 17 00:00:00 2001 From: tarzst Date: Sun, 25 Jan 2026 16:43:52 +0530 Subject: [PATCH] zsh: refactor history to use setOptions --- modules/programs/zsh/history.nix | 66 +++++++------------ tests/modules/programs/fabric-ai/zshrc | 24 ++----- tests/modules/programs/zsh/aliases.nix | 25 +++---- .../history-substring-search-expected.zshrc | 24 ++----- .../zsh/smart-formatting-expected.zshrc | 16 +---- .../programs/zsh/zshrc-content-priorities.nix | 24 ++----- 6 files changed, 56 insertions(+), 123 deletions(-) diff --git a/modules/programs/zsh/history.nix b/modules/programs/zsh/history.nix index fcd880c4..d65e710b 100644 --- a/modules/programs/zsh/history.nix +++ b/modules/programs/zsh/history.nix @@ -186,6 +186,30 @@ in '' ]; + programs.zsh.setOptions = + let + historyOptions = { + APPEND_HISTORY = cfg.history.append; + HIST_IGNORE_DUPS = cfg.history.ignoreDups; + HIST_IGNORE_ALL_DUPS = cfg.history.ignoreAllDups; + HIST_SAVE_NO_DUPS = cfg.history.saveNoDups; + HIST_FIND_NO_DUPS = cfg.history.findNoDups; + HIST_IGNORE_SPACE = cfg.history.ignoreSpace; + HIST_EXPIRE_DUPS_FIRST = cfg.history.expireDuplicatesFirst; + SHARE_HISTORY = cfg.history.share; + EXTENDED_HISTORY = cfg.history.extended; + } + // lib.optionalAttrs (cfg.autocd != null) { + inherit (cfg) autocd; + }; + + enabledOpts = lib.filterAttrs (_: enabled: enabled) historyOptions; + disabledOpts = lib.filterAttrs (_: enabled: !enabled) historyOptions; + in + [ "HIST_FCNTL_LOCK" ] + ++ (lib.mapAttrsToList (name: _: name) enabledOpts) + ++ (lib.mapAttrsToList (name: _: "NO_" + name) disabledOpts); + programs.zsh.initContent = lib.mkMerge [ (lib.mkOrder 910 '' # History options should be set in .zshrc and after oh-my-zsh sourcing. @@ -197,48 +221,6 @@ in ) "HISTORY_IGNORE=${lib.escapeShellArg "(${lib.concatStringsSep "|" cfg.history.ignorePatterns})"}"} HISTFILE="${mkShellVarPathStr cfg.history.path}" mkdir -p "$(dirname "$HISTFILE")" - - setopt HIST_FCNTL_LOCK - - ${ - let - historyOptions = { - APPEND_HISTORY = cfg.history.append; - HIST_IGNORE_DUPS = cfg.history.ignoreDups; - HIST_IGNORE_ALL_DUPS = cfg.history.ignoreAllDups; - HIST_SAVE_NO_DUPS = cfg.history.saveNoDups; - HIST_FIND_NO_DUPS = cfg.history.findNoDups; - HIST_IGNORE_SPACE = cfg.history.ignoreSpace; - HIST_EXPIRE_DUPS_FIRST = cfg.history.expireDuplicatesFirst; - SHARE_HISTORY = cfg.history.share; - EXTENDED_HISTORY = cfg.history.extended; - } - // lib.optionalAttrs (cfg.autocd != null) { - inherit (cfg) autocd; - }; - - enabledOpts = lib.filterAttrs (_: enabled: enabled) historyOptions; - disabledOpts = lib.filterAttrs (_: enabled: !enabled) historyOptions; - in - lib.concatStringsSep "\n\n" ( - lib.filter (s: s != "") [ - (lib.optionalString (enabledOpts != { }) '' - # Enabled history options - ${lib.hm.zsh.define "enabled_opts" (lib.mapAttrsToList (name: _: name) enabledOpts)} - for opt in "''${enabled_opts[@]}"; do - setopt "$opt" - done - unset opt enabled_opts'') - (lib.optionalString (disabledOpts != { }) '' - # Disabled history options - ${lib.hm.zsh.define "disabled_opts" (lib.mapAttrsToList (name: _: name) disabledOpts)} - for opt in "''${disabled_opts[@]}"; do - unsetopt "$opt" - done - unset opt disabled_opts'') - ] - ) - } '') (lib.mkIf (cfg.historySubstringSearch.enable or false) ( diff --git a/tests/modules/programs/fabric-ai/zshrc b/tests/modules/programs/fabric-ai/zshrc index 82c9888a..d567f77e 100644 --- a/tests/modules/programs/fabric-ai/zshrc +++ b/tests/modules/programs/fabric-ai/zshrc @@ -14,26 +14,16 @@ SAVEHIST="10000" HISTFILE="/home/hm-user/.zsh_history" mkdir -p "$(dirname "$HISTFILE")" -setopt HIST_FCNTL_LOCK - -# Enabled history options -enabled_opts=( - HIST_IGNORE_DUPS HIST_IGNORE_SPACE SHARE_HISTORY +# Set shell options +set_opts=( + HIST_FCNTL_LOCK HIST_IGNORE_DUPS HIST_IGNORE_SPACE SHARE_HISTORY + NO_APPEND_HISTORY NO_EXTENDED_HISTORY NO_HIST_EXPIRE_DUPS_FIRST + NO_HIST_FIND_NO_DUPS NO_HIST_IGNORE_ALL_DUPS NO_HIST_SAVE_NO_DUPS ) -for opt in "${enabled_opts[@]}"; do +for opt in "${set_opts[@]}"; do setopt "$opt" done -unset opt enabled_opts - -# Disabled history options -disabled_opts=( - APPEND_HISTORY EXTENDED_HISTORY HIST_EXPIRE_DUPS_FIRST HIST_FIND_NO_DUPS - HIST_IGNORE_ALL_DUPS HIST_SAVE_NO_DUPS -) -for opt in "${disabled_opts[@]}"; do - unsetopt "$opt" -done -unset opt disabled_opts +unset opt set_opts diff --git a/tests/modules/programs/zsh/aliases.nix b/tests/modules/programs/zsh/aliases.nix index ee91806a..b1456cf4 100644 --- a/tests/modules/programs/zsh/aliases.nix +++ b/tests/modules/programs/zsh/aliases.nix @@ -31,29 +31,20 @@ HISTFILE="/home/hm-user/.zsh_history" mkdir -p "$(dirname "$HISTFILE")" - setopt HIST_FCNTL_LOCK - - # Enabled history options - enabled_opts=( - HIST_IGNORE_DUPS HIST_IGNORE_SPACE SHARE_HISTORY + # Set shell options + set_opts=( + HIST_FCNTL_LOCK HIST_IGNORE_DUPS HIST_IGNORE_SPACE SHARE_HISTORY + NO_APPEND_HISTORY NO_EXTENDED_HISTORY NO_HIST_EXPIRE_DUPS_FIRST + NO_HIST_FIND_NO_DUPS NO_HIST_IGNORE_ALL_DUPS NO_HIST_SAVE_NO_DUPS ) - for opt in "''${enabled_opts[@]}"; do + for opt in "''${set_opts[@]}"; do setopt "$opt" done - unset opt enabled_opts - - # Disabled history options - disabled_opts=( - APPEND_HISTORY EXTENDED_HISTORY HIST_EXPIRE_DUPS_FIRST HIST_FIND_NO_DUPS - HIST_IGNORE_ALL_DUPS HIST_SAVE_NO_DUPS - ) - for opt in "''${disabled_opts[@]}"; do - unsetopt "$opt" - done - unset opt disabled_opts + unset opt set_opts alias -- test1=alias alias -- test2=alias2 alias -g -- global=test''} + ''; } diff --git a/tests/modules/programs/zsh/history-substring-search-expected.zshrc b/tests/modules/programs/zsh/history-substring-search-expected.zshrc index e95c88c4..7b234010 100644 --- a/tests/modules/programs/zsh/history-substring-search-expected.zshrc +++ b/tests/modules/programs/zsh/history-substring-search-expected.zshrc @@ -14,26 +14,16 @@ SAVEHIST="10000" HISTFILE="/home/hm-user/.zsh_history" mkdir -p "$(dirname "$HISTFILE")" -setopt HIST_FCNTL_LOCK - -# Enabled history options -enabled_opts=( - HIST_IGNORE_DUPS HIST_IGNORE_SPACE SHARE_HISTORY +# Set shell options +set_opts=( + HIST_FCNTL_LOCK HIST_IGNORE_DUPS HIST_IGNORE_SPACE SHARE_HISTORY + NO_APPEND_HISTORY NO_EXTENDED_HISTORY NO_HIST_EXPIRE_DUPS_FIRST + NO_HIST_FIND_NO_DUPS NO_HIST_IGNORE_ALL_DUPS NO_HIST_SAVE_NO_DUPS ) -for opt in "${enabled_opts[@]}"; do +for opt in "${set_opts[@]}"; do setopt "$opt" done -unset opt enabled_opts - -# Disabled history options -disabled_opts=( - APPEND_HISTORY EXTENDED_HISTORY HIST_EXPIRE_DUPS_FIRST HIST_FIND_NO_DUPS - HIST_IGNORE_ALL_DUPS HIST_SAVE_NO_DUPS -) -for opt in "${disabled_opts[@]}"; do - unsetopt "$opt" -done -unset opt disabled_opts +unset opt set_opts source @zsh-history-substring-search@/share/zsh-history-substring-search/zsh-history-substring-search.zsh bindkey "^[[A" history-substring-search-up diff --git a/tests/modules/programs/zsh/smart-formatting-expected.zshrc b/tests/modules/programs/zsh/smart-formatting-expected.zshrc index 3f5b1e33..931c685f 100644 --- a/tests/modules/programs/zsh/smart-formatting-expected.zshrc +++ b/tests/modules/programs/zsh/smart-formatting-expected.zshrc @@ -14,21 +14,11 @@ SAVEHIST="50000" HISTFILE="/home/hm-user/.zsh_history" mkdir -p "$(dirname "$HISTFILE")" -setopt HIST_FCNTL_LOCK - -# Disabled history options -disabled_opts=( - APPEND_HISTORY EXTENDED_HISTORY HIST_EXPIRE_DUPS_FIRST HIST_FIND_NO_DUPS - HIST_IGNORE_ALL_DUPS HIST_IGNORE_DUPS HIST_IGNORE_SPACE HIST_SAVE_NO_DUPS - SHARE_HISTORY -) -for opt in "${disabled_opts[@]}"; do - unsetopt "$opt" -done -unset opt disabled_opts - # Set shell options set_opts=( + HIST_FCNTL_LOCK NO_APPEND_HISTORY NO_EXTENDED_HISTORY + NO_HIST_EXPIRE_DUPS_FIRST NO_HIST_FIND_NO_DUPS NO_HIST_IGNORE_ALL_DUPS + NO_HIST_IGNORE_DUPS NO_HIST_IGNORE_SPACE NO_HIST_SAVE_NO_DUPS NO_SHARE_HISTORY AUTO_LIST AUTO_PARAM_SLASH AUTO_PUSHD ALWAYS_TO_END CORRECT HIST_FCNTL_LOCK HIST_VERIFY INTERACTIVE_COMMENTS MENU_COMPLETE PUSHD_IGNORE_DUPS PUSHD_TO_HOME PUSHD_SILENT NOTIFY PROMPT_SUBST MULTIOS NOFLOWCONTROL NO_CORRECT_ALL diff --git a/tests/modules/programs/zsh/zshrc-content-priorities.nix b/tests/modules/programs/zsh/zshrc-content-priorities.nix index fa149291..39d22f2a 100644 --- a/tests/modules/programs/zsh/zshrc-content-priorities.nix +++ b/tests/modules/programs/zsh/zshrc-content-priorities.nix @@ -49,26 +49,16 @@ HISTFILE="/home/hm-user/.zsh_history" mkdir -p "$(dirname "$HISTFILE")" - setopt HIST_FCNTL_LOCK - - # Enabled history options - enabled_opts=( - HIST_IGNORE_DUPS HIST_IGNORE_SPACE SHARE_HISTORY + # Set shell options + set_opts=( + HIST_FCNTL_LOCK HIST_IGNORE_DUPS HIST_IGNORE_SPACE SHARE_HISTORY + NO_APPEND_HISTORY NO_EXTENDED_HISTORY NO_HIST_EXPIRE_DUPS_FIRST + NO_HIST_FIND_NO_DUPS NO_HIST_IGNORE_ALL_DUPS NO_HIST_SAVE_NO_DUPS ) - for opt in "''${enabled_opts[@]}"; do + for opt in "''${set_opts[@]}"; do setopt "$opt" done - unset opt enabled_opts - - # Disabled history options - disabled_opts=( - APPEND_HISTORY EXTENDED_HISTORY HIST_EXPIRE_DUPS_FIRST HIST_FIND_NO_DUPS - HIST_IGNORE_ALL_DUPS HIST_SAVE_NO_DUPS - ) - for opt in "''${disabled_opts[@]}"; do - unsetopt "$opt" - done - unset opt disabled_opts + unset opt set_opts # Default priority echo "Default priority content"