
# We can't use the official rust docker image because we need to build the
# extension against the oldest glibc version available.
# So we use Rocky8 as the base image, which ships glibc 2.28
# Our hope is that an extension built against an old version of glibc will
# continue to work against newer versions of glibc.
#FROM rust:1

FROM rockylinux:8 AS rhel8_devtools

ARG PGRX_VERSION=0.18.0

COPY docker/pgrx/goreleaser.repo /etc/yum.repos.d/goreleaser.repo


# Fix ARM64 repository configuration (following pglinter PostgreSQL compatibility requirements)
RUN dnf update -y ca-certificates curl \
  && dnf install -y 'dnf-command(config-manager)' \
  && if [ "$(uname -m)" = "aarch64" ]; then \
  # For ARM64: Use CRB instead of PowerTools and skip problematic repos
  dnf config-manager -y --set-enabled crb || \
  dnf config-manager -y --set-enabled powertools || true; \
  else \
  # For AMD64: Use standard PowerTools
  dnf config-manager -y --set-enabled powertools; \
  fi \
  && dnf groupinstall -y 'Development Tools' \
  && dnf install -y epel-release


# https://github.com/pgcentralfoundation/pgrx?tab=readme-ov-file#system-requirements
RUN dnf install -y 'dnf-command(config-manager)' \
  && dnf groupinstall -y 'Development Tools' \
  && dnf install -y epel-release \
  && dnf install -y \
  cmake \
  git \
  clang \
  nfpm \
  bison-devel \
  libicu-devel \
  readline-devel \
  zlib-devel \
  openssl-devel \
  ccache \
  wget \
  && dnf clean all \
  && rm -rf /var/cache/yum


FROM rhel8_devtools AS rhel8_rust

# Create the postgres user with the given uid/gid
# If you're not using Docker
# You can fix that by rebuilding the image locally with:
#Desktop and your UID / GID is not 1000 then
# you'll get permission errors with volumes.
#
# docker compose build --build-arg UID=`id -u` --build-arg GID=`id- g`
#

ARG UID=1000
ARG GID=1000
RUN groupadd -g "${GID}" postgres \
  && useradd --create-home --no-log-init -u "${UID}" -g "${GID}" postgres

# GitHub Actions compatibility: install sudo and setup directories
RUN dnf install -y sudo \
  && echo "postgres ALL=(ALL) NOPASSWD:ALL" >> /etc/sudoers \
  && mkdir -p /__w/_temp/_runner_file_commands /__w/_actions /github/workspacem \
  && chown -R postgres:postgres /__w /github \
  && chmod -R 777 /__w \
  && chmod -R 755 /github

USER postgres
ENV USER=postgres

# Install Rust

RUN wget https://sh.rustup.rs -O /tmp/install-rust.sh \
  && chmod +x /tmp/install-rust.sh \
  && /tmp/install-rust.sh -y

ENV PATH="/home/postgres/.cargo/bin/:${PATH}"


FROM rhel8_rust AS rhel8_pgrx

RUN rustup self update \
  && rustup component add clippy llvm-tools-preview \
  && cargo install grcov \
  && cargo install --locked --version "${PGRX_VERSION}" cargo-pgrx \
  && cargo pgrx init

ENV CARGO_HOME=~postgres/.cargo/


WORKDIR /pgrx
VOLUME /pgrx
