Skip to content

Commit 9ccf82a

Browse files
authored
[remove nixpkgs] Enable installing and removing packages (#1255)
## Summary builds on top of #1236 Goal: We should be able to install the package without downloading nixpkgs. Changes made to: - `nixprofile.ProfileInstall`: use `/nix/store/<hash>` as `installable` - `nixprofile.ProfileListIndex`: return index when `item.nixStorePath == pkg.PathInStore` - `devpkgs.Package.ValidateExists`: `true` if `IsInBinaryStore` - `devpkgs.Package.EnsureNixpkgsPrefetched`: skip if `IsInBinaryStore`. ## How was it tested? `devbox add [email protected]` and `devbox rm [email protected]` - [x] nixpkgs not downloaded
1 parent 0467e39 commit 9ccf82a

File tree

4 files changed

+79
-6
lines changed

4 files changed

+79
-6
lines changed

internal/devpkg/package.go

Lines changed: 25 additions & 0 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/boxcli/featureflag"
1819
"go.jetpack.io/devbox/internal/boxcli/usererr"
1920
"go.jetpack.io/devbox/internal/cuecfg"
2021
"go.jetpack.io/devbox/internal/lock"
@@ -324,6 +325,15 @@ func (p *Package) ValidateExists() (bool, error) {
324325
if p.isVersioned() && p.version() == "" {
325326
return false, usererr.New("No version specified for %q.", p.Path)
326327
}
328+
329+
isInStore, err := p.IsInBinaryStore()
330+
if err != nil {
331+
return false, err
332+
}
333+
if isInStore {
334+
return true, nil
335+
}
336+
327337
info, err := p.NormalizedPackageAttributePath()
328338
return info != "", err
329339
}
@@ -378,6 +388,17 @@ func (p *Package) LegacyToVersioned() string {
378388
}
379389

380390
func (p *Package) EnsureNixpkgsPrefetched(w io.Writer) error {
391+
392+
isInStore, err := p.IsInBinaryStore()
393+
if err != nil {
394+
return err
395+
}
396+
if isInStore {
397+
// We can skip prefetching nixpkgs, if this package is in the binary
398+
// cache store.
399+
return nil
400+
}
401+
381402
hash := p.HashFromNixPkgsURL()
382403
if hash == "" {
383404
return nil
@@ -408,6 +429,10 @@ func (p *Package) HashFromNixPkgsURL() string {
408429
const BinaryCacheStore = "https://cache.nixos.org"
409430

410431
func (p *Package) IsInBinaryStore() (bool, error) {
432+
if !featureflag.RemoveNixpkgs.Enabled() {
433+
return false, nil
434+
}
435+
411436
if !p.isVersioned() {
412437
return false, nil
413438
}

internal/impl/packages.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ func (d *Devbox) addPackagesToProfile(ctx context.Context, mode installMode) err
243243

244244
// If packages are in profile but nixpkgs has been purged, the experience
245245
// will be poor when we try to run print-dev-env. So we ensure nixpkgs is
246-
// prefetched for all our packages.
246+
// prefetched for all relevant packages (those not in binary store).
247247
for _, input := range d.PackagesAsInputs() {
248248
if err := input.EnsureNixpkgsPrefetched(d.writer); err != nil {
249249
return err

internal/nix/nixprofile/profile.go

Lines changed: 51 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,23 @@ func ProfileListIndex(args *ProfileListIndexArgs) (int, error) {
6969
}
7070
}
7171

72+
inStore, err := args.Input.IsInBinaryStore()
73+
if err != nil {
74+
return -1, err
75+
}
76+
if inStore {
77+
pathInStore, err := args.Input.PathInBinaryStore()
78+
if err != nil {
79+
return -1, err
80+
}
81+
for _, item := range list {
82+
if pathInStore == item.nixStorePath {
83+
return item.index, nil
84+
}
85+
}
86+
}
87+
// else: fallback to checking if the Input matches an item's unlockedReference
88+
7289
// This is an optimization for happy path. A resolved devbox package
7390
// should match the unlockedReference of an existing profile item.
7491
ref, err := args.Input.NormalizedDevboxPackageReference()
@@ -191,7 +208,13 @@ type ProfileInstallArgs struct {
191208
// ProfileInstall calls nix profile install with default profile
192209
func ProfileInstall(args *ProfileInstallArgs) error {
193210
input := devpkg.PackageFromString(args.Package, args.Lockfile)
194-
if nix.IsGithubNixpkgsURL(input.URLForFlakeInput()) {
211+
212+
isInBinaryStore, err := input.IsInBinaryStore()
213+
if err != nil {
214+
return err
215+
}
216+
217+
if !isInBinaryStore && nix.IsGithubNixpkgsURL(input.URLForFlakeInput()) {
195218
if err := nix.EnsureNixpkgsPrefetched(args.Writer, input.HashFromNixPkgsURL()); err != nil {
196219
return err
197220
}
@@ -204,12 +227,12 @@ func ProfileInstall(args *ProfileInstallArgs) error {
204227
fmt.Fprintf(args.Writer, "%s\n", stepMsg)
205228
}
206229

207-
urlForInstall, err := input.URLForInstall()
230+
installable, err := installableForPackage(input)
208231
if err != nil {
209232
return err
210233
}
211234

212-
err = nix.ProfileInstall(args.Writer, args.ProfilePath, urlForInstall)
235+
err = nix.ProfileInstall(args.Writer, args.ProfilePath, installable)
213236
if err != nil {
214237
fmt.Fprintf(args.Writer, "%s: ", stepMsg)
215238
color.New(color.FgRed).Fprintf(args.Writer, "Fail\n")
@@ -234,3 +257,28 @@ func ProfileRemoveItems(profilePath string, items []*NixProfileListItem) error {
234257
}
235258
return nix.ProfileRemove(profilePath, indexes)
236259
}
260+
261+
// installableForPackage determines how nix profile should install this package.
262+
// Keeping in `nixprofile` package since its specific to how nix profile works,
263+
// rather than a general property of devpkg.Package
264+
func installableForPackage(pkg *devpkg.Package) (string, error) {
265+
isInBinaryStore, err := pkg.IsInBinaryStore()
266+
if err != nil {
267+
return "", err
268+
}
269+
270+
if isInBinaryStore {
271+
// TODO savil: change to ContentAddressablePath when that is implemented
272+
installable, err := pkg.PathInBinaryStore()
273+
if err != nil {
274+
return "", err
275+
}
276+
return installable, nil
277+
}
278+
279+
installable, err := pkg.URLForInstall()
280+
if err != nil {
281+
return "", err
282+
}
283+
return installable, nil
284+
}

internal/nix/profiles.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ func ProfileList(writer io.Writer, profilePath string) ([]string, error) {
4848
return lines, nil
4949
}
5050

51-
func ProfileInstall(writer io.Writer, profilePath string, urlForInstall string) error {
51+
func ProfileInstall(writer io.Writer, profilePath string, installable string) error {
5252

5353
cmd := command(
5454
"profile", "install",
@@ -58,7 +58,7 @@ func ProfileInstall(writer io.Writer, profilePath string, urlForInstall string)
5858
// Note that this is not really the priority we care about, since we
5959
// use the flake.nix to specify the priority.
6060
"--priority", nextPriority(profilePath),
61-
urlForInstall,
61+
installable,
6262
)
6363
cmd.Env = allowUnfreeEnv()
6464

0 commit comments

Comments
 (0)