zsh: escape named directory hashes

Named directory hash values are rendered into zsh code. Interpolating them inside double quotes is not enough for values containing quotes, glob syntax, or other shell metacharacters.

Render both the hash name and target with shell escaping so dirHashes entries are emitted as literal zsh arguments.
This commit is contained in:
Austin Horstman
2026-05-18 15:29:10 -05:00
parent d00b16c511
commit 3e17edd5e6
2 changed files with 17 additions and 3 deletions

View File

@@ -395,11 +395,13 @@ in
) cfg.shellAliases
);
dirHashesStr = concatStringsSep "\n" (
lib.mapAttrsToList (k: v: ''hash -d ${k}="${v}"'') cfg.dirHashes
);
# Keep double quotes so existing configs using shell variables like
# $HOME still expand, while escaping chars special inside them.
dirHashesStr = concatStringsSep "\n" (
lib.mapAttrsToList (
k: v: ''hash -d ${lib.escapeShellArg k}="${lib.escape [ "\\" "\"" "`" ] v}"''
) cfg.dirHashes
);
cdpathStr = concatStringsSep " " (map (v: ''"${lib.escape [ "\\" "\"" "`" ] v}"'') cfg.cdpath);
in
mkIf cfg.enable (

View File

@@ -8,6 +8,12 @@
"/tmp/with space"
"/tmp/with[glob]"
];
dirHashes = {
docs = "/tmp/with space";
glob = "/tmp/with[glob]";
home = "$HOME/Documents";
quoted = ''/tmp/with "quotes"'';
};
initContent = lib.mkMerge [
(lib.mkBefore ''
@@ -71,6 +77,12 @@
# Default priority
echo "Default priority content"
# Named Directory Hashes
hash -d docs="/tmp/with space"
hash -d glob="/tmp/with[glob]"
hash -d home="$HOME/Documents"
hash -d quoted="/tmp/with \"quotes\""
zprof
# Low priority (mkAfter)
echo "Low priority content"