add system-path output option

This commit is contained in:
Daylin Morgan 2024-06-18 12:23:42 -05:00
parent d1cd4900b4
commit c84ef6a384
Signed by: daylin
GPG key ID: 950D13E9719334AD
3 changed files with 69 additions and 23 deletions

View file

@ -14,6 +14,8 @@ var outputCmd = &cobra.Command{
}, },
} }
func init() { func init() {
rootCmd.AddCommand(outputCmd) rootCmd.AddCommand(outputCmd)
outputCmd.Flags().BoolVar(&systemPath, "system-path", false, "show system-path drv")
} }

View file

@ -32,6 +32,7 @@ var (
cacheName string cacheName string
verbose bool verbose bool
nom bool nom bool
systemPath bool
) )
var oizys = o.NewOizys() var oizys = o.NewOizys()
@ -40,7 +41,7 @@ var rootCmd = &cobra.Command{
Use: "oizys", Use: "oizys",
Short: "nix begat oizys", Short: "nix begat oizys",
PersistentPreRun: func(cmd *cobra.Command, args []string) { PersistentPreRun: func(cmd *cobra.Command, args []string) {
oizys.Set(flake, host, cacheName, verbose) oizys.Set(flake, host, cacheName, verbose, systemPath)
oizys.CheckFlake() oizys.CheckFlake()
}, },
} }

View file

@ -23,6 +23,7 @@ type Oizys struct {
host string host string
cache string cache string
verbose bool verbose bool
systemPath bool
} }
func NewOizys() *Oizys { func NewOizys() *Oizys {
@ -42,7 +43,44 @@ func NewOizys() *Oizys {
return &Oizys{flake: flake, host: hostname, cache: "daylin"} return &Oizys{flake: flake, host: hostname, cache: "daylin"}
} }
func (o *Oizys) Output() string { type Derivation struct {
InputDrvs map[string]interface{}
}
func parseSystemPath(derivation map[string]Derivation) (string, error) {
for _, nixosDrv := range derivation {
for drv := range nixosDrv.InputDrvs {
if strings.HasSuffix(drv, "system-path.drv") {
return drv, nil
}
}
}
return "", errors.New("failed to find path for system-path.drv")
}
// recreating this command
// nix derivation show `oizys output` | jq -r '.[].inputDrvs | with_entries(select(.key|match("system-path";"i"))) | keys | .[]'
func (o *Oizys) getSystemPath() string {
cmd := exec.Command("nix", "derivation", "show", o.nixosConfigAttr())
log.Println("evaluating to get system-path")
cmd.Stderr = os.Stderr
out, err := cmd.Output()
if err != nil {
log.Fatal(err)
}
var derivation map[string]Derivation
if err := json.Unmarshal(out, &derivation); err != nil {
log.Fatal(err)
}
systemPath, err := parseSystemPath(derivation)
if err != nil {
log.Fatal(err)
}
return systemPath
}
func (o *Oizys) nixosConfigAttr() string {
return fmt.Sprintf( return fmt.Sprintf(
"%s#nixosConfigurations.%s.config.system.build.toplevel", "%s#nixosConfigurations.%s.config.system.build.toplevel",
o.flake, o.flake,
@ -50,9 +88,17 @@ func (o *Oizys) Output() string {
) )
} }
func (o *Oizys) Output() string {
if o.systemPath {
return o.getSystemPath()
} else {
return o.nixosConfigAttr()
}
}
func (o *Oizys) Set( func (o *Oizys) Set(
flake, host, cache string, flake, host, cache string,
verbose bool, verbose, systemPath bool,
) { ) {
if host != "" { if host != "" {
o.host = host o.host = host
@ -64,6 +110,7 @@ func (o *Oizys) Set(
o.cache = cache o.cache = cache
} }
o.verbose = verbose o.verbose = verbose
o.systemPath = systemPath
} }
func terminalSize() (int, int) { func terminalSize() (int, int) {
@ -153,7 +200,11 @@ func (o *Oizys) git(rest ...string) *exec.Cmd {
} }
func showFailedOutput(buf []byte) { func showFailedOutput(buf []byte) {
arrow := lipgloss.NewStyle().Bold(true).Foreground(lipgloss.Color("9")).Render("->") arrow := lipgloss.
NewStyle().
Bold(true).
Foreground(lipgloss.Color("9")).
Render("->")
for _, line := range strings.Split(strings.TrimSpace(string(buf)), "\n") { for _, line := range strings.Split(strings.TrimSpace(string(buf)), "\n") {
fmt.Println(arrow, line) fmt.Println(arrow, line)
} }
@ -208,7 +259,7 @@ func showDryRunResult(nixOutput string, verbose bool) {
func (o *Oizys) NixDryRun(verbose bool, rest ...string) { func (o *Oizys) NixDryRun(verbose bool, rest ...string) {
args := []string{ args := []string{
"build", o.Output(), "--dry-run", "build", o.nixosConfigAttr(), "--dry-run",
} }
args = append(args, rest...) args = append(args, rest...)
cmd := exec.Command("nix", args...) cmd := exec.Command("nix", args...)
@ -302,7 +353,7 @@ func (o *Oizys) Checks(nom bool, rest ...string) {
func (o *Oizys) CacheBuild(rest ...string) { func (o *Oizys) CacheBuild(rest ...string) {
args := []string{ args := []string{
"watch-exec", o.cache, "--", "nix", "watch-exec", o.cache, "--", "nix",
"build", o.Output(), "--print-build-logs", "build", o.nixosConfigAttr(), "--print-build-logs",
"--accept-flake-config", "--accept-flake-config",
} }
args = append(args, rest...) args = append(args, rest...)
@ -325,14 +376,6 @@ func (o *Oizys) CI(rest ...string) {
runCommand(cmd) runCommand(cmd)
} }
func Output(flake string, host string) string {
return fmt.Sprintf(
"%s#nixosConfigurations.%s.config.system.build.toplevel",
flake,
host,
)
}
func nixSpinner(host string) *spinner.Spinner { func nixSpinner(host string) *spinner.Spinner {
msg := fmt.Sprintf("%s %s", " evaluating derivation for:", msg := fmt.Sprintf("%s %s", " evaluating derivation for:",
lipgloss.NewStyle().Bold(true).Foreground(lipgloss.Color("6")).Render(host), lipgloss.NewStyle().Bold(true).Foreground(lipgloss.Color("6")).Render(host),