# shellcheck shell=bash # prepare the job container before the first step, as root set -eu # the engine gives the directories as one space-separated list read -ra dirs <<< "$TANGLED_DIRS" read -ra trees <<< "$TANGLED_TREES" mkdir -p "${dirs[@]}" # steps run as this uid, so make a user for it if the image has none # a group or user with this id from the image is used as it is grep -q "^[^:]*:[^:]*:$TANGLED_UID:" /etc/group || echo "tangled:x:$TANGLED_UID:" >> /etc/group grep -q "^[^:]*:[^:]*:$TANGLED_UID:" /etc/passwd || echo "tangled:x:$TANGLED_UID:$TANGLED_UID::$TANGLED_HOME:/bin/bash" >> /etc/passwd chown "$TANGLED_UID:$TANGLED_UID" "${dirs[@]}" # give the step user the trees that steps write to, such as /nix and the cache volume # a tree from an earlier run is already the user's, so only change it when its top is not for tree in "${trees[@]}"; do if [ -d "$tree" ] && [ "$(stat -c %u "$tree")" != "$TANGLED_UID" ]; then chown -R "$TANGLED_UID:$TANGLED_UID" "$tree" fi done # let the step user boot virtual machines when the machine gives the container kvm if [ -e /dev/kvm ]; then chmod 666 /dev/kvm fi # let the step user become root without a password, but only if the image has sudo if command -v sudo >/dev/null; then mkdir -p /etc/sudoers.d echo "#$TANGLED_UID ALL=(ALL) NOPASSWD: ALL" > /etc/sudoers.d/tangled chmod 440 /etc/sudoers.d/tangled fi