Skip to content

[pure] Fix bad nixpkgs breaking pure mode #2583

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion internal/devbox/devbox.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ func (d *Devbox) Shell(ctx context.Context, envOpts devopt.EnvOptions) error {
WithShellStartTime(telemetry.ShellStart()),
}

shell, err := NewDevboxShell(d, envOpts, opts...)
shell, err := d.newShell(envOpts, opts...)
if err != nil {
return err
}
Expand Down
12 changes: 6 additions & 6 deletions internal/devbox/shell.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,15 @@ type DevboxShell struct {

type ShellOption func(*DevboxShell)

// NewDevboxShell initializes the DevboxShell struct so it can be used to start a shell environment
// newShell initializes the DevboxShell struct so it can be used to start a shell environment
// for the devbox project.
func NewDevboxShell(devbox *Devbox, envOpts devopt.EnvOptions, opts ...ShellOption) (*DevboxShell, error) {
shPath, err := shellPath(devbox, envOpts)
func (d *Devbox) newShell(envOpts devopt.EnvOptions, opts ...ShellOption) (*DevboxShell, error) {
shPath, err := d.shellPath(envOpts)
if err != nil {
return nil, err
}
sh := initShellBinaryFields(shPath)
sh.devbox = devbox
sh.devbox = d

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

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

cmd := exec.Command(
"nix", "eval", "--raw",
fmt.Sprintf("%s#bashInteractive", nix.FlakeNixpkgs(devbox.cfg.NixPkgsCommitHash())),
fmt.Sprintf("%s#bashInteractive", d.Lockfile().Stdenv().String()),
)
cmd.Args = append(cmd.Args, nix.ExperimentalFlags()...)
out, err := cmd.Output()
Expand Down
60 changes: 60 additions & 0 deletions internal/devbox/shell_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@ import (
"io/fs"
"os"
"path/filepath"
"regexp"
"strings"
"testing"

"github.com/google/go-cmp/cmp"
"go.jetify.com/devbox/internal/devbox/devopt"
"go.jetify.com/devbox/internal/envir"
"go.jetify.com/devbox/internal/shellgen"
)
Expand Down Expand Up @@ -105,3 +107,61 @@ If the new shellrc is correct, you can update the golden file with:
})
}
}

func TestShellPath(t *testing.T) {
tests := []struct {
name string
envOpts devopt.EnvOptions
expected string
env map[string]string
}{
{
name: "pure mode enabled",
envOpts: devopt.EnvOptions{
Pure: true,
},
expected: `^/nix/store/.*/bin/bash$`,
},
{
name: "pure mode disabled",
envOpts: devopt.EnvOptions{
Pure: false,
},
env: map[string]string{
envir.Shell: "/usr/local/bin/bash",
},
expected: "^/usr/local/bin/bash$",
},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
for k, v := range test.env {
t.Setenv(k, v)
}
tmpDir := t.TempDir()
err := InitConfig(tmpDir)
if err != nil {
t.Fatal("Got InitConfig error:", err)
}
d, err := Open(&devopt.Opts{
Dir: tmpDir,
Stderr: os.Stderr,
})
if err != nil {
t.Fatal("Got Open error:", err)
}
gotPath, err := d.shellPath(test.envOpts)
if err != nil {
t.Fatal("Got shellPath error:", err)
}
matched, err := regexp.MatchString(test.expected, gotPath)
if err != nil {
t.Fatal("Got regexp.MatchString error:", err)
}
if !matched {
t.Errorf("Expected shell path %s, but got %s", test.expected, gotPath)
}
})
}
}
Loading