@@ -12,6 +12,7 @@ import (
1212
1313 "github.com/AlecAivazis/survey/v2"
1414 "go.jetpack.io/devbox/internal/build"
15+ "go.jetpack.io/devbox/internal/debug"
1516 "go.jetpack.io/devbox/internal/devbox/providers/identity"
1617 "go.jetpack.io/devbox/internal/envir"
1718 "go.jetpack.io/devbox/internal/fileutil"
@@ -31,18 +32,30 @@ func Get() *Provider {
3132 return singleton
3233}
3334
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+
3538 rootConfig , err := p .rootAWSConfigPath ()
3639 if err != nil {
3740 return err
3841 }
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 {
4154 return nil
4255 }
4356
4457 if os .Getuid () == 0 {
45- err := p .configureRoot (username )
58+ err := p .configureRoot (ctx , username )
4659 if err != nil {
4760 return redact .Errorf ("update ~root/.aws/config with devbox credentials: %s" , err )
4861 }
@@ -72,7 +85,7 @@ func (p *Provider) rootAWSConfigPath() (string, error) {
7285 return filepath .Join (u .HomeDir , ".aws" , "config" ), nil
7386}
7487
75- func (p * Provider ) configureRoot (username string ) error {
88+ func (p * Provider ) configureRoot (ctx context. Context , username string ) error {
7689 exe := p .executable ()
7790 if exe == "" {
7891 return redact .Errorf ("get path to current devbox executable" )
@@ -113,7 +126,14 @@ credential_process = %s -u %s -i %s cache credentials
113126 if err != nil {
114127 return err
115128 }
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
117137}
118138
119139func (p * Provider ) sudoConfigureRoot (ctx context.Context , username string ) error {
@@ -140,9 +160,14 @@ func (p *Provider) sudoConfigureRoot(ctx context.Context, username string) error
140160 cmd .Stdout = os .Stdout
141161 cmd .Stderr = os .Stderr
142162
163+ debug .Log ("running sudo: %s" , cmd )
143164 if err := cmd .Run (); err != nil {
144165 return fmt .Errorf ("failed to relaunch with sudo: %w" , err )
145166 }
167+
168+ // Print a warning if we were unable to automatically make the user
169+ // trusted.
170+ checkIfUserCanAddSubstituter (ctx )
146171 return nil
147172}
148173
@@ -205,9 +230,6 @@ func (p *Provider) URI(ctx context.Context) (string, error) {
205230 if err != nil {
206231 return "" , redact .Errorf ("nixcache: get uri: %w" , redact .Safe (err ))
207232 }
208- if uri != "" {
209- checkIfUserCanAddSubstituter (ctx )
210- }
211233 return uri , nil
212234}
213235
@@ -227,7 +249,12 @@ func checkIfUserCanAddSubstituter(ctx context.Context) {
227249 if err != nil {
228250 return
229251 }
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 )
231258 if ! trusted {
232259 ux .Fwarning (
233260 os .Stderr ,
0 commit comments