-
Notifications
You must be signed in to change notification settings - Fork 174
/
build.go
58 lines (52 loc) · 1.69 KB
/
build.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package nix
import (
"context"
"io"
"os"
"os/exec"
"github.com/pkg/errors"
"go.jetpack.io/devbox/internal/debug"
"go.jetpack.io/devbox/internal/redact"
)
type BuildArgs struct {
AllowInsecure bool
Env []string
ExtraSubstituter string
Flags []string
Writer io.Writer
}
func Build(ctx context.Context, args *BuildArgs, installables ...string) error {
// --impure is required for allowUnfreeEnv/allowInsecureEnv to work.
cmd := commandContext(ctx, "build", "--impure")
cmd.Args = append(cmd.Args, args.Flags...)
cmd.Args = append(cmd.Args, installables...)
// Adding extra substituters only here to be conservative, but this could also
// be added to ExperimentalFlags() in the future.
if args.ExtraSubstituter != "" {
cmd.Args = append(cmd.Args, "--extra-substituters", args.ExtraSubstituter)
}
cmd.Env = append(allowUnfreeEnv(os.Environ()), args.Env...)
if args.AllowInsecure {
debug.Log("Setting Allow-insecure env-var\n")
cmd.Env = allowInsecureEnv(cmd.Env)
}
// If nix build runs as tty, the output is much nicer. If we ever
// need to change this to our own writers, consider that you may need
// to implement your own nicer output. --print-build-logs flag may be useful.
cmd.Stdin = os.Stdin
cmd.Stdout = args.Writer
cmd.Stderr = args.Writer
debug.Log("Running cmd: %s\n", cmd)
if err := cmd.Run(); err != nil {
if exitErr := (&exec.ExitError{}); errors.As(err, &exitErr) {
debug.Log("Nix build exit code: %d, output: %s\n", exitErr.ExitCode(), exitErr.Stderr)
return redact.Errorf("nix build exit code: %d, output: %s, err: %w",
redact.Safe(exitErr.ExitCode()),
exitErr.Stderr,
err,
)
}
return err
}
return nil
}