9
9
"io"
10
10
"io/fs"
11
11
"os"
12
- "os/user"
13
12
"path/filepath"
14
13
"runtime/trace"
15
14
"slices"
@@ -25,10 +24,10 @@ import (
25
24
"go.jetpack.io/devbox/internal/devpkg"
26
25
"go.jetpack.io/devbox/internal/devpkg/pkgtype"
27
26
"go.jetpack.io/devbox/internal/lock"
28
- "go.jetpack.io/devbox/internal/redact "
27
+ "go.jetpack.io/devbox/internal/setup "
29
28
"go.jetpack.io/devbox/internal/shellgen"
30
29
"go.jetpack.io/devbox/internal/telemetry"
31
- nixv1alpha1 "go.jetpack.io/pkg/api/gen/priv/nix/v1alpha1 "
30
+ "go.jetpack.io/pkg/auth "
32
31
33
32
"go.jetpack.io/devbox/internal/boxcli/usererr"
34
33
"go.jetpack.io/devbox/internal/debug"
@@ -461,42 +460,9 @@ func (d *Devbox) installNixPackagesToStore(ctx context.Context, mode installMode
461
460
Flags : flags ,
462
461
Writer : d .stderr ,
463
462
}
464
- caches , err := nixcache . CachedReadCaches (ctx )
463
+ err = d . appendExtraSubstituters (ctx , args )
465
464
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
500
466
}
501
467
502
468
packageNames := lo .Map (
@@ -540,6 +506,54 @@ func (d *Devbox) installNixPackagesToStore(ctx context.Context, mode installMode
540
506
return nil
541
507
}
542
508
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
+
543
557
func (d * Devbox ) packagesToInstallInStore (ctx context.Context , mode installMode ) ([]* devpkg.Package , error ) {
544
558
defer debug .FunctionTimer ().End ()
545
559
// First, get and prepare all the packages that must be installed in this project
0 commit comments