Last active
March 9, 2025 23:04
-
-
Save IgnisDa/f4c1ffb3b798686df3193a80cbd5b3eb to your computer and use it in GitHub Desktop.
Helix devcontainer
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
set -e | |
remove_flag="" | |
if [ "$1" = "true" ]; then | |
remove_flag="--remove-existing-container" | |
fi | |
# Generate certificate | |
path=tmp/devcontainers | |
mkdir -p $path && pushd $path | |
ssh_key=$(cat /dev/urandom | tr -dc '[:alpha:]' | fold -w ${1:-20} | head -n 1) | |
ssh-keygen -q -N '' -t rsa -f $ssh_key | |
popd | |
# Start container | |
devcontainer up $remove_flag \ | |
--mount "type=bind,source=$HOME/.config/helix,target=/home/archlinux/.config/helix" \ | |
--mount "type=bind,source=$HOME/.wakatime.cfg,target=/home/archlinux/.wakatime.cfg" \ | |
--workspace-folder . | |
script=" | |
# Copy generated keys | |
mkdir -p \$HOME/.ssh | |
cat \$PWD/$path/$ssh_key.pub > \$HOME/.ssh/authorized_keys | |
chmod 644 \$HOME/.ssh/authorized_keys | |
chmod 700 \$HOME/.ssh | |
" | |
# Add pub key to SSH allow list | |
devcontainer exec --workspace-folder . sh -c "$script" | |
devcontainer exec --workspace-folder . sudo /usr/local/share/ssh-init.sh | |
name=$(devcontainer read-configuration --workspace-folder . | jq -r '.configuration.name') | |
container_name="${name}_devcontainer-app-1" # you might also need to change this if `devcontainer-cli` creates a differently named container | |
ip_addr=$(docker inspect -f '{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}' $container_name) | |
PORT=${PORT:-2222} | |
# Create a `known_hosts` file that will be used by only this project | |
ssh-keyscan -t ssh-rsa -p $PORT $ip_addr >> tmp/known_hosts | |
# Connect directly via IP address instead of localhost | |
ssh -t -i $PWD/$path/$ssh_key \ | |
-o UserKnownHostsFile=tmp/known_hosts \ | |
-o NoHostAuthenticationForLocalhost=yes \ | |
-o UserKnownHostsFile=/dev/null \ | |
-o GlobalKnownHostsFile=/dev/null \ | |
-p $PORT "archlinux@$ip_addr" \ | |
"cd /workspaces/${PWD##*/}; fish --login" # you might need to change this according to the shell inside the image |
Am I understanding this correctly that I'd be using helix
from the container instead of locally?
If so, how's responsiveness (and a bit about your network performance metrics)?
It's been a long time since I last used this, but i don't remember having any problems about responsiveness. The connection is made over localhost and ssh is quite fast :)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The basic idea is that you should have a running ssh daemon inside your devcontainer to connect to. The links I have provided above lead to my specific devcontainer configuration that do that. Of course you're free to devise your own configuration.
Yes i have worked extensively with vscode devcontainers. Devcontainers in vscode work on almost the same premise, but instead of connecting to an ssh daemon, they connect to a vscode-server (which gets installed once the devcontainer is created the first time) and work via a client server model (AFAIK).