devbox icon indicating copy to clipboard operation
devbox copied to clipboard

Proposal: Add a devcontainer config for VS Code / Github Codespaces

Open pomdtr opened this issue 3 years ago • 2 comments

I did not find a way to effectively integrate devbox with vscode. As the broad majority of devs are using this editor these days, this is a concern for adoption in large teams.

You can start a devbox shell in the vscode terminal, but it does not set the env of all other vscode process (tasks, debugger, extensions...)

I feel like writing a devcontainer could be a great way to fix this (and free user for the required nix dependency), but I was not successful in implementing it.

pomdtr avatar Oct 07 '22 19:10 pomdtr

Hey @pomdtr, we'll look into this feature. Is this something you use to develop locally on your own machine as well? Or is it for remote development?

Lagoja avatar Oct 18 '22 21:10 Lagoja

I use this workflow both on my local machine and my remote workspaces. It allows me to get an uniform dev env across all my devices.

pomdtr avatar Oct 18 '22 21:10 pomdtr

@pomdtr We recently published the first version of devbox VSCode extension. We'll try to see if we can have the environment for the whole IDE (debugger & path to binaries and dependencies) be updated by the extension. Devcontainer seems like a possible solution as well. We'll update under this issue once we have a solution to move forward on this.

mohsenari avatar Oct 21 '22 20:10 mohsenari

on a local environment a workaround for the time being is to open your terminal (outside of IDE) go to devbox shell, then open your IDE from inside devbox shell.

cd my-devbox-project/
devbox shell
code .

mohsenari avatar Oct 21 '22 20:10 mohsenari

Cool ! Here is an interesting read on the devcontainer / nix integration: https://levelup.gitconnected.com/vs-code-remote-containers-with-nix-2a6f230d1e4e

pomdtr avatar Oct 24 '22 10:10 pomdtr

I've got a draft version working for devbox. Please take a look at it: https://github.com/pomdtr/devbox-devcontainer

image

pomdtr avatar Oct 24 '22 13:10 pomdtr

@pomdtr Thanks for sharing this. I don't have codespaces enabled on my github account but I tested it locally with Docker desktop and Dev Containers extension on VSCode and it woks great. Although, it runs vscode from inside a Docker container with a shared volume between the host machine and Docker. That means in a non-Linux OS there might be a performance hit.

That said, I really liked the Dev Containers extension when it detects a .devcontainer it prompts with "Reopen this in devcontainers". Maybe we can add a similar functionality to Devbox's extension. Because if we open vscode from inside a devbox shell, the language binaries and interpreter settings in vscode will be set to devbox's shell environment.

mohsenari avatar Oct 24 '22 22:10 mohsenari

I think the performance hit should be quite minimal since the bind mount only contains the workspace folder. There are way to mitigate the performance penalty (see : https://code.visualstudio.com/remote/advancedcontainers/improve-performance)

pomdtr avatar Oct 24 '22 23:10 pomdtr

Ah interesting! @pomdtr I've shared your example on devcontainer setup with Devbox with the team. Are you ok if we add it to our docs/guides?

mohsenari avatar Oct 25 '22 21:10 mohsenari

Go ahead !

pomdtr avatar Oct 25 '22 21:10 pomdtr

@mohsenari I have made some improvements on the template. The dependencies are now populated during the build, and the .devbox dir is no longer in the workspace directory (since the symlinks only work in the container, it should not be synced with the host).

The only issue is that the devbox cli is no longer working in the container. It would be cool to add support for setting the .devbox dir path via config.

Ex:

devbox --config .devcontainer/devbox.json --cache /devbox/.devbox

Ideally I would also like to be able to set these options using environment variable since it would allow me set them in the dockerfile:

ENV DEVBOX_CONFIG=.decontainer/.devbox.json
ENV DEVBOX_CACHE=/devbox/.devbox

Then the user would be able to use devbox add python310 inside the dev container. Currently I need to do this:

devbox --config /devbox/devbox.json add python310 && copy /devbox/devbox.json .devcontainer/devbox.json

Also a devbox install command would be a better alternative to devbox shell --config /devbox/devbox.json -- echo "Devbox Store Populated".

pomdtr avatar Oct 26 '22 05:10 pomdtr

Thanks a ton for improving the templates! to summarize your suggestions are:

  • "add support for setting the .devbox/ dir path via config"
  • setting --config and --cache flags using env variables
  • adding a devbox install command as alternative to devbox shell --config /devbox/devbox.json -- echo "Devbox Store Populated"

I'll make separate issues for them to track each independently.

mohsenari avatar Oct 26 '22 18:10 mohsenari

Thanks a lot ! If you have some time, it would loke to get some feedback on the dockerfile. I tried to use the buildkit cache feature to reduce the build time on successive builds, but I was not successful.

pomdtr avatar Oct 26 '22 21:10 pomdtr

Also, is it ok to move the .devbox file out of the workspace dir ? It looks like a specific version of the nixpkgs channel is pinned in it.

pomdtr avatar Oct 27 '22 10:10 pomdtr

@pomdtr I looked at the Dockerfile, there is not much feedback I have on making it faster. Nix installation takes some time and also first time running devbox shell after a new installation also takes time due to Nix copying stuff to /nix/store. So One idea is to have everything before calling devbox shell and publish it as a public Docker image. That way Nix and devbox get cached as a layer and subsequent runs won't install nix and devbox. Other than that, the Dockerfile looks ok imo.

For moving .devbox/ out of the workspace dir, I don't think it is supported as of now. But if there is a reason other than preference for it, we can prioritize implementing it similar to --config for custom devbox.json path.

mohsenari avatar Oct 28 '22 21:10 mohsenari

@pomdtr we have added a feature to the VSCode extension that can generate a .devcontainer.json file and Dockerfile that works with your Devbox project. If you have a chance to try it out, we'd love your feedback on how it works! Closing this comment for now, but can reopen if there are any issues with the Extension

Lagoja avatar Dec 01 '22 05:12 Lagoja