FROM ubuntu:noble

## NOTA BENE: In order for this to be built successfully, you have to run ./devops/coreNLP/build.sh first.

ARG DEBIAN_FRONTEND=noninteractive

ENV TZ=Europe/Rome
ENV LANG='en_US.UTF-8' LANGUAGE='en_US:en' LC_ALL='en_US.UTF-8'
ENV USER=root
ENV SHELL /bin/bash
ENV PATH=/root/.nix-profile/bin:$PATH
ENV PATH=/root/.local/bin:$PATH


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/* && \
    mkdir -m 0755 /nix && groupadd -r nixbld && chown root /nix && \
    for n in $(seq 1 10); do useradd -c "Nix build user $n" -d /var/empty -g nixbld -G nixbld -M -N -r -s "$(command -v nologin)" "nixbld$n"; done && \
    gpg --batch --keyserver keys.openpgp.org     --recv-keys 7D1E8AFD1D4A16D71FADA2F2CCC85C0E40C06A8C && \
    gpg --batch --keyserver keyserver.ubuntu.com --recv-keys FE5AB6C91FEA597C3B31180B73EDE9E8CFBAEF01

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

RUN set -o pipefail && \
    locale-gen en_US.UTF-8 && \
    bash <(curl -L https://releases.nixos.org/nix/nix-2.29.1/install) --no-daemon && \
    chown root -R /nix && \
    . "$HOME/.nix-profile/etc/profile.d/nix.sh" && \
    mkdir -vp "$HOME/.config/nix" && echo "experimental-features = nix-command flakes" >> $HOME/.config/nix/nix.conf && \
    # prefetch corenlp
    nix build 'git+https://gitlab.iscpif.fr/gargantext/corenlp-nix.git' && \
    mkdir -p "/builds/gargantext/" && chmod 777 -R "/builds/gargantext" && \
    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 set -o pipefail && \
    pushd /nix-ci-build/ && nix-build shell.nix && popd

WORKDIR "/builds/gargantext/"

