Skip to content

Commit 4ee4487

Browse files
committed
fixes
1 parent a1eb81d commit 4ee4487

File tree

5 files changed

+27
-34
lines changed

5 files changed

+27
-34
lines changed

internal/devbox/nixprofile.go

Lines changed: 23 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,24 @@ import (
44
"context"
55
"encoding/json"
66
"fmt"
7-
"os/exec"
87
"slices"
98

109
"go.jetpack.io/devbox/internal/nix"
1110
)
1211

1312
func syncFlakeToProfile(ctx context.Context, flakePath, profilePath string) error {
14-
cmd := exec.CommandContext(ctx, "nix", "eval", "--json", flakePath+"#devShells."+nix.System()+".default.buildInputs")
15-
cmd.Args = append(cmd.Args, nix.ExperimentalFlags()...)
16-
b, err := cmd.Output()
13+
cmd := nix.CommandContext(ctx, "nix", "eval", "--json", flakePath+"#devShells."+nix.System()+".default.buildInputs")
14+
buildInputPaths, err := cmd.Output()
1715
if err != nil {
18-
1916
return fmt.Errorf("nix eval devShells: %v", err)
2017
}
2118
storePaths := []string{}
22-
if err := json.Unmarshal(b, &storePaths); err != nil {
23-
return fmt.Errorf("unmarshal store paths: %s: %v", b, err)
19+
if err := json.Unmarshal(buildInputPaths, &storePaths); err != nil {
20+
return fmt.Errorf("unmarshal store paths: %s: %v", buildInputPaths, err)
2421
}
2522

26-
listCmd := exec.CommandContext(ctx, "nix", "profile", "list", "--json", "--profile", profilePath)
27-
listCmd.Args = append(listCmd.Args, nix.ExperimentalFlags()...)
28-
b, err = listCmd.Output()
23+
listCmd := nix.CommandContext(ctx, "nix", "profile", "list", "--json", "--profile", profilePath)
24+
buildInputPaths, err = listCmd.Output()
2925
if err != nil {
3026
return err
3127
}
@@ -34,7 +30,7 @@ func syncFlakeToProfile(ctx context.Context, flakePath, profilePath string) erro
3430
StorePaths []string
3531
}
3632
}
37-
if err := json.Unmarshal(b, &profile); err != nil {
33+
if err := json.Unmarshal(buildInputPaths, &profile); err != nil {
3834
return fmt.Errorf("unmarshal profile: %v", err)
3935
}
4036
got := make([]string, 0, len(profile.Elements))
@@ -44,17 +40,15 @@ func syncFlakeToProfile(ctx context.Context, flakePath, profilePath string) erro
4440

4541
add, remove := diffStorePaths(got, storePaths)
4642
if len(remove) > 0 {
47-
removeCmd := exec.CommandContext(ctx, "nix", "profile", "remove", "--profile", profilePath)
43+
removeCmd := nix.CommandContext(ctx, "nix", "profile", "remove", "--profile", profilePath)
4844
removeCmd.Args = append(removeCmd.Args, remove...)
49-
removeCmd.Args = append(removeCmd.Args, nix.ExperimentalFlags()...)
5045
if err := removeCmd.Run(); err != nil {
5146
return err
5247
}
5348
}
5449
if len(add) > 0 {
55-
addCmd := exec.CommandContext(ctx, "nix", "profile", "install", "--profile", profilePath)
50+
addCmd := nix.CommandContext(ctx, "nix", "profile", "install", "--profile", profilePath)
5651
addCmd.Args = append(addCmd.Args, add...)
57-
addCmd.Args = append(addCmd.Args, nix.ExperimentalFlags()...)
5852
if err := addCmd.Run(); err != nil {
5953
return err
6054
}
@@ -66,27 +60,27 @@ func diffStorePaths(got, want []string) (add, remove []string) {
6660
slices.Sort(got)
6761
slices.Sort(want)
6862

69-
var g, w int
63+
var gotIdx, wantIdx int
7064
for {
71-
if g >= len(got) {
72-
add = append(add, want[w:]...)
65+
if gotIdx >= len(got) {
66+
add = append(add, want[wantIdx:]...)
7367
break
7468
}
75-
if w >= len(want) {
76-
remove = append(remove, got[g:]...)
69+
if wantIdx >= len(want) {
70+
remove = append(remove, got[gotIdx:]...)
7771
break
7872
}
7973

8074
switch {
81-
case got[g] == want[w]:
82-
g++
83-
w++
84-
case got[g] < want[w]:
85-
remove = append(remove, got[g])
86-
g++
87-
case got[g] > want[w]:
88-
add = append(add, want[w])
89-
w++
75+
case got[gotIdx] == want[wantIdx]:
76+
gotIdx++
77+
wantIdx++
78+
case got[gotIdx] < want[wantIdx]:
79+
remove = append(remove, got[gotIdx])
80+
gotIdx++
81+
case got[gotIdx] > want[wantIdx]:
82+
add = append(add, want[wantIdx])
83+
wantIdx++
9084
}
9185
}
9286
return add, remove

internal/nix/command.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ import (
66
)
77

88
func command(args ...string) *exec.Cmd {
9-
return commandContext(context.Background(), args...)
9+
return CommandContext(context.Background(), args...)
1010
}
1111

12-
func commandContext(ctx context.Context, args ...string) *exec.Cmd {
12+
func CommandContext(ctx context.Context, args ...string) *exec.Cmd {
1313
cmd := exec.CommandContext(ctx, "nix", args...)
1414
cmd.Args = append(cmd.Args, ExperimentalFlags()...)
1515
return cmd

internal/nix/store.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
)
77

88
func StorePathFromHashPart(ctx context.Context, hash, storeAddr string) (string, error) {
9-
cmd := commandContext(ctx, "store", "path-from-hash-part", "--store", storeAddr, hash)
9+
cmd := CommandContext(ctx, "store", "path-from-hash-part", "--store", storeAddr, hash)
1010
resultBytes, err := cmd.Output()
1111
if err != nil {
1212
return "", err

testscripts/languages/php.test.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ stdout 'done\n'
1818
{
1919
"packages": [
2020
"php81@latest",
21-
"php81Packages.composer@latest",
2221
"php81Extensions.ds@latest"
2322
]
2423
}

testscripts/packages/unfree.test.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@ exec devbox init
44

55
# we could test with slack and/or vscode. Using slack since it is lighter.
66
exec devbox add slack
7-
stderr 'Installing package: slack.'
7+
stderr 'Adding package "slack@latest" to devbox.json'
88

99
exec devbox rm slack

0 commit comments

Comments
 (0)