shellPath returns the path to a shell binary, or error if none found.
(envOpts devopt.EnvOptions)
| 90 | |
| 91 | // shellPath returns the path to a shell binary, or error if none found. |
| 92 | func (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. |