Skip to content

Commit 92ec7c3

Browse files
authored
[pure] Fix bad nixpkgs breaking pure mode (#2583)
## Summary fixes: #2532 Possibly also fixes #2535 but need to test. blame: #2465 cc: @Lagoja ## How was it tested? * unit test * manual testing using --pure flag # Community Contribution License All community contributions in this pull request are licensed to the project maintainers under the terms of the [Apache 2 License](https://www.apache.org/licenses/LICENSE-2.0). By creating this pull request, I represent that I have the right to license the contributions to the project maintainers under the Apache 2 License as stated in the [Community Contribution License](https://github.com/jetify-com/opensource/blob/main/CONTRIBUTING.md#community-contribution-license).
1 parent f1ccc2d commit 92ec7c3

File tree

3 files changed

+67
-7
lines changed

3 files changed

+67
-7
lines changed

internal/devbox/devbox.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ func (d *Devbox) Shell(ctx context.Context, envOpts devopt.EnvOptions) error {
249249
WithShellStartTime(telemetry.ShellStart()),
250250
}
251251

252-
shell, err := NewDevboxShell(d, envOpts, opts...)
252+
shell, err := d.newShell(envOpts, opts...)
253253
if err != nil {
254254
return err
255255
}

internal/devbox/shell.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -69,15 +69,15 @@ type DevboxShell struct {
6969

7070
type ShellOption func(*DevboxShell)
7171

72-
// NewDevboxShell initializes the DevboxShell struct so it can be used to start a shell environment
72+
// newShell initializes the DevboxShell struct so it can be used to start a shell environment
7373
// for the devbox project.
74-
func NewDevboxShell(devbox *Devbox, envOpts devopt.EnvOptions, opts ...ShellOption) (*DevboxShell, error) {
75-
shPath, err := shellPath(devbox, envOpts)
74+
func (d *Devbox) newShell(envOpts devopt.EnvOptions, opts ...ShellOption) (*DevboxShell, error) {
75+
shPath, err := d.shellPath(envOpts)
7676
if err != nil {
7777
return nil, err
7878
}
7979
sh := initShellBinaryFields(shPath)
80-
sh.devbox = devbox
80+
sh.devbox = d
8181

8282
for _, opt := range opts {
8383
opt(sh)
@@ -88,7 +88,7 @@ func NewDevboxShell(devbox *Devbox, envOpts devopt.EnvOptions, opts ...ShellOpti
8888
}
8989

9090
// shellPath returns the path to a shell binary, or error if none found.
91-
func shellPath(devbox *Devbox, envOpts devopt.EnvOptions) (path string, err error) {
91+
func (d *Devbox) shellPath(envOpts devopt.EnvOptions) (path string, err error) {
9292
defer func() {
9393
if err != nil {
9494
path = filepath.Clean(path)
@@ -110,7 +110,7 @@ func shellPath(devbox *Devbox, envOpts devopt.EnvOptions) (path string, err erro
110110

111111
cmd := exec.Command(
112112
"nix", "eval", "--raw",
113-
fmt.Sprintf("%s#bashInteractive", nix.FlakeNixpkgs(devbox.cfg.NixPkgsCommitHash())),
113+
fmt.Sprintf("%s#bashInteractive", d.Lockfile().Stdenv().String()),
114114
)
115115
cmd.Args = append(cmd.Args, nix.ExperimentalFlags()...)
116116
out, err := cmd.Output()

internal/devbox/shell_test.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,12 @@ import (
99
"io/fs"
1010
"os"
1111
"path/filepath"
12+
"regexp"
1213
"strings"
1314
"testing"
1415

1516
"github.com/google/go-cmp/cmp"
17+
"go.jetify.com/devbox/internal/devbox/devopt"
1618
"go.jetify.com/devbox/internal/envir"
1719
"go.jetify.com/devbox/internal/shellgen"
1820
)
@@ -105,3 +107,61 @@ If the new shellrc is correct, you can update the golden file with:
105107
})
106108
}
107109
}
110+
111+
func TestShellPath(t *testing.T) {
112+
tests := []struct {
113+
name string
114+
envOpts devopt.EnvOptions
115+
expected string
116+
env map[string]string
117+
}{
118+
{
119+
name: "pure mode enabled",
120+
envOpts: devopt.EnvOptions{
121+
Pure: true,
122+
},
123+
expected: `^/nix/store/.*/bin/bash$`,
124+
},
125+
{
126+
name: "pure mode disabled",
127+
envOpts: devopt.EnvOptions{
128+
Pure: false,
129+
},
130+
env: map[string]string{
131+
envir.Shell: "/usr/local/bin/bash",
132+
},
133+
expected: "^/usr/local/bin/bash$",
134+
},
135+
}
136+
137+
for _, test := range tests {
138+
t.Run(test.name, func(t *testing.T) {
139+
for k, v := range test.env {
140+
t.Setenv(k, v)
141+
}
142+
tmpDir := t.TempDir()
143+
err := InitConfig(tmpDir)
144+
if err != nil {
145+
t.Fatal("Got InitConfig error:", err)
146+
}
147+
d, err := Open(&devopt.Opts{
148+
Dir: tmpDir,
149+
Stderr: os.Stderr,
150+
})
151+
if err != nil {
152+
t.Fatal("Got Open error:", err)
153+
}
154+
gotPath, err := d.shellPath(test.envOpts)
155+
if err != nil {
156+
t.Fatal("Got shellPath error:", err)
157+
}
158+
matched, err := regexp.MatchString(test.expected, gotPath)
159+
if err != nil {
160+
t.Fatal("Got regexp.MatchString error:", err)
161+
}
162+
if !matched {
163+
t.Errorf("Expected shell path %s, but got %s", test.expected, gotPath)
164+
}
165+
})
166+
}
167+
}

0 commit comments

Comments
 (0)