static-nix-shell: add infrastructure for configuring PATH/XDG_DATA_DIRS without wrapping, in a shell-specific manner

This commit is contained in:
2025-07-19 11:35:09 +00:00
parent d426a9e9e8
commit 96a18c86dd

View File

@@ -87,12 +87,56 @@ in rec {
runtimePrefixes = pkgsEnv';
patchPhase = ''
configurePhase = ''
runHook preConfigure
# generate `extraPaths`, `extraXdgDataDirs` colon-separated paths for use in the build phase.
concatTo runtimePrefixesList runtimePrefixes
append_PATH=
append_XDG_DATA_DIRS=
for p in "''${runtimePrefixesList[@]}"; do
echo "considering if dependency needs to be added to runtime environment(s): $p"
# `addToSearchPath` behaves as no-op if the provided path doesn't exist
addToSearchPath append_PATH "$p/bin"
addToSearchPath append_XDG_DATA_DIRS "$p/share"
done
runHook postConfigure
'';
buildPhase = ''
runHook preBuild
#^ TODO: each shell should use preBuild to compute a `shellPreamble`
die() {
echo "$@"
exit 1
}
substituteInPlace ${srcPath} \
--replace-fail '#!/usr/bin/env nix-shell' '#!${interpreter}' \
--replace-fail \
'#!nix-shell -i ${interpreterName}${pkgsStr}' \
'# nix deps evaluated statically'
$'#!nix-shell -i ${interpreterName}${pkgsStr}\n' \
$'# nix deps evaluated statically\n'"''${shellPreamble:-}"
# if no specialized `shellPreamble` was populated, then inject dependencies via wrapper
if [[ -z "''${shellPreamble:-}" ]]; then
if [[ -n "$append_PATH" ]]; then
makeWrapperArgs+=("--suffix" "PATH" ":" "$append_PATH")
fi
if [[ -n "$append_XDG_DATA_DIRS" ]]; then
makeWrapperArgs+=("--suffix" "XDG_DATA_DIRS" ":" "$append_XDG_DATA_DIRS")
fi
else
if [[ -n "$append_PATH" ]]; then
die "shellPreamble failed to clear 'append_PATH' variable: $append_PATH"
fi
if [[ -n "$append_XDG_DATA_DIRS" ]]; then
die "shellPreamble failed to clear 'append_XDG_DATA_DIRS' variable: $append_XDG_DATA_DIRS"
fi
fi
runHook postBuild
'';
installPhase = ''
@@ -101,35 +145,12 @@ in rec {
mkdir -p $out/bin
mv ${srcPath} $out/bin/${srcPath}
die() {
echo "$@"
exit 1
}
# ensure that all nix-shell references were substituted
(! grep '#![ \t]*nix-shell' $out/bin/${srcPath}) || die 'not all #!nix-shell directives were processed in ${srcPath}'
# ensure that there weren't some trailing deps we *didn't* substitute
grep '^# nix deps evaluated statically$' $out/bin/${srcPath} || die 'trailing characters in nix-shell directive for ${srcPath}'
# wrap the program to place each dependency on PATH and XDG_DATA_DIRS:
# TODO: language-specific hooks to inject this into the source code (for e.g. bash/ysh), instead of an extra wrapper
concatTo runtimePrefixesList runtimePrefixes
extraPaths=
extraXdgDataDirs=
for p in "''${runtimePrefixesList[@]}"; do
echo "considering if dependency needs to be added to runtime environment(s): $p"
# `addToSearchPath` behaves as no-op if the provided path doesn't exist
addToSearchPath extraPaths "$p/bin"
addToSearchPath extraXdgDataDirs "$p/share"
done
if [ -n "$extraPaths" ]; then
makeWrapperArgs+=("--suffix" "PATH" ":" "$extraPaths")
fi
if [ -n "$extraXdgDataDirs" ]; then
makeWrapperArgs+=("--suffix" "XDG_DATA_DIRS" ":" "$extraXdgDataDirs")
fi
if [[ ''${#makeWrapperArgs[@]} != 0 ]]; then
if [[ ''${#makeWrapperArgs[@]} != 0 || ''${#extraMakeWrapperArgs[@]} ]]; then
wrapProgram $out/bin/${srcPath} \
"''${makeWrapperArgs[@]}" \
"''${extraMakeWrapperArgs[@]}"