package nix

import (
	"context"
	"iter"
)

// Sign recursively signs installables using the key at keyPath.
func Sign(ctx Context, keyPath string, installables iter.Seq[string]) error {
	c, cancel := context.WithCancel(ctx.Unwrap())
	defer cancel()

	cmd := ctx.Nix(c, CommandStore, CommandStoreSign,
		FlagPrintBuildLogs, FlagVerbose,
		// this is quite useless if not signing recursively
		FlagRecursive,
		FlagKeyFile, keyPath,
		FlagStdin)

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