From 0e132b93724b439a7f262d66fe28dc07f55683c4 Mon Sep 17 00:00:00 2001 From: Daylin Morgan Date: Mon, 17 Jun 2024 16:15:37 -0500 Subject: [PATCH] QOL oizys-go --- lib/default.nix | 2 +- modules/nix.nix | 3 ++ pkgs/oizys/oizys-go/cmd/build.go | 3 +- pkgs/oizys/oizys-go/cmd/checks.go | 18 +++++++ pkgs/oizys/oizys-go/cmd/root.go | 1 + pkgs/oizys/oizys-go/default.nix | 15 +++++- pkgs/oizys/oizys-go/internal/oizys/main.go | 58 +++++++++++++++++++--- 7 files changed, 90 insertions(+), 10 deletions(-) create mode 100644 pkgs/oizys/oizys-go/cmd/checks.go diff --git a/lib/default.nix b/lib/default.nix index 7ac7953..1af41db 100644 --- a/lib/default.nix +++ b/lib/default.nix @@ -56,7 +56,7 @@ in oizys-zig = pkgs.callPackage ../pkgs/oizys/oizys-zig { inherit zig2nix; }; oizys-nim = pkgs.callPackage ../pkgs/oizys/oizys-nim { }; oizys-rs = pkgs.callPackage ../pkgs/oizys/oizys-rs { }; - oizys-go = pkgs.callPackage ../pkgs/oizys/oizys-go { }; + oizys-go = pkgs.callPackage ../pkgs/oizys/oizys-go { inherit self;}; default = oizys-go; }); devShells = forAllSystems (pkgs: { diff --git a/modules/nix.nix b/modules/nix.nix index c46fd16..36ccc72 100644 --- a/modules/nix.nix +++ b/modules/nix.nix @@ -24,6 +24,9 @@ environment.systemPackages = [ pkgs.nixd pkgs.nixfmt-rfc-style + pkgs.nix-output-monitor + + self.packages.${pkgs.system}.default ]; diff --git a/pkgs/oizys/oizys-go/cmd/build.go b/pkgs/oizys/oizys-go/cmd/build.go index fecfa85..486ca91 100644 --- a/pkgs/oizys/oizys-go/cmd/build.go +++ b/pkgs/oizys/oizys-go/cmd/build.go @@ -8,10 +8,11 @@ var buildCmd = &cobra.Command{ Use: "build", Short: "nix build", Run: func(cmd *cobra.Command, args []string) { - oizys.NixBuild(args...) + oizys.NixBuild(nom, args...) }, } func init() { rootCmd.AddCommand(buildCmd) + buildCmd.Flags().BoolVar(&nom, "nom", false, "display result with nom") } diff --git a/pkgs/oizys/oizys-go/cmd/checks.go b/pkgs/oizys/oizys-go/cmd/checks.go new file mode 100644 index 0000000..fb599ca --- /dev/null +++ b/pkgs/oizys/oizys-go/cmd/checks.go @@ -0,0 +1,18 @@ +package cmd + +import ( + "github.com/spf13/cobra" +) + +var checksCmd = &cobra.Command{ + Use: "checks", + Short: "nix build checks", + Run: func(cmd *cobra.Command, args []string) { + oizys.Checks(nom, args...) + }, +} + +func init() { + rootCmd.AddCommand(checksCmd) + checksCmd.Flags().BoolVar(&nom, "nom", false, "display result with nom") +} diff --git a/pkgs/oizys/oizys-go/cmd/root.go b/pkgs/oizys/oizys-go/cmd/root.go index fef51d9..15ece0b 100644 --- a/pkgs/oizys/oizys-go/cmd/root.go +++ b/pkgs/oizys/oizys-go/cmd/root.go @@ -31,6 +31,7 @@ var ( host string cacheName string verbose bool + nom bool ) var oizys = o.NewOizys() diff --git a/pkgs/oizys/oizys-go/default.nix b/pkgs/oizys/oizys-go/default.nix index 0a47345..f1123c7 100644 --- a/pkgs/oizys/oizys-go/default.nix +++ b/pkgs/oizys/oizys-go/default.nix @@ -1,14 +1,25 @@ { + self, lib, installShellFiles, buildGoModule, makeWrapper, gh, + nix-output-monitor, ... }: +let +mkDate = + longDate: + (lib.concatStringsSep "-" [ + (builtins.substring 0 4 longDate) + (builtins.substring 4 2 longDate) + (builtins.substring 6 2 longDate) + ]); +in buildGoModule { pname = "oizys"; - version = "unstable"; + version = "date=${mkDate self.lastModifiedDate}"; src = lib.cleanSource ./.; vendorHash = "sha256-Geqcp0/7I1IF2IfaYyIChz7SOCF+elIEdcz2VsAU0hQ="; @@ -25,6 +36,6 @@ buildGoModule { postFixup = '' wrapProgram $out/bin/oizys \ - --prefix PATH ${lib.makeBinPath [ gh ]} + --prefix PATH ':' ${lib.makeBinPath [ gh nix-output-monitor ]} ''; } diff --git a/pkgs/oizys/oizys-go/internal/oizys/main.go b/pkgs/oizys/oizys-go/internal/oizys/main.go index 8c77511..7d7833e 100644 --- a/pkgs/oizys/oizys-go/internal/oizys/main.go +++ b/pkgs/oizys/oizys-go/internal/oizys/main.go @@ -1,6 +1,7 @@ package oizys import ( + "encoding/json" "errors" "fmt" "io/fs" @@ -24,7 +25,6 @@ type Oizys struct { verbose bool } - func NewOizys() *Oizys { hostname, err := os.Hostname() if err != nil { @@ -246,13 +246,59 @@ func runCommand(cmd *exec.Cmd) { } } -func (o *Oizys) NixBuild(rest ...string) { - args := []string{"build", o.Output()} - args = append(args, rest...) - cmd := exec.Command("nix", args...) +// func runBuildWithNom(buildCmd *exec.Cmd) { +// log.Println("starting build?") +// nomCmd := exec.Command("nom","--json") +// var err error +// buildCmd.Args = append(buildCmd.Args, "--log-format", "internal-json", "-v") +// nomCmd.Stdin, err = buildCmd.StderrPipe() +// if err != nil { +// log.Fatal(err) +// } +// nomCmd.Stdout = os.Stdout +// nomCmd.Start() +// log.Println("starting buildcmd?") +// buildCmd.Run() +// nomCmd.Wait() +// } + +func (o *Oizys) NixBuild(nom bool, rest ...string) { + var cmdName string + if nom { + cmdName = "nom" + } else { + cmdName = "nix" + } + cmd := exec.Command(cmdName, "build") + cmd.Args = append(cmd.Args, rest...) runCommand(cmd) } +func (o *Oizys) getChecks() []string { + attrName := fmt.Sprintf("%s#%s", o.flake, "checks.x86_64-linux") + cmd := exec.Command("nix", "eval", attrName, "--apply", "builtins.attrNames", "--json") + out, err := cmd.Output() + if err != nil { + log.Fatal(err) + } + var checks []string + if err := json.Unmarshal(out, &checks); err != nil { + log.Fatal(err) + } + return checks +} + +func (o *Oizys) checkPath(name string) string { + return fmt.Sprintf("%s#checks.x86_64-linux.%s", o.flake, name) +} + +func (o *Oizys) Checks(nom bool, rest ...string) { + checks := o.getChecks() + for _, check := range checks { + o.NixBuild(nom, o.checkPath(check)) + } +} + func (o *Oizys) CacheBuild(rest ...string) { args := []string{ "watch-exec", o.cache, "--", "nix", @@ -289,7 +335,7 @@ func Output(flake string, host string) string { func nixSpinner(host string) *spinner.Spinner { 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), ) s := spinner.New( spinner.CharSets[14],