Skip to content

Commit b311788

Browse files
authored
[nixpkgs] Use lock.stdenv instead of NixPkgsCommitHash (#2584)
## Summary `NixPkgsCommitHash` is no longer defaulted so it's best to use lockfile.Stdenv to get the nixpkgs for patching and telemetry. This ensures we use the correct resolved version at all times. (also I think, but have not confirmed that before this change, `newGlibcPatchFlake` will not work is nixpkgs hash is not specified in devbox.json) ## How was it tested? unit tests ## 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 92ec7c3 commit b311788

File tree

5 files changed

+107
-4
lines changed

5 files changed

+107
-4
lines changed

internal/boxcli/midcobra/telemetry.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,5 +103,5 @@ func getPackagesAndCommitHash(c *cobra.Command) ([]string, string) {
103103
}
104104

105105
return box.AllPackageNamesIncludingRemovedTriggerPackages(),
106-
box.Config().NixPkgsCommitHash()
106+
box.Lockfile().Stdenv().Rev
107107
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package midcobra
2+
3+
import (
4+
"testing"
5+
6+
"github.com/spf13/cobra"
7+
"go.jetify.com/devbox/internal/devbox"
8+
"go.jetify.com/devbox/internal/devbox/devopt"
9+
)
10+
11+
func TestGetPackagesAndCommitHash(t *testing.T) {
12+
dir := t.TempDir()
13+
err := devbox.InitConfig(dir)
14+
if err != nil {
15+
t.Errorf("Expected no error, got %s", err)
16+
}
17+
box, err := devbox.Open(&devopt.Opts{
18+
Dir: dir,
19+
})
20+
// Create a mock cobra command
21+
cmd := &cobra.Command{
22+
Use: "test",
23+
Run: func(cmd *cobra.Command, args []string) {},
24+
}
25+
26+
// Add a mock flag to the command
27+
cmd.Flags().String("config", "", "config file")
28+
if err := cmd.Flags().Set("config", dir); err != nil {
29+
t.Errorf("Expected no error, got %s", err)
30+
}
31+
32+
// Call the function with the mock command
33+
packages, commitHash := getPackagesAndCommitHash(cmd)
34+
35+
// Check if the returned packages and commitHash are as expected
36+
if len(packages) != 0 {
37+
t.Errorf("Expected no packages, got %d", len(packages))
38+
}
39+
40+
if err != nil {
41+
t.Errorf("Expected no error, got %s", err)
42+
}
43+
44+
if commitHash != box.Lockfile().Stdenv().Rev {
45+
t.Errorf("Expected commitHash %s, got %s", box.Lockfile().Stdenv().Rev, commitHash)
46+
}
47+
}

internal/shellgen/flake_plan.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,15 +90,15 @@ type glibcPatchFlake struct {
9090
Dependencies []string
9191
}
9292

93-
func newGlibcPatchFlake(nixpkgsGlibcRev string, packages []*devpkg.Package) (glibcPatchFlake, error) {
93+
func newGlibcPatchFlake(nixpkgs flake.Ref, packages []*devpkg.Package) (glibcPatchFlake, error) {
9494
patchFlake := glibcPatchFlake{
9595
DevboxFlake: flake.Ref{
9696
Type: flake.TypeGitHub,
9797
Owner: "jetify-com",
9898
Repo: "devbox",
9999
Ref: build.Version,
100100
},
101-
NixpkgsGlibcFlakeRef: "flake:nixpkgs/" + nixpkgsGlibcRev,
101+
NixpkgsGlibcFlakeRef: nixpkgs.String(),
102102
}
103103

104104
// In dev builds, use the local Devbox flake for patching packages

internal/shellgen/flake_plan_test.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package shellgen
2+
3+
import (
4+
"testing"
5+
6+
"go.jetify.com/devbox/internal/devbox/devopt"
7+
"go.jetify.com/devbox/internal/devconfig/configfile"
8+
"go.jetify.com/devbox/internal/devpkg"
9+
"go.jetify.com/devbox/internal/lock"
10+
"go.jetify.com/devbox/nix/flake"
11+
)
12+
13+
type lockMock struct{}
14+
15+
func (l *lockMock) Get(key string) *lock.Package {
16+
return nil
17+
}
18+
19+
func (l *lockMock) Stdenv() flake.Ref {
20+
return flake.Ref{}
21+
}
22+
23+
func (l *lockMock) ProjectDir() string {
24+
return ""
25+
}
26+
27+
func (l *lockMock) Resolve(key string) (*lock.Package, error) {
28+
return &lock.Package{
29+
Resolved: "github:NixOS/nixpkgs/10b813040df67c4039086db0f6eaf65c536886c6#python312",
30+
}, nil
31+
}
32+
33+
func TestNewGlibcPatchFlake(t *testing.T) {
34+
stdenv := flake.Ref{
35+
Type: flake.TypeGitHub,
36+
URL: "https://github.com/NixOS/nixpkgs",
37+
Ref: "nixpkgs-unstable",
38+
}
39+
40+
packages := devpkg.PackagesFromStringsWithOptions([]string{"python@latest"}, &lockMock{}, devopt.AddOpts{
41+
Patch: string(configfile.PatchAlways),
42+
})
43+
44+
patchFlake, err := newGlibcPatchFlake(stdenv, packages)
45+
if err != nil {
46+
t.Fatalf("expected no error, got %v", err)
47+
}
48+
49+
if patchFlake.NixpkgsGlibcFlakeRef != stdenv.String() {
50+
t.Errorf("expected NixpkgsGlibcFlakeRef to be %s, got %s", stdenv.String(), patchFlake.NixpkgsGlibcFlakeRef)
51+
}
52+
53+
if len(patchFlake.Outputs.Packages) != 1 {
54+
t.Errorf("expected 1 package in Outputs, got %d", len(patchFlake.Outputs.Packages))
55+
}
56+
}

internal/shellgen/generate.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ func GenerateForPrintEnv(ctx context.Context, devbox devboxer) error {
4949
}
5050

5151
if plan.needsGlibcPatch() {
52-
patch, err := newGlibcPatchFlake(devbox.Config().NixPkgsCommitHash(), plan.Packages)
52+
patch, err := newGlibcPatchFlake(devbox.Lockfile().Stdenv(), plan.Packages)
5353
if err != nil {
5454
return redact.Errorf("generate glibc patch flake: %v", err)
5555
}

0 commit comments

Comments
 (0)