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

@ -8,7 +8,7 @@ let
cfg = config.programs.zsh;
relToDotDir = file: (lib.optionalString (cfg.dotDir != null) (cfg.dotDir + "/")) + file;
inherit (import ../lib.nix { inherit config lib; }) pluginsDir;
in
{
imports = [
@ -88,49 +88,45 @@ in
};
};
config =
let
pluginsDir = if cfg.dotDir != null then relToDotDir "plugins" else ".zsh/plugins";
in
lib.mkIf (cfg.plugins != [ ]) {
home.file = lib.foldl' (a: b: a // b) { } (
map (plugin: { "${pluginsDir}/${plugin.name}".source = plugin.src; }) cfg.plugins
);
config = lib.mkIf (cfg.plugins != [ ]) {
home.file = lib.foldl' (a: b: a // b) { } (
map (plugin: { "${pluginsDir}/${plugin.name}".source = plugin.src; }) cfg.plugins
);
programs.zsh = {
# Many plugins require compinit to be called
# but allow the user to opt out.
enableCompletion = lib.mkDefault true;
programs.zsh = {
# Many plugins require compinit to be called
# but allow the user to opt out.
enableCompletion = lib.mkDefault true;
initContent = lib.mkMerge [
(lib.mkOrder 560 (
lib.concatStrings (
map (plugin: ''
path+="$HOME/${pluginsDir}/${plugin.name}"
fpath+="$HOME/${pluginsDir}/${plugin.name}"
${
(lib.optionalString (plugin.completions != [ ]) ''
fpath+=(${
lib.concatMapStringsSep " " (
completion: "\"$HOME/${pluginsDir}/${plugin.name}/${completion}\""
) plugin.completions
})
'')
}
'') cfg.plugins
)
))
initContent = lib.mkMerge [
(lib.mkOrder 560 (
lib.concatStrings (
map (plugin: ''
path+="${pluginsDir}/${plugin.name}"
fpath+="${pluginsDir}/${plugin.name}"
${
(lib.optionalString (plugin.completions != [ ]) ''
fpath+=(${
lib.concatMapStringsSep " " (
completion: "\"${pluginsDir}/${plugin.name}/${completion}\""
) plugin.completions
})
'')
}
'') cfg.plugins
)
))
(lib.mkOrder 900 (
lib.concatStrings (
map (plugin: ''
if [[ -f "$HOME/${pluginsDir}/${plugin.name}/${plugin.file}" ]]; then
source "$HOME/${pluginsDir}/${plugin.name}/${plugin.file}"
fi
'') cfg.plugins
)
))
];
};
(lib.mkOrder 900 (
lib.concatStrings (
map (plugin: ''
if [[ -f "${pluginsDir}/${plugin.name}/${plugin.file}" ]]; then
source "${pluginsDir}/${plugin.name}/${plugin.file}"
fi
'') cfg.plugins
)
))
];
};
};
}

View file

@ -7,9 +7,7 @@
let
inherit (lib) mkOption optionalString types;
relToDotDir =
file:
(lib.optionalString (config.programs.zsh.dotDir != null) (config.programs.zsh.dotDir + "/")) + file;
inherit (import ../lib.nix { inherit config lib; }) dotDirRel;
cfg = config.programs.zsh;
@ -34,7 +32,7 @@ let
custom = mkOption {
default = "";
type = types.str;
example = "$HOME/my_customizations";
example = "\${config.home.homeDirectory}/my_customizations";
description = ''
Path to a custom oh-my-zsh package to override config of
oh-my-zsh. See <https://github.com/robbyrussell/oh-my-zsh/wiki/Customization>
@ -76,7 +74,7 @@ in
packages = [ cfg.oh-my-zsh.package ];
file = {
"${relToDotDir ".zshenv"}".text = ''
"${dotDirRel}/.zshenv".text = ''
ZSH="${cfg.oh-my-zsh.package}/share/oh-my-zsh";
ZSH_CACHE_DIR="${config.xdg.cacheHome}/oh-my-zsh";
'';

View file

@ -14,9 +14,7 @@ let
cfg = config.programs.zsh.prezto;
relToDotDir =
file:
(optionalString (config.programs.zsh.dotDir != null) (config.programs.zsh.dotDir + "/")) + file;
inherit (import ../lib.nix { inherit config lib; }) dotDirRel;
preztoModule = types.submodule {
options = {
@ -425,25 +423,25 @@ in
{
home.packages = [ cfg.package ];
home.file."${relToDotDir ".zprofile"}".text = ''
home.file."${dotDirRel}/.zprofile".text = ''
# Generated by Nix
source ${cfg.package}/share/zsh-prezto/runcoms/zprofile
'';
home.file."${relToDotDir ".zlogin"}".text = ''
home.file."${dotDirRel}/.zlogin".text = ''
# Generated by Nix
source ${cfg.package}/share/zsh-prezto/runcoms/zlogin
'';
home.file."${relToDotDir ".zlogout"}".text = ''
home.file."${dotDirRel}/.zlogout".text = ''
# Generated by Nix
source ${cfg.package}/share/zsh-prezto/runcoms/zlogout
'';
# Using mkAfter to make sure we load Home-Manager's environment
# variables first (see modules/prgrams/zsh.nix)
home.file."${relToDotDir ".zshenv"}".text = lib.mkAfter ''
home.file."${dotDirRel}/.zshenv".text = lib.mkAfter ''
# Generated by Nix
source ${cfg.package}/share/zsh-prezto/runcoms/zshenv
'';
home.file."${relToDotDir ".zpreztorc"}".text = ''
home.file."${dotDirRel}/.zpreztorc".text = ''
# Generated by Nix
${optionalString (cfg.caseSensitive != null) ''
zstyle ':prezto:*:*' case-sensitive '${lib.hm.booleans.yesNo cfg.caseSensitive}'