Skip to content

Commit 0118cbe

Browse files
authored
[nix] Don't add bad package to devbox.json (#525)
## Summary If we get any error while trying to add packages we revert devbox.json and show error. ## How was it tested? Tried to add a broken package and made sure it did not get added to devbox.json ```bash devbox add php81Extensions.blackfire Installing nix packages. error: attribute 'extensionName' missing at /nix/store/j5a7i3dvxslg2ychfy53wdhg1m3xfrwm-source/pkgs/development/interpreters/php/generic.nix:94:29: 93| 94| getExtName = ext: ext.extensionName; | ^ 95| (use '--show-trace' to show detailed location information) There was an error installing nix packages: php81Extensions.blackfire. Packages were not added to devbox.json Error: apply Nix derivation: running command /nix/var/nix/profiles/default/bin/nix-env --profile /Users/mike/dev/scratch/php/.devbox/nix/profile/default --install -f /Users/mike/dev/scratch/php/.devbox/gen/development.nix: exit status 1 with command stderr: ```
1 parent fa4aa27 commit 0118cbe

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

internal/impl/devbox.go

+13
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ func (d *Devbox) Config() *Config {
111111
}
112112

113113
func (d *Devbox) Add(pkgs ...string) error {
114+
original := d.cfg.Packages
114115
// Check packages are valid before adding.
115116
for _, pkg := range pkgs {
116117
ok := nix.PkgExists(d.cfg.Nixpkgs.Commit, pkg)
@@ -132,6 +133,18 @@ func (d *Devbox) Add(pkgs ...string) error {
132133

133134
d.pluginManager.ApplyOptions(plugin.WithAddMode())
134135
if err := d.ensurePackagesAreInstalled(install); err != nil {
136+
// if error installing, revert devbox.json
137+
// This is not perfect because there may be more than 1 package being
138+
// installed and we don't know which one failed. But it's better than
139+
// blindly add all packages.
140+
color.New(color.FgRed).Fprintf(
141+
d.writer,
142+
"There was an error installing nix packages: %v. "+
143+
"Packages were not added to devbox.json\n",
144+
strings.Join(pkgs, ", "),
145+
)
146+
d.cfg.Packages = original
147+
_ = d.saveCfg() // ignore error to ensure we return the original error
135148
return err
136149
}
137150

0 commit comments

Comments
 (0)