Skip to content

Instantly share code, notes, and snippets.

@IgnisDa
Last active May 24, 2024 10:35
Show Gist options
  • Save IgnisDa/f4c1ffb3b798686df3193a80cbd5b3eb to your computer and use it in GitHub Desktop.
Save IgnisDa/f4c1ffb3b798686df3193a80cbd5b3eb to your computer and use it in GitHub Desktop.
Helix devcontainer
#!/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
@legout
Copy link

legout commented Mar 19, 2023

This is interesting. So, from an existing project to get helix inside a devcontainer running, I have to do the followig:

  1. Create a .devcontainer folder with at least a devcontainer.json inside
  2. Run the script above

Did you work with devcontainer in vscode too? How is this different from this approach?

Thanks!

@IgnisDa
Copy link
Author

IgnisDa commented Mar 19, 2023

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).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment