FROM ubuntu:noble

ARG DEBIAN_FRONTEND=noninteractive

ENV TZ=Europe/Rome
ENV LANG='en_US.UTF-8' LANGUAGE='en_US:en' LC_ALL='en_US.UTF-8'

RUN apt-get update && \
    apt-get install --no-install-recommends -y \
      apt-transport-https \
      ca-certificates \
      curl \
      fontconfig \
      git \
      gnupg2 \
      locales \
      software-properties-common \
      sudo \
      xz-utils \
      #zlib1g-dev \
      unzip && \
    apt-get clean && rm -rf /var/lib/apt/lists/* && \
    gpg --batch --keyserver keys.openpgp.org     --recv-keys 7D1E8AFD1D4A16D71FADA2F2CCC85C0E40C06A8C && \
    gpg --batch --keyserver keyserver.ubuntu.com --recv-keys FE5AB6C91FEA597C3B31180B73EDE9E8CFBAEF01

RUN locale-gen en_US.UTF-8

RUN useradd -m test
ENV USER=test
ENV SHELL=/bin/bash
ENV PATH=/home/test/.nix-profile/bin:$PATH
ENV PATH=/home/test/.local/bin:$PATH
RUN mkdir -m 0777 /nix && chown test /nix && \
    mkdir -p "/builds/gargantext/" && chmod 777 -R "/builds/gargantext"
RUN echo 'test ALL=(ALL) NOPASSWD:ALL' >> /etc/sudoers
USER test

SHELL ["/bin/bash", "-o", "pipefail", "-c"]

RUN bash <(curl --proto '=https' --tlsv1.2 -L https://releases.nixos.org/nix/nix-2.30.2/install) --no-daemon && \
    #chown root -R /nix && \
    mkdir -vp "$HOME/.config/nix" && \
    echo "experimental-features = nix-command flakes" >> $HOME/.config/nix/nix.conf

WORKDIR /nix-ci-build

# prefetch corenlp
RUN nix build 'git+https://gitlab.iscpif.fr/gargantext/corenlp-nix.git' && \
    echo "source $HOME/.nix-profile/etc/profile.d/nix.sh" >> "$HOME/.bashrc" && \
    echo `which nix-env` && \
    . $HOME/.bashrc && nix-env --version

# We want to cache nix artifacts in the Dockerfile to improve CI speed
COPY ./shell.nix /nix-ci-build/
COPY ./nix       /nix-ci-build/
RUN nix-build shell.nix

WORKDIR "/builds/gargantext/"

