MCPcopy Index your code
hub / github.com/jetify-com/devbox / shellPath

Method shellPath

internal/devbox/shell.go:92–138  ·  view source on GitHub ↗

shellPath returns the path to a shell binary, or error if none found.

(envOpts devopt.EnvOptions)

Source from the content-addressed store, hash-verified

90
91// shellPath returns the path to a shell binary, or error if none found.
92func (d *Devbox) shellPath(envOpts devopt.EnvOptions) (path string, err error) {
93 defer func() {
94 if err != nil {
95 path = filepath.Clean(path)
96 }
97 }()
98
99 if !envOpts.Pure {
100 // First, check the SHELL environment variable.
101 path = os.Getenv(envir.Shell)
102 if path != "" {
103 slog.Debug("using SHELL env var for shell binary path", "shell", path)
104 return path, nil
105 }
106 }
107
108 // Second, fallback to using the bash that nix uses by default.
109
110 var bashNixStorePath string // of the form /nix/store/{hash}-bash-{version}/
111
112 cmd := exec.Command(
113 "nix", "eval", "--raw",
114 fmt.Sprintf("%s#bashInteractive", d.Lockfile().Stdenv().String()),
115 )
116 cmd.Args = append(cmd.Args, nix.ExperimentalFlags()...)
117 out, err := cmd.Output()
118 if err != nil {
119 return "", errors.WithStack(err)
120 }
121 bashNixStorePath = string(out)
122
123 // install bashInteractive in nix/store without creating a symlink to local directory (--no-link)
124 cmd = exec.Command("nix", "build", bashNixStorePath, "--no-link")
125 cmd.Args = append(cmd.Args, nix.ExperimentalFlags()...)
126 err = cmd.Run()
127 if err != nil {
128 return "", errors.WithStack(err)
129 }
130
131 if bashNixStorePath != "" {
132 // the output is the raw path to the bash installation in the /nix/store
133 return fmt.Sprintf("%s/bin/bash", bashNixStorePath), nil
134 }
135
136 // Else, return an error
137 return "", ErrNoRecognizableShellFound
138}
139
140// initShellBinaryFields initializes the fields specific to the shell binary that will be used
141// for the devbox shell.

Callers 2

newShellMethod · 0.95
TestShellPathFunction · 0.80

Calls 7

LockfileMethod · 0.95
ExperimentalFlagsFunction · 0.92
CommandMethod · 0.80
StdenvMethod · 0.65
RunMethod · 0.65
StringMethod · 0.45
OutputMethod · 0.45

Tested by 1

TestShellPathFunction · 0.64