FROM docker.io/library/ubuntu:26.04

# Cleanup scripts on a host often prune unused images (`podman image prune
# -a`, `podman system prune -a`). Between sessions no container references
# this image, so the tag is not protection. The label lets such a script
# exclude umwelt's images: podman image prune -af --filter
# label!=io.umwelt.managed  (inherited by a machine-local overlay build).
LABEL io.umwelt.managed="true"

# Remove default user
RUN userdel -r ubuntu

# The image bakes in the host user's identity, supplied by build.sh as build
# args: the stock /etc/passwd plus the user's line, and USER, so the container
# (entrypoint and execs) runs as the same user the host sees. No defaults:
# a build without an identity fails instead of silently defaulting to root.
ARG USERNAME
ARG USER_UID
ARG USER_GID
ARG USER_HOME
ARG GO_VERSION=1.27.0
ARG NODE_VERSION=24.21.0
ARG UV_VERSION=0.12.19
ARG RUST_VERSION=1.99.0
ARG FJ_VERSION=0.6.0

# The prebuilt toolchains below are fetched as architecture-specific tarballs
# (the sandbox has no network at runtime, so nothing is downloaded at run
# time). TARGETARCH is the build platform's architecture, set automatically by
# podman/buildx to `amd64` or `arm64` - the only architectures these upstreams
# ship here. Each project names its artifacts differently (linux-amd64 vs
# linux-arm64, linux-x64 vs linux-arm64, x86_64 vs aarch64), so every download
# maps TARGETARCH with a `case` and fails loudly on anything else rather than
# silently fetching a wrong-architecture tarball that only breaks at runtime.
ARG TARGETARCH

