Skip to content

Commit b41e94c

Browse files
khanelimanauscyber
authored andcommitted
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 <[email protected]>
1 parent a432b91 commit b41e94c

File tree

3 files changed

+35
-7
lines changed

3 files changed

+35
-7
lines changed

modules/programs/zsh/default.nix

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -344,9 +344,25 @@ in
344344
mkIf cfg.enable (
345345
lib.mkMerge [
346346
{
347+
assertions = [
348+
{
349+
assertion = !lib.hasInfix "$" cfg.dotDir;
350+
message = ''
351+
programs.zsh.dotDir cannot contain shell variables as it is used for file creation at build time.
352+
Current dotDir: ${cfg.dotDir}
353+
Consider using an absolute path or home-manager config options instead.
354+
You can replace shell variables with options like:
355+
- config.home.homeDirectory (user's home directory)
356+
- config.xdg.configHome (XDG config directory)
357+
- config.xdg.dataHome (XDG data directory)
358+
- config.xdg.cacheHome (XDG cache directory)
359+
'';
360+
}
361+
];
362+
347363
warnings =
348364
lib.optionals
349-
(cfg.dotDir != homeDir && !lib.hasPrefix "/" cfg.dotDir && !lib.hasPrefix "$" cfg.dotDir)
365+
(cfg.dotDir != homeDir && !lib.hasPrefix "/" cfg.dotDir && !lib.hasInfix "$" cfg.dotDir)
350366
[
351367
''
352368
Using relative paths in programs.zsh.dotDir is deprecated and will be removed in a future release.

tests/modules/programs/zsh/default.nix

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
zsh-dotdir-absolute = import ./dotdir.nix "absolute";
55
zsh-dotdir-default = import ./dotdir.nix "default";
66
zsh-dotdir-relative = import ./dotdir.nix "relative";
7-
zsh-dotdir-xdg-variable = import ./dotdir.nix "xdg-variable";
7+
zsh-dotdir-shell-variable = import ./dotdir.nix "shell-variable";
88
zsh-history-ignore-pattern = ./history-ignore-pattern.nix;
99
zsh-history-path-absolute = import ./history-path.nix "absolute";
1010
zsh-history-path-default = import ./history-path.nix "default";

tests/modules/programs/zsh/dotdir.nix

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ let
1818
subDir
1919
else if case == "default" then
2020
options.programs.zsh.dotDir.default
21-
else if case == "xdg-variable" then
21+
else if case == "shell-variable" then
2222
"\${XDG_CONFIG_HOME:-\$HOME/.config}/zsh"
2323
else
2424
abort "Test condition not provided.";
@@ -48,12 +48,24 @@ in
4848
''
4949
];
5050

51+
test.asserts.assertions.expected = lib.optionals (case == "shell-variable") [
52+
''
53+
programs.zsh.dotDir cannot contain shell variables as it is used for file creation at build time.
54+
Current dotDir: ''${XDG_CONFIG_HOME:-''$HOME/.config}/zsh
55+
Consider using an absolute path or home-manager config options instead.
56+
You can replace shell variables with options like:
57+
- config.home.homeDirectory (user's home directory)
58+
- config.xdg.configHome (XDG config directory)
59+
- config.xdg.dataHome (XDG data directory)
60+
- config.xdg.cacheHome (XDG cache directory)
61+
''
62+
];
63+
5164
nmt.script =
52-
if case == "xdg-variable" then
65+
if case == "shell-variable" then
5366
''
54-
# For XDG variable case, check that shell variables are preserved in the generated shell code
55-
# The fixed implementation should preserve variables without wrapping them in single quotes
56-
assertFileContains home-files/.zshenv 'source ''${XDG_CONFIG_HOME:-''$HOME/.config}/zsh/.zshenv'
67+
# Shell variable case should fail assertion, no files to check
68+
echo "Shell variable case should trigger assertion failure"
5769
''
5870
else
5971
lib.concatStringsSep "\n" [

0 commit comments

Comments
 (0)