package nix

import (
	"context"
	"iter"
)

// Build builds all entries yielded by installables.
func Build(ctx Context, installables iter.Seq[string]) error {
	c, cancel := context.WithCancel(ctx.Unwrap())
	defer cancel()
	stdout, stderr := ctx.Streams()

	cmd := ctx.Nix(c, CommandBuild,
		FlagKeepGoing, FlagNoLink, FlagStdin)
	if stdout != nil {
		cmd.Stdout = stdout
		cmd.Args = append(cmd.Args, FlagPrintBuildLogs)
	} else {
		cmd.Args = append(cmd.Args, FlagQuiet)
	}
	if stderr != nil {
		cmd.Stderr = stderr
		cmd.Args = append(cmd.Args, FlagVerbose)
	}

	_, err := ctx.WriteStdin(cmd, installables, nil)
	return err
}
