Skip to content

Commit 755eff3

Browse files
committed
Call resolve from Package FromPath getter; use nix.Package in template
1 parent 44e7ba2 commit 755eff3

File tree

8 files changed

+62
-140
lines changed

8 files changed

+62
-140
lines changed

internal/lock/interfaces.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,4 @@ type Locker interface {
1818
resolver
1919
LegacyNixpkgsPath(string) string
2020
ProjectDir() string
21-
SystemInfo(pkg string) (*SystemInfo, error)
2221
}

internal/lock/lockfile.go

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -129,33 +129,6 @@ func (l *File) Resolve(pkg string) (*Package, error) {
129129
return l.Packages[pkg], nil
130130
}
131131

132-
func (l *File) SystemInfo(pkgVersionedName string) (*SystemInfo, error) {
133-
if !featureflag.RemoveNixpkgs.Enabled() {
134-
return nil, nil
135-
}
136-
137-
pkg, ok := l.Packages[pkgVersionedName]
138-
if !ok {
139-
return nil, errors.Errorf("package %s not found in lockfile", pkgVersionedName)
140-
}
141-
142-
if sysInfo, ok := pkg.Systems[l.system]; ok {
143-
// Found it! We are done.
144-
return sysInfo, nil
145-
}
146-
147-
// If the systemInfo is missing, then resolve the package to get it from the search api.
148-
var err error
149-
pkg, err = l.Resolve(pkgVersionedName)
150-
if err != nil {
151-
return nil, err
152-
}
153-
if sysInfo, ok := pkg.Systems[l.system]; ok {
154-
return sysInfo, nil
155-
}
156-
return nil, errors.Errorf("Unable to get system %s info for package %s", l.system, pkgVersionedName)
157-
}
158-
159132
func (l *File) ForceResolve(pkg string) (*Package, error) {
160133
delete(l.Packages, pkg)
161134
return l.Resolve(pkg)

internal/nix/input.go

Lines changed: 38 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ import (
1313
"regexp"
1414
"strings"
1515

16+
"github.com/pkg/errors"
1617
"github.com/samber/lo"
1718
"go.jetpack.io/devbox/internal/boxcli/usererr"
1819
"go.jetpack.io/devbox/internal/cuecfg"
19-
"go.jetpack.io/devbox/internal/debug"
2020
"go.jetpack.io/devbox/internal/lock"
2121
)
2222

@@ -42,9 +42,6 @@ type Package struct {
4242
Raw string
4343

4444
normalizedPackageAttributePathCache string // memoized value from normalizedPackageAttributePath()
45-
46-
// sysInfo contains system-specific information about this package in the nix store
47-
sysInfo *lock.SystemInfo
4845
}
4946

5047
// PackageFromStrings constructs Package from the list of package names provided.
@@ -82,17 +79,6 @@ func PackageFromString(raw string, locker lock.Locker) *Package {
8279
lockfile: locker,
8380
Raw: raw,
8481
normalizedPackageAttributePathCache: "",
85-
sysInfo: nil,
86-
}
87-
88-
if pkg.isVersioned() {
89-
sysInfo, err := locker.SystemInfo(raw)
90-
if err != nil {
91-
// ignoring for coding convenience. TODO savil. handle error
92-
debug.Log("ERROR: failed to get locker.SystemInfo for pkg %s. Error is %s", raw, err)
93-
} else if sysInfo != nil {
94-
pkg.sysInfo = sysInfo
95-
}
9682
}
9783

9884
return pkg
@@ -421,16 +407,51 @@ func (p *Package) version() string {
421407
return version
422408
}
423409

410+
// TODO savil. In next PR, change all callers to IsVersioned. Not doing it in
411+
// this PR to keep the diff scoped.
424412
func (p *Package) isVersioned() bool {
425413
return p.isDevboxPackage() && strings.Contains(p.Path, "@")
426414
}
427415

416+
func (p *Package) IsVersioned() bool {
417+
return p.isVersioned()
418+
}
419+
428420
func (p *Package) hashFromNixPkgsURL() string {
429421
return HashFromNixPkgsURL(p.URLForFlakeInput())
430422
}
431423

432-
func (p *Package) SystemInfo() *lock.SystemInfo {
433-
return p.sysInfo
424+
// BinaryCacheStore is the store from which to fetch this package's binaries.
425+
// It is used as FromStore in builts.fetchClosure.
426+
const BinaryCacheStore = "https://cache.nixos.org"
427+
428+
func (p *Package) IsInFromBinaryStore() bool {
429+
return p.isVersioned()
430+
}
431+
432+
// PathInBinaryStore is the key in the BinaryCacheStore for this package
433+
func (p *Package) PathInBinaryStore() (string, error) {
434+
if !p.IsInFromBinaryStore() {
435+
return "", errors.Errorf("Package %q cannot be fetched from binary cache store", p.Raw)
436+
}
437+
438+
entry, err := p.lockfile.Resolve(p.Raw)
439+
if err != nil {
440+
return "", err
441+
}
442+
443+
userSystem, err := System()
444+
if err != nil {
445+
return "", err
446+
}
447+
448+
sysInfo := entry.Systems[userSystem]
449+
storeDirParts := []string{sysInfo.FromHash, sysInfo.StoreName}
450+
if sysInfo.StoreVersion != "" {
451+
storeDirParts = append(storeDirParts, sysInfo.StoreVersion)
452+
}
453+
storeDir := strings.Join(storeDirParts, "-")
454+
return filepath.Join("/nix/store", storeDir), nil
434455
}
435456

436457
// IsGithubNixpkgsURL returns true if the package is a flake of the form:

internal/nix/input_test.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -131,10 +131,6 @@ func (l *lockfile) Resolve(pkg string) (*lock.Package, error) {
131131
}
132132
}
133133

134-
func (l *lockfile) SystemInfo(pkg string) (*lock.SystemInfo, error) {
135-
return nil, nil
136-
}
137-
138134
func testInputFromString(s, projectDir string) *testInput {
139135
return lo.ToPtr(testInput{Package: *PackageFromString(s, &lockfile{projectDir})})
140136
}

internal/shellgen/flake_input.go

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -65,31 +65,21 @@ func (f *flakeInput) BuildInputs() []string {
6565
// i.e. have a commit hash and always resolve to the same package/version.
6666
// Note: inputs returned by this function include plugin packages. (php only for now)
6767
// It's not entirely clear we always want to add plugin packages to the top level
68-
func flakeInputs(ctx context.Context, devbox devboxer) ([]*flakeInput, error) {
68+
func flakeInputs(ctx context.Context, packages []*nix.Package) ([]*flakeInput, error) {
6969
defer trace.StartRegion(ctx, "flakeInputs").End()
7070

7171
// Use the verbose name flakeInputs to distinguish from `inputs`
7272
// which refer to `nix.Input` in most of the codebase.
7373
flakeInputs := map[string]*flakeInput{}
7474

75-
userInputs := devbox.PackagesAsInputs()
76-
pluginInputs, err := devbox.PluginManager().PluginInputs(userInputs)
77-
if err != nil {
78-
return nil, err
79-
}
80-
81-
// We prioritize plugin packages so that the php plugin works. Not sure
82-
// if this is behavior we want for user plugins. We may need to add an optional
83-
// priority field to the config.
84-
allInputs := append(pluginInputs, userInputs...)
85-
allInputs = lo.Filter(allInputs, func(item *nix.Package, _ int) bool {
86-
// This is temporary, so that we can support the existing flake.nix.tmpl
87-
// as well as the new flake_remove_nixpkgs.nix.tmpl.
88-
return !featureflag.RemoveNixpkgs.Enabled() || item.SystemInfo() == nil
75+
packages = lo.Filter(packages, func(item *nix.Package, _ int) bool {
76+
// Include packages (like local or remote flakes) that cannot be
77+
// fetched from a Binary Cache Store.
78+
return !featureflag.RemoveNixpkgs.Enabled() || !item.IsInFromBinaryStore()
8979
})
9080

9181
order := []string{}
92-
for _, input := range allInputs {
82+
for _, input := range packages {
9383
AttributePath, err := input.FullPackageAttributePath()
9484
if err != nil {
9585
return nil, err

internal/shellgen/flake_package.go

Lines changed: 0 additions & 66 deletions
This file was deleted.

internal/shellgen/flake_plan.go

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"runtime/trace"
66

7+
"github.com/samber/lo"
78
"go.jetpack.io/devbox/internal/nix"
89
)
910

@@ -12,7 +13,7 @@ import (
1213
type flakePlan struct {
1314
NixpkgsInfo *NixpkgsInfo
1415
FlakeInputs []*flakeInput
15-
Packages []*flakePackage
16+
Packages []*nix.Package
1617
System string
1718
}
1819

@@ -39,17 +40,25 @@ func newFlakePlan(ctx context.Context, devbox devboxer) (*flakePlan, error) {
3940
}
4041
}
4142

42-
var err error
43-
flakeInputs, err := flakeInputs(ctx, devbox)
43+
userPackages := devbox.PackagesAsInputs()
44+
pluginPackages, err := devbox.PluginManager().PluginInputs(userPackages)
4445
if err != nil {
4546
return nil, err
4647
}
48+
// We prioritize plugin packages so that the php plugin works. Not sure
49+
// if this is behavior we want for user plugins. We may need to add an optional
50+
// priority field to the config.
51+
packages := append(pluginPackages, userPackages...)
4752

48-
flakePackages, err := flakePackages(devbox)
53+
flakeInputs, err := flakeInputs(ctx, packages)
4954
if err != nil {
5055
return nil, err
5156
}
5257

58+
versionedPackages := lo.Filter(packages, func(pkg *nix.Package, _ int) bool {
59+
return pkg.IsInFromBinaryStore()
60+
})
61+
5362
nixpkgsInfo := getNixpkgsInfo(devbox.Config().NixPkgsCommitHash())
5463

5564
// This is an optimization. Try to reuse the nixpkgs info from the flake
@@ -69,7 +78,7 @@ func newFlakePlan(ctx context.Context, devbox devboxer) (*flakePlan, error) {
6978
return &flakePlan{
7079
FlakeInputs: flakeInputs,
7180
NixpkgsInfo: nixpkgsInfo,
72-
Packages: flakePackages,
81+
Packages: versionedPackages,
7382
System: system,
7483
}, nil
7584
}

internal/shellgen/tmpl/flake_remove_nixpkgs.nix.tmpl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,17 @@
1919
let
2020
pkgs = nixpkgs.legacyPackages.{{ .System }};
2121
{{- range $_, $pkg := .Packages }}
22-
{{ $pkg.Name }}.{{ $.System }} = builtins.fetchClosure{
23-
fromStore = "{{ $pkg.FromStore }}";
24-
fromPath = "{{ $pkg.FromPath }}";
22+
{{ $pkg.PackageAttributePath }}.{{ $.System }} = builtins.fetchClosure{
23+
fromStore = "{{ $pkg.BinaryCacheStore }}";
24+
fromPath = "{{ $pkg.PathInBinaryStore }}";
2525
};
2626
{{- end }}
2727
in
2828
{
2929
devShells.{{ .System }}.default = pkgs.mkShell {
3030
buildInputs = [
3131
{{- range $_, $pkg := .Packages }}
32-
{{ $pkg.Name }}.{{ $.System }}
32+
{{ $pkg.PackageAttributePath }}.{{ $.System }}
3333
{{- end }}
3434
{{- range $_, $flake := .FlakeInputs }}
3535
{{- range $flake.BuildInputs }}

0 commit comments

Comments
 (0)