Skip to content

Commit

Permalink
Merge pull request #408 from replit/dstewart/bug/python-wrapper-legac…
Browse files Browse the repository at this point in the history
…y-channel

Avoid incorrectly setting LD_LIBRARY_PATH
  • Loading branch information
blast-hardcheese authored Oct 16, 2024
2 parents 458dcf0 + 81418d2 commit e898a8c
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 11 deletions.
10 changes: 5 additions & 5 deletions pkgs/python-utils/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,19 @@ let
ldLibraryPathConvertWrapper = pkgs.writeShellApplication {
inherit name;
text = ''
if test "''${REPLIT_RTLD_LOADER:-}" = "1" && test "''${REPLIT_NIX_CHANNEL:-}" != "legacy"
if test "''${REPLIT_NIX_CHANNEL:-}" = "legacy" || test "''${REPLIT_NIX_CHANNEL:-}" = "stable-21_11"
then
# activate RTLD loader!
export LD_AUDIT="${pkgs.replit-rtld-loader}/rtld_loader.so"
export REPLIT_LD_LIBRARY_PATH=${python-ld-library-path}:''${REPLIT_LD_LIBRARY_PATH:-}
else
export LD_LIBRARY_PATH=${python-ld-library-path}
if [ -n "''${PYTHON_LD_LIBRARY_PATH-}" ]; then
export LD_LIBRARY_PATH=''${PYTHON_LD_LIBRARY_PATH}:$LD_LIBRARY_PATH
fi
if [ -n "''${REPLIT_LD_LIBRARY_PATH-}" ]; then
export LD_LIBRARY_PATH=''${REPLIT_LD_LIBRARY_PATH}:$LD_LIBRARY_PATH
fi
else
# activate RTLD loader!
export LD_AUDIT="${pkgs.replit-rtld-loader}/rtld_loader.so"
export REPLIT_LD_LIBRARY_PATH=${python-ld-library-path}:''${REPLIT_LD_LIBRARY_PATH:-}
fi
exec "${bin}" "$@"
'';
Expand Down
32 changes: 26 additions & 6 deletions pkgs/python-wrapped/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,8 @@ import (
var PythonExePath string
var ReplitPythonLdLibraryPath string

func main() {
if ldAudit := os.Getenv("REPLIT_LD_AUDIT"); ldAudit != "" {
os.Setenv("LD_AUDIT", ldAudit)
}
os.Unsetenv("PYTHONNOUSERSITE")

// Set up environment for legacy nixpkgs
func legacy() {
ldLibraryPath := []string{}
for _, key := range []string{
"REPLIT_LD_LIBRARY_PATH",
Expand All @@ -30,7 +26,31 @@ func main() {
if len(ldLibraryPath) > 0 {
os.Setenv("LD_LIBRARY_PATH", strings.Join(ldLibraryPath, ":"))
}
}

// Set up environment for non-legacy nixpkgs
func modern() {
if ldAudit := os.Getenv("REPLIT_LD_AUDIT"); ldAudit != "" {
os.Setenv("LD_AUDIT", ldAudit)
}
if val, ok := os.LookupEnv("REPLIT_LD_LIBRARY_PATH"); ok && val != "" {
os.Setenv("REPLIT_LD_LIBRARY_PATH", strings.Join([]string{ReplitPythonLdLibraryPath, val}, ":"))
}
}

// returns whether a Nix channel works with RTLD loader
func channelWorksWithRtldLoader(channel string) bool {
return channel != "" && channel != "legacy" && channel != "stable-21_11"
}

func main() {
os.Unsetenv("PYTHONNOUSERSITE")

if val, ok := os.LookupEnv("REPLIT_NIX_CHANNEL"); ok && channelWorksWithRtldLoader(val) {
modern()
} else {
legacy()
}

if err := syscall.Exec(PythonExePath, os.Args, os.Environ()); err != nil {
panic(err)
Expand Down

0 comments on commit e898a8c

Please sign in to comment.