cmd/nixbuild: implement dependency resolver
This replaces the bash implementation of this hack.
This commit is contained in:
parent
1e30aef337
commit
aa4bbbc2fe
36
cmd/nixbuild/format.go
Normal file
36
cmd/nixbuild/format.go
Normal 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")
|
||||||
|
}
|
||||||
|
}
|
@ -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()
|
|
||||||
|
log.Println("building instantiated derivations")
|
||||||
|
if err := nixbuild.Build(ctx, slices.Values(installables)); err != nil {
|
||||||
|
return commandHandlerError(fmt.Sprintf("cannot build: %v", err))
|
||||||
|
}
|
||||||
|
|
||||||
|
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 {
|
if err != nil {
|
||||||
return commandHandlerError(fmt.Sprintf("cannot get hostname: %v", err))
|
return commandHandlerError(fmt.Sprintf("cannot create store paths file: %v", err))
|
||||||
}
|
}
|
||||||
installable = nixbuild.NixOSInstallable(fields[0], hostname)
|
if flagJSON {
|
||||||
default:
|
if err = json.NewEncoder(f).Encode(collective); err != nil {
|
||||||
return commandHandlerError("unexpected installable")
|
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
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
Loading…
x
Reference in New Issue
Block a user