Skip to content

Commit 8521fa4

Browse files
committed
code review feedback fixes
1 parent 1f418d5 commit 8521fa4

File tree

5 files changed

+62
-83
lines changed

5 files changed

+62
-83
lines changed

internal/devbox/nixprofile.go

Lines changed: 5 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@ import (
55
"fmt"
66
"strings"
77

8+
"github.com/samber/lo"
89
"go.jetpack.io/devbox/internal/nix"
910
"go.jetpack.io/devbox/internal/nix/nixprofile"
11+
"go.jetpack.io/devbox/internal/ux"
1012
)
1113

1214
// syncNixProfile ensures the nix profile has the packages specified in wantStorePaths.
@@ -28,17 +30,17 @@ func (d *Devbox) syncNixProfile(ctx context.Context, wantStorePaths []string) er
2830
}
2931

3032
// Diff the store paths and install/remove packages as needed
31-
add, remove := diffStorePaths(gotStorePaths, wantStorePaths)
33+
remove, add := lo.Difference(gotStorePaths, wantStorePaths)
3234
if len(remove) > 0 {
3335
packagesToRemove := make([]string, 0, len(remove))
3436
for _, p := range remove {
3537
storePath := nix.NewStorePathParts(p)
3638
packagesToRemove = append(packagesToRemove, fmt.Sprintf("%s@%s", storePath.Name, storePath.Version))
3739
}
3840
if len(packagesToRemove) == 1 {
39-
fmt.Fprintf(d.stderr, "Removing %s\n", strings.Join(packagesToRemove, ", "))
41+
ux.Finfo(d.stderr, "Removing %s\n", strings.Join(packagesToRemove, ", "))
4042
} else {
41-
fmt.Fprintf(d.stderr, "Removing packages: %s\n", strings.Join(packagesToRemove, ", "))
43+
ux.Finfo(d.stderr, "Removing packages: %s\n", strings.Join(packagesToRemove, ", "))
4244
}
4345

4446
if err := nix.ProfileRemove(profilePath, remove...); err != nil {
@@ -70,27 +72,3 @@ func (d *Devbox) syncNixProfile(ctx context.Context, wantStorePaths []string) er
7072
}
7173
return nil
7274
}
73-
74-
func diffStorePaths(got, want []string) (add, remove []string) {
75-
gotSet := map[string]bool{}
76-
for _, g := range got {
77-
gotSet[g] = true
78-
}
79-
wantSet := map[string]bool{}
80-
for _, w := range want {
81-
wantSet[w] = true
82-
}
83-
84-
for _, g := range got {
85-
if _, ok := wantSet[g]; !ok {
86-
remove = append(remove, g)
87-
}
88-
}
89-
90-
for _, w := range want {
91-
if _, ok := gotSet[w]; !ok {
92-
add = append(add, w)
93-
}
94-
}
95-
return add, remove
96-
}

internal/devbox/packages.go

Lines changed: 52 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ const (
249249
// 1. Skipping certain operations that may not apply.
250250
// 2. User messaging to explain what operations are happening, because this function may take time to execute.
251251
//
252-
// nolint:revive to turn off linter complaining about function complexity
252+
// linter:revive is turned off due to complaining about function complexity
253253
func (d *Devbox) ensureStateIsUpToDate(ctx context.Context, mode installMode) error { //nolint:revive
254254
defer trace.StartRegion(ctx, "devboxEnsureStateIsUpToDate").End()
255255
defer debug.FunctionTimer().End()
@@ -259,10 +259,7 @@ func (d *Devbox) ensureStateIsUpToDate(ctx context.Context, mode installMode) er
259259
// then we skip some operations below for speed.
260260
// Remove the local.lock file so that we re-compute the project state when
261261
// we are in the devbox environment again.
262-
fmt.Printf("mode is not ensure and env is not enabled, so will remove local.lock on exit\n")
263-
264262
defer func() { _ = d.lockfile.RemoveLocal() }()
265-
// return nil
266263
}
267264

268265
// if mode is install or uninstall, then we need to update the nix-profile
@@ -275,12 +272,10 @@ func (d *Devbox) ensureStateIsUpToDate(ctx context.Context, mode installMode) er
275272
if mode == ensure {
276273
// if mode is ensure and we are up to date, then we can skip the rest
277274
if upToDate {
278-
fmt.Printf("state is up to date. Returning early\n")
279275
return nil
280276
}
281277
fmt.Fprintln(d.stderr, "Ensuring packages are installed.")
282278
}
283-
fmt.Printf("state is not up to date. Continuing...\n")
284279

285280
// Validate packages. Must be run up-front and definitely prior to computeEnv
286281
// and syncNixProfile below that will evaluate the flake and may give
@@ -300,9 +295,6 @@ func (d *Devbox) ensureStateIsUpToDate(ctx context.Context, mode installMode) er
300295
return err
301296
}
302297

