From c5092c5123c626408ef9e9302631b77ebe44b115 Mon Sep 17 00:00:00 2001 From: Savil Srivastava <676452+savil@users.noreply.github.com> Date: Thu, 1 Feb 2024 05:26:16 -0800 Subject: [PATCH] fix nix.readManifest and nix search --- internal/nix/profiles.go | 37 +++++++++++++++++++++++++++++++++---- internal/nix/search.go | 9 ++++++++- 2 files changed, 41 insertions(+), 5 deletions(-) diff --git a/internal/nix/profiles.go b/internal/nix/profiles.go index 5ca87e3d5f6..2589305c071 100644 --- a/internal/nix/profiles.go +++ b/internal/nix/profiles.go @@ -94,8 +94,8 @@ func ProfileRemove(profilePath string, indexes ...string) error { type manifest struct { Elements []struct { - Priority int `json:"priority"` - } `json:"elements"` + Priority int + } } func readManifest(profilePath string) (manifest, error) { @@ -107,8 +107,37 @@ func readManifest(profilePath string) (manifest, error) { return manifest{}, err } - var m manifest - return m, json.Unmarshal(data, &m) + type manifestModern struct { + Elements map[string]struct { + Priority int `json:"priority"` + } `json:"elements"` + } + var modernMani manifestModern + if err := json.Unmarshal(data, &modernMani); err == nil { + // Convert to the result format + result := manifest{} + for _, e := range modernMani.Elements { + result.Elements = append(result.Elements, struct{ Priority int }{e.Priority}) + } + return result, nil + } + + type manifestLegacy struct { + Elements []struct { + Priority int `json:"priority"` + } `json:"elements"` + } + var legacyMani manifestLegacy + if err := json.Unmarshal(data, &legacyMani); err != nil { + return manifest{}, err + } + + // Convert to the result format + result := manifest{} + for _, e := range legacyMani.Elements { + result.Elements = append(result.Elements, struct{ Priority int }{e.Priority}) + } + return result, nil } const DefaultPriority = 5 diff --git a/internal/nix/search.go b/internal/nix/search.go index a8ea01f0463..b7d1c33e53f 100644 --- a/internal/nix/search.go +++ b/internal/nix/search.go @@ -98,7 +98,8 @@ func searchSystem(url, system string) (map[string]*Info, error) { _ = EnsureNixpkgsPrefetched(writer, hash) } - cmd := exec.Command("nix", "search", "--json", url) + // The `^` is added to indicate we want to show all packages + cmd := exec.Command("nix", "search", url, "^" /*regex*/, "--json") cmd.Args = append(cmd.Args, ExperimentalFlags()...) if system != "" { cmd.Args = append(cmd.Args, "--system", system) @@ -106,7 +107,13 @@ func searchSystem(url, system string) (map[string]*Info, error) { debug.Log("running command: %s\n", cmd) out, err := cmd.Output() if err != nil { + if exitErr := (&exec.ExitError{}); errors.As(err, &exitErr) { + err = fmt.Errorf("nix search exit code: %d, stderr: %s, original error: %w", exitErr.ExitCode(), exitErr.Stderr, err) + } + // for now, assume all errors are invalid packages. + // TODO: check the error string for "did not find attribute" and + // return ErrPackageNotFound only for that case. return nil, fmt.Errorf("error searching for pkg %s: %w", url, err) } return parseSearchResults(out), nil