These things currently require manual testing unfortunately since writing a nix stub would take a long time.
106 lines
2.7 KiB
Go
106 lines
2.7 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"log"
|
|
"os"
|
|
"os/signal"
|
|
"slices"
|
|
"strings"
|
|
"syscall"
|
|
|
|
"git.gensokyo.uk/yonah/nixbuild"
|
|
"hakurei.app/command"
|
|
)
|
|
|
|
type commandHandlerError string
|
|
|
|
func (c commandHandlerError) Error() string { return string(c) }
|
|
|
|
func main() {
|
|
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
|
|
defer stop()
|
|
|
|
var (
|
|
flagNixOS bool
|
|
flagVerbose bool
|
|
flagJSON bool
|
|
)
|
|
c := command.New(os.Stderr, log.Printf, "nixbuild", func(args []string) error {
|
|
log.SetFlags(0)
|
|
log.SetPrefix("nixbuild: ")
|
|
nixbuild.Stdout = os.Stdout
|
|
if flagVerbose {
|
|
nixbuild.Stderr = os.Stderr
|
|
}
|
|
return nil
|
|
}).
|
|
Flag(&flagNixOS, "nixos", command.BoolFlag(false), "Interpret input as NixOS flake installable").
|
|
Flag(&flagVerbose, "v", command.BoolFlag(false), "Connect nix stderr").
|
|
Flag(&flagJSON, "json", command.BoolFlag(false), "Serialise output in JSON when applicable")
|
|
|
|
c.Command("build", "Build a list of installables", func(args []string) error {
|
|
if len(args) < 1 {
|
|
return commandHandlerError("build requires at least 1 argument")
|
|
}
|
|
|
|
if err := nixbuild.Build(ctx, slices.Values(args)); err != nil {
|
|
return commandHandlerError(fmt.Sprintf("cannot build: %v", err))
|
|
}
|
|
return nil
|
|
})
|
|
|
|
c.Command("instantiated", "Evaluate an installable and output all derivations instantiated during evaluation", func(args []string) error {
|
|
if len(args) != 1 {
|
|
return commandHandlerError("instantiated requires exactly 1 argument")
|
|
}
|
|
|
|
installable := args[0]
|
|
if flagNixOS {
|
|
fields := strings.SplitN(installable, "#", 2)
|
|
switch len(fields) {
|
|
case 2:
|
|
installable = nixbuild.NixOSInstallable(fields[0], fields[1])
|
|
case 1:
|
|
hostname, err := os.Hostname()
|
|
if err != nil {
|
|
return commandHandlerError(fmt.Sprintf("cannot get hostname: %v", err))
|
|
}
|
|
installable = nixbuild.NixOSInstallable(fields[0], hostname)
|
|
default:
|
|
return commandHandlerError("unexpected installable")
|
|
}
|
|
}
|
|
|
|
if v, err := nixbuild.EvalInstantiated(ctx, installable); err != nil {
|
|
return commandHandlerError(fmt.Sprintf("cannot evaluate for instantiated derivations: %v", err))
|
|
} else if flagJSON {
|
|
if err = json.NewEncoder(os.Stdout).Encode(v); err != nil {
|
|
return commandHandlerError(fmt.Sprintf("cannot serialise JSON: %v", err))
|
|
}
|
|
return nil
|
|
} else {
|
|
for _, drv := range v {
|
|
if strings.HasSuffix(drv, ".drv") {
|
|
fmt.Println(drv + "^*")
|
|
} else {
|
|
fmt.Println(drv)
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
})
|
|
|
|
c.MustParse(os.Args[1:], func(err error) {
|
|
var ce commandHandlerError
|
|
if errors.As(err, &ce) {
|
|
log.Fatal(ce.Error())
|
|
return
|
|
}
|
|
log.Fatalf("cannot parse command: %v", err)
|
|
})
|
|
}
|