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