Skip to content

Commit 9fca491

Browse files
kjkentkhaneliman
authored andcommitted
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 <[email protected]>
1 parent 21399de commit 9fca491

File tree

9 files changed

+51
-65
lines changed

9 files changed

+51
-65
lines changed

modules/programs/zsh/history.nix

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,8 @@ let
88
cfg = config.programs.zsh;
99

1010
inherit (lib) literalExpression mkOption types;
11-
inherit (config.home) stateVersion;
1211

13-
relToDotDir = file: (lib.optionalString (cfg.dotDir != null) (cfg.dotDir + "/")) + file;
12+
inherit (import ./lib.nix { inherit config lib; }) dotDirAbs mkAbsPathStr;
1413
in
1514
{
1615
options =
@@ -49,16 +48,9 @@ in
4948

5049
path = mkOption {
5150
type = types.str;
52-
default =
53-
if lib.versionAtLeast stateVersion "20.03" then
54-
"$HOME/.zsh_history"
55-
else
56-
relToDotDir ".zsh_history";
57-
defaultText = literalExpression ''
58-
"$HOME/.zsh_history" if state version ≥ 20.03,
59-
"$ZDOTDIR/.zsh_history" otherwise
60-
'';
61-
example = literalExpression ''"''${config.xdg.dataHome}/zsh/zsh_history"'';
51+
default = "${dotDirAbs}/.zsh_history";
52+
defaultText = "`\${config.programs.zsh.dotDir}/.zsh_history`";
53+
example = "`\${config.xdg.dataHome}/zsh/zsh_history`";
6254
description = "History file location";
6355
};
6456

@@ -188,12 +180,7 @@ in
188180
${lib.optionalString (
189181
cfg.history.ignorePatterns != [ ]
190182
) "HISTORY_IGNORE=${lib.escapeShellArg "(${lib.concatStringsSep "|" cfg.history.ignorePatterns})"}"}
191-
${
192-
if lib.versionAtLeast stateVersion "20.03" then
193-
''HISTFILE="${cfg.history.path}"''
194-
else
195-
''HISTFILE="$HOME/${cfg.history.path}"''
196-
}
183+
HISTFILE="${mkAbsPathStr cfg.history.path}"
197184
mkdir -p "$(dirname "$HISTFILE")"
198185
199186
setopt HIST_FCNTL_LOCK

tests/modules/programs/zsh/aliases.nix

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
HISTSIZE="10000"
2929
SAVEHIST="10000"
3030
31-
HISTFILE="$HOME/.zsh_history"
31+
HISTFILE="/home/hm-user/.zsh_history"
3232
mkdir -p "$(dirname "$HISTFILE")"
3333
3434
setopt HIST_FCNTL_LOCK

tests/modules/programs/zsh/default.nix

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,9 @@
55
zsh-dotdir-default = import ./dotdir.nix "default";
66
zsh-dotdir-relative = import ./dotdir.nix "relative";
77
zsh-history-ignore-pattern = ./history-ignore-pattern.nix;
8-
zsh-history-path-new-custom = ./history-path-new-custom.nix;
9-
zsh-history-path-new-default = ./history-path-new-default.nix;
10-
zsh-history-path-old-default = ./history-path-old-default.nix;
11-
zsh-history-path-old-custom = ./history-path-old-custom.nix;
8+
zsh-history-path-absolute = import ./history-path.nix "absolute";
9+
zsh-history-path-default = import ./history-path.nix "default";
10+
zsh-history-path-relative = import ./history-path.nix "relative";
1211
zsh-history-substring-search = ./history-substring-search.nix;
1312
zsh-plugins = ./plugins.nix;
1413
zsh-prezto = ./prezto.nix;

tests/modules/programs/zsh/history-path-new-custom.nix

Lines changed: 0 additions & 11 deletions
This file was deleted.

tests/modules/programs/zsh/history-path-new-default.nix

Lines changed: 0 additions & 9 deletions
This file was deleted.

tests/modules/programs/zsh/history-path-old-custom.nix

Lines changed: 0 additions & 12 deletions
This file was deleted.

tests/modules/programs/zsh/history-path-old-default.nix

Lines changed: 0 additions & 9 deletions
This file was deleted.
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
case:
2+
{ config, ... }:
3+
4+
let
5+
homeDir = config.home.homeDirectory;
6+
7+
histfileName = ".zsh_history";
8+
9+
customHistRelPath = "some/subdir/${histfileName}";
10+
customHistAbsPath = "${homeDir}/${customHistRelPath}";
11+
12+
# default option isn't exposed by submodule so this won't reflect
13+
# changes to the the module and may need to be updated in future
14+
defaultHistPath = "${homeDir}/${histfileName}";
15+
16+
testPath =
17+
if case == "absolute" then
18+
customHistAbsPath
19+
else if case == "relative" then
20+
customHistRelPath
21+
else if case == "default" then
22+
defaultHistPath
23+
else
24+
abort "Test condition not provided";
25+
26+
expectedPath = if case == "default" then defaultHistPath else customHistAbsPath;
27+
in
28+
{
29+
config = {
30+
programs.zsh = {
31+
enable = true;
32+
history.path = testPath;
33+
};
34+
35+
test.stubs.zsh = { };
36+
37+
nmt.script = ''
38+
assertFileRegex home-files/.zshrc '^HISTFILE="${expectedPath}"$'
39+
'';
40+
};
41+
}

tests/modules/programs/zsh/zshrc-content-priorities.nix

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
HISTSIZE="10000"
4747
SAVEHIST="10000"
4848
49-
HISTFILE="$HOME/.zsh_history"
49+
HISTFILE="/home/hm-user/.zsh_history"
5050
mkdir -p "$(dirname "$HISTFILE")"
5151
5252
setopt HIST_FCNTL_LOCK

0 commit comments

Comments
 (0)