#!/bin/bash -i LOCATION="${LOCATION:=$(pwd)}" IMAGE=ghcr.io/hauxir/devenv:latest # The container runs as this unprivileged user (see Dockerfile), so that Claude # can run with --dangerously-skip-permissions (refused as root) and files it # writes into the bind mounts stay owned by the host user. The uid must match # the host user's; rebuild with --build-arg UID=... if it doesn't. DEVENV_USER=hauxir DEVENV_UID=1001 DEVENV_HOME="/home/$DEVENV_USER" DEVENV_WORK="$DEVENV_HOME/work" if [ "$(id -u)" != "$DEVENV_UID" ]; then echo "warning: you are uid $(id -u) but the devenv user is uid $DEVENV_UID; files written from the container won't be owned by you (rebuild the image with --build-arg UID=$(id -u))." >&2 fi # Exact-name match: a bare name=devenv also matches devenv-foo. ACTIVE_CONTAINER_ID=$(docker ps -aqf 'name=^devenv$') docker pull "$IMAGE" # Cap devenv so it can never starve the host (sshd/dockerd live outside it). # Reserve exactly one core for the host: pin devenv to all-but-the-last core # via cpuset, and set --cpus to match so it doesn't double-reserve a second. # Scales to whatever core count this machine has. # Ask the Docker daemon (not nproc) for the core count: it reports the CPUs # Docker can actually hand out, and since dockerd runs on the host, the answer # doesn't shrink when this script is run from inside a cpuset-limited devenv. TOTAL_CPUS=$(docker info --format '{{.NCPU}}') DEVENV_CPUSET="0-$(( TOTAL_CPUS - 2 > 0 ? TOTAL_CPUS - 2 : 0 ))" DEVENV_CPUS=$(( TOTAL_CPUS - 1 > 1 ? TOTAL_CPUS - 1 : 1 )) # Supplementary groups for the non-root user. The docker socket and the DRM # render nodes are group-owned (docker, render) with gids that differ per host, # so the container's /etc/group can't know them; pass the host's numeric gids. GROUP_ARGS=() add_group_of() { local gid gid=$(stat -c %g "$1" 2>/dev/null) || return case " ${GROUP_ARGS[*]} " in *" $gid "*) return ;; esac GROUP_ARGS+=(--group-add "$gid") } add_group_of /var/run/docker.sock # GPU passthrough, only when the host has a working NVIDIA driver. # The devices are also passed explicitly with --device: Docker remembers those, # so they survive `docker update` (which otherwise drops the device access that # --gpus grants and leaves nvidia-smi failing with "Failed to initialize NVML"). GPU_ARGS=() if nvidia-smi >/dev/null 2>&1; then # Make sure /dev/nvidia-uvm exists; it's only created on first CUDA use. nvidia-modprobe -u -c=0 >/dev/null 2>&1 || true # CDI (Container Device Interface) rather than --gpus: Docker 29 fails # `--gpus all` with "AMD CDI spec not found" unless the nvidia runtime is # configured in daemon.json, while the CDI device works with the stock # runtime. The spec is /etc/cdi/nvidia.yaml, generated by # `nvidia-ctk cdi generate` and kept fresh by nvidia-cdi-refresh.service. GPU_ARGS=(--device nvidia.com/gpu=all) # The CDI spec injects the graphics libraries too (libEGL_nvidia, # libGLX_nvidia, the Vulkan ICD), which headless Chromium needs to render on # the GPU instead of SwiftShader (e.g. the streetsim screenshots and smoke # checks). The image provides the loaders (libegl1, libgl1, libvulkan1); the # spec drops in the driver's libraries at the host's version, so nothing is # apt-installed inside. NVIDIA_DRIVER_CAPABILITIES is kept for the legacy # hook path in case --gpus is ever used again; CDI ignores it. GPU_ARGS+=(-e NVIDIA_DRIVER_CAPABILITIES=all) # /dev/dri (the DRM render node, there when nvidia-drm is loaded) lets Chromium # share a WebGPU canvas with its compositor via dma-buf; without it the page # renders but the canvas stays blank in screenshots (headless Chromium, 2026-10-21). # The /dev/nvidia* nodes are world-rw; /dev/dri/* are group-owned (video, # render), so the user needs those gids too. for d in /dev/nvidia0 /dev/nvidiactl /dev/nvidia-uvm /dev/nvidia-uvm-tools /dev/nvidia-modeset /dev/dri; do [ -e "$d" ] && GPU_ARGS+=(--device "$d") done for d in /dev/dri/*; do [ -c "$d" ] && add_group_of "$d" done fi SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" # The container mounts $LOCATION at $DEVENV_WORK. Tools inside the container # (e.g. docker compose via kh) pass $DEVENV_WORK/… paths to the Docker daemon, # which resolves them on the HOST filesystem. A symlink on the host lets the # daemon follow $DEVENV_WORK → $LOCATION so nested bind-mounts work. # Skipped when $DEVENV_WORK is a real directory, i.e. when this script runs # inside the container (where it is the mount itself); linking there would # only drop a work → work loop into the mounted tree. if [ -L "$DEVENV_WORK" ] || [ ! -e "$DEVENV_WORK" ]; then if [ "$(readlink "$DEVENV_WORK" 2>/dev/null)" != "$LOCATION" ]; then { mkdir -p "$DEVENV_HOME" && ln -sfn "$LOCATION" "$DEVENV_WORK"; } 2>/dev/null \ || { sudo mkdir -p "$DEVENV_HOME" && sudo ln -sfn "$LOCATION" "$DEVENV_WORK"; } 2>/dev/null \ || true fi fi mkdir -p "$HOME/.local/share/fish/" "$HOME/.config" touch "$HOME/.local/share/fish/fish_history" touch "$HOME/.config/.env" # Claude's account/onboarding state lives in ~/.claude.json (not ~/.claude), so # it's bind-mounted too; without it every recreated container needs a fresh # login. Must exist as a non-empty file, or Docker makes a directory of it. [ -s "$HOME/.claude.json" ] || echo '{}' > "$HOME/.claude.json" # Deploy tracked Claude global config to the host before it's bind-mounted in. # Repo is the source of truth; runtime edits to ~/.claude/settings.json are # overwritten on each launch. mkdir -p "$HOME/.claude" if [ -f "$SCRIPT_DIR/.claude/settings.json" ]; then cp "$SCRIPT_DIR/.claude/settings.json" "$HOME/.claude/settings.json" else echo "warning: $SCRIPT_DIR/.claude/settings.json not found; run this script from a clone of the repo. Keeping existing ~/.claude/settings.json." >&2 fi # Deploy tracked tmux helper scripts (branch.sh, stat.sh) to the host too. # ~/.config is bind-mounted over the container's ~/.config, which hides the # copies baked into the image; without them the window tabs and CPU/mem status # render empty. mkdir -p "$HOME/.config/tmux" if ls "$SCRIPT_DIR"/.config/tmux/*.sh >/dev/null 2>&1; then cp "$SCRIPT_DIR"/.config/tmux/*.sh "$HOME/.config/tmux/" else echo "warning: $SCRIPT_DIR/.config/tmux/*.sh not found; tmux tab names and status will be empty." >&2 fi # Bind mounts shared by the one-time ownership fix and the container itself. MOUNTS=( "$HOME/.local/share/fish/fish_history:$DEVENV_HOME/.local/share/fish/fish_history" "$HOME/.ssh:$DEVENV_HOME/.ssh" "$HOME/.aws:$DEVENV_HOME/.aws" "$HOME/.config:$DEVENV_HOME/.config" "$HOME/.claude:$DEVENV_HOME/.claude" "$HOME/.claude.json:$DEVENV_HOME/.claude.json" "$LOCATION:$DEVENV_WORK/" ) MOUNT_ARGS=() for m in "${MOUNTS[@]}"; do MOUNT_ARGS+=(-v "$m"); done if [ -z "$ACTIVE_CONTAINER_ID" ] then # Everything the old root-running devenv wrote into the bind mounts is owned by # root, which the non-root user can't modify. Hand root-owned files there to # $DEVENV_UID once, before the first start. Done through the Docker daemon # (root) so it needs no sudo; files owned by anyone but root are left alone. echo "devenv: chowning root-owned files under the bind mounts to uid $DEVENV_UID (one-time, can take a minute)..." docker run --rm --user root --entrypoint chown "${MOUNT_ARGS[@]}" "$IMAGE" \ -Rh --from=0 "$DEVENV_UID:$DEVENV_UID" "$DEVENV_HOME" ACTIVE_CONTAINER_ID=$( docker run \ --platform linux/amd64 \ --cpus="$DEVENV_CPUS" \ --cpuset-cpus="$DEVENV_CPUSET" \ "${GPU_ARGS[@]}" \ "${GROUP_ARGS[@]}" \ "${MOUNT_ARGS[@]}" \ -v /var/run/docker.sock:/var/run/docker.sock \ --network host \ --name devenv \ -d \ -it \ "$IMAGE" \ fish -lc 'tmux -u new-session -d -s work; exec tail -f /dev/null' ) fi # Stop here instead of cascading into confusing start/update/exec errors. if [ -z "$ACTIVE_CONTAINER_ID" ]; then echo "error: failed to create the devenv container (see the docker error above)" >&2 exit 1 fi docker start "$ACTIVE_CONTAINER_ID" >/dev/null # An existing container keeps running its old image; `docker pull` above alone # changes nothing. Say so instead of silently attaching to a stale image. if [ "$(docker inspect -f '{{.Image}}' "$ACTIVE_CONTAINER_ID")" != "$(docker image inspect -f '{{.Id}}' "$IMAGE")" ]; then echo "note: a newer devenv image was pulled; run 'docker rm -f devenv' and rerun this script to recreate the container from it." >&2 fi # Keep an existing container's limits in sync with the detected core count, # but only call `docker update` when something actually changed, so it doesn't # touch the container's device access on every launch. CURRENT_LIMITS=$(docker inspect -f '{{.HostConfig.NanoCpus}} {{.HostConfig.CpusetCpus}}' "$ACTIVE_CONTAINER_ID") WANTED_LIMITS="$(( DEVENV_CPUS * 1000000000 )) $DEVENV_CPUSET" if [ "$CURRENT_LIMITS" != "$WANTED_LIMITS" ]; then docker update --cpus="$DEVENV_CPUS" --cpuset-cpus="$DEVENV_CPUSET" "$ACTIVE_CONTAINER_ID" >/dev/null fi # attach -d detaches any other client from the session first, so repeated # `devenv.sh` runs (or terminals that died without detaching) can't pile up as # stale clients — at most one live client per session. docker exec -it "$ACTIVE_CONTAINER_ID" tmux attach-session -d || docker exec -it "$ACTIVE_CONTAINER_ID" tmux -u new-session