Home Manager, zsh HIST_STAMPS not taking

Hello, using nix-darwin in conjunction with home manager. I have a flake that I am using to configure my environment.

In viewing the zsh options, I do not see a built-in option for HIST_STAMPS, so that I may define the format of timestamps. As such, I have added the HIST_STAMPS definition into my initExtra block. I also have extended history enabled via the pre-defined option.

However, while my compiled .zshrc file contains this definition, my history file does not reflect the changes. I still have just the # of the commands, but no timestamp.

Curious what I am doing incorrectly. See my zsh definition below. Note that I have removed plugins, functions, etc. as they simply bloat the code block. The configuration compiles just fine (before and after I added HIST_STAMPS).

{ config, pkgs, ...}:

{
  programs.zsh = {
    enable = true;
    autocd = true;
    enableCompletion = true;
    completionInit = "autoload -U compinit; compinit";
    autosuggestion.enable= true;
    syntaxHighlighting = {
      enable = true;
    };
    history = {
      save = 10000000;
      size = 10000000;
      share = true;
      ignoreSpace = true;
      ignorePatterns = [
        "ls *"
        "cd *"
        "pwd *"
        "exit *"
        "cd *"
      ];
      extended = true;
    };
    oh-my-zsh = {
      enable = true;
      plugins = [
        "sudo"
        "direnv"
        ...
      ];
    };
    initExtra = ''
      if [ -f $HOME/.env ]; then
         source $HOME/.env
      fi
      source $HOME/.zshenv

      HIST_STAMPS="yyyy-mm-dd"
      ...
    '';
    envExtra = ''
    '';
    shellAliases = {
      v="nvim";
      ...
    };
    shellGlobalAliases = {
      UUID = "$(uuidgen | tr -d \\n)";
    };
  };

  home.packages = with pkgs; [
    zsh-fast-syntax-highlighting
    nix-zsh-completions
    zsh-nix-shell
  ];
}

After rebuilding:

~ ❯  cat ~/.zshrc | grep HIST_STAMPS                                                                             
HIST_STAMPS="yyyy-mm-dd"

~ ❯  history | tail -n 1
 3809  cat ~/.zshrc | grep HIST_STAMPS

Note: I have also attempted completely exiting my terminal, which I am not sure is necessary as it would be for a new environment variable. Regardless, that did not help either.

Thanks for whatever help you can provide.

Where did you get this HIST_STAMPS from? zsh uses setopt EXTENDED_HISTORY if you want to store timestamps in the command history.

1 Like

I was following this blog post, which is where I initially learned of it: Shell History Is Your Best Productivity Tool | Martin Heinz | Personal Website & Blog

In doing a bit more digging, I found this various references to HIST_STAMPS. However, a closer look makes me think that this is not a zsh option in and of itself, but rather an option provided by oh-my-zsh.

All references included on Github are for the boilerplate included in .zshrc file, specifically in the context of an oh-my-zsh installation: Code search results · GitHub

Given that the author of the original blog posts references that he is using omz, I think he was just unclear that this was an omz option, rather than a zsh one.

Thanks for the response. If I have inaccurately assessed this, more than happy to receive feedback to that end.

With all that in mind, I should still have it configured for timestamps. Home manager shows the option for extended (Appendix A. Home Manager Configuration Options, ctrl/cmd+F for: programs.zsh.history.extended), which I do have configured:

{
  programs.zsh = {
    enable = true;
    autocd = true;
    enableCompletion = true;
    completionInit = "autoload -U compinit; compinit";
    autosuggestion.enable= true;
    syntaxHighlighting = {
      enable = true;
    };
    history = {
      ...
      extended = true;
    };

Further, I appear to have oh-my-zsh installed:

    oh-my-zsh = {
      enable = true;
      plugins = [
        "sudo"
        "direnv"
        ...
      ];
    };

However, maybe my omz install is bugged out:

$ omz version                                                                                                
<detached> ()

Indeed :+1:

HIST_STAMPS

Oh My Zsh provides a wrapper for the history command. You can use this setting to decide whether to show a timestamp for each command in the history output.

Valid values are:

  • "mm/dd/yyyy": for <month>/<day>/<year> (12/31/2020).
  • "dd.mm.yyyy": for <day>.<month>.<year> (31.12.2020).
  • "yyyy-mm-dd": for <year>-<month>-<day> (2020-12-31).
  • Custom value: you can specify another format using the strftime format (for example, "%d/%m/%Y" for 31/12/2020).

Example, if HIST_STAMPS="dd.mm.yyyy":

$ history -5 10001 10.10.2020 13:29 gd 10002 10.10.2020 13:29 z oh 10003 10.10.2020 13:29 gd 10004 10.10.2020 13:54 vsc /home/marc/code/ohmyzsh/wiki 10005 10.10.2020 14:36 history -5

1 Like

I’m not familiar with omz, last time I looked at it was 15 years ago…
But zsh itself supports timestamps, and if you want a specific format you can force that.
Set the option I mentioned earlier and run history -t "%Y-%m-%d" which should be the equivalent of the format you mentioned.

1 Like

Ah ha - extended by itself doesn’t add timestamps.

❯  history -t "%Y-%m-%d %H:%M:%S" | tail -n1
 3850  2024-10-24 14:22:06  history -t "%Y-%m-%d %H:%M:%S"

I can set an alias for history and call it a day.

I appreciate the help!