FROM ghcr.io/benradey/workstation-nvidia-base:latest

# Build nvidia akmod with no special arguments - defaults to open kernel driver for newer hardware
RUN mkdir -p /var/log/akmods /var/cache/akmods/nvidia /var/tmp && \
    chmod 777 /var/tmp && \
    /usr/sbin/akmods --force --kernels "$(rpm -qa kernel --queryformat '%{VERSION}-%{RELEASE}.%{ARCH}')"

# blueman necessary for cinnamon, not necessary for gnome
RUN dnf5 install -y \
      dkms \
      powertop \
      python3-pyusb \
      radeontop \
      waydroid \
      xinput && \
    dnf5 clean all

RUN git clone https://github.com/johnfanv2/LenovoLegionLinux.git && \
    mkdir -p /usr/src/LenovoLegionLinux-1.0.0 && \
    cp /LenovoLegionLinux/kernel_module/* /usr/src/LenovoLegionLinux-1.0.0 -r && \
    dkms add -m LenovoLegionLinux -v 1.0.0 && \
    dkms build -m LenovoLegionLinux -v 1.0.0 -k $(rpm -qa kernel --queryformat '%{VERSION}-%{RELEASE}.%{ARCH}') && \
    dkms install -m LenovoLegionLinux -v 1.0.0 -k $(rpm -qa kernel --queryformat '%{VERSION}-%{RELEASE}.%{ARCH}') && \
    echo legion-laptop > /etc/modules-load.d/legion_laptop.conf && \
    rm -rf LenovoLegionLinux

# The first check makes sure that we have exactly 4 gpg pubkeys trusted in the rpmdb (the ones from the base image).
# Any more than that means that dnf automatically added a new one, which is shady!
# Then: cleanup image for linting
# Finally: verify image is good
# FIXME - remove the `--skip nonempty-run-tmp` when it's no longer needed
RUN [[ 4 -eq $(rpm -qa gpg-pubkey* | wc -l) ]] && \
    rm -rf /var /boot && mkdir /var /boot && \
    bootc container lint --fatal-warnings --skip nonempty-run-tmp
