Skip to content

Commit 2ffe94f

Browse files
authored
[plugins] Improve how we determine package name in plugins (#1062)
## Summary This improves plugins by using attribute path instead of canonical name. for example, in `[email protected]` the attribute path is `php82` and not `php`. Previously we were using canonical name which may not be the correct version. (and may not even exist). ## How was it tested? Since existing PHP example had canonical name that matches attribute name, I changed the php version to `[email protected]` and the inspected generated `php/flake.nix`. Also did `devbox run "php -b | grep imagick" to verify plugin worked.
1 parent a25dd4d commit 2ffe94f

File tree

6 files changed

+28
-9
lines changed

6 files changed

+28
-9
lines changed

internal/impl/flakes.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ func (d *Devbox) flakeInputs() ([]*plansdk.FlakeInput, error) {
3030
// if this is behavior we want for user plugins. We may need to add an optional
3131
// priority field to the config.
3232
for _, pkg := range append(pluginPackages, userPackages...) {
33-
AttributePath, err := pkg.PackageAttributePath()
33+
AttributePath, err := pkg.FullPackageAttributePath()
3434
if err != nil {
3535
return nil, err
3636
}

internal/nix/input.go

+17-3
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ func (i *Input) URLForInstall() (string, error) {
100100
}
101101
return entry.Resolved, nil
102102
}
103-
attrPath, err := i.PackageAttributePath()
103+
attrPath, err := i.FullPackageAttributePath()
104104
if err != nil {
105105
return "", err
106106
}
@@ -135,11 +135,25 @@ func (i *Input) normalizedDevboxPackageReference() (string, error) {
135135
return "", nil
136136
}
137137

138-
// PackageAttributePath returns the attribute path for a package. It is not
138+
// PackageAttributePath returns the short attribute path for a package which
139+
// does not include packages/legacyPackages or the system name.
140+
func (i *Input) PackageAttributePath() (string, error) {
141+
if i.IsDevboxPackage() {
142+
entry, err := i.lockfile.Resolve(i.String())
143+
if err != nil {
144+
return "", err
145+
}
146+
_, fragment, _ := strings.Cut(entry.Resolved, "#")
147+
return fragment, nil
148+
}
149+
return i.Fragment, nil
150+
}
151+
152+
// FullPackageAttributePath returns the attribute path for a package. It is not
139153
// always normalized which means it should not be used to compare packages.
140154
// During happy paths (devbox packages and nix flakes that contains a fragment)
141155
// it is much faster than NormalizedPackageAttributePath
142-
func (i *Input) PackageAttributePath() (string, error) {
156+
func (i *Input) FullPackageAttributePath() (string, error) {
143157
if i.IsDevboxPackage() {
144158
reference, err := i.normalizedDevboxPackageReference()
145159
if err != nil {

internal/plugin/plugin.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -150,13 +150,18 @@ func (m *Manager) createFile(
150150
return err
151151
}
152152

153+
attributePath, err := pkg.PackageAttributePath()
154+
if err != nil {
155+
return err
156+
}
157+
153158
var buf bytes.Buffer
154159
if err = tmpl.Execute(&buf, map[string]any{
155160
"DevboxConfigDir": m.ProjectDir(),
156161
"DevboxDir": filepath.Join(m.ProjectDir(), devboxDirName, name),
157162
"DevboxDirRoot": filepath.Join(m.ProjectDir(), devboxDirName),
158163
"DevboxProfileDefault": filepath.Join(m.ProjectDir(), nix.ProfilePath),
159-
"PackageName": name,
164+
"PackageAttributePath": attributePath,
160165
"Packages": m.Packages(),
161166
"System": system,
162167
"URLForInput": pkg.URLForInput(),

plugins/haskell/flake.nix

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77

88
outputs = { self, nixpkgs }:
99
let
10-
version = builtins.elemAt (builtins.match "^haskell\.compiler\.(.*)$" "{{ .PackageName }}") 0;
10+
version = builtins.elemAt (builtins.match "^haskell\.compiler\.(.*)$" "{{ .PackageAttributePath }}") 0;
1111

12-
ghcWithPackages = if "{{ .PackageName }}" == "ghc"
12+
ghcWithPackages = if "{{ .PackageAttributePath }}" == "ghc"
1313
then nixpkgs.legacyPackages.{{ .System }}.pkgs.haskellPackages.ghcWithPackages
1414
else nixpkgs.legacyPackages.{{ .System }}.pkgs.haskell.packages.${version}.ghcWithPackages;
1515

plugins/mariadb/flake.nix

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
mariadb-bin = nixpkgs.legacyPackages.{{.System}}.symlinkJoin {
1111

1212
name = "mariadb-wrapped";
13-
paths = [nixpkgs.legacyPackages.{{ .System }}.{{.PackageName}}];
13+
paths = [nixpkgs.legacyPackages.{{ .System }}.{{.PackageAttributePath}}];
1414
nativeBuildInputs = [ nixpkgs.legacyPackages.{{.System}}.makeWrapper];
1515
postBuild = ''
1616

plugins/php/flake.nix

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
{{- end }}
1414
]);
1515

16-
php = nixpkgs.legacyPackages.{{ .System }}.php.withExtensions (
16+
php = nixpkgs.legacyPackages.{{ .System }}.{{ .PackageAttributePath }}.withExtensions (
1717
{ enabled, all }: enabled ++ (with all;
1818
map (ext: all.${ext}) extensions
1919
)

0 commit comments

Comments
 (0)