forked from jetify-com/devbox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
devbox.go
505 lines (431 loc) · 12.9 KB
/
devbox.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
// Copyright 2022 Jetpack Technologies Inc and contributors. All rights reserved.
// Use of this source code is governed by the license in the LICENSE file.
// Package devbox creates isolated development environments.
package devbox
import (
"fmt"
"io"
"os"
"os/exec"
"path/filepath"
"strconv"
"strings"
"github.com/fatih/color"
"github.com/pkg/errors"
"github.com/samber/lo"
"go.jetpack.io/devbox/boxcli/featureflag"
"go.jetpack.io/devbox/cuecfg"
"go.jetpack.io/devbox/debug"
"go.jetpack.io/devbox/nix"
"go.jetpack.io/devbox/pkgcfg"
"go.jetpack.io/devbox/planner"
"go.jetpack.io/devbox/planner/plansdk"
"golang.org/x/exp/slices"
)
const (
// configFilename is name of the JSON file that defines a devbox environment.
configFilename = "devbox.json"
// shellHistoryFile keeps the history of commands invoked inside devbox shell
shellHistoryFile = ".devbox/shell_history"
)
// InitConfig creates a default devbox config file if one doesn't already
// exist.
func InitConfig(dir string) (created bool, err error) {
cfgPath := filepath.Join(dir, configFilename)
config := &Config{
Nixpkgs: NixpkgsConfig{
Commit: plansdk.DefaultNixpkgsCommit,
},
}
return cuecfg.InitFile(cfgPath, config)
}
// Devbox provides an isolated development environment that contains a set of
// Nix packages.
type Devbox struct {
cfg *Config
// configDir is the directory where the config file (devbox.json) resides
configDir string
writer io.Writer
}
// Open opens a devbox by reading the config file in dir.
// TODO savil. dir is technically path since it could be a dir or file
func Open(dir string, writer io.Writer) (*Devbox, error) {
cfgDir, err := findConfigDir(dir)
if err != nil {
return nil, err
}
cfgPath := filepath.Join(cfgDir, configFilename)
cfg, err := ReadConfig(cfgPath)
if err != nil {
return nil, errors.WithStack(err)
}
if err = upgradeConfig(cfg, cfgPath); err != nil {
return nil, err
}
box := &Devbox{
cfg: cfg,
configDir: cfgDir,
writer: writer,
}
return box, nil
}
func (d *Devbox) ConfigDir() string {
return d.configDir
}
func (d *Devbox) Config() *Config {
return d.cfg
}
// Add adds a Nix package to the config so that it's available in the devbox
// environment. It validates that the Nix package exists, but doesn't install
// it. Adding a duplicate package is a no-op.
func (d *Devbox) Add(pkgs ...string) error {
// Check packages are valid before adding.
for _, pkg := range pkgs {
ok := nix.PkgExists(d.cfg.Nixpkgs.Commit, pkg)
if !ok {
return errors.Errorf("package %s not found", pkg)
}
}
// Add to Packages to config only if it's not already there
for _, pkg := range pkgs {
if slices.Contains(d.cfg.Packages, pkg) {
continue
}
d.cfg.Packages = append(d.cfg.Packages, pkg)
}
if err := d.saveCfg(); err != nil {
return err
}
if err := d.ensurePackagesAreInstalled(install); err != nil {
return err
}
if featureflag.PKGConfig.Enabled() {
for _, pkg := range pkgs {
if err := pkgcfg.PrintReadme(pkg, d.configDir, d.writer, IsDevboxShellEnabled()); err != nil {
return err
}
}
}
return d.printPackageUpdateMessage(install, pkgs)
}
// Remove removes Nix packages from the config so that it no longer exists in
// the devbox environment.
func (d *Devbox) Remove(pkgs ...string) error {
// First, save which packages are being uninstalled. Do this before we modify d.cfg.Packages below.
uninstalledPackages := lo.Intersect(d.cfg.Packages, pkgs)
var missingPkgs []string
d.cfg.Packages, missingPkgs = lo.Difference(d.cfg.Packages, pkgs)
if len(missingPkgs) > 0 {
fmt.Fprintf(
d.writer,
"%s the following packages were not found in your devbox.json: %s\n",
color.HiYellowString("Warning:"),
strings.Join(missingPkgs, ", "),
)
}
if err := d.saveCfg(); err != nil {
return err
}
if featureflag.PKGConfig.Enabled() {
if err := pkgcfg.Remove(d.configDir, uninstalledPackages); err != nil {
return err
}
}
if err := d.ensurePackagesAreInstalled(uninstall); err != nil {
return err
}
return d.printPackageUpdateMessage(uninstall, uninstalledPackages)
}
// ShellPlan creates a plan of the actions that devbox will take to generate its
// shell environment.
func (d *Devbox) ShellPlan() (*plansdk.ShellPlan, error) {
userDefinedPkgs := d.cfg.Packages
shellPlan := planner.GetShellPlan(d.configDir, userDefinedPkgs)
shellPlan.DevPackages = userDefinedPkgs
if nixpkgsInfo, err := plansdk.GetNixpkgsInfo(d.cfg.Nixpkgs.Commit); err != nil {
return nil, err
} else {
shellPlan.NixpkgsInfo = nixpkgsInfo
}
return shellPlan, nil
}
// BuildPlan creates a plan of the actions that devbox will take to generate its
// shell environment.
func (d *Devbox) BuildPlan() (*plansdk.BuildPlan, error) {
userPlan := d.convertToBuildPlan()
buildPlan, err := planner.GetBuildPlan(d.configDir, d.cfg.Packages)
if err != nil {
return nil, err
}
plan, err := plansdk.MergeUserBuildPlan(userPlan, buildPlan)
if err != nil {
return nil, err
}
if nixpkgsInfo, err := plansdk.GetNixpkgsInfo(d.cfg.Nixpkgs.Commit); err != nil {
return nil, err
} else {
plan.NixpkgsInfo = nixpkgsInfo
}
return plan, nil
}
// Generate creates the directory of Nix files and the Dockerfile that define
// the devbox environment.
func (d *Devbox) Generate() error {
if err := d.generateShellFiles(); err != nil {
return errors.WithStack(err)
}
return nil
}
// Shell generates the devbox environment and launches nix-shell as a child
// process.
func (d *Devbox) Shell() error {
if err := d.ensurePackagesAreInstalled(install); err != nil {
return err
}
plan, err := d.ShellPlan()
if err != nil {
return err
}
profileDir, err := d.profileDir()
if err != nil {
return err
}
nixShellFilePath := filepath.Join(d.configDir, ".devbox/gen/shell.nix")
opts := []nix.ShellOption{
nix.WithPlanInitHook(strings.Join(plan.ShellInitHook, "\n")),
nix.WithProfile(profileDir),
nix.WithHistoryFile(filepath.Join(d.configDir, shellHistoryFile)),
nix.WithConfigDir(d.configDir),
}
if featureflag.PKGConfig.Enabled() {
env, err := pkgcfg.Env(plan.DevPackages, d.configDir)
if err != nil {
return err
}
opts = append(
opts,
nix.WithEnvVariables(env),
nix.WithPKGConfigDir(filepath.Join(d.configDir, ".devbox/conf/bin")),
)
}
shell, err := nix.DetectShell(opts...)
if err != nil {
// Fall back to using a plain Nix shell.
shell = &nix.Shell{}
}
allPkgs := planner.GetShellPackageSuggestion(d.configDir, d.cfg.Packages)
pkgsToSuggest, _ := lo.Difference(allPkgs, d.cfg.Packages)
if len(pkgsToSuggest) > 0 {
s := fmt.Sprintf("devbox add %s", strings.Join(pkgsToSuggest, " "))
fmt.Fprintf(
d.writer,
"We detected extra packages you may need. To install them, run `%s`\n",
color.HiYellowString(s),
)
}
shell.UserInitHook = d.cfg.Shell.InitHook.String()
return shell.Run(nixShellFilePath)
}
// TODO: consider unifying the implementations of RunScript and Shell.
func (d *Devbox) RunScript(scriptName string) error {
if err := d.ensurePackagesAreInstalled(install); err != nil {
return err
}
plan, err := d.ShellPlan()
if err != nil {
return err
}
profileDir, err := d.profileDir()
if err != nil {
return err
}
nixShellFilePath := filepath.Join(d.configDir, ".devbox/gen/shell.nix")
script := d.cfg.Shell.Scripts[scriptName]
if script == nil {
return errors.Errorf("unable to find a script with name %s", scriptName)
}
shell, err := nix.DetectShell(
nix.WithPlanInitHook(strings.Join(plan.ShellInitHook, "\n")),
nix.WithProfile(profileDir),
nix.WithHistoryFile(filepath.Join(d.configDir, shellHistoryFile)),
nix.WithUserScript(scriptName, script.String()),
nix.WithConfigDir(d.configDir),
)
if err != nil {
fmt.Print(err)
shell = &nix.Shell{}
}
shell.UserInitHook = d.cfg.Shell.InitHook.String()
return shell.Run(nixShellFilePath)
}
func (d *Devbox) ListScripts() []string {
keys := make([]string, len(d.cfg.Shell.Scripts))
i := 0
for k := range d.cfg.Shell.Scripts {
keys[i] = k
i++
}
return keys
}
func (d *Devbox) Exec(cmds ...string) error {
if err := d.ensurePackagesAreInstalled(install); err != nil {
return err
}
profileBinDir, err := d.profileBinDir()
if err != nil {
return err
}
pathWithProfileBin := fmt.Sprintf("PATH=%s:$PATH", profileBinDir)
cmds = append([]string{pathWithProfileBin}, cmds...)
nixDir := filepath.Join(d.configDir, ".devbox/gen/shell.nix")
return nix.Exec(nixDir, cmds)
}
func (d *Devbox) PrintShellEnv() error {
profileBinDir, err := d.profileBinDir()
if err != nil {
return errors.WithStack(err)
}
// TODO: For now we just updated the PATH but this may need to evolve
// to essentially a parsed shellrc.tmpl
fmt.Fprintf(d.writer, "export PATH=\"%s:$PATH\"", profileBinDir)
return nil
}
func (d *Devbox) Info(pkg string) error {
info, hasInfo := nix.PkgInfo(d.cfg.Nixpkgs.Commit, pkg)
if !hasInfo {
_, err := fmt.Fprintf(d.writer, "Package %s not found\n", pkg)
return errors.WithStack(err)
}
if _, err := fmt.Fprintf(d.writer, "%s\n", info); err != nil {
return errors.WithStack(err)
}
return pkgcfg.PrintReadme(pkg, d.configDir, d.writer, false /*showSourceEnv*/)
}
// saveCfg writes the config file to the devbox directory.
func (d *Devbox) saveCfg() error {
cfgPath := filepath.Join(d.configDir, configFilename)
return cuecfg.WriteFile(cfgPath, d.cfg)
}
func (d *Devbox) convertToBuildPlan() *plansdk.BuildPlan {
configStages := []*Stage{d.cfg.InstallStage, d.cfg.BuildStage, d.cfg.StartStage}
planStages := []*plansdk.Stage{{}, {}, {}}
for i, stage := range configStages {
if stage != nil {
planStages[i] = &plansdk.Stage{
Command: stage.Command,
}
}
}
return &plansdk.BuildPlan{
DevPackages: d.cfg.Packages,
RuntimePackages: d.cfg.Packages,
InstallStage: planStages[0],
BuildStage: planStages[1],
StartStage: planStages[2],
}
}
func (d *Devbox) generateShellFiles() error {
plan, err := d.ShellPlan()
if err != nil {
return err
}
return generateForShell(d.configDir, plan)
}
func (d *Devbox) profileDir() (string, error) {
absPath := filepath.Join(d.configDir, nix.ProfilePath)
if err := os.MkdirAll(filepath.Dir(absPath), 0755); err != nil {
return "", errors.WithStack(err)
}
return absPath, nil
}
func (d *Devbox) profileBinDir() (string, error) {
profileDir, err := d.profileDir()
if err != nil {
return "", err
}
return filepath.Join(profileDir, "bin"), nil
}
// installMode is an enum for helping with ensurePackagesAreInstalled implementation
type installMode string
const (
install installMode = "install"
uninstall installMode = "uninstall"
)
func (d *Devbox) ensurePackagesAreInstalled(mode installMode) error {
if err := d.generateShellFiles(); err != nil {
return err
}
installingVerb := "Installing"
if mode == uninstall {
installingVerb = "Uninstalling"
}
_, _ = fmt.Fprintf(d.writer, "%s nix packages. This may take a while... ", installingVerb)
// We need to re-install the packages
if err := d.applyDevNixDerivation(); err != nil {
fmt.Println()
return errors.Wrap(err, "apply Nix derivation")
}
fmt.Println("done.")
if featureflag.PKGConfig.Enabled() {
if err := pkgcfg.RemoveInvalidSymlinks(d.configDir); err != nil {
return err
}
}
return nil
}
func (d *Devbox) printPackageUpdateMessage(mode installMode, pkgs []string) error {
installedVerb := "installed"
if mode == uninstall {
installedVerb = "removed"
}
if len(pkgs) > 0 {
successMsg := fmt.Sprintf("%s is now %s.", pkgs[0], installedVerb)
if len(pkgs) > 1 {
successMsg = fmt.Sprintf("%s are now %s.", strings.Join(pkgs, ", "), installedVerb)
}
fmt.Fprint(d.writer, successMsg)
// (Only when in devbox shell) Prompt the user to run `hash -r` to ensure their
// shell can access the most recently installed binaries, or ensure their
// recently uninstalled binaries are not accidentally still available.
if !IsDevboxShellEnabled() {
fmt.Fprintln(d.writer)
} else {
fmt.Fprintln(d.writer, " Run `hash -r` to ensure your shell is updated.")
}
} else {
fmt.Fprintf(d.writer, "No packages %s.\n", installedVerb)
}
return nil
}
// applyDevNixDerivation installs or uninstalls packages to or from this
// devbox's Nix profile so that it matches what's in development.nix.
func (d *Devbox) applyDevNixDerivation() error {
profileDir, err := d.profileDir()
if err != nil {
return err
}
cmd := exec.Command("nix-env",
"--profile", profileDir,
"--install",
"-f", filepath.Join(d.configDir, ".devbox/gen/development.nix"),
)
debug.Log("Running command: %s\n", cmd.Args)
_, err = cmd.Output()
var exitErr *exec.ExitError
if errors.As(err, &exitErr) {
return errors.Errorf("running command %s: exit status %d with command output: %s",
cmd, exitErr.ExitCode(), string(exitErr.Stderr))
}
if err != nil {
return errors.Errorf("running command %s: %v", cmd, err)
}
return nil
}
// Move to a utility package?
func IsDevboxShellEnabled() bool {
inDevboxShell, err := strconv.ParseBool(os.Getenv("DEVBOX_SHELL_ENABLED"))
if err != nil {
return false
}
return inDevboxShell
}