Skip to content

Commit 625b2f3

Browse files
authored
[lockfile] Use lockfile for legacy packages (#964)
## Summary This changes saves legacy packages to lockfile. It does not save absolute flake references to lockfile (e.g. `github:/my/flake#foo`) but we could in the future. Merging legacy and versioned packages let's us simplify the code quite a bit. Specifically now all inputs are treated like flakes (either explicit or resolved). One complication this exposes is the php and haskell planners. Specifically, each planner creates definitions must be included as packages. As a quick hack I just include the definitions as is at the top of the list. This ensures these take precedence over any other package. It also means that packages can show up twice if these planners are activated. That doesn't break the functionality, it just looks weird in the flake.nix @Lagoja are there any example that exercises the Haskell planner? A few possible follow ups: - [ ] We only create lockfile when packages need to be resolved. This may lead to some perf issues is people use an existing project on a read only platform (e.g. CICD) because not having a lockfile makes things a bit slower. - [ ] Out flake.nix is a bit ugly because now it has multiple duplicate inputs. We can clean this up. ## How was it tested? Need to do a lot more testing but what I did: ``` devbox rm go_1_20 && devbox add go_1_20 rm -rf .devbox devbox install devbox run echo 5 devbox add [email protected] devbox run "python --version" devbox shell ``` Need to test: - [x] LOCKFILE flag off on legacy project - [x] LOCKFILE flag on on legacy project - [x] LOCKFILE flag off on project with lockfile - [x] LOCKFILE flag on on project with lockfile
1 parent 253aacf commit 625b2f3

28 files changed

+263
-228
lines changed

devbox.lock

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"lockfile_version": "1",
3+
"packages": {
4+
"actionlint": {
5+
"last_modified": "",
6+
"resolved": "github:NixOS/nixpkgs/3364b5b117f65fe1ce65a3cdd5612a078a3b31e3#actionlint",
7+
"version": ""
8+
},
9+
"go_1_20": {
10+
"last_modified": "",
11+
"resolved": "github:NixOS/nixpkgs/3364b5b117f65fe1ce65a3cdd5612a078a3b31e3#go_1_20",
12+
"version": ""
13+
},
14+
"golangci-lint": {
15+
"last_modified": "",
16+
"resolved": "github:NixOS/nixpkgs/3364b5b117f65fe1ce65a3cdd5612a078a3b31e3#golangci-lint",
17+
"version": ""
18+
}
19+
}
20+
}

examples/flakes/remote/devbox.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,4 @@
1010
"nixpkgs": {
1111
"commit": "f80ac848e3d6f0c12c52758c0f25c10c97ca3b62"
1212
}
13-
}
13+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package featureflag
2+
3+
var LockFile = disabled("LOCKFILE")

internal/boxcli/featureflag/versionedpkgs.go

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

internal/impl/devbox.go

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -140,15 +140,15 @@ func (d *Devbox) ConfigHash() (string, error) {
140140
return d.cfg.Hash()
141141
}
142142

