Skip to content

Commit

Permalink
fix nix.readManifest and nix search
Browse files Browse the repository at this point in the history
  • Loading branch information
savil committed Feb 1, 2024
1 parent 4d62299 commit c5092c5
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 5 deletions.
37 changes: 33 additions & 4 deletions internal/nix/profiles.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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
Expand Down
9 changes: 8 additions & 1 deletion internal/nix/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,15 +98,22 @@ 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)
}
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
Expand Down

0 comments on commit c5092c5

Please sign in to comment.