Files
devcontainer/devc.sh
maurice baad26211e Fix :)
2025-10-31 12:31:04 +01:00

125 lines
3.9 KiB
Bash
Executable File

#!/bin/bash
# =============================================== #
# devc.sh v2.0; job79, maurice #
# Dev container enter script. Handles setting up #
# different dev containers, resuming sessions and #
# automatic container updates. #
# =============================================== #
set -eu
log() { printf '\e[%sm%s\e[0m %s\n' "${3:-36}" "${2:-}" "$1"; }
arg() { echo -n " $@"; }
# run_args returns the podman run arguments required for
# starting a new container.
default_args() {
arg "--name $name"
arg "--hostname $name"
# Pull newer container image if available.
arg "--pull=newer"
# Use keep-id so the container user matches the host user.
arg "--userns=keep-id"
# Disable selinux labeling so unix sockets can be mounted
# without problems.
arg "--security-opt label=disable"
# Mount the wayland socket. Required to get the system
# clipboard (wl-copy) and gui applications working.
[ -e "/run/user/$UID/wayland-0" ] && arg "-v /run/user/$UID/wayland-0:/run/user/1000/wayland-0"
# Mount the ssh socket to get ssh working.
[ -e "$SSH_AUTH_SOCK" ] && arg "-v $SSH_AUTH_SOCK:/run/user/1000/ssh-auth-sock"
# Make the user home dir a volume so it survives container
# restarts. Use copy to keep the files from the image.
arg "-v $name:/home/user:copy"
# If there is custom configuration for the container, load
# it here.
config_file="$(dirname "$(realpath "$0")")/containers/$name/config.sh"
[ -f "$config_file" ] && source "${config_file}"
}
# param_args returns the podman run arguments based on the
# arguments provided to this script.
param_args() {
while test $# -gt 0; do
case "$1" in
-gpu) # Enable gpu acceleration.
arg "--device /dev/dri" ;;
-kvm) # Enable KVM
arg "--device /dev/kvm" ;;
-usb) # Enable USB access
arg "--device /dev/bus/usb" ;;
-host-spawn) # Enable spawning host commands from inside the container using host-spawn.
arg "-v /run/user/$UID/bus:/tmp/bus"
arg "-e HOST_HOME=$HOME" # Used to translate paths.
;;
-net) # Enable network dev-<container name>
arg "--network dev-$name"
;;
-mnt) # Mount directory.
shift
arg "-w /workdir/"
arg "-v $1:/workdir/$([ ! -d "$1" ] && echo $1)"
;;
-mdf) # Mount dev folder
arg "-v $HOME/dev:/home/user/dev"
;;
-x11) # Enable X11 support.
arg "-v /tmp/.X11-unix:/tmp/.X11-unix"
arg "-v $XAUTHORITY:/run/user/1000/.Xauthority:ro"
arg "-e DISPLAY=$DISPLAY"
arg "-e XAUTHORITY=/run/user/1000/.Xauthority"
;;
*) # Use unknown arguments a podman arguments.
arg "$1" ;;
esac
shift
done
}
### MAIN ###
# Get the devcontainer name from the first argument. If not
# provided, use the last used name when possible.
if [[ $# -gt 0 ]] && [[ ${1:-} != -* ]]; then
image="$1"
[[ "$image" != *:* ]] && image="$image:main"
echo "$image" >"$HOME/.local/share/devc-previous-container"
shift
elif [ -f "$HOME/.local/share/devc-previous-container" ]; then
image=$(<"$HOME/.local/share/devc-previous-container")
else
log "no container name specified" 'x' 31
exit 1
fi
name="${image%:*}"
# Get container registry from the DEVC_REGISTRY env
# variable.
if [ -n "${DEVC_REGISTRY:-}" ]; then
registry="$DEVC_REGISTRY"
else
log "registry unknown; set the DEVC_REGISTRY environment variable" 'x' 31
exit 1
fi
# Get container command from the DEVC_COMMAND env variable
# if set, else use bash -l.
if [ -z "${DEVC_COMMAND:-}" ]; then
DEVC_COMMAND="bash -l"
fi
# When container is not running or arguments are provided,
# recreate it.
if [ "$(podman container inspect "$name" -f {{.State.Running}} 2>&1)" != 'true' ] || [[ $# -gt 0 ]]; then
log "starting devcontainer..."
podman network create --ignore "dev-$name"
podman container rm -f -t 0 "$name" 1>/dev/null
podman run -td $(default_args) $(param_args $@) "$registry/$image"
fi
podman exec --detach-keys "ctrl-@" -it "$name" ${DEVC_COMMAND:-}