zsh: improve dotDir handling

Previously, `config.programs.zsh.dotDir` prepended strings with `$HOME`.
This caused issues like nix-community#5100, where `$HOME` is
inconsistently resolved in time for the evaluation of the option. The handling
of this variable is also inconsistent with how paths are handled elsewhere,
including within the same module, where `config.programs.zsh.history.path`
does not mutate the supplied string.

To preserve backwards compatibility, this change prepends
`config.home.homeDirectory` to relative paths, while assigning absolute paths
unchanged. Tests for both cases are added.

Signed-off-by: Austin Horstman <khaneliman12@gmail.com>
This commit is contained in:
Kristopher James Kent (kjkent) 2024-12-31 19:53:48 +00:00 committed by Austin Horstman
parent ef8a9767fc
commit 21399deff2
11 changed files with 173 additions and 82 deletions

View file

@ -1,16 +1,19 @@
{
zsh-abbr = ./zsh-abbr.nix;
zsh-aliases = ./aliases.nix;
zsh-dotdir-absolute = import ./dotdir.nix "absolute";
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-custom = ./history-path-old-custom.nix;
zsh-history-path-old-default = ./history-path-old-default.nix;
zsh-history-path-old-custom = ./history-path-old-custom.nix;
zsh-history-substring-search = ./history-substring-search.nix;
zsh-plugins = ./plugins.nix;
zsh-prezto = ./prezto.nix;
zsh-zprof = ./zprof.nix;
zsh-session-variables = ./session-variables.nix;
zsh-syntax-highlighting = ./syntax-highlighting.nix;
zsh-zprof = ./zprof.nix;
zshrc-contents-priorities = ./zshrc-content-priorities.nix;
}

View file

@ -0,0 +1,52 @@
case:
{
config,
lib,
options,
...
}:
let
home = config.home.homeDirectory;
dotDir =
let
subDir = "subdir/subdir2";
in
if case == "absolute" then
"${home}/${subDir}"
else if case == "relative" then
subDir
else if case == "default" then
options.programs.zsh.dotDir.default
else
abort "Test condition not provided.";
absDotDir = lib.optionalString (!lib.hasPrefix home dotDir) "${home}/" + dotDir;
relDotDir = lib.removePrefix home dotDir;
in
{
config = {
programs.zsh = {
enable = true;
inherit dotDir;
};
test.stubs.zsh = { };
nmt.script = lib.concatStringsSep "\n" [
# check dotDir entrypoint exists
"assertFileExists home-files/${relDotDir}/.zshenv"
# for non-default dotDir only:
(lib.optionalString (case != "default") ''
# check .zshenv in homeDirectory sources .zshenv in dotDir
assertFileRegex home-files/.zshenv \
"source [\"']\?${absDotDir}/.zshenv[\"']\?"
# check that .zshenv in dotDir exports ZDOTDIR
assertFileRegex home-files/${relDotDir}/.zshenv \
"export ZDOTDIR=[\"']\?${absDotDir}[\"']\?"
'')
];
};
}

View file

@ -3,6 +3,7 @@
programs.zsh.enable = true;
nmt.script = ''
assertFileRegex home-files/.zshrc '^HISTFILE="$HOME/.zsh_history"$'
assertFileRegex home-files/.zshrc \
'^HISTFILE="${config.home.homeDirectory}/.zsh_history"$'
'';
}

View file

@ -6,6 +6,7 @@
};
nmt.script = ''
assertFileRegex home-files/.zshrc '^HISTFILE="$HOME/some/directory/zsh_history"$'
assertFileRegex home-files/.zshrc \
'^HISTFILE="${config.home.homeDirectory}/some/directory/zsh_history"$'
'';
}

View file

@ -3,6 +3,7 @@
programs.zsh.enable = true;
nmt.script = ''
assertFileRegex home-files/.zshrc '^HISTFILE="$HOME/.zsh_history"$'
assertFileRegex home-files/.zshrc \
'^HISTFILE="${config.home.homeDirectory}/.zsh_history"$'
'';
}

View file

@ -23,9 +23,9 @@ in
test.stubs.zsh = { };
nmt.script = ''
assertFileRegex home-files/.zshrc '^path+="$HOME/.zsh/plugins/mockPlugin"$'
assertFileRegex home-files/.zshrc '^fpath+="$HOME/.zsh/plugins/mockPlugin"$'
assertFileRegex home-files/.zshrc '^fpath+=("$HOME/.zsh/plugins/mockPlugin/share/zsh/site-functions" "$HOME/.zsh/plugins/mockPlugin/share/zsh/vendor-completions")$'
assertFileRegex home-files/.zshrc '^path+="/home/hm-user/.zsh/plugins/mockPlugin"$'
assertFileRegex home-files/.zshrc '^fpath+="/home/hm-user/.zsh/plugins/mockPlugin"$'
assertFileRegex home-files/.zshrc '^fpath+=("/home/hm-user/.zsh/plugins/mockPlugin/share/zsh/site-functions" "/home/hm-user/.zsh/plugins/mockPlugin/share/zsh/vendor-completions")$'
'';
};
}