# python3-venv + python3-pytest: a working offline package channel (PEP 668
# otherwise locks the system interpreter, and runtime has no network).
# librsvg2-bin (rsvg-convert) rasterizes SVG -> PNG so an agent can visually
# verify its own output; the DejaVu fonts are named explicitly so text
# rendering does not rely on transitive apt deps.
# cmake + ninja-build: C/C++ project builds (e.g. llama.cpp) that need a
# build system beyond make.
# ripgrep: the `rg` the workspace AGENTS.md tells agents to search with.
# opencode ships its own copy host-side (~/.cache/opencode/bin/rg), which
# is not on the sandbox PATH; this makes the prescribed command work.
# sqlite3 (agents inspect databases), time (the `time` keyword - dash has
# none), default-jdk-headless (java/jar builds and inspection).
# flex + bison (lex/yacc parsers) and the GNU autotools (autoconf, automake,
# libtool, m4) plus pkg-config, so an agent can run `autoreconf -fi &&
# ./configure && make` on a source checkout; autoconf-archive adds the common
# AX_* macros, gettext + autopoint cover AM_GNU_GETTEXT/NLS projects, and
# gperf generates perfect hash functions. The image already carries gcc/g++
# and make, so this completes the lex/yacc/autotools build toolchain.
#
# rpm (rpmbuild, rpmspec, rpm2cpio and the query CLI) + rpmlint: build and
# lint .rpm packages. desktop-file-utils (desktop-file-validate) backs the
# package checks in both the rpm and deb test scripts; lintian makes the
# .deb checks more thorough (the deb test skips it when absent).
RUN apt-get update && apt-get install -y \
    bash coreutils grep findutils ripgrep \
    jq python3 python3-pip python3-venv python3-pytest \
    git curl wget \
    file librsvg2-bin \
    cmake ninja-build \
    fonts-dejavu-core fonts-dejavu-mono \
    unzip \
    sqlite3 time default-jdk-headless \
    flex bison autoconf automake libtool pkg-config gperf m4 \
    autoconf-archive gettext autopoint \
    rpm rpmlint desktop-file-utils lintian \
    && rm -rf /var/lib/apt/lists/*
RUN case "$TARGETARCH" in \
        amd64) GOARCH=amd64 ;; \
        arm64) GOARCH=arm64 ;; \
        *) echo "unsupported TARGETARCH: $TARGETARCH" >&2; exit 1 ;; \
    esac \
    && curl -fsSL "https://go.dev/dl/go${GO_VERSION}.linux-${GOARCH}.tar.gz" -o /tmp/go.tgz \
    && tar -C /usr/local -xzf /tmp/go.tgz && rm /tmp/go.tgz \
    && ln -sf /usr/local/go/bin/go   /usr/local/bin/go \
    && ln -sf /usr/local/go/bin/gofmt /usr/local/bin/gofmt

# Node.js pinned by version like Go: the sandbox has no network at runtime,
# and the apt node is too old (18 on 24.04, 22 on 26.04) for the modern TS
# tooling the agent drives. Stripped into /usr/local so node, npm, npx and
# corepack land in /usr/local/bin; the global tsx makes `npx tsx` resolve
# locally, no registry round-trip at runtime.
RUN case "$TARGETARCH" in \
        amd64) NODEARCH=x64 ;; \
        arm64) NODEARCH=arm64 ;; \
        *) echo "unsupported TARGETARCH: $TARGETARCH" >&2; exit 1 ;; \
    esac \
    && curl -fsSL "https://nodejs.org/dist/v${NODE_VERSION}/node-v${NODE_VERSION}-linux-${NODEARCH}.tar.xz" \
    -o /tmp/node.tgz \
    && tar -C /usr/local --strip-components=1 -xJf /tmp/node.tgz && rm /tmp/node.tgz \
    && npm install -g tsx@4.23.15

# uv pinned by version like Go and Node: a fast Python package and
# environment manager for the in-sandbox venv workflow (uv venv, uv pip).
# Extracted into /usr/local/uv; uv and uvx are symlinked into
# /usr/local/bin.
RUN case "$TARGETARCH" in \
        amd64) UVARCH=x86_64 ;; \
        arm64) UVARCH=aarch64 ;; \
        *) echo "unsupported TARGETARCH: $TARGETARCH" >&2; exit 1 ;; \
    esac \
    && curl -fsSL "https://github.com/astral-sh/uv/releases/download/${UV_VERSION}/uv-${UVARCH}-unknown-linux-gnu.tar.gz" \
    -o /tmp/uv.tgz \
    && mkdir -p /usr/local/uv \
    && tar -C /usr/local/uv --strip-components=1 -xzf /tmp/uv.tgz && rm /tmp/uv.tgz \
    && ln -sf /usr/local/uv/uv  /usr/local/bin/uv \
    && ln -sf /usr/local/uv/uvx /usr/local/bin/uvx

# rustc + cargo via rustup, pinned by version like Go/Node/uv: the sandbox has
# no network at runtime, so the toolchain ships in the image (the default
# profile adds rust-std, rustfmt and clippy to rustc and cargo). RUSTUP_HOME
# and CARGO_HOME are baked here; run.py mounts a fresh anonymous volume over
# each at runtime so an agent can `rustup component add` (given egress) even
# though the root filesystem is read-only. They are chowned to the baked user
# so the sandbox user can write to the copied-up volume (podman preserves the
# image ownership, and the container is not root). The shims are symlinked
# into /usr/local/bin like Go/uv, and CARGO_HOME/bin joins PATH so binaries
# from `cargo install` resolve too. Unlike the tarballs above, rustup's
# installer detects the host architecture itself (uname -m), so it needs no
# TARGETARCH mapping and installs the matching amd64 or arm64 toolchain.
ENV RUSTUP_HOME=/usr/local/rustup \
    CARGO_HOME=/usr/local/cargo \
    PATH=/usr/local/cargo/bin:$PATH
RUN curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs \
        -o /tmp/rustup-init.sh \
    && sh /tmp/rustup-init.sh -y --no-modify-path --profile default \
         --default-toolchain "${RUST_VERSION}" \
    && rm /tmp/rustup-init.sh \
    && chown -R "$USER_UID:$USER_GID" /usr/local/rustup /usr/local/cargo \
    && for b in rustup cargo rustc rustfmt cargo-clippy clippy-driver; do \
         ln -sf "/usr/local/cargo/bin/$b" "/usr/local/bin/$b"; \
       done

# forgejo-cli (`fj`), pinned by version like Go/Node/uv: the Forgejo command
# line client the egress design assumes is available in-sandbox (Forgejo API
# and git-host work alongside `git`). The release tarball holds a single
# top-level `fj`; it is installed into /usr/local/bin and smoke-checked here so
# an unresolved shared library (libssl, from the apt layer) fails the build.
RUN case "$TARGETARCH" in \
        amd64) FJARCH=x86_64 ;; \
        arm64) FJARCH=aarch64 ;; \
        *) echo "unsupported TARGETARCH: $TARGETARCH" >&2; exit 1 ;; \
    esac \
    && curl -fsSL "https://codeberg.org/forgejo-contrib/forgejo-cli/releases/download/v${FJ_VERSION}/forgejo-cli-${FJARCH}-linux.tar.gz" \
    -o /tmp/fj.tgz \
    && tar -C /usr/local/bin -xzf /tmp/fj.tgz fj && rm /tmp/fj.tgz \
    && chmod 0755 /usr/local/bin/fj \
    && fj version

RUN set -eux; \
    groupadd -g "$USER_GID" "$USERNAME"; \
    useradd -u "$USER_UID" -g "$USER_GID" -d "$USER_HOME" -m -s /bin/bash "$USERNAME"

ENV HOME=$USER_HOME \
    USER=$USERNAME \
    LOGNAME=$USERNAME
ENV GOTOOLCHAIN=local \
    GOCACHE=/tmp/go-build \
    GOPATH=/tmp/gopath

# npm cache on the /tmp tmpfs: $HOME is read-only in the sandbox.
ENV npm_config_cache=/tmp/npm-cache

USER ${USER_UID}:${USER_GID}

ENTRYPOINT ["/bin/sh", "-c", "while true; do sleep 1; done"]