303-
fmt.Printf("mode is %s\n", mode)
304-
fmt.Printf("env is enabled: %v\n", d.IsEnvEnabled())
305-
306298
if err := shellgen.GenerateForPrintEnv(ctx, d); err != nil {
307299
return err
308300
}
@@ -341,7 +333,6 @@ func (d *Devbox) ensureStateIsUpToDate(ctx context.Context, mode installMode) er
341333
}
342334
}
343335

344-
fmt.Printf("calling lockfile.tidy\n")
345336
// Ensure we clean out packages that are no longer needed.
346337
d.lockfile.Tidy()
347338

@@ -428,72 +419,46 @@ func (d *Devbox) InstallRunXPackages(ctx context.Context) error {
428419

429420
// installNixPackagesToStore will install all the packages in the nix store, if
430421
// mode is install or update, and we're not in a devbox environment.
431-
// This is done by running `nix build` on the flake
422+
// This is done by running `nix build` on the flake. We do this so that the
423+
// packages will be available in the nix store when computing the devbox environment
424+
// and installing in the nix profile (even if offline).
432425
func (d *Devbox) installNixPackagesToStore(ctx context.Context) error {
433-
packages, err := d.AllInstallablePackages()
426+
packages, err := d.packagesToInstallInProfile(ctx)
434427
if err != nil {
435428
return err
436429
}
437-
packages = lo.Filter(packages, devpkg.IsNix) // Remove non-nix packages from the list
438430

431+
names := []string{}
439432
installables := []string{}
440433
for _, pkg := range packages {
441434
i, err := pkg.Installable()
442435
if err != nil {
443436
return err
444437
}
445438
installables = append(installables, i)
439+
names = append(names, pkg.String())
446440
}
447441

448442
if len(installables) == 0 {
449443
return nil
450444
}
451445

446+
ux.Finfo(d.stderr, "Installing to the nix store: %s. This may take a brief while.\n", strings.Join(names, " "))
447+
452448
// --no-link to avoid generating the result objects
453449
return nix.Build(ctx, []string{"--no-link"}, installables...)
454450
}
455451

456452
// validatePackagesToBeInstalled will ensure that packages are available to be installed
457453
// in the user's current system.
458454
func (d *Devbox) validatePackagesToBeInstalled(ctx context.Context) error {
459-
// First, fetch the profile items from the nix-profile,
460-
profileDir, err := d.profilePath()
461-
if err != nil {
462-
return err
463-
}
464-
profileItems, err := nixprofile.ProfileListItems(ctx, d.stderr, profileDir)
455+
// First, get the packages to install
456+
packagesToInstall, err := d.packagesToInstallInProfile(ctx)
465457
if err != nil {
466458
return err
467459
}
468460

469-
// Second, get and prepare all the packages that must be installed in this project
470-
packages, err := d.AllInstallablePackages()
471-
if err != nil {
472-
return err
473-
}
474-
packages = lo.Filter(packages, devpkg.IsNix) // Remove non-nix packages from the list
475-
if err := devpkg.FillNarInfoCache(ctx, packages...); err != nil {
476-
return err
477-
}
478-
479-
// Third, compute which packages need to be installed
480-
packagesToInstall := []*devpkg.Package{}
481-
// Note: because devpkg.Package uses memoization when normalizing attribute paths (slow operation),
482-
// and since we're reusing the Package objects, this O(n*m) loop becomes O(n+m) wrt the slow operation.
483-
for _, pkg := range packages {
484-
found := false
485-
for _, item := range profileItems {
486-
if item.Matches(pkg, d.lockfile) {
487-
found = true
488-
break
489-
}
490-
}
491-
if !found {
492-
packagesToInstall = append(packagesToInstall, pkg)
493-
}
494-
}
495-
496-
// Last, validate that packages that need to be installed are in fact installable
461+
// Then, validate that packages that need to be installed are in fact installable
497462
// on the user's current system.
498463
for _, pkg := range packagesToInstall {
499464
inCache, err := pkg.IsInBinaryCache()
@@ -524,3 +489,43 @@ func (d *Devbox) validatePackagesToBeInstalled(ctx context.Context) error {
524489
}
525490
return nil
526491
}
492+
493+
func (d *Devbox) packagesToInstallInProfile(ctx context.Context) ([]*devpkg.Package, error) {
494+
// First, fetch the profile items from the nix-profile,
495+
profileDir, err := d.profilePath()
496+
if err != nil {
497+
return nil, err
498+
}
499+
profileItems, err := nixprofile.ProfileListItems(ctx, d.stderr, profileDir)
500+
if err != nil {
501+
return nil, err
502+
}
503+
504+
// Second, get and prepare all the packages that must be installed in this project
505+
packages, err := d.AllInstallablePackages()
506+
if err != nil {
507+
return nil, err
508+
}
509+
packages = lo.Filter(packages, devpkg.IsNix) // Remove non-nix packages from the list
510+
if err := devpkg.FillNarInfoCache(ctx, packages...); err != nil {
511+
return nil, err
512+
}
513+
514+
// Third, compute which packages need to be installed
515+
packagesToInstall := []*devpkg.Package{}
516+
// Note: because devpkg.Package uses memoization when normalizing attribute paths (slow operation),
517+
// and since we're reusing the Package objects, this O(n*m) loop becomes O(n+m) wrt the slow operation.
518+
for _, pkg := range packages {
519+
found := false
520+
for _, item := range profileItems {
521+
if item.Matches(pkg, d.lockfile) {
522+
found = true
523+
break
524+
}
525+
}
526+
if !found {
527+
packagesToInstall = append(packagesToInstall, pkg)
528+
}
529+
}
530+
return packagesToInstall, nil
531+
}

internal/lock/local.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ package lock
55

66
import (
77
"errors"
8-
"fmt"
98
"io/fs"
109
"os"
1110
"path/filepath"
@@ -51,8 +50,6 @@ func isLocalUpToDate(project devboxProject) (bool, error) {
5150
}
5251

5352
func updateLocal(project devboxProject) error {
54-
fmt.Printf("updating local.lock\n")
55-
// debug.PrintStack()
5653
l, err := readLocal(project)
5754
if err != nil {
5855
return err

internal/lock/lockfile.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ func (f *File) Add(pkgs ...string) error {
5555
return err
5656
}
5757
}
58-
fmt.Printf("calling lockfile.Add with pkgs: %v\n", pkgs)
5958
return f.Save()
6059
}
6160

internal/nix/build.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ package nix
22

33
import (
44
"context"
5-
"fmt"
65
"os/exec"
76

87
"github.com/pkg/errors"
8+
"go.jetpack.io/devbox/internal/debug"
99
)
1010

1111
func Build(ctx context.Context, flags []string, installables ...string) error {
@@ -15,15 +15,15 @@ func Build(ctx context.Context, flags []string, installables ...string) error {
1515
cmd.Args = append(cmd.Args, installables...)
1616
// We need to allow Unfree packages to be installed. We choose to not also add os.Environ() to the environment
1717
// to keep the command as pure as possible, even though we must pass --impure to nix build.
18-
cmd.Env = allowUnfreeEnv([]string{}) // allowUnfreeEnv(os.Environ())
18+
cmd.Env = allowUnfreeEnv([]string{})
1919

20-
out, err := cmd.Output()
20+
debug.Log("Running cmd: %s\n", cmd)
21+
_, err := cmd.Output()
2122
if err != nil {
2223
if exitErr := (&exec.ExitError{}); errors.As(err, &exitErr) {
23-
fmt.Printf("Exit code: %d, output: %s\n", exitErr.ExitCode(), exitErr.Stderr)
24+
debug.Log("Nix build exit code: %d, output: %s\n", exitErr.ExitCode(), exitErr.Stderr)
2425
}
2526
return err
2627
}
27-
fmt.Printf("Ran nix build, output: %s\n", out)
2828
return nil
2929
}

0 commit comments

Comments
 (0)