|
| 1 | +#!/usr/bin/env bash |
| 2 | +set -euo pipefail |
| 3 | + |
| 4 | +# Remap the dev user's UID/GID to match the workspace owner so that |
| 5 | +# bind-mounted files are accessible without running as root. |
| 6 | +# |
| 7 | +# Standard Docker: Workspace is owned by the host user's UID (e.g. 1000). |
| 8 | +# We remap dev to that UID — fast and seamless. |
| 9 | +# |
| 10 | +# Rootless Docker: Workspace appears as root-owned (UID 0) inside the |
| 11 | +# container due to user-namespace mapping. We can't remap |
| 12 | +# dev to UID 0 (that's root), so we grant access with |
| 13 | +# POSIX ACLs instead. |
| 14 | + |
| 15 | +WORKSPACE=/workspace |
| 16 | +TARGET_USER=dev |
| 17 | + |
| 18 | +WS_UID=$(stat -c '%u' "$WORKSPACE") |
| 19 | +WS_GID=$(stat -c '%g' "$WORKSPACE") |
| 20 | +DEV_UID=$(id -u "$TARGET_USER") |
| 21 | +DEV_GID=$(id -g "$TARGET_USER") |
| 22 | + |
| 23 | +DEV_HOME=/home/"$TARGET_USER" |
| 24 | + |
| 25 | +# Ensure directories that tools expect exist under ~dev. |
| 26 | +# ~/.local is a named Docker volume — ensure subdirs exist and are owned by dev. |
| 27 | +mkdir -p "$DEV_HOME"/.local/state "$DEV_HOME"/.local/share |
| 28 | +chown -R "$TARGET_USER":"$TARGET_USER" "$DEV_HOME"/.local |
| 29 | + |
| 30 | +# Copy host configs mounted as *.host into their real locations. |
| 31 | +# This gives the dev user owned copies without touching host originals. |
| 32 | +if [ -d "$DEV_HOME/.ssh.host" ]; then |
| 33 | + cp -a "$DEV_HOME/.ssh.host" "$DEV_HOME/.ssh" |
| 34 | + chmod 700 "$DEV_HOME/.ssh" |
| 35 | + chmod 600 "$DEV_HOME"/.ssh/id_* 2>/dev/null || true |
| 36 | + chown -R "$TARGET_USER":"$TARGET_USER" "$DEV_HOME/.ssh" |
| 37 | +fi |
| 38 | +if [ -d "$DEV_HOME/.config/nvim.host" ]; then |
| 39 | + mkdir -p "$DEV_HOME/.config" |
| 40 | + cp -a "$DEV_HOME/.config/nvim.host" "$DEV_HOME/.config/nvim" |
| 41 | + chown -R "$TARGET_USER":"$TARGET_USER" "$DEV_HOME/.config/nvim" |
| 42 | +fi |
| 43 | + |
| 44 | +# Already matching — nothing to do. |
| 45 | +if [ "$WS_UID" = "$DEV_UID" ] && [ "$WS_GID" = "$DEV_GID" ]; then |
| 46 | + exit 0 |
| 47 | +fi |
| 48 | + |
| 49 | +if [ "$WS_UID" != "0" ]; then |
| 50 | + # ── Standard Docker ────────────────────────────────────────────── |
| 51 | + # Workspace is owned by a non-root UID (the host user). |
| 52 | + # Remap dev's UID/GID to match. |
| 53 | + if [ "$DEV_GID" != "$WS_GID" ]; then |
| 54 | + if ! groupmod -g "$WS_GID" "$TARGET_USER" 2>&1; then |
| 55 | + echo "warning: failed to remap $TARGET_USER GID to $WS_GID" >&2 |
| 56 | + fi |
| 57 | + fi |
| 58 | + if [ "$DEV_UID" != "$WS_UID" ]; then |
| 59 | + if ! usermod -u "$WS_UID" -g "$WS_GID" "$TARGET_USER" 2>&1; then |
| 60 | + echo "warning: failed to remap $TARGET_USER UID to $WS_UID" >&2 |
| 61 | + fi |
| 62 | + fi |
| 63 | + if ! chown -R "$TARGET_USER":"$TARGET_USER" /home/"$TARGET_USER" 2>&1; then |
| 64 | + echo "warning: failed to chown /home/$TARGET_USER" >&2 |
| 65 | + fi |
| 66 | +else |
| 67 | + # ── Rootless Docker ────────────────────────────────────────────── |
| 68 | + # Workspace is root-owned inside the container. Grant dev access |
| 69 | + # via POSIX ACLs (preserves ownership, works across the namespace |
| 70 | + # boundary). |
| 71 | + if command -v setfacl &>/dev/null; then |
| 72 | + setfacl -Rm "u:${TARGET_USER}:rwX" "$WORKSPACE" |
| 73 | + setfacl -Rdm "u:${TARGET_USER}:rwX" "$WORKSPACE" # default ACL for new files |
| 74 | + |
| 75 | + # Git refuses to operate in repos owned by a different UID. |
| 76 | + # Host gitconfig is mounted readonly as ~/.gitconfig.host. |
| 77 | + # Create a real ~/.gitconfig that includes it plus container overrides. |
| 78 | + printf '[include]\n\tpath = %s/.gitconfig.host\n[safe]\n\tdirectory = %s\n' \ |
| 79 | + "$DEV_HOME" "$WORKSPACE" > "$DEV_HOME/.gitconfig" |
| 80 | + chown "$TARGET_USER":"$TARGET_USER" "$DEV_HOME/.gitconfig" |
| 81 | + |
| 82 | + # If this is a worktree, the main .git dir is bind-mounted at its |
| 83 | + # host absolute path. Grant dev access so git operations work. |
| 84 | + GIT_COMMON_DIR=$(git -C "$WORKSPACE" rev-parse --git-common-dir 2>/dev/null || true) |
| 85 | + if [ -n "$GIT_COMMON_DIR" ] && [ "$GIT_COMMON_DIR" != "$WORKSPACE/.git" ]; then |
| 86 | + [ ! -d "$GIT_COMMON_DIR" ] && GIT_COMMON_DIR="$WORKSPACE/$GIT_COMMON_DIR" |
| 87 | + if [ -d "$GIT_COMMON_DIR" ]; then |
| 88 | + setfacl -Rm "u:${TARGET_USER}:rwX" "$GIT_COMMON_DIR" |
| 89 | + setfacl -Rdm "u:${TARGET_USER}:rwX" "$GIT_COMMON_DIR" |
| 90 | + git config -f "$DEV_HOME/.gitconfig" --add safe.directory "$(dirname "$GIT_COMMON_DIR")" |
| 91 | + fi |
| 92 | + fi |
| 93 | + |
| 94 | + # Also fix bind-mounted dirs under ~dev that appear root-owned. |
| 95 | + for dir in /home/"$TARGET_USER"/.claude; do |
| 96 | + [ -d "$dir" ] && setfacl -Rm "u:${TARGET_USER}:rwX" "$dir" && setfacl -Rdm "u:${TARGET_USER}:rwX" "$dir" |
| 97 | + done |
| 98 | + [ -f /home/"$TARGET_USER"/.claude.json ] && \ |
| 99 | + setfacl -m "u:${TARGET_USER}:rw" /home/"$TARGET_USER"/.claude.json |
| 100 | + else |
| 101 | + echo "warning: setfacl not found; dev user may not have write access to workspace" >&2 |
| 102 | + echo " install the 'acl' package or set remoteUser to root" >&2 |
| 103 | + fi |
| 104 | +fi |
0 commit comments