|
| 1 | +package filegen |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "runtime/trace" |
| 6 | + "strings" |
| 7 | + |
| 8 | + "github.com/samber/lo" |
| 9 | + "go.jetpack.io/devbox/internal/goutil" |
| 10 | + "go.jetpack.io/devbox/internal/nix" |
| 11 | + "go.jetpack.io/devbox/internal/planner/plansdk" |
| 12 | +) |
| 13 | + |
| 14 | +type flakeInput struct { |
| 15 | + Name string |
| 16 | + Packages []string |
| 17 | + URL string |
| 18 | +} |
| 19 | + |
| 20 | +// IsNixpkgs returns true if the input is a nixpkgs flake of the form: |
| 21 | +// github:NixOS/nixpkgs/... |
| 22 | +// |
| 23 | +// While there are many ways to specify this input, devbox always uses |
| 24 | +// github:NixOS/nixpkgs/<hash> as the URL. If the user wishes to reference nixpkgs |
| 25 | +// themselves, this function may not return true. |
| 26 | +func (f *flakeInput) IsNixpkgs() bool { |
| 27 | + return nix.IsGithubNixpkgsURL(f.URL) |
| 28 | +} |
| 29 | + |
| 30 | +func (f *flakeInput) HashFromNixPkgsURL() string { |
| 31 | + if !f.IsNixpkgs() { |
| 32 | + return "" |
| 33 | + } |
| 34 | + return nix.HashFromNixPkgsURL(f.URL) |
| 35 | +} |
| 36 | + |
| 37 | +func (f *flakeInput) URLWithCaching() string { |
| 38 | + if !f.IsNixpkgs() { |
| 39 | + return f.URL |
| 40 | + } |
| 41 | + hash := nix.HashFromNixPkgsURL(f.URL) |
| 42 | + return plansdk.GetNixpkgsInfo(hash).URL |
| 43 | +} |
| 44 | + |
| 45 | +func (f *flakeInput) PkgImportName() string { |
| 46 | + return f.Name + "-pkgs" |
| 47 | +} |
| 48 | + |
| 49 | +func (f *flakeInput) BuildInputs() []string { |
| 50 | + if !f.IsNixpkgs() { |
| 51 | + return lo.Map(f.Packages, func(pkg string, _ int) string { |
| 52 | + return f.Name + "." + pkg |
| 53 | + }) |
| 54 | + } |
| 55 | + return lo.Map(f.Packages, func(pkg string, _ int) string { |
| 56 | + parts := strings.Split(pkg, ".") |
| 57 | + // Ugh, not sure if this is reliable? |
| 58 | + return f.PkgImportName() + "." + strings.Join(parts[2:], ".") |
| 59 | + }) |
| 60 | +} |
| 61 | + |
| 62 | +// flakeInputs returns a list of flake inputs for the top level flake.nix |
| 63 | +// created by devbox. We map packages to the correct flake and attribute path |
| 64 | +// and group flakes by URL to avoid duplication. All inputs should be locked |
| 65 | +// i.e. have a commit hash and always resolve to the same package/version. |
| 66 | +// Note: inputs returned by this function include plugin packages. (php only for now) |
| 67 | +// It's not entirely clear we always want to add plugin packages to the top level |
| 68 | +func flakeInputs(ctx context.Context, devbox devboxer) ([]*flakeInput, error) { |
| 69 | + defer trace.StartRegion(ctx, "flakeInputs").End() |
| 70 | + |
| 71 | + inputs := map[string]*flakeInput{} |
| 72 | + |
| 73 | + userPackages := devbox.PackagesAsInputs() |
| 74 | + pluginPackages, err := devbox.PluginManager().PluginPackages(userPackages) |
| 75 | + if err != nil { |
| 76 | + return nil, err |
| 77 | + } |
| 78 | + |
| 79 | + order := []string{} |
| 80 | + // We prioritize plugin packages so that the php plugin works. Not sure |
| 81 | + // if this is behavior we want for user plugins. We may need to add an optional |
| 82 | + // priority field to the config. |
| 83 | + for _, pkg := range append(pluginPackages, userPackages...) { |
| 84 | + AttributePath, err := pkg.FullPackageAttributePath() |
| 85 | + if err != nil { |
| 86 | + return nil, err |
| 87 | + } |
| 88 | + if input, ok := inputs[pkg.URLForInput()]; !ok { |
| 89 | + order = append(order, pkg.URLForInput()) |
| 90 | + inputs[pkg.URLForInput()] = &flakeInput{ |
| 91 | + Name: pkg.InputName(), |
| 92 | + URL: pkg.URLForInput(), |
| 93 | + Packages: []string{AttributePath}, |
| 94 | + } |
| 95 | + } else { |
| 96 | + input.Packages = lo.Uniq( |
| 97 | + append(inputs[pkg.URLForInput()].Packages, AttributePath), |
| 98 | + ) |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + return goutil.PickByKeysSorted(inputs, order), nil |
| 103 | +} |
0 commit comments