iTranslated by AI
Trying Out Devbox
I want to try out the much-discussed Devbox
This article is quite the hot topic, isn't it?
The biggest feature of Devbox is that it allows you to set up isolated development environments on your local machine without using virtualization technologies like Docker containers.
Apparently, the fact that it works without overhead is quite impressive.
If that's all there is to it, it feels like Docker & DevContainer would suffice.
Installation for now
First, let's just install it. Here are the official docs. I'll be testing this on an M1 Mac.
It says this is how to install it, so I'll just run it as instructed (since I'm on Mac using zsh, I'll run it again later).
$ curl -fsSL https://get.jetpack.io/devbox | bash
It asks for the location to install the devbox command, so I'll go with Yes.
Devbox 📦 by jetpack.io
Instant and predictable development environments and containers.
This script downloads and installs the latest devbox binary.
Confirm Installation Details
Location: /usr/local/bin/devbox
Download URL: https://releases.jetpack.io/devbox
? Install devbox to /usr/local/bin (requires sudo)? [Y/n] Y
Downloading and Installing
✓ Downloading devbox binary... [DONE]
→ Installing in /usr/local/bin/devbox (requires sudo)...
✓ Installing in /usr/local/bin/devbox... [DONE]
✓ Successfully installed devbox 🚀
Next Steps
1. Learn how to use devbox
Run devbox help or read the docs at https://github.com/jetpack-io/devbox
2. Get help and give feedback
Join our community at https://discord.gg/jetpack-io
Running $ devbox triggers the download.
$ devbox
✓ Downloading version 0.2.0... [DONE]
✓ Verifying checksum... [DONE]
✓ Unpacking binary... [DONE]
Instant, easy, predictable development environments
Usage:
devbox [flags]
devbox [command]
Available Commands:
add Add a new package to your devbox
completion Generate the autocompletion script for the specified shell
generate
help Help about any command
info Display package info
init Initialize a directory as a devbox project
rm Remove a package from your devbox
run Starts a new devbox shell and runs the target script
services Interact with devbox services
shell Start a new shell or run a command with access to your packages
version Print version information
Flags:
-h, --help help for devbox
Use "devbox [command] --help" for more information about a command.
$ devbox version
0.2.0
Running it (Trying out the Readme)
Adding packages
Running $ devbox init creates a devbox.json file.
{
"packages": [],
"shell": {
"init_hook": null
},
"nixpkgs": {
"commit": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
}
}%
It seems you add various things to this packages array, and you can look them up here (apparently Devbox is a wrapper for Nix, which I'm not very familiar with).
For example, searching for python brings up python39, python311, and so on.
So, I tried $ devbox add python311, but it complained that Nix was missing.
The article I introduced at the beginning mentioned it wasn't necessary, so I might have missed something.
In Devbox 0.2.0 released this time, the installer has evolved, and Nix installation—which previously had to be done manually—is now performed automatically.
This means you only need the Devbox installer, making the setup easier.
$ devbox add python311
Nix is not installed. Devbox will attempt to install it.
Well, since I'm not sure, I'll just follow the documentation below to install Nix properly.
After that, running it again works fine.
$ devbox add python311
Installing nix packages. This may take a while... done.
python NOTES:
python on devbox works best when used with a virtual environment (vent, virtualenv, etc). For example with python3:
> python -m venv .venv
> source .venv/bin/activate
Package managers like poetry (https://python-poetry.org/) automatically create virtual environments for you.
To show this information, run `devbox info python311`
python311 (python3-3.11.0) is now installed.
It is properly added to devbox.json.
{
"packages": [
"python311"
],
"shell": {
"init_hook": null
},
"nixpkgs": {
"commit": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
}
}
Starting the shell
On my local Mac, the version is 3.9.1 (come to think of it, I hadn't updated it).
$ python --version
Python 3.9.1
When you run $ devbox shell, it sets everything up nicely while displaying Installing nix packages..
Checking the Python version again, it's correctly updated.
$ python --version
Python 3.11.0
(devbox)
By the way, you can return to the original shell with exit.
This covers the basics mentioned in the Readme.
Trying it out a bit more
Trying to edit devbox.json directly
First, after setting things up with devbox add go, I'll manually delete "python311" without using devbox rm, then run $ devbox shell.
"packages": [
"python311",
"go"
],
The result is as follows, and the local Python is correctly used.
$ python --version
Python 3.9.1
(devbox)
Feature to output a Dockerfile
As mentioned in the article at the beginning, it seems you can output a Dockerfile with devbox generate. Looking at the help, it seems you can also create devcontainer.json.
$ devbox help generate
Usage:
devbox generate [command]
Available Commands:
devcontainer Generate Dockerfile and devcontainer.json files under .devcontainer/ directory
dockerfile Generate a Dockerfile that replicates devbox shell
Flags:
-c, --config string path to directory containing a devbox.json config file
-h, --help help for generate
Use "devbox generate [command] --help" for more information about a command.
So, I ran $ devbox generate devcontainer. The .devcontainer/ directory was created, and the following two files were generated. It seems to be a format that allows you to reproduce the same environment by installing Devbox and copying devbox.json. It also includes extensions like the Python extension.
{
"name": "Devbox Remote Container",
"build": {
"dockerfile": "./Dockerfile",
"context": ".."
},
"customizations": {
"vscode": {
"settings": {
"python.defaultInterpreterPath": "/devbox/.devbox/nix/profile/default/bin/python3"
},
"extensions": [
"jetpack-io.devbox",
"ms-python.python"
]
}
},
"remoteUser": "devbox"
}
FROM alpine:3
# Setting up devbox user
ENV DEVBOX_USER=devbox
RUN adduser -h /home/$DEVBOX_USER -D -s /bin/bash $DEVBOX_USER
RUN addgroup sudo
RUN addgroup $DEVBOX_USER sudo
RUN echo " $DEVBOX_USER ALL=(ALL:ALL) NOPASSWD: ALL" >> /etc/sudoers
# installing dependencies
RUN apk add --no-cache bash binutils git libstdc++ xz sudo
USER $DEVBOX_USER
# installing devbox
RUN wget --quiet --output-document=/dev/stdout https://get.jetpack.io/devbox | bash -s -- -f
RUN chown -R "${DEVBOX_USER}:${DEVBOX_USER}" /usr/local/bin/devbox
# nix installer script
RUN wget --quiet --output-document=/dev/stdout https://nixos.org/nix/install | sh -s -- --no-daemon
RUN . ~/.nix-profile/etc/profile.d/nix.sh
# updating PATH
ENV PATH="/home/${DEVBOX_USER}/.nix-profile/bin:/home/${DEVBOX_USER}/.devbox/nix/profile/default/bin:${PATH}"
WORKDIR /code
COPY devbox.json devbox.json
RUN devbox shell -- echo "Installing packages"
ENTRYPOINT ["devbox"]
CMD ['shell']
While messing around with the "> <"-like icon in the bottom left of VSCode and selecting "Reopen in Container," I was indeed able to get inside the container, but I couldn't get it to work quite right, so I might have made a mistake. I'm not sure, so please let me know if anyone gets it working.
I tried it one more time and it worked. You can see that the Python added via add is usable like this. Of course, things that were available locally but not configured in devbox were naturally not usable (I tested with yarn). For some reason, files in the same layer as .devcontainer/ were also inside the container. Strange (or perhaps just my lack of Docker knowledge?).
(devbox) xxxx:/workspaces/xxxx$ python --version
Python 3.11.0
Other Tips
Testing if you can run devbox shell inside an active devbox shell
Naturally, you can't.
$ devbox shell
Error: You are already in an active devbox shell.
Run 'exit' before calling devbox shell again. Shell inception is not supported.
Recommended to install nodejs when running in a folder with package.json
$ devbox shell
Installing nix packages. This may take a while... done.
We detected extra packages you may need. To install them, run `devbox add nodejs yarn`
The "scripts" setting
It feels like a version of npm scripts that isn't limited to Node.js. As you might expect, it works similarly to the scripts in package.json (executed via devbox run xxxx).
{
"packages": ["python311"],
"shell": {
"init_hook": null,
"scripts": {
"echo_world": "echo \"World\""
}
},
"nixpkgs": {
"commit": "xxxxxxxxxxxxxxxxxxxxxx"
}
}
$ devbox run echo_world
World
Plugins and background execution
You can add things called "plugins" as described below (via the add command).
Currently, the following types are available:
Apache (apacheHttpd)
Nginx (nginx)
PostgreSQL (postgresql)
PHP (php, php80, php81, php82)
Ruby(ruby, ruby_3_1, ruby_3_0)
You can check the added plugins with $ devbox services ls (I tried adding postgresql as a test).
"packages": [
"python311",
"postgresql"
],
Python is not displayed.
$ devbox services ls
postgresql
It seems you can run them with $ devbox services start (you can also start them individually if there are multiple).
$ devbox shell$ initdb$ devbox services start
$ devbox services start
waiting for server to start.... done
server started
Service "postgresql" started
Discussion