This guide will show you how to download, compile and install the nvidia-legacy-340xx-driver on Debian 12. Some people install the driver directly from sid but this will mix packages from both stable and unstable which may create issues. It's recommended to update and upgrade your system before you start.
It was reported to work with nvidia-legacy-390xx-driver just replace the appropriate package names.
Install these packages.
sudo apt install build-essential fakeroot devscripts