Skip to content

Commit 52d9fbd

Browse files
authored
[refactor] intro nixprofile package (#1238)
## Summary There are three motivations for introducing the `nixprofile` package: 1. We should keep `nix` package focussed on executing core nix apis. This would reduce its dependencies on other packages, enabling more packages to call `nix` functions without hitting cycle errors. `nixprofile` has a lot of devbox-specific logic. So, it should be its own package that calls `nix` apis (which themselves can be improved) 2. I tried moving `nix.Package` to `devpkg` but it was extremely difficult to avoid new cyclic import errors. This PR will help avoid those. 3. This will also help break the import cycle for calling `nix.System` from `lock.Lockfile`. ## How was it tested? compiles - [x] testscript should pass
1 parent e3bf74d commit 52d9fbd

File tree

12 files changed

+226
-179
lines changed

12 files changed

+226
-179
lines changed

internal/impl/packages.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515

1616
"github.com/pkg/errors"
1717
"github.com/samber/lo"
18+
"go.jetpack.io/devbox/internal/nix/nixprofile"
1819
"go.jetpack.io/devbox/internal/shellgen"
1920
"golang.org/x/exp/slices"
2021

@@ -276,7 +277,7 @@ func (d *Devbox) addPackagesToProfile(ctx context.Context, mode installMode) err
276277

277278
stepMsg := fmt.Sprintf("[%d/%d] %s", stepNum, total, pkg)
278279

279-
if err := nix.ProfileInstall(&nix.ProfileInstallArgs{
280+
if err := nixprofile.ProfileInstall(&nixprofile.ProfileInstallArgs{
280281
CustomStepMessage: stepMsg,
281282
Lockfile: d.lockfile,
282283
Package: pkg,
@@ -299,7 +300,7 @@ func (d *Devbox) removePackagesFromProfile(ctx context.Context, pkgs []string) e
299300
}
300301

301302
for _, input := range nix.PackageFromStrings(pkgs, d.lockfile) {
302-
index, err := nix.ProfileListIndex(&nix.ProfileListIndexArgs{
303+
index, err := nixprofile.ProfileListIndex(&nixprofile.ProfileListIndexArgs{
303304
Lockfile: d.lockfile,
304305
Writer: d.writer,
305306
Input: input,
@@ -345,7 +346,7 @@ func (d *Devbox) tidyProfile(ctx context.Context) error {
345346
}
346347

347348
// Remove by index to avoid comparing nix.ProfileListItem <> nix.Inputs again.
348-
return nix.ProfileRemoveItems(profileDir, extras)
349+
return nixprofile.ProfileRemoveItems(profileDir, extras)
349350
}
350351

351352
// pendingPackagesForInstallation returns a list of packages that are in
@@ -361,12 +362,12 @@ func (d *Devbox) pendingPackagesForInstallation(ctx context.Context) ([]string,
361362
}
362363

363364
pending := []string{}
364-
list, err := nix.ProfileListItems(d.writer, profileDir)
365+
list, err := nixprofile.ProfileListItems(d.writer, profileDir)
365366
if err != nil {
366367
return nil, err
367368
}
368369
for _, input := range d.PackagesAsInputs() {
369-
_, err := nix.ProfileListIndex(&nix.ProfileListIndexArgs{
370+
_, err := nixprofile.ProfileListIndex(&nixprofile.ProfileListIndexArgs{
370371
List: list,
371372
Lockfile: d.lockfile,
372373
Writer: d.writer,
@@ -388,15 +389,15 @@ func (d *Devbox) pendingPackagesForInstallation(ctx context.Context) ([]string,
388389
//
389390
// NOTE: as an optimization, this implementation assumes that all packages in
390391
// devbox.json have already been added to the nix profile.
391-
func (d *Devbox) extraPackagesInProfile(ctx context.Context) ([]*nix.NixProfileListItem, error) {
392+
func (d *Devbox) extraPackagesInProfile(ctx context.Context) ([]*nixprofile.NixProfileListItem, error) {
392393
defer trace.StartRegion(ctx, "extraPackagesInProfile").End()
393394

394395
profileDir, err := d.profilePath()
395396
if err != nil {
396397
return nil, err
397398
}
398399

399-
profileItems, err := nix.ProfileListItems(d.writer, profileDir)
400+
profileItems, err := nixprofile.ProfileListItems(d.writer, profileDir)
400401
if err != nil {
401402
return nil, err
402403
}
@@ -409,12 +410,12 @@ func (d *Devbox) extraPackagesInProfile(ctx context.Context) ([]*nix.NixProfileL
409410
return nil, nil
410411
}
411412

412-
extras := []*nix.NixProfileListItem{}
413+
extras := []*nixprofile.NixProfileListItem{}
413414
// Note: because nix.Input uses memoization when normalizing attribute paths (slow operation),
414415
// and since we're reusing the Input objects, this O(n*m) loop becomes O(n+m) wrt the slow operation.
415416
outer:
416417
for _, item := range profileItems {
417-
profileInput := nix.PackageFromProfileItem(item, d.lockfile)
418+
profileInput := item.ToPackage(d.lockfile)
418419
for _, devboxInput := range devboxInputs {
419420
if profileInput.Equals(devboxInput) {
420421
continue outer

internal/impl/update.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99

1010
"go.jetpack.io/devbox/internal/devpkg"
1111
"go.jetpack.io/devbox/internal/nix"
12+
"go.jetpack.io/devbox/internal/nix/nixprofile"
1213
"go.jetpack.io/devbox/internal/searcher"
1314
"go.jetpack.io/devbox/internal/shellgen"
1415
"go.jetpack.io/devbox/internal/ux"
@@ -120,7 +121,7 @@ func (d *Devbox) attemptToUpgradeFlake(pkg *nix.Package) error {
120121
pkg.Raw,
121122
)
122123

123-
err = nix.ProfileUpgrade(profilePath, pkg, d.lockfile)
124+
err = nixprofile.ProfileUpgrade(profilePath, pkg, d.lockfile)
124125
if err != nil {
125126
ux.Ferror(
126127
d.writer,

internal/impl/util.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ import (
99
"path/filepath"
1010

1111
"github.com/pkg/errors"
12+
"go.jetpack.io/devbox/internal/nix/nixprofile"
1213

13-
"go.jetpack.io/devbox/internal/nix"
1414
"go.jetpack.io/devbox/internal/xdg"
1515
)
1616

@@ -24,7 +24,7 @@ func (d *Devbox) addDevboxUtilityPackage(pkg string) error {
2424
return err
2525
}
2626

27-
return nix.ProfileInstall(&nix.ProfileInstallArgs{
27+
return nixprofile.ProfileInstall(&nixprofile.ProfileInstallArgs{
2828
Lockfile: d.lockfile,
2929
Package: pkg,
3030
ProfilePath: profilePath,

internal/nix/command.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package nix
2+
3+
import (
4+
"os"
5+
"os/exec"
6+
)
7+
8+
func command(args ...string) *exec.Cmd {
9+
10+
cmd := exec.Command("nix", args...)
11+
cmd.Args = append(cmd.Args, ExperimentalFlags()...)
12+
return cmd
13+
}
14+
15+
func allowUnfreeEnv() []string {
16+
return append(os.Environ(), "NIXPKGS_ALLOW_UNFREE=1")
17+
}

internal/nix/input.go

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -77,12 +77,6 @@ func PackageFromString(raw string, locker lock.Locker) *Package {
7777
return &Package{*pkgURL, locker, raw, ""}
7878
}
7979

80-
// PackageFromProfileItem constructs a package using the the unlocked reference
81-
// from profile list item.
82-
func PackageFromProfileItem(item *NixProfileListItem, locker lock.Locker) *Package {
83-
return PackageFromString(item.unlockedReference, locker)
84-
}
85-
8680
// isLocal specifies whether this package is a local flake.
8781
// Usually, this is of the form: `path:./local_flake_subdir#myPackage`
8882
func (p *Package) isLocal() bool {
@@ -165,7 +159,7 @@ func (p *Package) URLForInstall() (string, error) {
165159
return p.urlWithoutFragment() + "#" + attrPath, nil
166160
}
167161

168-
func (p *Package) normalizedDevboxPackageReference() (string, error) {
162+
func (p *Package) NormalizedDevboxPackageReference() (string, error) {
169163
if !p.isDevboxPackage() {
170164
return "", nil
171165
}
@@ -213,7 +207,7 @@ func (p *Package) PackageAttributePath() (string, error) {
213207
// it is much faster than NormalizedPackageAttributePath
214208
func (p *Package) FullPackageAttributePath() (string, error) {
215209
if p.isDevboxPackage() {
216-
reference, err := p.normalizedDevboxPackageReference()
210+
reference, err := p.NormalizedDevboxPackageReference()
217211
if err != nil {
218212
return "", err
219213
}
@@ -383,11 +377,11 @@ func (p *Package) LegacyToVersioned() string {
383377
}
384378

385379
func (p *Package) EnsureNixpkgsPrefetched(w io.Writer) error {
386-
hash := p.hashFromNixPkgsURL()
380+
hash := p.HashFromNixPkgsURL()
387381
if hash == "" {
388382
return nil
389383
}
390-
return ensureNixpkgsPrefetched(w, hash)
384+
return EnsureNixpkgsPrefetched(w, hash)
391385
}
392386

393387
// version returns the version of the package
@@ -404,7 +398,7 @@ func (p *Package) isVersioned() bool {
404398
return p.isDevboxPackage() && strings.Contains(p.Path, "@")
405399
}
406400

407-
func (p *Package) hashFromNixPkgsURL() string {
401+
func (p *Package) HashFromNixPkgsURL() string {
408402
return HashFromNixPkgsURL(p.URLForFlakeInput())
409403
}
410404

internal/nix/nixpkgs.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ import (
1919
"go.jetpack.io/devbox/internal/xdg"
2020
)
2121

22-
// ensureNixpkgsPrefetched runs the prefetch step to download the flake of the registry
23-
func ensureNixpkgsPrefetched(w io.Writer, commit string) error {
22+
// EnsureNixpkgsPrefetched runs the prefetch step to download the flake of the registry
23+
func EnsureNixpkgsPrefetched(w io.Writer, commit string) error {
2424
// Look up the cached map of commitHash:nixStoreLocation
2525
commitToLocation, err := nixpkgsCommitFileContents()
2626
if err != nil {

0 commit comments

Comments
 (0)