Skip to content

Commit 8e8cfc1

Browse files
authored
internal/devbox: fix warning when user refuses cache setup (#2095)
Don't add extra substituters when the user answers "no" to the confirmation prompt for configuring Nix caches. This stops Nix from printing warnings such as: warning: ignoring untrusted substituter 's3://mycache', you are not a trusted user. Run man nix.conf for more information on the substituters configuration option. Also print a message saying that `devbox cache configure` can be used to configure the cache after answering no.
1 parent 7003726 commit 8e8cfc1

File tree

2 files changed

+67
-47
lines changed

2 files changed

+67
-47
lines changed

internal/devbox/packages.go

Lines changed: 52 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
"io"
1010
"io/fs"
1111
"os"
12-
"os/user"
1312
"path/filepath"
1413
"runtime/trace"
1514
"slices"
@@ -25,10 +24,10 @@ import (
2524
"go.jetpack.io/devbox/internal/devpkg"
2625
"go.jetpack.io/devbox/internal/devpkg/pkgtype"
2726
"go.jetpack.io/devbox/internal/lock"
28-
"go.jetpack.io/devbox/internal/redact"
27+
"go.jetpack.io/devbox/internal/setup"
2928
"go.jetpack.io/devbox/internal/shellgen"
3029
"go.jetpack.io/devbox/internal/telemetry"
31-
nixv1alpha1 "go.jetpack.io/pkg/api/gen/priv/nix/v1alpha1"
30+
"go.jetpack.io/pkg/auth"
3231

3332
"go.jetpack.io/devbox/internal/boxcli/usererr"
3433
"go.jetpack.io/devbox/internal/debug"
@@ -461,42 +460,9 @@ func (d *Devbox) installNixPackagesToStore(ctx context.Context, mode installMode
461460
Flags: flags,
462461
Writer: d.stderr,
463462
}
464-
caches, err := nixcache.CachedReadCaches(ctx)
463+
err = d.appendExtraSubstituters(ctx, args)
465464
if err != nil {
466-
debug.Log("error getting nix cache URI, assuming user doesn't have access: %v", err)
467-
}
468-
469-
args.ExtraSubstituters = lo.Map(
470-
caches, func(c *nixv1alpha1.NixBinCache, _ int) string {
471-
return c.GetUri()
472-
})
473-
474-
// TODO (Landau): handle errors that are not auth.ErrNotLoggedIn
475-
// Only lookup credentials if we have a cache to use
476-
if len(args.ExtraSubstituters) > 0 {
477-
creds, err := nixcache.CachedCredentials(ctx)
478-
if err == nil {
479-
args.Env = creds.Env()
480-
}
481-
482-
u, err := user.Current()
483-
if err != nil {
484-
err = redact.Errorf("lookup current user: %v", err)
485-
debug.Log("error configuring cache: %v", err)
486-
}
487-
err = nixcache.Configure(ctx, u.Username)
488-
if err != nil {
489-
debug.Log("error configuring cache: %v", err)
490-
491-
var daemonErr *nix.DaemonError
492-
if errors.As(err, &daemonErr) {
493-
// Error here to give the user a chance to restart the daemon.
494-
return usererr.New("Devbox configured Nix to use a new cache. Please restart the Nix daemon and re-run Devbox.")
495-
}
496-
// Other errors indicate we couldn't update nix.conf, so just warn and continue
497-
// by building from source if necessary.
498-
ux.Fwarning(d.stderr, "Devbox was unable to configure Nix to use your organization's private cache. Some packages might be built from source.\n")
499-
}
465+
return err
500466
}
501467

502468
packageNames := lo.Map(
@@ -540,6 +506,54 @@ func (d *Devbox) installNixPackagesToStore(ctx context.Context, mode installMode
540506
return nil
541507
}
542508

509+
func (d *Devbox) appendExtraSubstituters(ctx context.Context, args *nix.BuildArgs) error {
510+
creds, err := nixcache.CachedCredentials(ctx)
511+
if errors.Is(err, auth.ErrNotLoggedIn) {
512+
return nil
513+
}
514+
if err != nil {
515+
ux.Fwarning(d.stderr, "Devbox was unable to authenticate with the Jetify Nix cache. Some packages might be built from source.\n")
516+
return nil //nolint:nilerr
517+
}
518+
519+
caches, err := nixcache.CachedReadCaches(ctx)
520+
if err != nil {
521+
debug.Log("error getting list of caches from the Jetify API, assuming the user doesn't have access to any: %v", err)
522+
return nil
523+
}
524+
if len(caches) == 0 {
525+
return nil
526+
}
527+
528+
err = nixcache.Configure(ctx)
529+
if errors.Is(err, setup.ErrAlreadyRefused) {
530+
debug.Log("user previously refused to configure nix cache, not re-prompting")
531+
return nil
532+
}
533+
if errors.Is(err, setup.ErrUserRefused) {
534+
ux.Finfo(d.stderr, "Skipping cache setup. Run `devbox cache configure` to enable the cache at a later time.\n")
535+
return nil
536+
}
537+
var daemonErr *nix.DaemonError
538+
if errors.As(err, &daemonErr) {
539+
// Error here to give the user a chance to restart the daemon.
540+
return usererr.New("Devbox configured Nix to use a new cache. Please restart the Nix daemon and re-run Devbox.")
541+
}
542+
// Other errors indicate we couldn't update nix.conf, so just warn and
543+
// continue by building from source if necessary.
544+
if err != nil {
545+
debug.Log("error configuring nix cache: %v", err)
546+
ux.Fwarning(d.stderr, "Devbox was unable to configure Nix to use the Jetify Nix cache. Some packages might be built from source.\n")
547+
return nil
548+
}
549+
550+
for _, cache := range caches {
551+
args.ExtraSubstituters = append(args.ExtraSubstituters, cache.GetUri())
552+
}
553+
args.Env = append(args.Env, creds.Env()...)
554+
return nil
555+
}
556+
543557
func (d *Devbox) packagesToInstallInStore(ctx context.Context, mode installMode) ([]*devpkg.Package, error) {
544558
defer debug.FunctionTimer().End()
545559
// First, get and prepare all the packages that must be installed in this project

internal/devbox/providers/nixcache/setup.go

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,12 @@ import (
2121
"go.jetpack.io/devbox/internal/ux"
2222
)
2323

24-
func Configure(ctx context.Context, username string) error {
25-
return configure(ctx, username, false)
24+
func Configure(ctx context.Context) error {
25+
u, err := user.Current()
26+
if err != nil {
27+
return redact.Errorf("nixcache: lookup current user: %v", err)
28+
}
29+
return configure(ctx, u.Username, false)
2630
}
2731

2832
func ConfigureReprompt(ctx context.Context, username string) error {
@@ -39,8 +43,8 @@ func configure(ctx context.Context, username string, reprompt bool) error {
3943
const sudoPrompt = "You're logged into a Devbox account that now has access to a Nix cache. " +
4044
"Allow Devbox to configure Nix to use the new cache (requires sudo)?"
4145
err := setup.ConfirmRun(ctx, key, task, sudoPrompt)
42-
if err != nil && !errors.Is(err, setup.ErrUserRefused) {
43-
return redact.Errorf("nixcache: run setup: %v", err)
46+
if err != nil {
47+
return redact.Errorf("nixcache: run setup: %w", err)
4448
}
4549
return nil
4650
}
@@ -88,6 +92,13 @@ func (s *setupTask) Run(ctx context.Context) error {
8892
return err
8993
}
9094

95+
// Update the AWS config before configuring and restarting the Nix
96+
// daemon.
97+
err = s.updateAWSConfig()
98+
if err != nil {
99+
return redact.Errorf("update root aws config: %v", err)
100+
}
101+
91102
trusted := false
92103
cfg, err := nix.CurrentConfig(ctx)
93104
if err == nil {
@@ -101,11 +112,6 @@ func (s *setupTask) Run(ctx context.Context) error {
101112
return redact.Errorf("update nix config: %v", err)
102113
}
103114
}
104-
105-
err = s.updateAWSConfig()
106-
if err != nil {
107-
return redact.Errorf("update root aws config: %v", err)
108-
}
109115
return nil
110116
}
111117

0 commit comments

Comments
 (0)