Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[shellenv] filter out buildInput paths from PATH #1091

Merged
merged 5 commits into from
Jun 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions internal/impl/devbox.go
Original file line number Diff line number Diff line change
Expand Up @@ -841,6 +841,23 @@ func (d *Devbox) computeNixEnv(ctx context.Context, usePrintDevEnvCache bool) (m
nixEnvPath := env["PATH"]
debug.Log("PATH after plugins and config is: %s", nixEnvPath)

// We filter out nix store paths of devbox-packages (represented here as buildInputs).
// Motivation: if a user removes a package from their devbox it should no longer
// be available in their environment.
buildInputs := strings.Split(env["buildInputs"], " ")
nixEnvPath = filterPathList(nixEnvPath, func(path string) bool {
savil marked this conversation as resolved.
Show resolved Hide resolved
for _, input := range buildInputs {
// input is of the form: /nix/store/<hash>-<package-name>-<version>
// path is of the form: /nix/store/<hash>-<package-name>-<version>/bin
if strings.TrimSpace(input) != "" && strings.HasPrefix(path, input) {
debug.Log("returning false for path %s and input %s\n", path, input)
return false
}
}
return true
})
debug.Log("PATH after filtering with buildInputs (%v) is: %s", buildInputs, nixEnvPath)

env["PATH"] = JoinPathLists(nixEnvPath, originalPath)
debug.Log("computed environment PATH is: %s", env["PATH"])

Expand Down
2 changes: 1 addition & 1 deletion internal/impl/devbox_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,5 +126,5 @@ func TestComputeNixPathWhenRemoving(t *testing.T) {
path2 := env["PATH"]
assert.NotContains(t, path2, "/tmp/my/path", "path should not contain /tmp/my/path")

assert.NotEqual(t, path, path2, "path should be the same")
assert.NotEqual(t, path, path2, "path should not be the same")
}
10 changes: 10 additions & 0 deletions internal/impl/shell.go
Original file line number Diff line number Diff line change
Expand Up @@ -399,3 +399,13 @@ func JoinPathLists(pathLists ...string) string {
}
return strings.Join(cleaned, string(filepath.ListSeparator))
}

func filterPathList(pathList string, keep func(string) bool) string {
filtered := []string{}
for _, path := range filepath.SplitList(pathList) {
if keep(path) {
filtered = append(filtered, path)
}
}
return strings.Join(filtered, string(filepath.ListSeparator))
}
7 changes: 4 additions & 3 deletions testscripts/run/path.test.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@ exec devbox run echo '$PATH'
! stdout '/some//dirty/../clean/path'
stdout '/some/clean/path'

# Path contains path to installed nix packages
stdout '-which-'
# Path contains path to installed nix packages in the wrappers and nix profile
stdout '.devbox/virtenv/.wrappers/bin'
stdout '.devbox/nix/profile/default/bin'

# Verify PATH is set in correct order: virtual env path nix packages, host path.
path.order '-which-' 'some/clean/path'
path.order '.devbox/virtenv/.wrappers/bin' '/some/clean/path'

# TODO: verify that bashrc file prepends do not prepend before nix paths.