143+
func (d *Devbox) NixPkgsCommitHash() string {
144+
return d.cfg.Nixpkgs.Commit
145+
}
146+
143147
func (d *Devbox) ShellPlan() (*plansdk.ShellPlan, error) {
144148
shellPlan := planner.GetShellPlan(d.projectDir, d.packages())
145-
shellPlan.DevPackages = lo.Uniq(append(d.localPackages(), shellPlan.DevPackages...))
146149
shellPlan.FlakeInputs = d.flakeInputs()
147150

148-
nixpkgsInfo, err := plansdk.GetNixpkgsInfo(d.cfg.Nixpkgs.Commit)
149-
if err != nil {
150-
return nil, err
151-
}
151+
nixpkgsInfo := plansdk.GetNixpkgsInfo(d.cfg.Nixpkgs.Commit)
152152
shellPlan.NixpkgsInfo = nixpkgsInfo
153153

154154
return shellPlan, nil
@@ -628,7 +628,7 @@ func (d *Devbox) StartProcessManager(
628628
processComposePath, err := utilityLookPath("process-compose")
629629
if err != nil {
630630
fmt.Fprintln(d.writer, "Installing process-compose. This may take a minute but will only happen once.")
631-
if err = d.addDevboxUtilityPackage("process-compose"); err != nil {
631+
if err = d.addDevboxUtilityPackage("github:F1bonacc1/process-compose/v0.43.1"); err != nil {
632632
return err
633633
}
634634

@@ -937,14 +937,6 @@ func (d *Devbox) packages() []string {
937937
return d.cfg.Packages
938938
}
939939

940-
// TODO(landau): localPackages, and flakeInput packages could
941-
// be merged into a single buildInput map of the form: source => []pkg
942-
func (d *Devbox) localPackages() []string {
943-
return lo.Filter(d.cfg.Packages, func(pkg string, _ int) bool {
944-
return !nix.InputFromString(pkg, d.lockfile).IsFlake()
945-
})
946-
}
947-
948940
// configEnvs takes the computed env variables (nix + plugin) and adds env
949941
// variables defined in Config. It also parses variables in config
950942
// that are referenced by $VAR or ${VAR} and replaces them with

internal/impl/flakes.go

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,26 +10,28 @@ import (
1010
"go.jetpack.io/devbox/internal/planner/plansdk"
1111
)
1212

13+
// flakeInputs returns a list of flake inputs for the top level flake.nix
14+
// created by devbox. We map packages to the correct flake and attribute path
15+
// and group flakes by URL to avoid duplication. All inputs should be locked
16+
// i.e. have a commit hash and always resolve to the same package/version.
1317
func (d *Devbox) flakeInputs() []*plansdk.FlakeInput {
1418
inputs := map[string]*plansdk.FlakeInput{}
1519
for _, p := range d.packages() {
1620
pkg := nix.InputFromString(p, d.lockfile)
17-
if pkg.IsFlake() {
18-
AttributePath, err := pkg.PackageAttributePath()
19-
if err != nil {
20-
panic(err)
21-
}
22-
if input, ok := inputs[pkg.URLForInput()]; !ok {
23-
inputs[pkg.URLForInput()] = &plansdk.FlakeInput{
24-
Name: pkg.Name(),
25-
URL: pkg.URLForInput(),
26-
Packages: []string{AttributePath},
27-
}
28-
} else {
29-
input.Packages = lo.Uniq(
30-
append(inputs[pkg.URLForInput()].Packages, AttributePath),
31-
)
21+
AttributePath, err := pkg.PackageAttributePath()
22+
if err != nil {
23+
panic(err)
24+
}
25+
if input, ok := inputs[pkg.URLForInput()]; !ok {
26+
inputs[pkg.URLForInput()] = &plansdk.FlakeInput{
27+
Name: pkg.Name(),
28+
URL: pkg.URLForInput(),
29+
Packages: []string{AttributePath},
3230
}
31+
} else {
32+
input.Packages = lo.Uniq(
33+
append(inputs[pkg.URLForInput()].Packages, AttributePath),
34+
)
3335
}
3436
}
3537

internal/impl/generate.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ func (d *Devbox) generateShellFiles() error {
6060
}
6161
}
6262

63-
for _, pkg := range plan.DevPackages {
63+
for _, pkg := range d.packages() {
6464
if err := d.pluginManager.CreateFilesAndShowReadme(d.writer, pkg, d.projectDir); err != nil {
6565
return err
6666
}

internal/impl/generate_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ var testFlakeTmplPlan = &struct {
8080
NixpkgsInfo struct {
8181
URL string
8282
}
83-
Definitions []string
83+
Definitions map[string]string
8484
DevPackages []string
8585
FlakeInputs []plansdk.FlakeInput
8686
}{
@@ -89,9 +89,9 @@ var testFlakeTmplPlan = &struct {
8989
}{
9090
URL: "https://github.com/nixos/nixpkgs/archive/b9c00c1d41ccd6385da243415299b39aa73357be.tar.gz",
9191
},
92-
Definitions: []string{
93-
"php = pkgs.php.withExtensions ({ enabled, all }: enabled ++ (with all; [ blackfire ]));",
94-
"php81Packages.composer = php.packages.composer;",
92+
Definitions: map[string]string{
93+
"php": "pkgs.php.withExtensions ({ enabled, all }: enabled ++ (with all; [ blackfire ]));",
94+
"php81Packages.composer": "php.packages.composer;",
9595
},
9696
DevPackages: []string{
9797
"php",

internal/impl/global.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ func (d *Devbox) AddGlobal(pkgs ...string) error {
3535

3636
// validate all packages exist. Don't install anything if any are missing
3737
for _, pkg := range pkgs {
38-
found, err := nix.PkgExists(plansdk.DefaultNixpkgsCommit, pkg, d.lockfile)
38+
found, err := nix.PkgExists(pkg, d.lockfile)
3939
if err != nil {
4040
return err
4141
} else if !found {
@@ -55,7 +55,6 @@ func (d *Devbox) AddGlobal(pkgs ...string) error {
5555
err = nix.ProfileInstall(&nix.ProfileInstallArgs{
5656
CustomStepMessage: stepMsg,
5757
Lockfile: d.lockfile,
58-
NixpkgsCommit: d.cfg.Nixpkgs.Commit,
5958
Package: pkg,
6059
ProfilePath: profilePath,
6160
Writer: d.writer,

internal/impl/packages.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ func (d *Devbox) Add(ctx context.Context, pkgs ...string) error {
3939

4040
// Check packages are valid before adding.
4141
for _, pkg := range pkgs {
42-
ok, err := nix.PkgExists(d.cfg.Nixpkgs.Commit, pkg, d.lockfile)
42+
ok, err := nix.PkgExists(pkg, d.lockfile)
4343
if err != nil {
4444
return err
4545
}
@@ -237,7 +237,6 @@ func (d *Devbox) addPackagesToProfile(ctx context.Context, mode installMode) err
237237
if err := nix.ProfileInstall(&nix.ProfileInstallArgs{
238238
CustomStepMessage: stepMsg,
239239
Lockfile: d.lockfile,
240-
NixpkgsCommit: d.cfg.Nixpkgs.Commit,
241240
Package: pkg,
242241
ProfilePath: profileDir,
243242
Writer: d.writer,

0 commit comments

Comments
 (0)