1
0
Fork 0
mirror of https://github.com/nix-community/home-manager.git synced 2025-12-16 22:11:07 +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 ( mkIf cfg.enable (
lib.mkMerge [ 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 = warnings =
lib.optionals 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. 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-absolute = import ./dotdir.nix "absolute";
zsh-dotdir-default = import ./dotdir.nix "default"; zsh-dotdir-default = import ./dotdir.nix "default";
zsh-dotdir-relative = import ./dotdir.nix "relative"; 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-ignore-pattern = ./history-ignore-pattern.nix;
zsh-history-path-absolute = import ./history-path.nix "absolute"; zsh-history-path-absolute = import ./history-path.nix "absolute";
zsh-history-path-default = import ./history-path.nix "default"; zsh-history-path-default = import ./history-path.nix "default";

View file

@ -18,7 +18,7 @@ let
subDir subDir
else if case == "default" then else if case == "default" then
options.programs.zsh.dotDir.default options.programs.zsh.dotDir.default
else if case == "xdg-variable" then else if case == "shell-variable" then
"\${XDG_CONFIG_HOME:-\$HOME/.config}/zsh" "\${XDG_CONFIG_HOME:-\$HOME/.config}/zsh"
else else
abort "Test condition not provided."; abort "Test condition not provided.";
@ -48,12 +48,24 @@ in
'' ''
]; ];
test.asserts.assertions.expected = lib.optionals (case == "shell-variable") [
''
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 = nmt.script =
if case == "xdg-variable" then if case == "shell-variable" then
'' ''
# For XDG variable case, check that shell variables are preserved in the generated shell code # Shell variable case should fail assertion, no files to check
# The fixed implementation should preserve variables without wrapping them in single quotes echo "Shell variable case should trigger assertion failure"
assertFileContains home-files/.zshenv 'source ''${XDG_CONFIG_HOME:-''$HOME/.config}/zsh/.zshenv'
'' ''
else else
lib.concatStringsSep "\n" [ lib.concatStringsSep "\n" [