cmd/nixbuild: implement dependency resolver

This replaces the bash implementation of this hack.
This commit is contained in:
Ophestra 2025-07-15 04:44:55 +09:00
parent cbbec15246
commit d4572eb50f
Signed by: cat
SSH Key Fingerprint: SHA256:gQ67O0enBZ7UdZypgtspB2FDM1g3GVw8nX0XSdcFw8Q
2 changed files with 86 additions and 26 deletions

36
cmd/nixbuild/format.go Normal file
View File

@ -0,0 +1,36 @@
package main
import (
"fmt"
"os"
"strings"
"git.gensokyo.uk/yonah/nixbuild"
)
func formatInstallable(name string, installable *string, flagNixOS bool, args []string) error {
if len(args) != 1 {
return commandHandlerError(name + " requires exactly 1 argument")
}
*installable = args[0]
if !flagNixOS {
return nil
}
fields := strings.SplitN(*installable, "#", 2)
switch len(fields) {
case 2:
*installable = nixbuild.NixOSInstallable(fields[0], fields[1])
return nil
case 1:
hostname, err := os.Hostname()
if err != nil {
return commandHandlerError(fmt.Sprintf("cannot get hostname: %v", err))
}
*installable = nixbuild.NixOSInstallable(fields[0], hostname)
return nil
default:
return commandHandlerError("unexpected installable")
}
}

View File

@ -9,7 +9,6 @@ import (
"os" "os"
"os/signal" "os/signal"
"slices" "slices"
"strings"
"syscall" "syscall"
"git.gensokyo.uk/yonah/nixbuild" "git.gensokyo.uk/yonah/nixbuild"
@ -44,7 +43,7 @@ func main() {
c.Command("show", command.UsageInternal, func(args []string) error { c.Command("show", command.UsageInternal, func(args []string) error {
if len(args) < 1 { if len(args) < 1 {
return commandHandlerError("usage requires at least 1 argument") return commandHandlerError("show requires at least 1 argument")
} }
if drv, err := nixbuild.DerivationShow(ctx, slices.Values(args)); err != nil { if drv, err := nixbuild.DerivationShow(ctx, slices.Values(args)); err != nil {
@ -66,26 +65,57 @@ func main() {
return nil return nil
}) })
c.Command("instantiated", "Evaluate an installable and output all derivations instantiated during evaluation", func(args []string) error { var (
if len(args) != 1 { resolveFlagOut string
return commandHandlerError("instantiated requires exactly 1 argument") )
c.Command("resolve", "Resolve possible build-time dependencies", func(args []string) error {
var installable string
if err := formatInstallable("resolve", &installable, flagNixOS, args); err != nil {
return err
} }
installable := args[0] log.Printf("evaluating %s", installable)
if flagNixOS { var installables []string
fields := strings.SplitN(installable, "#", 2) if v, err := nixbuild.EvalInstantiated(ctx, installable); err != nil {
switch len(fields) { return commandHandlerError(fmt.Sprintf("cannot evaluate for instantiated derivations: %v", err))
case 2: } else {
installable = nixbuild.NixOSInstallable(fields[0], fields[1]) installables = v
case 1: }
hostname, err := os.Hostname()
if err != nil { log.Println("building instantiated derivations")
return commandHandlerError(fmt.Sprintf("cannot get hostname: %v", err)) if err := nixbuild.Build(ctx, slices.Values(installables)); err != nil {
} return commandHandlerError(fmt.Sprintf("cannot build: %v", err))
installable = nixbuild.NixOSInstallable(fields[0], hostname) }
default:
return commandHandlerError("unexpected installable") var collective []string
log.Println("collecting store paths")
if derivations, err := nixbuild.DerivationShow(ctx, slices.Values(installables)); err != nil {
return commandHandlerError(fmt.Sprintf("cannot show: %v", err))
} else {
collective = nixbuild.CollectFromDerivations(derivations)
}
f, err := os.Create(resolveFlagOut)
if err != nil {
return commandHandlerError(fmt.Sprintf("cannot create store paths file: %v", err))
}
if flagJSON {
if err = json.NewEncoder(f).Encode(collective); err != nil {
return commandHandlerError(fmt.Sprintf("cannot serialise JSON: %v", err))
} }
} else {
if _, err = nixbuild.WriteStdin(f, slices.Values(collective)); err != nil {
return commandHandlerError(fmt.Sprintf("cannot write store path list: %v", err))
}
}
return nil
}).
Flag(&resolveFlagOut, "o", command.StringFlag("store-paths"), "Path to write collected store paths")
c.Command("instantiated", "Evaluate an installable and output all derivations instantiated during evaluation", func(args []string) error {
var installable string
if err := formatInstallable("instantiated", &installable, flagNixOS, args); err != nil {
return err
} }
if v, err := nixbuild.EvalInstantiated(ctx, installable); err != nil { if v, err := nixbuild.EvalInstantiated(ctx, installable); err != nil {
@ -96,13 +126,7 @@ func main() {
} }
return nil return nil
} else { } else {
for _, drv := range v { _, _ = nixbuild.WriteStdin(os.Stdout, slices.Values(v))
if strings.HasSuffix(drv, ".drv") {
fmt.Println(drv + "^*")
} else {
fmt.Println(drv)
}
}
return nil return nil
} }
}) })