#!/bin/bash # =============================================== # # devc.sh v2.1; job79, maurice # # Dev container entry script. # # =============================================== # set -euo pipefail log() { printf '\e[%sm%s\e[0m %s\n' "${3:-36}" "${2:-○}" "$1"; } die() { log "$1" 'x' 31 exit 1 } # default_args configures standard container options. default_args() { run_opts+=( "--name" "$name" "--hostname" "$name" "--pull=newer" # Update image. "--userns=keep-id" # Map host user. "--net=devc" # Shared network. "-v" "$name:/home/user:copy" # Persistent home volume. ) # Unix sockets require SELinux label disable. [[ -d /sys/fs/selinux ]] && run_opts+=("--security-opt" "label=disable") # Desktop integration (Wayland, SSH). [[ -e "/run/user/$UID/wayland-0" ]] && run_opts+=("-v" "/run/user/$UID/wayland-0:/run/user/1000/wayland-0") [[ -e "${SSH_AUTH_SOCK:-}" ]] && run_opts+=("-v" "$SSH_AUTH_SOCK:/run/user/1000/ssh-auth-sock") # Load custom container config. local config_file="$(dirname "$(realpath "$0")")/containers/$name/config.sh" [[ -f "$config_file" ]] && source "${config_file}" } # param_args parses CLI arguments into podman run options. param_args() { while (($# > 0)); do case "$1" in -gpu) run_opts+=("--device" "/dev/dri") ;; -host-spawn) run_opts+=("-v" "/run/user/$UID/bus:/tmp/bus" "-e" "HOST_HOME=$HOME") ;; -container-sock) run_opts+=("-v" "${XDG_RUNTIME_DIR:-/run/user/$UID}/podman/podman.sock:/var/run/docker.sock") ;; -x11) run_opts+=("-v" "/tmp/.X11-unix:/tmp/.X11-unix" "-v" "${XAUTHORITY:-$HOME/.Xauthority}:/run/user/1000/.Xauthority:ro") run_opts+=("-e" "DISPLAY=${DISPLAY:-:0}" "-e" "XAUTHORITY=/run/user/1000/.Xauthority") ;; -mnt) local type='' [[ ! -d "$2" ]] && type='file' run_opts+=("-w" "/mnt/" "-v" "$2:/mnt/$type") shift ;; *) run_opts+=("$1") ;; esac shift done } main() { local state_file="$HOME/.local/share/devc-previous-container" local image="${1:-}" # Resolve container name (CLI arg > Last used > Error). if [[ $image && $image != -* ]]; then shift [[ $image == *:* ]] || image+=":main" mkdir -p "${state_file%/*}" && echo "$image" >"$state_file" elif [[ -f $state_file ]]; then image=$(<$state_file) else die "no container name specified" fi local name="${image%:*}" # Start/Restart if not running or if arguments change configuration. if [[ -z "$(podman ps -q -f name="^$name$" -f status=running)" ]] || (($# > 0)); then log "starting $image..." [[ -n "${DEVC_REGISTRY:-}" ]] || die "registry unknown; set the DEVC_REGISTRY environment variable" default_args param_args "$@" podman network create --ignore "devc" &>/dev/null podman run --replace --stop-timeout 0 -td "${run_opts[@]}" "$DEVC_REGISTRY/$image" fi exec podman exec --detach-keys "ctrl-@,ctrl-@" -it "$name" ${DEVC_COMMAND:-bash -l} } main "$@"