distrobox icon indicating copy to clipboard operation
distrobox copied to clipboard

[Suggestion] - [Docs]: When inside a distrobox, a way to run host's vscode and attach to current distrobox and directory

Open ragibkl opened this issue 1 year ago • 5 comments

Is your feature request related to a problem? Please describe.

Before I use distrobox, I always cd into the project directory that I'm working on, and then run code .. That opens vscode and the current directory.

When I'm working in a distrobox, I would like it to do the same, but also automatically attach to the current distrobox I'm calling it from. I've followed the guides:

  • https://distrobox.it/posts/integrate_vscode_distrobox/
  • https://distrobox.it/posts/execute_commands_on_host/

With that, running code . from inside a distrobox only opens vs code on the current directory on the host, without attaching to the current container.

I've figured out how to do this. Inside the distrobox, I can add the following file: /usr/local/bin/code

#!/usr/bin/env bash

ID=$(echo -n ${CONTAINER_ID:-default} | od -An -t x1 | tr -d ' ' | cat) # hex dump of container name. vscode needs this
DIR=$(pwd)/$1 # naive way to tell vscode to open in current directory

distrobox-host-exec code --folder-uri vscode-remote://attached-container+${ID}/$DIR

With this, when I run code ., it opens vscode on the host install, attach to the current container, and opens current directory. The DIR definition can be made more smart and robust with further iteration.

Describe the solution you'd like

I would like feedback if this is a good workflow. I would like this to be added to the docs at https://distrobox.it/posts/integrate_vscode_distrobox/ I think this is useful for others who like me, also think that vscode should just live on the host and not the distrobox, which should be as lightweight as possible

Describe alternatives you've considered

A good alternative would be to just install vscode inside the container, and call that. I think this is a valid pattern, but also think that it's valuable to add this script to docs for other who want to have vscode installed only on the host.

Additional context Nope.

ragibkl avatar Jan 13 '24 03:01 ragibkl

Hello Ragib! Indeed I agree this will be a nice feature to have.

Just a question though, have you had any luck on attaching distrobox containers using a custom home directory? I use a custom home directory (/home/masum/Desktop/Dev-Enviornment) for dbxDebian (using debian:testing). It works fine on a normal terminal.

However when I try to attach it to VS code on my host system, it errors out.

I checked the logs and seems like VS code runs some commands inside the container during attachment process. But it uses the host system absolute paths in commands, which is why it fails. Ex: mkdir -p /home/masum/Desktop/Dev-Enviornment/.vscode-server/bin/8b3775030ed1a69b13e4f4c628c612102e30a681_1705866143834 Output: Permission denied

If the home directory is set to default (host's home directory) attaching distrobox containers works fine.

Any idea how to fix it?

JohnRTitor avatar Jan 21 '24 20:01 JohnRTitor

Hi @JohnRTitor , I am also using a custom home dir and do not face this issue. This is my home dir inside my distrobox:

$ echo $HOME
/home/ragib/.distrobox-homes/default

With this, attaching vscode to the distrobox work fine.

I checked the logs and seems like VS code runs some commands inside the container during attachment process. But it uses the host system absolute paths in commands, which is why it fails. Ex: mkdir -p /home/masum/Desktop/Dev-Enviornment/.vscode-server/bin/8b3775030ed1a69b13e4f4c628c612102e30a681_1705866143834 Output: Permission denied

If I understand your setup correctly, the home dir inside your distrobox is set to /home/masum/Desktop/Dev-Enviornment/ and this seems correct. I believe vs code is trying to install it's server component to .vscode-server/bin, and this seems like the correct behaviour.

From outside your distrobox, can you check if the home directory /home/masum/Desktop/Dev-Enviornment/ exists? I think distrobox won't automatically create it for you. If it doesn't exist, just create it manually yourself. Also check the dir permissions are what you might expect.

The other thing that you can try is instead of debian:testing, pick an image from here instead: https://github.com/toolbx-images/images Maybe that will give you better outcome.

I hope that works.

ragibkl avatar Jan 22 '24 02:01 ragibkl

Yes the home directory /home/masum/Desktop/Dev-Enviornment/ exists outside of Distrobox. I have some repositories cloned there.

I tried using an image from the link you mentioned (quay.io/toolbx-images/debian-toolbox:testing) with the above as home directory. But attaching to it does not work. Here's a snippet of VS code log. https://pastebin.com/zn6cVecV

I also did some further testing. Created "Test" folder inside host's home directory and used Test as the home directory of a a new distrobox container, dbxDebianV2 Attaching VS code to it works! I guess Desktop and its subfolders have unusual permissions that could be interfering.

Another thing, if I open the container dbxDebianV2 inside VS code. It starts the integrated shell as root. Is there a way to use a normal non privileged user by default? Starting dbxDebianV2 in a normal terminal, uses "masum", non-privileged user though.

JohnRTitor avatar Jan 23 '24 05:01 JohnRTitor

Attaching VS code to it works! I guess Desktop and its subfolders have unusual permissions that could be interfering.

Wow, that's a really interesting piece of info! I guess I got lucky that I went with a different home folder!

Another thing, if I open the container dbxDebianV2 inside VS code. It starts the integrated shell as root. Is there a way to use a normal non privileged user by default?

Did you follow the guides in here? https://distrobox.it/posts/integrate_vscode_distrobox/ I think the custom podman-host script there should have taken care of it.

ragibkl avatar Jan 23 '24 05:01 ragibkl

Thanks for the guide. I tried it just now but unfortunately it didn't work. I should add though, I removed flatpak-spawn --host, since I don't use VS code flatpak, the deb package can access podman just fine.

Before this, I set Docker path to podman, it works as I mentioned earlier, but run the container as root user.

Here's the log: https://pastebin.com/zHLmigyj

JohnRTitor avatar Jan 23 '24 10:01 JohnRTitor

I closed this. I don't think I've done enough work to justify this issue. @JohnRTitor , I think I haven't tested use case with flatpak vscode yet. I'll do more testing fisrt.

ragibkl avatar Feb 20 '24 03:02 ragibkl

Would be nice if you could test this on a Debian/Ubuntu system. I have no idea what's causing this. I use deb packaged distrobox (Ubuntu PPA) and deb packaged VS code.

JohnRTitor avatar Feb 20 '24 04:02 JohnRTitor