@@ -4,28 +4,24 @@ import (
4
4
"context"
5
5
"encoding/json"
6
6
"fmt"
7
- "os/exec"
8
7
"slices"
9
8
10
9
"go.jetpack.io/devbox/internal/nix"
11
10
)
12
11
13
12
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 ()
17
15
if err != nil {
18
-
19
16
return fmt .Errorf ("nix eval devShells: %v" , err )
20
17
}
21
18
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 )
24
21
}
25
22
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 ()
29
25
if err != nil {
30
26
return err
31
27
}
@@ -34,7 +30,7 @@ func syncFlakeToProfile(ctx context.Context, flakePath, profilePath string) erro
34
30
StorePaths []string
35
31
}
36
32
}
37
- if err := json .Unmarshal (b , & profile ); err != nil {
33
+ if err := json .Unmarshal (buildInputPaths , & profile ); err != nil {
38
34
return fmt .Errorf ("unmarshal profile: %v" , err )
39
35
}
40
36
got := make ([]string , 0 , len (profile .Elements ))
@@ -44,17 +40,15 @@ func syncFlakeToProfile(ctx context.Context, flakePath, profilePath string) erro
44
40
45
41
add , remove := diffStorePaths (got , storePaths )
46
42
if len (remove ) > 0 {
47
- removeCmd := exec .CommandContext (ctx , "nix" , "profile" , "remove" , "--profile" , profilePath )
43
+ removeCmd := nix .CommandContext (ctx , "nix" , "profile" , "remove" , "--profile" , profilePath )
48
44
removeCmd .Args = append (removeCmd .Args , remove ... )
49
- removeCmd .Args = append (removeCmd .Args , nix .ExperimentalFlags ()... )
50
45
if err := removeCmd .Run (); err != nil {
51
46
return err
52
47
}
53
48
}
54
49
if len (add ) > 0 {
55
- addCmd := exec .CommandContext (ctx , "nix" , "profile" , "install" , "--profile" , profilePath )
50
+ addCmd := nix .CommandContext (ctx , "nix" , "profile" , "install" , "--profile" , profilePath )
56
51
addCmd .Args = append (addCmd .Args , add ... )
57
- addCmd .Args = append (addCmd .Args , nix .ExperimentalFlags ()... )
58
52
if err := addCmd .Run (); err != nil {
59
53
return err
60
54
}
@@ -66,27 +60,27 @@ func diffStorePaths(got, want []string) (add, remove []string) {
66
60
slices .Sort (got )
67
61
slices .Sort (want )
68
62
69
- var g , w int
63
+ var gotIdx , wantIdx int
70
64
for {
71
- if g >= len (got ) {
72
- add = append (add , want [w :]... )
65
+ if gotIdx >= len (got ) {
66
+ add = append (add , want [wantIdx :]... )
73
67
break
74
68
}
75
- if w >= len (want ) {
76
- remove = append (remove , got [g :]... )
69
+ if wantIdx >= len (want ) {
70
+ remove = append (remove , got [gotIdx :]... )
77
71
break
78
72
}
79
73
80
74
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 ++
90
84
}
91
85
}
92
86
return add , remove
0 commit comments