|
| 1 | +package nix |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "os" |
| 7 | + "os/exec" |
| 8 | + |
| 9 | + "github.com/pkg/errors" |
| 10 | + "github.com/samber/lo" |
| 11 | + "go.jetpack.io/devbox/internal/debug" |
| 12 | + "go.jetpack.io/devbox/internal/lock" |
| 13 | +) |
| 14 | + |
| 15 | +var ErrPackageNotFound = errors.New("package not found") |
| 16 | +var ErrPackageNotInstalled = errors.New("package not installed") |
| 17 | + |
| 18 | +func PkgExists(pkg string, lock *lock.File) (bool, error) { |
| 19 | + return InputFromString(pkg, lock).validateExists() |
| 20 | +} |
| 21 | + |
| 22 | +type Info struct { |
| 23 | + // attribute key is different in flakes vs legacy so we should only use it |
| 24 | + // if we know exactly which version we are using |
| 25 | + attributeKey string |
| 26 | + PName string |
| 27 | + Version string |
| 28 | +} |
| 29 | + |
| 30 | +func (i *Info) String() string { |
| 31 | + return fmt.Sprintf("%s-%s", i.PName, i.Version) |
| 32 | +} |
| 33 | + |
| 34 | +func PkgInfo(nixpkgsCommit, pkg string) *Info { |
| 35 | + exactPackage := fmt.Sprintf("%s#%s", FlakeNixpkgs(nixpkgsCommit), pkg) |
| 36 | + if nixpkgsCommit == "" { |
| 37 | + exactPackage = fmt.Sprintf("nixpkgs#%s", pkg) |
| 38 | + } |
| 39 | + |
| 40 | + results := search(exactPackage) |
| 41 | + if len(results) == 0 { |
| 42 | + return nil |
| 43 | + } |
| 44 | + // we should only have one result |
| 45 | + return lo.Values(results)[0] |
| 46 | +} |
| 47 | + |
| 48 | +func search(url string) map[string]*Info { |
| 49 | + return searchSystem(url, "") |
| 50 | +} |
| 51 | + |
| 52 | +func parseSearchResults(data []byte) map[string]*Info { |
| 53 | + var results map[string]map[string]any |
| 54 | + err := json.Unmarshal(data, &results) |
| 55 | + if err != nil { |
| 56 | + panic(err) |
| 57 | + } |
| 58 | + infos := map[string]*Info{} |
| 59 | + for key, result := range results { |
| 60 | + infos[key] = &Info{ |
| 61 | + attributeKey: key, |
| 62 | + PName: result["pname"].(string), |
| 63 | + Version: result["version"].(string), |
| 64 | + } |
| 65 | + |
| 66 | + } |
| 67 | + return infos |
| 68 | +} |
| 69 | + |
| 70 | +// pkgExistsForAnySystem is a bit slow (~600ms). Only use it if there's already |
| 71 | +// been an error and we want to provide a better error message. |
| 72 | +func pkgExistsForAnySystem(pkg string) bool { |
| 73 | + systems := []string{ |
| 74 | + // Check most common systems first. |
| 75 | + "x86_64-linux", |
| 76 | + "x86_64-darwin", |
| 77 | + "aarch64-linux", |
| 78 | + "aarch64-darwin", |
| 79 | + |
| 80 | + "armv5tel-linux", |
| 81 | + "armv6l-linux", |
| 82 | + "armv7l-linux", |
| 83 | + "i686-linux", |
| 84 | + "mipsel-linux", |
| 85 | + "powerpc64le-linux", |
| 86 | + "riscv64-linux", |
| 87 | + } |
| 88 | + for _, system := range systems { |
| 89 | + if len(searchSystem(pkg, system)) > 0 { |
| 90 | + return true |
| 91 | + } |
| 92 | + } |
| 93 | + return false |
| 94 | +} |
| 95 | + |
| 96 | +func searchSystem(url string, system string) map[string]*Info { |
| 97 | + cmd := exec.Command("nix", "search", "--json", url) |
| 98 | + cmd.Args = append(cmd.Args, ExperimentalFlags()...) |
| 99 | + if system != "" { |
| 100 | + cmd.Args = append(cmd.Args, "--system", system) |
| 101 | + } |
| 102 | + cmd.Stderr = os.Stderr |
| 103 | + debug.Log("running command: %s\n", cmd) |
| 104 | + out, err := cmd.Output() |
| 105 | + if err != nil { |
| 106 | + // for now, assume all errors are invalid packages. |
| 107 | + return nil |
| 108 | + } |
| 109 | + return parseSearchResults(out) |
| 110 | +} |
0 commit comments