nixbuild/context.go
Ophestra 8d502f1574
context: expose more internals
This makes overriding the Nix method easier.
2025-09-13 18:32:12 +09:00

48 lines
1.3 KiB
Go

package nix
import (
"context"
"io"
"iter"
)
// Cmd represents an external command or container being prepared to run.
type Cmd interface {
Start() error
Wait() error
StdinPipe() (io.WriteCloser, error)
StdoutPipe() (io.ReadCloser, error)
StderrPipe() (io.ReadCloser, error)
Stdin(io.Reader)
Stdout(io.Writer)
Stderr(io.Writer)
AppendArgs(a ...string)
ReplaceEnv(env []string)
}
// Context holds configuration and environment information for interacting with nix.
type Context interface {
// Streams returns the stdout and stderr writers held by this [Context].
Streams() (stdout, stderr io.Writer)
// Extra returns a copy of extra arguments held by this [Context].
Extra() []string
// StoreEnv returns extra [Store] environment variables.
StoreEnv() []string
// Nix returns an implementation of [Cmd] for running a nix command.
Nix(ctx context.Context, arg ...string) Cmd
// WriteStdin calls [WriteStdin] for [Cmd]. The function f points to is called if [WriteStdin] succeeds.
WriteStdin(cmd Cmd, installables iter.Seq[string], f func() error) (int, error)
// Unwrap returns the stored [context.Context]
Unwrap() context.Context
}
// setStreams sets the stdout and stderr streams of [Cmd] according to [Context].
func setStreams(ctx Context, cmd Cmd) {
stdout, stderr := ctx.Streams()
cmd.Stdout(stdout)
cmd.Stderr(stderr)
}