Skip to content

Commit 1373e65

Browse files
authored
[nix-cache] Add ability to use nix cache with env var (#1895)
## Summary Sets up boilerplate to use nix cache and allows use via environment variable. TODOs: (in follow ups) - [ ] Setup user permissions automatically to make user trusted in multi-user setups - [ ] Hook up to API to fetch cache url and credentials ## How was it tested? `DEVBOX_NIX_BINCACHE_URI="s3://mike-test-nix-cache?region=us-west-2" devbox add hello`
1 parent 971b3b1 commit 1373e65

File tree

4 files changed

+47
-6
lines changed

4 files changed

+47
-6
lines changed

.golangci.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,3 +86,6 @@ linters-settings:
8686
wrapcheck:
8787
ignorePackageGlobs:
8888
- go.jetpack.io/devbox/*
89+
misspell:
90+
ignore-words:
91+
- substituters
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package bincache
2+
3+
import "os"
4+
5+
// ExtraSubstituter returns the URI of the extra substituter to use.
6+
// a substituter is a bin cache URI that nix can use to fetch pre-built
7+
// binaries from.
8+
func ExtraSubstituter() (string, error) {
9+
if err := ensureTrustedUser(); err != nil {
10+
return "", err
11+
}
12+
13+
// TODO: if user is logged in (or if we have token we can refresh)
14+
// then we try to fetch the bincache URI from the API.
15+
16+
// DEVBOX_NIX_BINCACHE_URI seems like a friendlier name than "substituter"
17+
return os.Getenv("DEVBOX_NIX_BINCACHE_URI"), nil
18+
}
19+
20+
func ensureTrustedUser() error {
21+
// TODO: we need to ensure that the user can actually use the extra
22+
// substituter. If the user did a root install, then we need to add
23+
// the extra substituter to the nix.conf file and restart the daemon.
24+
return nil
25+
}

internal/devbox/packages.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"github.com/fatih/color"
1818
"github.com/pkg/errors"
1919
"github.com/samber/lo"
20+
"go.jetpack.io/devbox/internal/devbox/bincache"
2021
"go.jetpack.io/devbox/internal/devbox/devopt"
2122
"go.jetpack.io/devbox/internal/devconfig"
2223
"go.jetpack.io/devbox/internal/devpkg"
@@ -445,11 +446,17 @@ func (d *Devbox) installNixPackagesToStore(ctx context.Context, mode installMode
445446
flags = append(flags, "--refresh")
446447
}
447448

449+
extraSubstituter, err := bincache.ExtraSubstituter()
450+
if err != nil {
451+
return err
452+
}
453+
448454
for _, installable := range installables {
449455
args := &nix.BuildArgs{
450-
AllowInsecure: pkg.HasAllowInsecure(),
451-
Flags: flags,
452-
Writer: d.stderr,
456+
AllowInsecure: pkg.HasAllowInsecure(),
457+
Flags: flags,
458+
Writer: d.stderr,
459+
ExtraSubstituter: extraSubstituter,
453460
}
454461
err = nix.Build(ctx, args, installable)
455462
if err != nil {

internal/nix/build.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,22 @@ import (
1212
)
1313

1414
type BuildArgs struct {
15-
AllowInsecure bool
16-
Flags []string
17-
Writer io.Writer
15+
AllowInsecure bool
16+
ExtraSubstituter string
17+
Flags []string
18+
Writer io.Writer
1819
}
1920

2021
func Build(ctx context.Context, args *BuildArgs, installables ...string) error {
2122
// --impure is required for allowUnfreeEnv/allowInsecureEnv to work.
2223
cmd := commandContext(ctx, "build", "--impure")
2324
cmd.Args = append(cmd.Args, args.Flags...)
2425
cmd.Args = append(cmd.Args, installables...)
26+
// Adding extra substituters only here to be conservative, but this could also
27+
// be added to ExperimentalFlags() in the future.
28+
if args.ExtraSubstituter != "" {
29+
cmd.Args = append(cmd.Args, "--extra-substituters", args.ExtraSubstituter)
30+
}
2531
cmd.Env = allowUnfreeEnv(os.Environ())
2632
if args.AllowInsecure {
2733
debug.Log("Setting Allow-insecure env-var\n")

0 commit comments

Comments
 (0)