Skip to content

Commit e8ad37d

Browse files
authored
Update plugin documentation (#1502)
## Summary Update Plugin documentation in response to #1499. * Adds more details about how to include/add a plugin * Updated schema to match upcoming init_hook improvements * Adds suggestions on how to structure the plugin directory, along with an example from mongodb ## How was it tested? Localhost, also check the preview URL from Vercel below
1 parent e55ef34 commit e8ad37d

File tree

2 files changed

+108
-37
lines changed

2 files changed

+108
-37
lines changed

docs/app/docs/guides/creating_plugins.md

Lines changed: 80 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -8,40 +8,33 @@ Plugins make it easier to get started with packages that require additional setu
88

99
Before writing a plugin, we recommend reading the [User Documentation](https://www.jetpack.io/devbox/docs/guides/plugins/) on plugins, as well as inspecting and testing a few of the plugins in the [plugin directory](https://github.com/jetpack-io/devbox/tree/main/plugins) of our repo. Note that the plugins in this directory are compiled into the Devbox binary, but your plugin can be sourced from a local directory or from within your project.
1010

11-
1211
If you're looking for plugin ideas, check out our [Issues page](https://github.com/jetpack-io/devbox/issues?q=is%3Aissue+is%3Aopen+label%3A%22plugin+request%22) for any user requests.
1312

1413
Before contributing, please consult our [Contributing Guide](https://github.com/jetpack-io/devbox/CONTRIBUTING.md) and [Code of Conduct](https://github.com/jetpack-io/devbox/CODE_OF_CONDUCT.md) for details on how to contribute to Devbox.
1514

16-
### Testing your Plugin
15+
## Creating a Plugin
1716

18-
1. Create a new `devbox.json` in an empty directory using `devbox init`.
19-
2. Add your plugin to the `include` section of the `devbox.json` file. Add any expected packages using `devbox add <pkg>`.
20-
3. Check that your plugin creates the correct files and environment variables when running `devbox shell`
21-
4. If you are looking for sample projects to test your plugin with, check out our [examples](https://github.com/jetpack-io/devbox/tree/main/examples).
17+
We recommend organizing your plugin with the following directory structure, where the top-level folder matches the name of your plugin:
2218

23-
## Plugin Design
19+
```
20+
my-plugin/
21+
├── README.md
22+
├── plugin.json
23+
├── config/
24+
│   ├── my-plugin.conf
25+
│   └── process-compose.yaml
26+
└── test/
27+
├── devbox.json
28+
└── devbox.lock
29+
```
2430

25-
Plugins are defined as Go JSON Template files, using the following schema:
31+
* **README.md** -- Should contain a description of how your plugin works, and what files, variables, and services it adds to Devbox Projects
32+
* **plugin.json** -- This file is a Go JSON Template that defines your plugin. See the sections below for more detail
33+
* **config/** -- This folder contains any support or configuration files required by your plugin, as well as the process-compose.yaml for defining services
34+
* **test/** -- This directory contains an example project for testing your plugin
2635

27-
```json
28-
{
29-
"name": "",
30-
"version": "",
31-
"readme": "",
32-
"env": {
33-
"<key>": "<value>"
34-
},
35-
"create_files": {
36-
"<destination>": "<source>"
37-
},
38-
"init_hook": [
39-
"<bash commands>"
40-
]
41-
}
42-
```
4336

44-
A plugin can define services by adding a `process-compose.yaml` file in its `create_files` stanza.
37+
## Plugin Design
4538

4639
### Plugin Lifecycle
4740

@@ -62,6 +55,31 @@ flowchart TD
6255
H[Start Services]
6356
```
6457

58+
### Plugin.json Schema
59+
60+
Plugins are defined as Go JSON Template files, using the following schema:
61+
62+
```json
63+
{
64+
"name": "",
65+
"version": "",
66+
"readme": "",
67+
"env": {
68+
"<key>": "<value>"
69+
},
70+
"create_files": {
71+
"<destination>": "<source>"
72+
},
73+
"shell": {
74+
"init_hook": [
75+
"<bash commands>"
76+
]
77+
}
78+
}
79+
```
80+
81+
A plugin can define services by adding a `process-compose.yaml` file in its `create_files` stanza.
82+
6583
### Template Placeholders
6684

6785
Devbox's Plugin System provides a few special placeholders that should be used when specifying paths for env variables and helper files:
@@ -108,9 +126,11 @@ Will copy the Caddyfile in the `plugins/caddy` folder to `devbox.d/caddy/Caddyfi
108126

109127
You should use this to copy starter config files or templates needed to run the plugin's package.
110128

111-
#### `init_hook` *string | string[]*
129+
#### `shell.init_hook` *string | string[]*
130+
131+
A single `bash` command or list of `bash` commands that should run before the user's shell is initialized.
112132

113-
A single `bash` command or list of `bash` commands that should run before the user's shell is initialized. This will run every time a shell is started, so you should avoid any resource heavy or long running processes in this step.
133+
This will run every time a shell is started, so you should avoid any resource heavy or long running processes in this step.
114134

115135
### Adding Services
116136

@@ -120,6 +140,39 @@ Plugins can add services to a user's project by adding a `process-compose.yaml`
120140

121141
See the process compose [docs](https://github.com/F1bonacc1/process-compose) for details on how to write define services in `process-compose.yaml`. You can also check the plugins in this directory for examples on how to write services.
122142

143+
## Testing your Plugin
144+
145+
Testing plugins can be done using an example Devbox project. Follow the steps below to create a new test project
146+
147+
1. Create a new `devbox.json` in an empty directory using `devbox init`.
148+
2. Add your plugin to the `include` section of the `devbox.json` file.
149+
2. Add any expected packages using `devbox add <pkg>`.
150+
3. Check that your plugin creates the correct files and environment variables when running `devbox shell`
151+
4. If you are looking for sample projects to test your plugin with, check out our [examples](https://github.com/jetpack-io/devbox/tree/main/examples).
152+
153+
154+
## Example: MongoDB
155+
156+
The plugin.json below sets the environment variables and config needed to run MongoDB in Devbox. You can view the full example at
157+
158+
```json
159+
{
160+
"name": "mongodb",
161+
"version": "0.0.1",
162+
"readme": "Plugin for the [`mongodb`](https://www.nixhub.io/packages/mongodb) package. This plugin configures MonogoDB to use a local config file and data directory for this project, and configures a mongodb service.",
163+
"env": {
164+
"MONGODB_DATA": "{{.Virtenv}}/data",
165+
"MONGODB_CONFIG": "{{.DevboxDir}}/mongod.conf"
166+
},
167+
"create_files": {
168+
"{{.Virtenv}}/data": "",
169+
"{{.Virtenv}}/process-compose.yaml": "config/process-compose.yaml",
170+
"{{.DevboxDir}}/mongod.conf": "config/mongod.conf"
171+
}
172+
}
173+
174+
```
175+
123176
## Tips for Writing Plugins
124177

125178
* Only add plugins for packages that require configuration to work with Devbox.

docs/app/docs/guides/plugins.md

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,22 @@ title: Using Plugins
44

55
This doc describes how to use Devbox Plugins with your project. **Plugins** provide a default Devbox configuration for a Nix package. Plugins make it easier to get started with packages that require additional setup when installed with Nix, and they offer a familiar interface for configuring packages. They also help keep all of your project's configuration within your project directory, which helps maintain portability and isolation.
66

7-
If a plugin is available for your package, it will activate when you install the plugin using `devbox add <package name>`.
7+
## Using Plugins
8+
9+
### Built-in PLguins
10+
11+
If you add one of the packages listed above to your project using `devbox add <pkg>`, Devbox will automatically activate the plugin for that package.
12+
13+
You can also explicitly add a built-in plugin in your project by adding it to the [`include` section](../configuration.md#include) of your `devbox.json` file. For example, to explicitly add the plugin for Nginx, you can add the following to your `devbox.json` file:
14+
15+
```json
16+
{
17+
"include": [
18+
"plugin:nginx"
19+
]
20+
}
21+
```
822

9-
## Current Plugins
1023
Built-in plugins are available for the following packages. You can activate the plugins for these packages by running `devbox add <package_name>`
1124

1225
* [Apache](../devbox_examples/servers/apache.md) (apacheHttpd)
@@ -20,20 +33,25 @@ Built-in plugins are available for the following packages. You can activate the
2033
* [Pip](../devbox_examples/languages/python.md) (python39Packages.pip, python310Packages.pip, python311Packages.pip...)
2134
* [Ruby](../devbox_examples/languages/ruby.md)(ruby, ruby_3_1, ruby_3_0...)
2235

23-
Our team is rapidly adding new plugins to Devbox. If you want to request a plugin, please file an issue in the Devbox Repo.
2436

25-
## Using Plugins
37+
### Local PLugins
2638

27-
If you add one of the packages listed above to your project using `devbox add <pkg>`, Devbox will automatically activate the plugin for that package.
39+
You can also [define your own plugins](./creating_plugins.md) and use them in your project. To use a local plugin, add the following to the `include` section of your devbox.json:
2840

29-
You can also explicitly add a plugin in your project by adding it to the [`includes` section](../configuration.md#includes) of your `devbox.json` file. For example, to explicitly add the plugin for Nginx, you can add the following to your `devbox.json` file:
41+
```json
42+
"include": [
43+
"path:./path/to/plugin.json"
44+
]
45+
```
46+
47+
### Github Hosted Plugins
48+
49+
Sometimes, you may want to share a plugin across multiple projects or users. In this case, you provide a Github reference to a plugin hosted on Github. To install a github hosted plugin, add the following to the include section of your devbox.json
3050

3151
```json
32-
{
33-
"includes": [
34-
"plugin:nginx"
52+
"include": [
53+
"github:<org>/<repo>?dir=<plugin-dir>"
3554
]
36-
}
3755
```
3856

3957
## An Example of a Plugin: Nginx

0 commit comments

Comments
 (0)