Skip to content

[lockfile] add lockfile.Tidy and remove devboxProject embed #1120

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions internal/impl/devbox.go
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ func (d *Devbox) Info(pkg string, markdown bool) error {
return errors.WithStack(err)
}
return plugin.PrintReadme(
nix.InputFromString(pkg, d.lockfile),
nix.InputFromString(pkg, d.projectDir, d.lockfile),
d.projectDir,
d.writer,
markdown,
Expand Down Expand Up @@ -974,13 +974,13 @@ func (d *Devbox) Packages() []string {
}

func (d *Devbox) packagesAsInputs() []*nix.Input {
return nix.InputsFromStrings(d.Packages(), d.lockfile)
return nix.InputsFromStrings(d.Packages(), d.projectDir, d.lockfile)
}

func (d *Devbox) findPackageByName(name string) (string, error) {
results := map[string]bool{}
for _, pkg := range d.cfg.Packages {
i := nix.InputFromString(pkg, d.lockfile)
i := nix.InputFromString(pkg, d.projectDir, d.lockfile)
if i.String() == name || i.CanonicalName() == name {
results[i.String()] = true
}
Expand Down
8 changes: 5 additions & 3 deletions internal/impl/packages.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func (d *Devbox) Add(ctx context.Context, pkgsNames ...string) error {
// Only add packages that are not already in config. If same canonical exists,
// replace it.
pkgs := []*nix.Input{}
for _, pkg := range nix.InputsFromStrings(lo.Uniq(pkgsNames), d.lockfile) {
for _, pkg := range nix.InputsFromStrings(lo.Uniq(pkgsNames), d.projectDir, d.lockfile) {
versioned := pkg.Versioned()

// If exact versioned package is already in the config, skip.
Expand All @@ -56,7 +56,7 @@ func (d *Devbox) Add(ctx context.Context, pkgsNames ...string) error {
}
}

pkgs = append(pkgs, nix.InputFromString(versioned, d.lockfile))
pkgs = append(pkgs, nix.InputFromString(versioned, d.projectDir, d.lockfile))
d.cfg.Packages = append(d.cfg.Packages, versioned)
}

Expand Down Expand Up @@ -199,6 +199,8 @@ func (d *Devbox) ensurePackagesAreInstalled(ctx context.Context, mode installMod
return err
}

d.lockfile.Tidy(d)

if err = d.lockfile.Save(); err != nil {
return err
}
Expand Down Expand Up @@ -285,7 +287,7 @@ func (d *Devbox) removePackagesFromProfile(ctx context.Context, pkgs []string) e
return err
}

for _, input := range nix.InputsFromStrings(pkgs, d.lockfile) {
for _, input := range nix.InputsFromStrings(pkgs, d.projectDir, d.lockfile) {
index, err := nix.ProfileListIndex(&nix.ProfileListIndexArgs{
Lockfile: d.lockfile,
Writer: d.writer,
Expand Down
2 changes: 1 addition & 1 deletion internal/lock/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package lock
type devboxProject interface {
ConfigHash() (string, error)
NixPkgsCommitHash() string
Packages() []string
ProjectDir() string
}

Expand All @@ -14,7 +15,6 @@ type resolver interface {
}

type Locker interface {
devboxProject
resolver
LegacyNixpkgsPath(string) string
}
23 changes: 18 additions & 5 deletions internal/lock/lockfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"path/filepath"
"strings"

"github.com/samber/lo"
"go.jetpack.io/devbox/internal/boxcli/featureflag"
"go.jetpack.io/devbox/internal/cuecfg"
)
Expand All @@ -18,7 +19,7 @@ const lockFileVersion = "1"

// Lightly inspired by package-lock.json
type File struct {
devboxProject
project devboxProject
resolver

LockFileVersion string `json:"lockfile_version"`
Expand All @@ -34,8 +35,8 @@ type Package struct {

func GetFile(project devboxProject, resolver resolver) (*File, error) {
lockFile := &File{
devboxProject: project,
resolver: resolver,
project: project,
resolver: resolver,

LockFileVersion: lockFileVersion,
Packages: map[string]*Package{},
Expand Down Expand Up @@ -99,17 +100,21 @@ func (l *File) Save() error {
return nil
}

return cuecfg.WriteFile(lockFilePath(l), l)
return cuecfg.WriteFile(lockFilePath(l.project), l)
}

func (l *File) LegacyNixpkgsPath(pkg string) string {
return fmt.Sprintf(
"github:NixOS/nixpkgs/%s#%s",
l.NixPkgsCommitHash(),
l.project.NixPkgsCommitHash(),
pkg,
)
}

func (l *File) Tidy(project devboxProject) {
l.Packages = lo.PickByKeys(l.Packages, project.Packages())
}

func IsVersionedPackage(pkg string) bool {
name, version, found := strings.Cut(pkg, "@")
return found && name != "" && version != ""
Expand Down Expand Up @@ -137,3 +142,11 @@ func getLockfileHash(project devboxProject) (string, error) {
}
return cuecfg.FileHash(lockFilePath(project))
}

func (l *File) ConfigHash() (string, error) {
return l.ConfigHash()
}

func (l *File) NixPkgsCommitHash() string {
return l.NixPkgsCommitHash()
}
8 changes: 4 additions & 4 deletions internal/nix/input.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,20 @@ type Input struct {
Raw string
}

func InputsFromStrings(names []string, l lock.Locker) []*Input {
func InputsFromStrings(names []string, projectDir string, l lock.Locker) []*Input {
inputs := []*Input{}
for _, name := range names {
inputs = append(inputs, InputFromString(name, l))
inputs = append(inputs, InputFromString(name, projectDir, l))
}
return inputs
}

func InputFromString(raw string, locker lock.Locker) *Input {
func InputFromString(raw string, projectDir string, locker lock.Locker) *Input {
u, _ := url.Parse(raw)
if u.Path == "" && u.Opaque != "" && u.Scheme == "path" {
// This normalizes url paths to be absolute. It also ensures all
// path urls have a single slash (instead of possibly 3 slashes)
normalizedURL := "path:" + filepath.Join(locker.ProjectDir(), u.Opaque)
normalizedURL := "path:" + filepath.Join(projectDir, u.Opaque)
if u.Fragment != "" {
normalizedURL += "#" + u.Fragment
}
Expand Down
6 changes: 4 additions & 2 deletions internal/nix/profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ type ProfileListIndexArgs struct {
Writer io.Writer
Input *Input
ProfileDir string
ProjectDir string
}

func ProfileListIndex(args *ProfileListIndexArgs) (int, error) {
Expand All @@ -107,7 +108,7 @@ func ProfileListIndex(args *ProfileListIndexArgs) (int, error) {
}

for _, item := range list {
existing := InputFromString(item.unlockedReference, args.Lockfile)
existing := InputFromString(item.unlockedReference, args.ProjectDir, args.Lockfile)

if args.Input.equals(existing) {
return item.index, nil
Expand Down Expand Up @@ -207,13 +208,14 @@ type ProfileInstallArgs struct {
ExtraFlags []string
Lockfile *lock.File
Package string
ProjectDir string
ProfilePath string
Writer io.Writer
}

// ProfileInstall calls nix profile install with default profile
func ProfileInstall(args *ProfileInstallArgs) error {
input := InputFromString(args.Package, args.Lockfile)
input := InputFromString(args.Package, args.ProjectDir, args.Lockfile)
if IsGithubNixpkgsURL(input.URLForInput()) {
if err := ensureNixpkgsPrefetched(args.Writer, input.hashFromNixPkgsURL()); err != nil {
return err
Expand Down
4 changes: 0 additions & 4 deletions internal/nix/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,6 @@ import (
var ErrPackageNotFound = errors.New("package not found")
var ErrPackageNotInstalled = errors.New("package not installed")

func PkgExists(pkg string, lock *lock.File) (bool, error) {
return InputFromString(pkg, lock).ValidateExists()
}

type Info struct {
// attribute key is different in flakes vs legacy so we should only use it
// if we know exactly which version we are using
Expand Down
2 changes: 1 addition & 1 deletion internal/plugin/includes.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@ func (m *Manager) parseInclude(include string) (*nix.Input, error) {
} else if name == "" {
return nil, usererr.New("include name is required")
}
return nix.InputFromString(name, m.lockfile), nil
return nix.InputFromString(name, m.ProjectDir(), m.lockfile), nil
}
2 changes: 1 addition & 1 deletion internal/plugin/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func (m *Manager) PluginPackages(inputs []*nix.Input) ([]*nix.Input, error) {
} else if config == nil {
continue
}
pkgs = append(pkgs, nix.InputsFromStrings(config.Packages, m.lockfile)...)
pkgs = append(pkgs, nix.InputsFromStrings(config.Packages, m.ProjectDir(), m.lockfile)...)
}
return pkgs, nil
}