FROM ubuntu:22.04

ARG USERNAME=dev
ARG UID=1000
ARG GID=${UID}

RUN export DEBIAN_FRONTEND=noninteractive && apt update \
    && apt install --no-install-recommends --yes \
        build-essential \
        ca-certificates \
        curl \
        sudo \
    && rm -rf /var/lib/apt/lists/*

# Create a non-root user.
#
# This creates a non-root user to run under. However, it is still capable of
# being a sudo'er, accordingly.
RUN groupadd --gid=${GID} ${USERNAME} \
    && useradd --uid ${UID} --gid $GID --create-home ${USERNAME} \
    && echo ${USERNAME} ALL=\(root\) NOPASSWD:ALL > /etc/sudoers.d/${USERNAME} \
    && chmod 0440 /etc/sudoers.d/${USERNAME}

USER ${USERNAME}

# Install Rust.
#
# This installs Rust as the non-root user. This is necessary in order to run the
# toolchain logged in as the non-root user.
RUN curl https://sh.rustup.rs -sSf | sh -s -- -y

# Add to PATH.
#
# To access the Rust toolchain, it needs to be added to the path, accordingly;
# else, the tools are not accessible.
ENV PATH="/home/${USERNAME}/.cargo/bin:${PATH}"
