Automate Project Environments with Devbox and Direnv

BY Mark Howell 10 July 20245 MINS READ
article cover

Today in Edworking News we want to talk about The challenge of managing multiple projects in the same operating system has been met with many different solutions over the years. We've progressed from virtual machines (VirtualBox) to container systems (Docker), and even isolated shells (nix-shell). We built Devbox to make isolated shell environments easy to learn, use, and configure. The combination of easy shell environments with Devbox, combined with convenient environment switching with Direnv makes it simple to manage multiple projects without having their setups interfere or leak into one another. In this blog, we’re going to explore why having an isolated environment is useful, explain why setting up an environment with Direnv can be tough, and showcase an example with our optimal combination: Direnv + Devbox for a sample NodeJS application.

Why have an isolated environment per project?

There are 4 main reasons why an isolated environment is needed:

  1. Dependencies Conflict: Projects have different dependencies that might conflict with each other.

  2. Reproducibility: Ensures that the same environment is used throughout the project lifecycle.

  3. Security: Limits the scope of dependencies to only what's needed.

  4. Efficiency in Switching: Quickly switch between projects without altering the global system state.
    The listed items above are not the only reasons why you might want to have an isolated environment. Based on the use case, there might be other motivations but regardless, setting up isolated environments can be cumbersome and time-consuming.

Direnv

Direnv is a tool that automates configuring environments on a per-directory basis. So that when you `cd` into a directory, it triggers Direnv to activate a pre-configured environment. It makes it very convenient in managing different environments and easily switching between them just by doing `cd dir_name`.
The problem with using Direnv is that despite it being a very useful tool, its setup requires investing time and getting over the Nix ecosystem's learning curve. In addition to that, each project requires the developer to spend some time writing a `.envrc` file and maintaining it as the requirements for the project change.

Direnv simplifies managing multiple environments but requires a considerable setup time.

Automatic Devbox Environments with Direnv

We at Jetpack loved the convenience of Direnv. So following our philosophy of simplifying complicated tools to make them easy to adopt, we developed an integration between Devbox and Direnv. With this integration, there is no need to setup any `.envrc` files to use Direnv. Just having Direnv and Devbox installed on your system is enough to initiate a project environment with Devbox, add its dependencies, and integrate it with Direnv just by running `devbox generate direnv`.
That way, whenever you `cd` to a Devbox + Direnv project, the Devbox environment is automatically setup and ready to go.
Take a look at the example below to see the simple steps required to setup a sample project with Devbox and Direnv:
```bash
mkdir node_project
cd node_project
devbox add nodejs
devbox generate direnv
That’s it! You have a working Devbox environment integrated with Direnv. Now any change done to your Devbox environment via `devbox.json` is applied by Direnv.
Now that we’ve setup the environment, let’s see how we can automate starting a sample NodeJS project:
```json
{
"devbox": {
"packages": [
"nodejs"
]
}
}
```bash
{
echo 'default.nix' > "with import <nixpkgs> {};\n[ http-server ]"
}
This sets up this project so that every time you `cd` into this project, all NodeJS dependencies get installed and `http-server` runs. To test this, `cd ..` and then `cd` back into your project directory and see how Direnv setups up your Devbox environment and runs `http-server`. Use `Ctrl+C` to stop `http-server`.

Next Steps

Devbox makes creating and working in isolated dev environments simple and easy. Direnv makes switching between multiple environments convenient. So combining the two provides a great experience for setting up and using your dev environments. We liked using Direnv so much that we started using it in our development of Devbox. In fact, we use both Devbox and Direnv to develop Devbox!
To see it in action, check out `devbox.json` and `envrc` files in our Github repo.

Stay up to Date with Jetify

If you're reading this, we'd love to hear from you about how you've been using Devbox for your projects. You can follow us on Twitter, or chat with our developers live on our Discord Server. We also welcome issues and pull requests on our Github Repo.
Sign up for more like this.
---

Remember these 3 key ideas for your startup:

  • Isolated Development Environments: Tools like Devbox and Direnv enable isolated and efficient environments, which help in managing different projects without conflict.

  • Automation Simplifies Workflows: Integrating Devbox with Direnv allows for an automated setup, saving time and reducing human errors during project environment setup.

  • Adapt the Best Tools for Productivity: Combining powerful tools can significantly enhance productivity. However, always look for integrated solutions that simplify your workflow.
    Edworking is the best and smartest decision for SMEs and startups to be more productive. Edworking is a FREE superapp of productivity that includes all you need for work powered by AI in the same superapp, connecting Task Management, Docs, Chat, Videocall, and File Management. Save money today by not paying for Slack, Trello, Dropbox, Zoom, and Notion.

Edworking integrates tools to help startups be productive and save money.
For more details, see the original source.

article cover
About the Author: Mark Howell Linkedin

Mark Howell is a talented content writer for Edworking's blog, consistently producing high-quality articles on a daily basis. As a Sales Representative, he brings a unique perspective to his writing, providing valuable insights and actionable advice for readers in the education industry. With a keen eye for detail and a passion for sharing knowledge, Mark is an indispensable member of the Edworking team. His expertise in task management ensures that he is always on top of his assignments and meets strict deadlines. Furthermore, Mark's skills in project management enable him to collaborate effectively with colleagues, contributing to the team's overall success and growth. As a reliable and diligent professional, Mark Howell continues to elevate Edworking's blog and brand with his well-researched and engaging content.

Trendy NewsSee All Articles
Try EdworkingA new way to work from  anywhere, for everyone for Free!
Sign up Now