1
1
// Copyright 2022 Jetpack Technologies Inc and contributors. All rights reserved.
2
2
// Use of this source code is governed by the license in the LICENSE file.
3
3
4
+ // Package devbox creates isolated development environments.
4
5
package devbox
5
6
6
7
import (
@@ -14,20 +15,26 @@ import (
14
15
"go.jetpack.io/devbox/planner"
15
16
)
16
17
18
+ // configFilename is name of the JSON file that defines a devbox environment.
19
+ const configFilename = "devbox.json"
20
+
21
+ // InitConfig creates a default devbox config file if one doesn't already
22
+ // exist.
23
+ func InitConfig (dir string ) (created bool , err error ) {
24
+ cfgPath := filepath .Join (dir , configFilename )
25
+ return cuecfg .InitFile (cfgPath , & Config {})
26
+ }
27
+
28
+ // Devbox provides an isolated development environment that contains a set of
29
+ // Nix packages.
17
30
type Devbox struct {
18
31
cfg * Config
19
32
srcDir string
20
33
}
21
34
22
- const CONFIG_FILENAME = "devbox.json"
23
-
24
- func Init (dir string ) (bool , error ) {
25
- cfgPath := filepath .Join (dir , CONFIG_FILENAME )
26
- return cuecfg .InitFile (cfgPath , & Config {})
27
- }
28
-
35
+ // Open opens a devbox by reading the config file in dir.
29
36
func Open (dir string ) (* Devbox , error ) {
30
- cfgPath := filepath .Join (dir , CONFIG_FILENAME )
37
+ cfgPath := filepath .Join (dir , configFilename )
31
38
32
39
cfg , err := ReadConfig (cfgPath )
33
40
if err != nil {
@@ -41,30 +48,32 @@ func Open(dir string) (*Devbox, error) {
41
48
return box , nil
42
49
}
43
50
51
+ // Add adds a Nix package to the config so that it's available in the devbox
52
+ // environment. It validates that the Nix package exists, but doesn't install
53
+ // it. Adding a duplicate package is a no-op.
44
54
func (d * Devbox ) Add (pkgs ... string ) error {
45
55
// Check packages exist before adding.
46
56
for _ , pkg := range pkgs {
47
57
ok := nix .PkgExists (pkg )
48
58
if ! ok {
49
- return errors .Errorf ("Package %s not found. " , pkg )
59
+ return errors .Errorf ("package %s not found" , pkg )
50
60
}
51
61
}
52
62
// Merge and remove duplicates:
53
63
merged := append (d .cfg .Packages , pkgs ... )
54
64
d .cfg .Packages = lo .FindUniques (merged )
55
-
56
- // Save config.
57
65
return d .saveCfg ()
58
66
}
59
67
68
+ // Remove removes Nix packages from the config so that it no longer exists in
69
+ // the devbox environment.
60
70
func (d * Devbox ) Remove (pkgs ... string ) error {
61
71
// Remove packages from config.
62
72
d .cfg .Packages = lo .Without (d .cfg .Packages , pkgs ... )
63
-
64
- // Save config.
65
73
return d .saveCfg ()
66
74
}
67
75
76
+ // Build creates a Docker image containing a shell with the devbox environment.
68
77
func (d * Devbox ) Build (opts ... docker.BuildOptions ) error {
69
78
defaultFlags := & docker.BuildFlags {
70
79
Name : "devbox" ,
@@ -79,18 +88,24 @@ func (d *Devbox) Build(opts ...docker.BuildOptions) error {
79
88
return docker .Build (d .srcDir , opts ... )
80
89
}
81
90
91
+ // Plan creates a plan of the actions that devbox will take to generate its
92
+ // environment.
82
93
func (d * Devbox ) Plan () * planner.BuildPlan {
83
94
basePlan := & planner.BuildPlan {
84
95
Packages : d .cfg .Packages ,
85
96
}
86
97
return planner .MergePlans (basePlan , planner .Plan (d .srcDir ))
87
98
}
88
99
100
+ // Generate creates the directory of Nix files and the Dockerfile that define
101
+ // the devbox environment.
89
102
func (d * Devbox ) Generate () error {
90
103
plan := d .Plan ()
91
104
return generate (d .srcDir , plan )
92
105
}
93
106
107
+ // Shell generates the devbox environment and launches nix-shell as a child
108
+ // process.
94
109
func (d * Devbox ) Shell () error {
95
110
err := d .Generate ()
96
111
if err != nil {
@@ -100,7 +115,8 @@ func (d *Devbox) Shell() error {
100
115
return nix .Shell (nixDir )
101
116
}
102
117
118
+ // saveCfg writes the config file to the devbox directory.
103
119
func (d * Devbox ) saveCfg () error {
104
- cfgPath := filepath .Join (d .srcDir , CONFIG_FILENAME )
120
+ cfgPath := filepath .Join (d .srcDir , configFilename )
105
121
return cuecfg .WriteFile (cfgPath , d .cfg )
106
122
}
0 commit comments