Skip to content

Commit f914c2c

Browse files
authored
internal/devbox: use exec.LookPath to find nix in --pure shell (#1869)
This fixes an issue where Devbox cannot find Nix when it's installed to a non-default path (i.e., something other than `/nix`). Fixes #1783.
1 parent fbeceab commit f914c2c

File tree

2 files changed

+4
-35
lines changed

2 files changed

+4
-35
lines changed

internal/devbox/devbox.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1243,12 +1243,12 @@ func (d *Devbox) parseEnvAndExcludeSpecialCases(currentEnv []string) (map[string
12431243
// Finding nix executables in path and passing it through
12441244
// As well as adding devbox itself to PATH
12451245
// Both are needed for devbox commands inside pure shell to work
1246-
includedInPath, err := findNixInPATH(env)
1246+
nixPath, err := exec.LookPath("nix")
12471247
if err != nil {
1248-
return nil, err
1248+
return nil, errors.New("could not find any nix executable in PATH. Make sure Nix is installed and in PATH, then try again")
12491249
}
1250-
includedInPath = append(includedInPath, dotdevboxBinPath(d))
1251-
env["PATH"] = envpath.JoinPathLists(includedInPath...)
1250+
nixPath = filepath.Dir(nixPath)
1251+
env["PATH"] = envpath.JoinPathLists(nixPath, dotdevboxBinPath(d))
12521252
}
12531253
return env, nil
12541254
}

internal/devbox/pure_shell.go

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -4,44 +4,13 @@
44
package devbox
55

66
import (
7-
"fmt"
87
"io/fs"
98
"os"
109
"path/filepath"
11-
"strings"
1210

1311
"github.com/pkg/errors"
14-
"go.jetpack.io/devbox/internal/debug"
15-
"go.jetpack.io/devbox/internal/xdg"
1612
)
1713

18-
// findNixInPATH looks for locations in PATH which nix might exist and
19-
// it returns a slice containing all paths that might contain nix.
20-
// For single-user, and multi-user installation there are default locations
21-
// unless XDG_* env variables are set. So we look for nix in 3 locations
22-
// to see if any of those exist in path.
23-
func findNixInPATH(env map[string]string) ([]string, error) {
24-
defaultSingleUserNixBin := fmt.Sprintf("%s/.nix-profile/bin", env["HOME"])
25-
defaultMultiUserNixBin := "/nix/var/nix/profiles/default/bin"
26-
xdgNixBin := xdg.StateSubpath("/nix/profile/bin")
27-
pathElements := strings.Split(env["PATH"], ":")
28-
debug.Log("path elements: %v", pathElements)
29-
nixBinsInPath := []string{}
30-
for _, el := range pathElements {
31-
if el == xdgNixBin ||
32-
el == defaultSingleUserNixBin ||
33-
el == defaultMultiUserNixBin {
34-
nixBinsInPath = append(nixBinsInPath, el)
35-
}
36-
}
37-
38-
if len(nixBinsInPath) == 0 {
39-
// did not find nix executable in PATH, return error
40-
return nil, errors.New("could not find any nix executable in PATH. Make sure Nix is installed and in PATH, then try again")
41-
}
42-
return nixBinsInPath, nil
43-
}
44-
4514
// Creates a symlink for devbox in .devbox/bin
4615
// so that devbox can be available inside a pure shell
4716
func createDevboxSymlink(d *Devbox) error {

0 commit comments

Comments
 (0)