@@ -12,6 +12,7 @@ import (
12
12
13
13
"github.com/AlecAivazis/survey/v2"
14
14
"go.jetpack.io/devbox/internal/build"
15
+ "go.jetpack.io/devbox/internal/debug"
15
16
"go.jetpack.io/devbox/internal/devbox/providers/identity"
16
17
"go.jetpack.io/devbox/internal/envir"
17
18
"go.jetpack.io/devbox/internal/fileutil"
@@ -31,18 +32,30 @@ func Get() *Provider {
31
32
return singleton
32
33
}
33
34
34
- func (p * Provider ) ConfigureAWS (ctx context.Context , username string ) error {
35
+ func (p * Provider ) Configure (ctx context.Context , username string ) error {
36
+ debug .Log ("checking if nix cache is configured for %s" , username )
37
+
35
38
rootConfig , err := p .rootAWSConfigPath ()
36
39
if err != nil {
37
40
return err
38
41
}
39
- if fileutil .Exists (rootConfig ) {
40
- // Already configured.
42
+ debug .Log ("root aws config path is: %s" , rootConfig )
43
+ awsConfigExists := fileutil .Exists (rootConfig )
44
+
45
+ cfg , err := nix .CurrentConfig (ctx )
46
+ if err != nil {
47
+ return err
48
+ }
49
+ trusted , _ := cfg .IsUserTrusted (ctx , username )
50
+
51
+ configured := awsConfigExists && trusted
52
+ debug .Log ("nix cache configured = %v (awsConfigExists == %v && trusted == %v)" , configured , awsConfigExists , trusted )
53
+ if configured {
41
54
return nil
42
55
}
43
56
44
57
if os .Getuid () == 0 {
45
- err := p .configureRoot (username )
58
+ err := p .configureRoot (ctx , username )
46
59
if err != nil {
47
60
return redact .Errorf ("update ~root/.aws/config with devbox credentials: %s" , err )
48
61
}
@@ -72,7 +85,7 @@ func (p *Provider) rootAWSConfigPath() (string, error) {
72
85
return filepath .Join (u .HomeDir , ".aws" , "config" ), nil
73
86
}
74
87
75
- func (p * Provider ) configureRoot (username string ) error {
88
+ func (p * Provider ) configureRoot (ctx context. Context , username string ) error {
76
89
exe := p .executable ()
77
90
if exe == "" {
78
91
return redact .Errorf ("get path to current devbox executable" )
@@ -113,7 +126,14 @@ credential_process = %s -u %s -i %s cache credentials
113
126
if err != nil {
114
127
return err
115
128
}
116
- return config .Close ()
129
+ if err := config .Close (); err != nil {
130
+ return err
131
+ }
132
+
133
+ if err := nix .IncludeDevboxConfig (ctx , username ); err != nil {
134
+ return redact .Errorf ("modify nix config: %v" , err )
135
+ }
136
+ return nil
117
137
}
118
138
119
139
func (p * Provider ) sudoConfigureRoot (ctx context.Context , username string ) error {
@@ -140,9 +160,14 @@ func (p *Provider) sudoConfigureRoot(ctx context.Context, username string) error
140
160
cmd .Stdout = os .Stdout
141
161
cmd .Stderr = os .Stderr
142
162
163
+ debug .Log ("running sudo: %s" , cmd )
143
164
if err := cmd .Run (); err != nil {
144
165
return fmt .Errorf ("failed to relaunch with sudo: %w" , err )
145
166
}
167
+
168
+ // Print a warning if we were unable to automatically make the user
169
+ // trusted.
170
+ checkIfUserCanAddSubstituter (ctx )
146
171
return nil
147
172
}
148
173
@@ -205,9 +230,6 @@ func (p *Provider) URI(ctx context.Context) (string, error) {
205
230
if err != nil {
206
231
return "" , redact .Errorf ("nixcache: get uri: %w" , redact .Safe (err ))
207
232
}
208
- if uri != "" {
209
- checkIfUserCanAddSubstituter (ctx )
210
- }
211
233
return uri , nil
212
234
}
213
235
@@ -227,7 +249,12 @@ func checkIfUserCanAddSubstituter(ctx context.Context) {
227
249
if err != nil {
228
250
return
229
251
}
230
- trusted , _ := cfg .IsUserTrusted (ctx )
252
+
253
+ u , err := user .Current ()
254
+ if err != nil {
255
+ return
256
+ }
257
+ trusted , _ := cfg .IsUserTrusted (ctx , u .Username )
231
258
if ! trusted {
232
259
ux .Fwarning (
233
260
os .Stderr ,
0 commit comments