@@ -2,8 +2,10 @@ package nixcache
2
2
3
3
import (
4
4
"context"
5
+ "slices"
5
6
"time"
6
7
8
+ "github.com/samber/lo"
7
9
"go.jetpack.io/devbox/internal/build"
8
10
"go.jetpack.io/devbox/internal/cachehash"
9
11
"go.jetpack.io/devbox/internal/devbox/providers/identity"
@@ -55,40 +57,49 @@ func (p *Provider) Credentials(ctx context.Context) (AWSCredentials, error) {
55
57
return creds , nil
56
58
}
57
59
58
- // URI queries the Jetify API for the URI that points to user's private cache.
59
- // If their account doesn't have access to a cache, it returns an empty string
60
- // and a nil error.
61
- func (p * Provider ) URI (ctx context.Context ) (string , error ) {
62
- cache := filecache.New [string ]("devbox/providers/nixcache" )
60
+ func (p * Provider ) Caches (
61
+ ctx context.Context ,
62
+ ) ([]* nixv1alpha1.NixBinCache , error ) {
63
63
token , err := identity .Get ().GenSession (ctx )
64
64
if err != nil {
65
- return "" , err
65
+ return nil , err
66
66
}
67
- // Landau: I think we can probably remove this cache? This endpoint is very
68
- // fast and we only use this for build/upload which are slow.
69
- uri , err := cache .GetOrSet (
70
- "uri-" + getSubOrAccessTokenHash (token ),
71
- func () (string , time.Duration , error ) {
72
- client := api .NewClient (ctx , build .JetpackAPIHost (), token )
73
- resp , err := client .GetBinCache (ctx )
74
- if err != nil {
75
- return "" , 0 , redact .Errorf ("nixcache: get uri: %w" , redact .Safe (err ))
76
- }
67
+ client := api .NewClient (ctx , build .JetpackAPIHost (), token )
68
+ resp , err := client .GetBinCache (ctx )
69
+ if err != nil {
70
+ return nil , redact .Errorf ("nixcache: get caches: %w" , redact .Safe (err ))
71
+ }
72
+ return resp .GetCaches (), nil
73
+ }
77
74
78
- // Don't cache negative responses.
79
- if resp .GetNixBinCacheUri () == "" {
80
- return "" , 0 , nil
81
- }
75
+ func (p * Provider ) ReadCaches (
76
+ ctx context.Context ,
77
+ ) ([]* nixv1alpha1.NixBinCache , error ) {
78
+ caches , err := p .Caches (ctx )
79
+ if err != nil {
80
+ return nil , err
81
+ }
82
+ return lo .Filter (caches , func (c * nixv1alpha1.NixBinCache , _ int ) bool {
83
+ return slices .Contains (
84
+ c .GetPermissions (),
85
+ nixv1alpha1 .Permission_PERMISSION_READ ,
86
+ )
87
+ }), nil
88
+ }
82
89
83
- // TODO(gcurtis): do a better job of invalidating the URI after
84
- // a Nix command fails to query the cache.
85
- return resp .GetNixBinCacheUri (), 24 * time .Hour , nil
86
- },
87
- )
90
+ func (p * Provider ) WriteCaches (
91
+ ctx context.Context ,
92
+ ) ([]* nixv1alpha1.NixBinCache , error ) {
93
+ caches , err := p .Caches (ctx )
88
94
if err != nil {
89
- return "" , redact . Errorf ( "nixcache: get uri: %w" , redact . Safe ( err ))
95
+ return nil , err
90
96
}
91
- return uri , nil
97
+ return lo .Filter (caches , func (c * nixv1alpha1.NixBinCache , _ int ) bool {
98
+ return slices .Contains (
99
+ c .GetPermissions (),
100
+ nixv1alpha1 .Permission_PERMISSION_WRITE ,
101
+ )
102
+ }), nil
92
103
}
93
104
94
105
// AWSCredentials are short-lived credentials that grant access to a private Nix
0 commit comments