Skip to content

Files

This branch is 798 commits behind jetify-com/devbox:main.

plugins

Contributing a Plugin

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.

Getting Started

Before writing a plugin, we recommend reading the User Documentation on plugins, as well as inspecting and testing a few of the plugins in this directory.

If you're looking for plugin ideas to contribute, check out our Issues page for any user requests.

Before contributing, please consult our Contributing Guide and Code of Conduct for details on how to contribute to Devbox.

Building a Devbox CLI with your Plugin

  1. Follow the instructions in our Contributing Guide to set up your Devbox development environment
  2. Create a new JSON file with your plugin's name. For example, for a mysql plugin, you would create mysql.json.
  3. Add the configuration, then build a new version of Devbox with your plugin using devbox run build
  4. You can now test your plugin using the CLI in dist/devbox.

Testing your Plugin

  1. Create a new devbox.json in an empty directory using devbox init.
  2. Add a package that matches your plugin using devbox add {{package name}}
  3. Check that your plugin creates the correct files and environment variables using devbox shell
  4. If you are looking for sample projects to test your plugin with, check out our examples repo.

Plugin Design

Plugins are defined as JSON Template files, using the following schema:

{
  "name": "",
  "version": "",
  "match": "",
  "readme": "",
  "env": {
    "<key>": "<value>"
  },
  "create_files": {
    "<destination>": "<source>",
  },
  "init_hook": [
    "<bash commands>"
  ],
  "services": {
    "service_name": {
      "start": "<start_command>",
      "stop": "<stop_command>",
      "port": <number>
    }
  }
}

Plugin Lifecycle

Plugins are activated whenever a developer runs devbox shell, runs a script with devbox run, or starts a service using devbox services start|restart. The lifecycle of a devbox shell with plugins goes in the following order.

Loading
---
title: Devbox Shell Lifecycle
---
flowchart TD
   A[Plugin env] --> B
   B[User env] --> C
   C[Plugin init_hook] --> D[User Init Hook]
   D -->  E{Start Shell}
   E --> F & G & H
   F[Interactive Shell]
   G[Run Scripts]
   H[Start Services]

Template Placeholders

Devbox's Plugin System provides a few special placeholders that should be used when specifying paths for env variables and helper files:

  • {{ .DevboxDirRoot }} – replaced with the root folder of their project, where the user's devbox.json is stored.
  • {{ .DevboxDir }} – replaced with {{ .DevboxDirRoot }}/devbox.d/{{ plugin.name }}. This directory is public and added to source control by default. This directory is not modified or recreated by Devbox after the initial package installation. You should use this location for files that a user will want to modify and check-in to source control alongside their project (e.g., .conf files or other configs).
  • {{ .Virtenv }} – replaced with {{ .DevboxDirRoot }}/.devbox/virtenv/{{ plugin.name }} whenever the plugin activates. This directory is hidden and added to .gitignore by default You should use this location for files or variables that a user should not check-in or edit directly. Files in this directory should be considered managed by Devbox, and may be recreated or modified after the initial installation.

Fields

name string

The name of your plugin. This is used to identify your plugin when a user runs devbox info. If match is not set, the plugin will automatically activate when a package is added to a devbox.json project that matches name.

version string

The version of your plugin. You should kstart your version at 0.0.1 and bump it whenever you merge an update to the plugin.

match string

A regex expression that is used to identify when the plugin will be activated. Devbox will activate your plugin when a package installed with devbox add matches this regular expression.

The regex you provide should match a plugin name in the nixpkgs repository. You can look up packages at search.nixos.org

readme string

Special usage instructions or notes to display when your plugin activates or when a user runs devbox info. You do not need to document variables, helper files, or services, since these are automatically printed when a user runs devbox info.

env object

A map of "key" : "value" pairs used to set environment variables in devbox shell when the plugin is activated. These variables will be printed when a user runs devbox info, and can be overridden by a user's devbox.json.

create_files object

A map of "destination":"source" pairs that can be used to create or copy files into the user's devbox directory when the plugin is activated. For example:

"create_files": {
    "{{ .DevboxDir }}/Caddyfile": "caddy/Caddyfile"
}

Will copy the Caddyfile in the plugins/caddy folder to devbox.d/caddy/Caddyfile in the user's project directory.

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

init_hook string | string[]

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.

services object

A map of services that your plugin exposes to the user through devbox services. Services should have a start command and stop command defined so that Devbox can safely start and stop your service. You can optionally specify a port for Devbox to use along with automatic port forwarding in Devbox Cloud

For more details, see our Services Documentation

Tips for Writing Plugins

  • Only add plugins for packages that require configuration to work with Devbox.
  • Plugins should try to use the same configuration conventions (environment variables, configuration files) as their packages. This lets developers configure their packages in a way that they are familiar with, using existing documentation.
  • If you think a user may want to override or change a parameter, define it as an environment variable in env. This makes it possible for a developer to override the parameter in their devbox.json file
  • If you're adding a helper file that you think a developer would want check into source control, create it in {{ .DevboxDir }}. If you're creating a file that would not be checked into source control, create it in {{ .Virtenv }}.
  • Unless there is a very good reason, we do not recommend creating files outside of {{ .DevboxDir }} or {{ .Virtenv }}. This helps keep user projects clean and well organized.