# claudia: Claude Code with full freedom inside the container.
#
# The user is `claudia` (uid/gid 1000) with passwordless sudo. That is safe
# with respect to the host: the container runs rootless, so root in here is
# only the invoking host user, and :ro bind mounts stay read-only for root.
#
# /home/claudia is replaced at run time by a per-instance directory from the
# host, so login, settings, history and auto-updates of Claude Code survive
# `--rm`. The installation made at build time is therefore stashed in
# /opt/claudia/seed and copied into an empty home by entrypoint.sh.
FROM docker.io/library/debian:trixie-slim

ARG BUILD_DATE=unknown
ARG GIT_REVISION=unknown
LABEL org.opencontainers.image.title="claudia" \
      org.opencontainers.image.description="Claude Code in a rootless Podman container" \
      org.opencontainers.image.source="https://kode.naiv.no/olemd/claudia" \
      org.opencontainers.image.licenses="MIT" \
      org.opencontainers.image.created="${BUILD_DATE}" \
      org.opencontainers.image.revision="${GIT_REVISION}"

ENV DEBIAN_FRONTEND=noninteractive \
    LANG=C.UTF-8

RUN apt-get update \
 && apt-get install -y --no-install-recommends \
      build-essential ca-certificates curl git jq less procps ripgrep sudo unzip \
      podman uidmap fuse-overlayfs passt \
 && rm -rf /var/lib/apt/lists/*

# Nested Podman: claudia gets every other uid/gid of the container's own
# user namespace (0-65536 under rootless keep-id) as subordinate ids, so
# images that use uids up to 65535 (e.g. nobody) can be unpacked.
RUN groupadd --gid 1000 claudia \
 && useradd --uid 1000 --gid 1000 --create-home --shell /bin/bash claudia \
 && printf 'claudia:1:999\nclaudia:1001:64536\n' | tee /etc/subuid > /etc/subgid \
 && printf 'claudia ALL=(ALL) NOPASSWD: ALL\n' > /etc/sudoers.d/claudia \
 && chmod 0440 /etc/sudoers.d/claudia

# Install as claudia into /home/claudia so the launcher symlink
# (~/.local/bin/claude -> ~/.local/share/claude/versions/X) already points
# at the path it will have once the seed is copied into the mounted home.
USER claudia
RUN curl -fsSL https://claude.ai/install.sh | bash \
 && /home/claudia/.local/bin/claude --version
USER root
RUN mkdir -p /opt/claudia \
 && mv /home/claudia /opt/claudia/seed \
 && install -d -o claudia -g claudia /home/claudia /work /work/input /work/output

COPY containers.conf /etc/containers/containers.conf
COPY log-instructions.md /usr/share/claudia/log-instructions.md
COPY entrypoint.sh /usr/local/bin/claudia-entrypoint
RUN chmod 0644 /usr/share/claudia/log-instructions.md \
 && chmod 0755 /usr/local/bin/claudia-entrypoint \
 && printf 'build_date=%s\ngit_revision=%s\n' "${BUILD_DATE}" "${GIT_REVISION}" > /etc/build-info

USER claudia
ENV HOME=/home/claudia \
    PATH=/home/claudia/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
WORKDIR /work/output
ENTRYPOINT ["/usr/local/bin/claudia-entrypoint"]
