Skip to content

Commit d92cbb7

Browse files
authored
Clean PATH of nix profile paths in print-dev-env shell/run (#618)
## Summary TSIA ## How was it tested? Verified that the following did _NOT_ include the nix profile path to `/Users/<user>/.nix-profile/bin` ``` DEVBOX_FEATURE_STRICT_RUN=1 ./devbox run -- echo '$PATH' ```
1 parent 3bf9582 commit d92cbb7

File tree

3 files changed

+5
-5
lines changed

3 files changed

+5
-5
lines changed

internal/impl/devbox.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -729,7 +729,7 @@ func (d *Devbox) computeNixEnv() ([]string, error) {
729729
return nil, err
730730
}
731731
nixPath := env["PATH"]
732-
hostPath := os.Getenv("PATH") // TODO: clean the nix paths, after confirming we actually need to do so.
732+
hostPath := nix.CleanEnvPath(os.Getenv("PATH"), os.Getenv("NIX_PROFILES"))
733733

734734
env["PATH"] = fmt.Sprintf("%s:%s:%s:%s", pluginVirtenvPath, nixProfilePath, nixPath, hostPath)
735735

internal/nix/shell.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ func rcfilePath(basename string) string {
173173
func (s *Shell) Run(nixShellFilePath, nixFlakesFilePath string) error {
174174
// Copy the current PATH into nix-shell, but clean and remove some
175175
// directories that are incompatible.
176-
parentPath := cleanEnvPath(os.Getenv("PATH"), os.Getenv("NIX_PROFILES"))
176+
parentPath := CleanEnvPath(os.Getenv("PATH"), os.Getenv("NIX_PROFILES"))
177177

178178
env := append(s.env, os.Environ()...)
179179
env = append(
@@ -572,14 +572,14 @@ func splitNixList(s string) []string {
572572
return split
573573
}
574574

575-
// cleanEnvPath takes a string formatted as a shell PATH and cleans it for
575+
// CleanEnvPath takes a string formatted as a shell PATH and cleans it for
576576
// passing to nix-shell. It does the following rules for each entry:
577577
//
578578
// 1. Applies filepath.Clean.
579579
// 2. Removes the path if it's relative (must begin with '/' and not be '.').
580580
// 3. Removes the path if it's a descendant of a user Nix profile directory
581581
// (the default Nix profile is kept).
582-
func cleanEnvPath(pathEnv string, nixProfilesEnv string) string {
582+
func CleanEnvPath(pathEnv string, nixProfilesEnv string) string {
583583
// Just to be safe, we need to guarantee that the NIX_PROFILES paths
584584
// have been filepath.Clean'ed. The shellrc.tmpl has some commands that
585585
// assume they are.

internal/nix/shell_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ func TestCleanEnvPath(t *testing.T) {
125125
}
126126
for _, test := range tests {
127127
t.Run(test.name, func(t *testing.T) {
128-
got := cleanEnvPath(test.inPath, test.nixProfiles)
128+
got := CleanEnvPath(test.inPath, test.nixProfiles)
129129
if got != test.outPath {
130130
t.Errorf("Got incorrect cleaned PATH.\ngot: %s\nwant: %s", got, test.outPath)
131131
}

0 commit comments

Comments
 (0)