From 5c00e3fdbb8c83e6a2ac9d7f3cf38961fd7ae123 Mon Sep 17 00:00:00 2001 From: Mike Landau Date: Mon, 14 Apr 2025 14:06:59 -0700 Subject: [PATCH 1/2] [nixpkgs] Use lock.stdenv instead of NixPkgsCommitHash --- internal/boxcli/midcobra/telemetry.go | 2 +- internal/boxcli/midcobra/telemetry_test.go | 47 ++++++++++++++++++ internal/shellgen/flake_plan.go | 4 +- internal/shellgen/flake_plan_test.go | 57 ++++++++++++++++++++++ internal/shellgen/generate.go | 2 +- 5 files changed, 108 insertions(+), 4 deletions(-) create mode 100644 internal/boxcli/midcobra/telemetry_test.go create mode 100644 internal/shellgen/flake_plan_test.go diff --git a/internal/boxcli/midcobra/telemetry.go b/internal/boxcli/midcobra/telemetry.go index 5000a71c328..44c7f8aefd9 100644 --- a/internal/boxcli/midcobra/telemetry.go +++ b/internal/boxcli/midcobra/telemetry.go @@ -103,5 +103,5 @@ func getPackagesAndCommitHash(c *cobra.Command) ([]string, string) { } return box.AllPackageNamesIncludingRemovedTriggerPackages(), - box.Config().NixPkgsCommitHash() + box.Lockfile().Stdenv().Rev } diff --git a/internal/boxcli/midcobra/telemetry_test.go b/internal/boxcli/midcobra/telemetry_test.go new file mode 100644 index 00000000000..fb436c23a98 --- /dev/null +++ b/internal/boxcli/midcobra/telemetry_test.go @@ -0,0 +1,47 @@ +package midcobra + +import ( + "testing" + + "github.com/spf13/cobra" + "go.jetify.com/devbox/internal/devbox" + "go.jetify.com/devbox/internal/devbox/devopt" +) + +func TestGetPackagesAndCommitHash(t *testing.T) { + dir := t.TempDir() + err := devbox.InitConfig(dir) + if err != nil { + t.Errorf("Expected no error, got %s", err) + } + box, err := devbox.Open(&devopt.Opts{ + Dir: dir, + }) + // Create a mock cobra command + cmd := &cobra.Command{ + Use: "test", + Run: func(cmd *cobra.Command, args []string) {}, + } + + // Add a mock flag to the command + cmd.Flags().String("config", "", "config file") + if err := cmd.Flags().Set("config", dir); err != nil { + t.Errorf("Expected no error, got %s", err) + } + + // Call the function with the mock command + packages, commitHash := getPackagesAndCommitHash(cmd) + + // Check if the returned packages and commitHash are as expected + if len(packages) != 0 { + t.Errorf("Expected no packages, got %d", len(packages)) + } + + if err != nil { + t.Errorf("Expected no error, got %s", err) + } + + if commitHash != box.Lockfile().Stdenv().Rev { + t.Errorf("Expected commitHash %s, got %s", box.Lockfile().Stdenv().Rev, commitHash) + } +} diff --git a/internal/shellgen/flake_plan.go b/internal/shellgen/flake_plan.go index 6f319953b13..91063fe593d 100644 --- a/internal/shellgen/flake_plan.go +++ b/internal/shellgen/flake_plan.go @@ -90,7 +90,7 @@ type glibcPatchFlake struct { Dependencies []string } -func newGlibcPatchFlake(nixpkgsGlibcRev string, packages []*devpkg.Package) (glibcPatchFlake, error) { +func newGlibcPatchFlake(nixpkgs flake.Ref, packages []*devpkg.Package) (glibcPatchFlake, error) { patchFlake := glibcPatchFlake{ DevboxFlake: flake.Ref{ Type: flake.TypeGitHub, @@ -98,7 +98,7 @@ func newGlibcPatchFlake(nixpkgsGlibcRev string, packages []*devpkg.Package) (gli Repo: "devbox", Ref: build.Version, }, - NixpkgsGlibcFlakeRef: "flake:nixpkgs/" + nixpkgsGlibcRev, + NixpkgsGlibcFlakeRef: nixpkgs.String(), } // In dev builds, use the local Devbox flake for patching packages diff --git a/internal/shellgen/flake_plan_test.go b/internal/shellgen/flake_plan_test.go new file mode 100644 index 00000000000..df5755e86a6 --- /dev/null +++ b/internal/shellgen/flake_plan_test.go @@ -0,0 +1,57 @@ +package shellgen + +import ( + "testing" + + "go.jetify.com/devbox/internal/devbox/devopt" + "go.jetify.com/devbox/internal/devconfig/configfile" + "go.jetify.com/devbox/internal/devpkg" + "go.jetify.com/devbox/internal/lock" + "go.jetify.com/devbox/nix/flake" +) + +type lockMock struct{} + +func (l *lockMock) Get(key string) *lock.Package { + return nil +} + +func (l *lockMock) Stdenv() flake.Ref { + return flake.Ref{} +} + +func (l *lockMock) ProjectDir() string { + return "" +} + +func (l *lockMock) Resolve(key string) (*lock.Package, error) { + return &lock.Package{ + Resolved: "github:NixOS/nixpkgs/10b813040df67c4039086db0f6eaf65c536886c6#python312", + }, nil +} + +func TestNewGlibcPatchFlake(t *testing.T) { + stdenv := flake.Ref{ + Type: flake.TypeGitHub, + URL: "https://github.com/NixOS/nixpkgs", + Ref: "nixpkgs-unstable", + } + + packages := devpkg.PackagesFromStringsWithOptions([]string{"python@latest"}, &lockMock{}, devopt.AddOpts{ + Patch: string(configfile.PatchAlways), + }) + + patchFlake, err := newGlibcPatchFlake(stdenv, packages) + if err != nil { + t.Fatalf("expected no error, got %v", err) + } + + if patchFlake.NixpkgsGlibcFlakeRef != stdenv.String() { + t.Errorf("expected NixpkgsGlibcFlakeRef to be %s, got %s", stdenv.String(), patchFlake.NixpkgsGlibcFlakeRef) + } + + if len(patchFlake.Outputs.Packages) != 1 { + t.Errorf("expected 1 package in Outputs, got %d", len(patchFlake.Outputs.Packages)) + } + +} diff --git a/internal/shellgen/generate.go b/internal/shellgen/generate.go index d4e601281a0..838152f4c61 100644 --- a/internal/shellgen/generate.go +++ b/internal/shellgen/generate.go @@ -49,7 +49,7 @@ func GenerateForPrintEnv(ctx context.Context, devbox devboxer) error { } if plan.needsGlibcPatch() { - patch, err := newGlibcPatchFlake(devbox.Config().NixPkgsCommitHash(), plan.Packages) + patch, err := newGlibcPatchFlake(devbox.Lockfile().Stdenv(), plan.Packages) if err != nil { return redact.Errorf("generate glibc patch flake: %v", err) } From 725aa99283fc68ca6b008d2263ab793a556970b6 Mon Sep 17 00:00:00 2001 From: Mike Landau Date: Mon, 14 Apr 2025 15:20:06 -0700 Subject: [PATCH 2/2] fmt --- internal/shellgen/flake_plan_test.go | 1 - 1 file changed, 1 deletion(-) diff --git a/internal/shellgen/flake_plan_test.go b/internal/shellgen/flake_plan_test.go index df5755e86a6..d1e12a501c0 100644 --- a/internal/shellgen/flake_plan_test.go +++ b/internal/shellgen/flake_plan_test.go @@ -53,5 +53,4 @@ func TestNewGlibcPatchFlake(t *testing.T) { if len(patchFlake.Outputs.Packages) != 1 { t.Errorf("expected 1 package in Outputs, got %d", len(patchFlake.Outputs.Packages)) } - }