static-nix-shell: only generate preambles when theyre needed

the previous logic would append : to PATH when the binary did not require PATH additions, however that's not a no-op. i think it causes PATH lookups in PWD. guard against that by only outputting preambles when we would expect them to have a side effect
This commit is contained in:
2025-07-23 07:59:01 +00:00
parent 30b0ba1a52
commit 005dddfb8f

View File

@@ -221,12 +221,17 @@ in rec {
inherit pkgsEnv pkgExprs;
interpreter = lib.getExe bash;
postConfigure = ''
shellPreamble='
export PATH=''${PATH:+$PATH:}'"$append_PATH"'
export XDG_DATA_DIRS=''${XDG_DATA_DIRS:+$XDG_DATA_DIRS:}'"$append_XDG_DATA_DIRS"'
'
if [[ -n "$append_PATH" ]]; then
shellPreamble="$shellPreamble"'
export PATH=''${PATH:+$PATH:}'"$append_PATH"
unset append_PATH
fi
if [[ -n "$append_XDG_DATA_DIRS" ]]; then
shellPreamble="$shellPreamble"'
export XDG_DATA_DIRS=''${XDG_DATA_DIRS:+$XDG_DATA_DIRS:}'"$append_XDG_DATA_DIRS"
unset append_XDG_DATA_DIRS
fi
'';
} // (removeAttrs attrs [ "bash" "pkgs" ])
);
@@ -241,6 +246,18 @@ in rec {
inherit pkgsEnv pkgExprs;
interpreter = lib.getExe' oils-for-unix "ysh";
postConfigure = ''
shellPreambleBody=
if [[ -n "$append_PATH" ]]; then
shellPreambleBody="$shellPreambleBody"'
addToSearchPath "PATH" "'"$append_PATH"'"'
unset append_PATH
fi
if [[ -n "$append_XDG_DATA_DIRS" ]]; then
shellPreambleBody="$shellPreambleBody"'
addToSearchPath "XDG_DATA_DIRS" "'"$append_XDG_DATA_DIRS"'"'
unset append_XDG_DATA_DIRS
fi
if [[ -n "$shellPreambleBody" ]]; then
shellPreamble='
{
proc addToSearchPath(envName, appendValue) {
@@ -251,15 +268,12 @@ in rec {
setglobal ENV[envName] = "$value:$appendValue"
}
}
addToSearchPath "PATH" "'"$append_PATH"'"
addToSearchPath "XDG_DATA_DIRS" "'"$append_XDG_DATA_DIRS"'"
'"$shellPreambleBody"'
unset addToSearchPath
}
'
unset append_PATH
unset append_XDG_DATA_DIRS
fi
'';
} // (removeAttrs attrs [ "oils-for-unix" "pkgs" ])
);
@@ -274,12 +288,17 @@ in rec {
inherit pkgsEnv pkgExprs;
interpreter = lib.getExe zsh;
postConfigure = ''
shellPreamble='
export PATH=''${PATH:+$PATH:}'"$append_PATH"'
export XDG_DATA_DIRS=''${XDG_DATA_DIRS:+$XDG_DATA_DIRS:}'"$append_XDG_DATA_DIRS"'
'
unset append_PATH
unset append_XDG_DATA_DIRS
if [[ -n "$append_PATH" ]]; then
shellPreamble="$shellPreamble"'
export PATH=''${PATH:+$PATH:}'"$append_PATH"
unset append_PATH
fi
if [[ -n "$append_XDG_DATA_DIRS" ]]; then
shellPreamble="$shellPreamble"'
export XDG_DATA_DIRS=''${XDG_DATA_DIRS:+$XDG_DATA_DIRS:}'"$append_XDG_DATA_DIRS"
unset append_XDG_DATA_DIRS
fi
'';
} // (removeAttrs attrs [ "pkgs" "zsh" ])
);
@@ -302,15 +321,32 @@ in rec {
addToSearchPath append_PYTHONPATH "$p/${python3.sitePackages}"
done
shellPreamble='
shellPreambleBody=
if [[ -n "$append_PATH" ]]; then
shellPreambleBody="$shellPreambleBody"'
addToSearchPath("PATH", "'"$append_PATH"'")'
unset append_PATH
fi
if [[ -n "$append_XDG_DATA_DIRS" ]]; then
shellPreambleBody="$shellPreambleBody"'
addToSearchPath("XDG_DATA_DIRS", "'"$append_XDG_DATA_DIRS"'")'
unset append_XDG_DATA_DIRS
fi
if [[ -n "$append_PYTHONPATH" ]]; then
shellPreambleBody="$shellPreambleBody"'
addSiteDirs("'"$append_PYTHONPATH"'")'
unset append_PYTHONPATH
fi
if [[ -n "$shellPreambleBody" ]]; then
shellPreamble='
# this preamble is unaware of the tab-style used in the rest of the file,
# so wrap it in a big `exec` to avoid conflicting tab styles
# (and to avoid polluting the globals)
exec("""
import os
import site
def addToSearchPath(envName, appendValue):
import os
value = os.environ.get(envName)
if value is None:
os.environ[envName] = appendValue
@@ -318,18 +354,15 @@ in rec {
os.environ[envName] = value + ":" + appendValue
def addSiteDirs(joinedDirs):
import site
known = site._init_pathinfo()
for p in joinedDirs.split(":"):
known = site.addsitedir(p, known)
addToSearchPath("PATH", "'"$append_PATH"'")
addToSearchPath("XDG_DATA_DIRS", "'"$append_XDG_DATA_DIRS"'")
addSiteDirs("'"$append_PYTHONPATH"'")
'"$shellPreambleBody"'
""", globals={})
'
unset append_PATH
unset append_PYTHONPATH
unset append_XDG_DATA_DIRS
fi
'';
} // (removeAttrs attrs [ "pkgs" "python3" ])
);