depot/go/nix/bnix/bnix.go

100 lines
2.3 KiB
Go

package main
import (
"context"
"flag"
"fmt"
"log"
"os"
"hg.lukegb.com/lukegb/depot/go/nix/nixdrv"
"hg.lukegb.com/lukegb/depot/go/nix/nixstore"
)
func ensure(ctx context.Context, d *nixstore.Daemon, at *nixstore.ActivityTracker, path string) error {
return d.EnsurePath(at, path)
}
var rs nixdrv.LocalFSResolver
func loadDerivation(ctx context.Context, path string) (*nixdrv.Derivation, error) {
return rs.LoadDerivation(path)
}
func buildDerivation(ctx context.Context, d *nixstore.Daemon, at *nixstore.ActivityTracker, path string) error {
drv, err := loadDerivation(ctx, path)
if err != nil {
return fmt.Errorf("loading derivation: %w", err)
}
basicDrv, err := drv.ToBasicDerivation(rs)
if err != nil {
return fmt.Errorf("resolving: %w", err)
}
brs, err := d.BuildDerivation(at, path, basicDrv, nixstore.BMNormal)
if err != nil {
return err
}
log.Printf("build result: %v", brs)
return nil
}
func main() {
flag.Parse()
badCall := func(f string, xs ...interface{}) {
fmt.Fprintf(os.Stderr, f+"\n", xs...)
flag.Usage()
os.Exit(1)
}
if flag.NArg() < 1 {
badCall("need a subcommand")
}
var cmd func(context.Context, *nixstore.Daemon, *nixstore.ActivityTracker) error
switch flag.Arg(0) {
case "ensure":
if flag.NArg() != 2 {
badCall("`ensure` needs a store path")
}
cmd = func(ctx context.Context, d *nixstore.Daemon, at *nixstore.ActivityTracker) error {
return ensure(ctx, d, at, flag.Arg(1))
}
case "show-derivation":
if flag.NArg() != 2 {
badCall("`show-derivation` needs a derivation")
}
cmd = func(ctx context.Context, d *nixstore.Daemon, at *nixstore.ActivityTracker) error {
drv, err := loadDerivation(ctx, flag.Arg(1))
if err != nil {
return err
}
fmt.Printf("%#v\n", drv)
return nil
}
case "build-derivation":
if flag.NArg() != 2 {
badCall("`build-derivation` needs a derivation")
}
cmd = func(ctx context.Context, d *nixstore.Daemon, at *nixstore.ActivityTracker) error {
return buildDerivation(ctx, d, at, flag.Arg(1))
}
default:
badCall("bad subcommand %s", flag.Arg(0))
}
at := nixstore.NewActivityTracker()
d, err := nixstore.OpenDaemon(nixstore.DaemonSock)
if err != nil {
log.Fatalf("OpenDaemon: %v", err)
}
defer d.Close()
ctx := context.Background()
if err := cmd(ctx, d, at); err != nil {
log.Fatalf("%s: %s", flag.Arg(0), err)
}
}