diff --git a/modules/programs/zsh.nix b/modules/programs/zsh.nix index 9b554c491..d0f94509f 100644 --- a/modules/programs/zsh.nix +++ b/modules/programs/zsh.nix @@ -60,6 +60,16 @@ let description = "History file location"; }; + ignorePatterns = mkOption { + type = types.listOf types.str; + default = []; + example = literalExample ''[ "rm *" "pkill *" ]''; + description = '' + Do not enter command lines into the history list + if they match any one of the given shell patterns. + ''; + }; + ignoreDups = mkOption { type = types.bool; default = true; @@ -495,6 +505,7 @@ in # See https://github.com/nix-community/home-manager/issues/177. HISTSIZE="${toString cfg.history.size}" SAVEHIST="${toString cfg.history.save}" + ${optionalString (cfg.history.ignorePatterns != []) "HISTORY_IGNORE=${lib.escapeShellArg "(${lib.concatStringsSep "|" cfg.history.ignorePatterns})"}"} ${if versionAtLeast config.home.stateVersion "20.03" then ''HISTFILE="${cfg.history.path}"'' else ''HISTFILE="$HOME/${cfg.history.path}"''} diff --git a/tests/modules/programs/zsh/default.nix b/tests/modules/programs/zsh/default.nix index 274ba0942..81e0d2d41 100644 --- a/tests/modules/programs/zsh/default.nix +++ b/tests/modules/programs/zsh/default.nix @@ -4,5 +4,6 @@ zsh-history-path-new-custom = ./history-path-new-custom.nix; zsh-history-path-old-default = ./history-path-old-default.nix; zsh-history-path-old-custom = ./history-path-old-custom.nix; + zsh-history-ignore-pattern = ./history-ignore-pattern.nix; zsh-prezto = ./prezto.nix; } diff --git a/tests/modules/programs/zsh/history-ignore-pattern.nix b/tests/modules/programs/zsh/history-ignore-pattern.nix new file mode 100644 index 000000000..ba423efe2 --- /dev/null +++ b/tests/modules/programs/zsh/history-ignore-pattern.nix @@ -0,0 +1,21 @@ +{ config, lib, pkgs, ... }: + +with lib; + +{ + imports = [ + ({ ... }: { config.programs.zsh.history.ignorePatterns = [ "echo *" ]; }) + ({ ... }: { config.programs.zsh.history.ignorePatterns = [ "rm *" ]; }) + ]; + + config = { + programs.zsh.enable = true; + + nixpkgs.overlays = + [ (self: super: { zsh = pkgs.writeScriptBin "dummy-zsh" ""; }) ]; + + nmt.script = '' + assertFileContains home-files/.zshrc "HISTORY_IGNORE='(echo *|rm *)'" + ''; + }; +}