Skip to content

Commit 5fb249f

Browse files
committed
always recompute Devbox environment, and reuse buildInputs from print-dev-env output
1 parent 00ad797 commit 5fb249f

File tree

2 files changed

+14
-25
lines changed

2 files changed

+14
-25
lines changed

internal/devbox/nixprofile.go

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package devbox
22

33
import (
44
"context"
5-
"encoding/json"
65
"fmt"
76
"strings"
87

@@ -12,39 +11,31 @@ import (
1211

1312
// syncFlakeToProfile ensures the buildInputs from the flake's devShell are
1413
// installed in the nix profile.
15-
func (d *Devbox) syncFlakeToProfile(ctx context.Context) error {
14+
// buildInputs is a space-separated list of store paths from the nix print-dev-env output's buildInputs.
15+
func (d *Devbox) syncFlakeToProfile(ctx context.Context, buildInputs string) error {
1616
profilePath, err := d.profilePath()
1717
if err != nil {
1818
return err
1919
}
2020

21-
// Get the build inputs (i.e. store paths) from the generated flake's devShell.
22-
buildInputPaths, err := nix.Eval(
23-
ctx,
24-
d.stderr,
25-
d.flakeDir()+"#devShells."+nix.System()+".default.buildInputs",
26-
"--json",
27-
)
28-
if err != nil {
29-
return fmt.Errorf("nix eval devShells: %v", err)
30-
}
31-
storePaths := []string{}
32-
if err := json.Unmarshal(buildInputPaths, &storePaths); err != nil {
33-
return fmt.Errorf("unmarshal store paths: %s: %v", buildInputPaths, err)
21+
// Get the build inputs (i.e. store paths) from the generated flake's print-dev-env output
22+
wantStorePaths := []string{}
23+
if buildInputs != "" { // if buildInputs is empty, then we don't want wantStorePaths to be an array with a single "" entry
24+
wantStorePaths = strings.Split(buildInputs, " ")
3425
}
3526

3627
// Get the store-paths of the packages currently installed in the nix profile
3728
items, err := nixprofile.ProfileListItems(ctx, d.stderr, profilePath)
3829
if err != nil {
3930
return fmt.Errorf("nix profile list: %v", err)
4031
}
41-
got := make([]string, 0, len(items))
32+
gotStorePaths := make([]string, 0, len(items))
4233
for _, item := range items {
43-
got = append(got, item.StorePaths()...)
34+
gotStorePaths = append(gotStorePaths, item.StorePaths()...)
4435
}
4536

4637
// Diff the store paths and install/remove packages as needed
47-
add, remove := diffStorePaths(got, storePaths)
38+
add, remove := diffStorePaths(gotStorePaths, wantStorePaths)
4839
if len(remove) > 0 {
4940
packagesToRemove := make([]string, 0, len(remove))
5041
for _, p := range remove {

internal/devbox/packages.go

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -292,17 +292,15 @@ func (d *Devbox) ensureStateIsUpToDate(ctx context.Context, mode installMode) er
292292
return err
293293
}
294294

295-
// Skip the print-dev-env's cache if we are in a devbox-environment. If not,
296-
// skip the cache if we're either installing packages or ensuring
297-
// the project state is up-to-date.
298-
skipPrintDevEnvCache := true
299-
// skipPrintDevEnvCache := d.IsEnvEnabled() || (mode == ensure || mode == install)
300-
if _, err := d.computeEnv(ctx, !skipPrintDevEnvCache /*usePrintDevEnvCache*/); err != nil {
295+
// Always re-compute print-dev-env to ensure all packages are installed, and
296+
// the correct set of packages are reflected in the nix-profile below.
297+
env, err := d.computeEnv(ctx, false /*usePrintDevEnvCache*/)
298+
if err != nil {
301299
return err
302300
}
303301

304302
// Ensure the nix profile has the packages from the flake.
305-
if err := d.syncFlakeToProfile(ctx); err != nil {
303+
if err := d.syncFlakeToProfile(ctx, env["buildInputs"]); err != nil {
306304
return err
307305
}
308306

0 commit comments

Comments
 (0)