1
0
Fork 0
mirror of https://github.com/nix-community/home-manager.git synced 2025-12-16 14:01:08 +01:00

zsh: env var assertion for dotdir

Needs to be known at build time. Ensure user doesn't accidentally use
env var in `dotDir`

Signed-off-by: Austin Horstman <khaneliman12@gmail.com>
This commit is contained in:
Austin Horstman 2025-07-28 11:45:50 -05:00
parent 40af7ba06b
commit e52c6c3da3
3 changed files with 35 additions and 7 deletions

View file

@ -344,9 +344,25 @@ in
mkIf cfg.enable (
lib.mkMerge [
{
assertions = [
{
assertion = !lib.hasInfix "$" cfg.dotDir;
message = ''
programs.zsh.dotDir cannot contain shell variables as it is used for file creation at build time.
Current dotDir: ${cfg.dotDir}
Consider using an absolute path or home-manager config options instead.
You can replace shell variables with options like:
- config.home.homeDirectory (user's home directory)
- config.xdg.configHome (XDG config directory)
- config.xdg.dataHome (XDG data directory)
- config.xdg.cacheHome (XDG cache directory)
'';
}
];
warnings =
lib.optionals
(cfg.dotDir != homeDir && !lib.hasPrefix "/" cfg.dotDir && !lib.hasPrefix "$" cfg.dotDir)
(cfg.dotDir != homeDir && !lib.hasPrefix "/" cfg.dotDir && !lib.hasInfix "$" cfg.dotDir)
[
''
Using relative paths in programs.zsh.dotDir is deprecated and will be removed in a future release.

View file

@ -4,7 +4,7 @@
zsh-dotdir-absolute = import ./dotdir.nix "absolute";
zsh-dotdir-default = import ./dotdir.nix "default";
zsh-dotdir-relative = import ./dotdir.nix "relative";
zsh-dotdir-xdg-variable = import ./dotdir.nix "xdg-variable";
zsh-dotdir-shell-variable = import ./dotdir.nix "shell-variable";
zsh-history-ignore-pattern = ./history-ignore-pattern.nix;
zsh-history-path-absolute = import ./history-path.nix "absolute";
zsh-history-path-default = import ./history-path.nix "default";

View file

@ -18,7 +18,7 @@ let
subDir
else if case == "default" then
options.programs.zsh.dotDir.default
else if case == "xdg-variable" then
else if case == "shell-variable" then
"\${XDG_CONFIG_HOME:-\$HOME/.config}/zsh"
else
abort "Test condition not provided.";
@ -48,12 +48,24 @@ in
''
];
nmt.script =
if case == "xdg-variable" then
test.asserts.assertions.expected = lib.optionals (case == "shell-variable") [
''
# For XDG variable case, check that shell variables are preserved in the generated shell code
# The fixed implementation should preserve variables without wrapping them in single quotes
assertFileContains home-files/.zshenv 'source ''${XDG_CONFIG_HOME:-''$HOME/.config}/zsh/.zshenv'
programs.zsh.dotDir cannot contain shell variables as it is used for file creation at build time.
Current dotDir: ''${XDG_CONFIG_HOME:-''$HOME/.config}/zsh
Consider using an absolute path or home-manager config options instead.
You can replace shell variables with options like:
- config.home.homeDirectory (user's home directory)
- config.xdg.configHome (XDG config directory)
- config.xdg.dataHome (XDG data directory)
- config.xdg.cacheHome (XDG cache directory)
''
];
nmt.script =
if case == "shell-variable" then
''
# Shell variable case should fail assertion, no files to check
echo "Shell variable case should trigger assertion failure"
''
else
lib.concatStringsSep "\n" [