zsh: improve histfile handling

Previously, a stateVersion check for 20.03 determined whether or not the input to
`programs.zsh.history.path` would be prepended with `$HOME`. However, this was not
communicated in the documentation, which stated the version check determined whether
the default histfile location would be in `programs.zsh.dotDir` or
`home.homeDirectory`.

The current change simplifies matters and brings path handling in-line with that of
the preceding work on dotDir path handling. If a relative path is provided, it is
parsed as being relative to `home.homeDirectory`. Both absolute and relative paths
are supported, and are cleaned before being passed to other functions.

Tests have been rewritten for the new logic, with case handling for reusability.

Signed-off-by: Austin Horstman <khaneliman12@gmail.com>
This commit is contained in:
Kristopher James Kent (kjkent)
2025-01-01 02:48:58 +00:00
committed by Austin Horstman
parent 21399deff2
commit 9fca491587
9 changed files with 51 additions and 65 deletions

View File

@@ -8,9 +8,8 @@ let
cfg = config.programs.zsh;
inherit (lib) literalExpression mkOption types;
inherit (config.home) stateVersion;
relToDotDir = file: (lib.optionalString (cfg.dotDir != null) (cfg.dotDir + "/")) + file;
inherit (import ./lib.nix { inherit config lib; }) dotDirAbs mkAbsPathStr;
in
{
options =
@@ -49,16 +48,9 @@ in
path = mkOption {
type = types.str;
default =
if lib.versionAtLeast stateVersion "20.03" then
"$HOME/.zsh_history"
else
relToDotDir ".zsh_history";
defaultText = literalExpression ''
"$HOME/.zsh_history" if state version 20.03,
"$ZDOTDIR/.zsh_history" otherwise
'';
example = literalExpression ''"''${config.xdg.dataHome}/zsh/zsh_history"'';
default = "${dotDirAbs}/.zsh_history";
defaultText = "`\${config.programs.zsh.dotDir}/.zsh_history`";
example = "`\${config.xdg.dataHome}/zsh/zsh_history`";
description = "History file location";
};
@@ -188,12 +180,7 @@ in
${lib.optionalString (
cfg.history.ignorePatterns != [ ]
) "HISTORY_IGNORE=${lib.escapeShellArg "(${lib.concatStringsSep "|" cfg.history.ignorePatterns})"}"}
${
if lib.versionAtLeast stateVersion "20.03" then
''HISTFILE="${cfg.history.path}"''
else
''HISTFILE="$HOME/${cfg.history.path}"''
}
HISTFILE="${mkAbsPathStr cfg.history.path}"
mkdir -p "$(dirname "$HISTFILE")"
setopt HIST_FCNTL_LOCK

View File

@@ -28,7 +28,7 @@
HISTSIZE="10000"
SAVEHIST="10000"
HISTFILE="$HOME/.zsh_history"
HISTFILE="/home/hm-user/.zsh_history"
mkdir -p "$(dirname "$HISTFILE")"
setopt HIST_FCNTL_LOCK

View File

@@ -5,10 +5,9 @@
zsh-dotdir-default = import ./dotdir.nix "default";
zsh-dotdir-relative = import ./dotdir.nix "relative";
zsh-history-ignore-pattern = ./history-ignore-pattern.nix;
zsh-history-path-new-custom = ./history-path-new-custom.nix;
zsh-history-path-new-default = ./history-path-new-default.nix;
zsh-history-path-old-default = ./history-path-old-default.nix;
zsh-history-path-old-custom = ./history-path-old-custom.nix;
zsh-history-path-absolute = import ./history-path.nix "absolute";
zsh-history-path-default = import ./history-path.nix "default";
zsh-history-path-relative = import ./history-path.nix "relative";
zsh-history-substring-search = ./history-substring-search.nix;
zsh-plugins = ./plugins.nix;
zsh-prezto = ./prezto.nix;

View File

@@ -1,11 +0,0 @@
{
home.stateVersion = "20.03";
programs.zsh = {
enable = true;
history.path = "$HOME/some/directory/zsh_history";
};
nmt.script = ''
assertFileRegex home-files/.zshrc '^HISTFILE="$HOME/some/directory/zsh_history"$'
'';
}

View File

@@ -1,9 +0,0 @@
{
home.stateVersion = "20.03";
programs.zsh.enable = true;
nmt.script = ''
assertFileRegex home-files/.zshrc \
'^HISTFILE="${config.home.homeDirectory}/.zsh_history"$'
'';
}

View File

@@ -1,12 +0,0 @@
{
home.stateVersion = "19.09";
programs.zsh = {
enable = true;
history.path = "some/directory/zsh_history";
};
nmt.script = ''
assertFileRegex home-files/.zshrc \
'^HISTFILE="${config.home.homeDirectory}/some/directory/zsh_history"$'
'';
}

View File

@@ -1,9 +0,0 @@
{
home.stateVersion = "19.03";
programs.zsh.enable = true;
nmt.script = ''
assertFileRegex home-files/.zshrc \
'^HISTFILE="${config.home.homeDirectory}/.zsh_history"$'
'';
}

View File

@@ -0,0 +1,41 @@
case:
{ config, ... }:
let
homeDir = config.home.homeDirectory;
histfileName = ".zsh_history";
customHistRelPath = "some/subdir/${histfileName}";
customHistAbsPath = "${homeDir}/${customHistRelPath}";
# default option isn't exposed by submodule so this won't reflect
# changes to the the module and may need to be updated in future
defaultHistPath = "${homeDir}/${histfileName}";
testPath =
if case == "absolute" then
customHistAbsPath
else if case == "relative" then
customHistRelPath
else if case == "default" then
defaultHistPath
else
abort "Test condition not provided";
expectedPath = if case == "default" then defaultHistPath else customHistAbsPath;
in
{
config = {
programs.zsh = {
enable = true;
history.path = testPath;
};
test.stubs.zsh = { };
nmt.script = ''
assertFileRegex home-files/.zshrc '^HISTFILE="${expectedPath}"$'
'';
};
}

View File

@@ -46,7 +46,7 @@
HISTSIZE="10000"
SAVEHIST="10000"
HISTFILE="$HOME/.zsh_history"
HISTFILE="/home/hm-user/.zsh_history"
mkdir -p "$(dirname "$HISTFILE")"
setopt HIST_FCNTL_LOCK