direnv
direnv is an open source environment management tool that allows setting unique environment variables per directory in your file system. This guide covers how to configure direnv to seamlessly work with a devbox project.
Prerequisites
- Install direnv and hook it to your shell. Follow this guide if you haven’t done it.
Setting up Devbox Shell and direnv
New Project
If you have direnv installed, Devbox will generate an .envrc file when you rundevbox generate direnv and enables it by running direnv allow in the background:
.envrc file doesn’t need any further configuration. Just having the generated file
along with installed direnv and Devbox, is enough to make direnv integration with Devbox work.
Adding Custom Env Variables or Env Files to your Direnv Config
In some cases, you may want to override certain environment variables in your Devbox config when running it locally. You can add custom environment variables from the command line or from a file using the--env and --env-file flags.
If you would like to add custom environment variables to your direnv config, you can do so by
passing the --env flag to devbox generate direnv. This flag takes a comma-separated list of
key-value pairs, where the key is the name of the environment variable and the value is the value of
the environment variable. For example, if you wanted to add a MY_CUSTOM_ENV_VAR environment
variable with a value of my-custom-value, you would run the following command:
.env file by passing the
--env-file flag to devbox generate direnv. This flag takes a path to a file containing
environment variables to set in the devbox environment. If the file does not exist, then this
parameter is ignored. For example, if you wanted to add a .env.devbox file located in your project
root, you would run the following command:
Global settings for direnv
Note that every time changes are made todevbox.json via devbox add ..., devbox rm ... or
directly editing the file, requires direnv allow to run so that direnv can setup the new
changes.
Alternatively, a project directory can be whitelisted so that changes will be automatically picked
up by direnv. This is done by adding following snippet to direnv config file typically at
~/.config/direnv/direnv.toml. You can create the file and directory if it doesn’t exist.
Direnv Limitations
Direnv works by creating a sub-shell using your.envrc file, your devbox.json, and other direnv
related files, and then exporting the diff in environment variables into your current shell. This
imposes some limitations on what it can load into your shell:
- Direnv cannot load shell aliases or shell functions that are sourced in your project’s
init_hook. If you want to use direnv and also configure custom aliases, we recommend using Devbox Scripts. - Direnv does not allow modifications to the $PS1 environment variable. This means
init_hooksthat modify your prompt will not work as expected. For more information, see the direnv wiki
$PS1 should work as expected when using devbox shell,
devbox run, and devbox services
VSCode setup with direnv
To seamlessly integrate VSCode with a direnv environment, follow these steps:- Open a terminal window and activate direnv with
direnv allow. - Launch VSCode from the same terminal window using the command
code .This ensures that VSCode inherits the direnv environment.