Great!!! 
You’re a hero 
My steps to achieve a working X11 config with my hibrid GPU setup:
-Install Debian as usual
-Install proprietary nvidia drivers:
apt install nvidia-driver libcuda1 libnvidia-encode1 nvidia-opencl-icd
because I need cuda for Davinci Resolve, and nvenc for ffmpeg/OBS encodings…
Put this content into /etc/X11/xorg.conf
Section "ServerLayout"
Identifier "layout"
Screen 0 "Screen0"
Inactive "nvidia"
Option "AutoAddDevices" "true"
Option "AutoAddGPU" "true"
Option "AllowNVIDIAGPUScreens"
EndSection
Section "Device"
Identifier "nvidia"
Driver "nvidia"
BusID "PCI:01:0:0"
Option "AllowEmptyInitialConfiguration"
Option "metamodes" "nvidia-auto-select +0+0 {ForceFullCompositionPipeline=On}"
#Option "TripleBuffer" "on"
EndSection
Section "Screen"
Identifier "nvidia"
Device "nvidia"
EndSection
Section "Device"
Identifier "intel"
Driver "intel"
BusID "PCI:0:02:0"
EndSection
Section "Screen"
Identifier "Screen0"
Device "intel"
#GPUDevice "intel"
EndSection
Section "ServerFlags"
#Option "AutoAddGPU" "false"
EndSection
Note the BusID’s in the config files, I get those from lspci
output, for me it is:
00:02.0 VGA compatible controller: Intel Corporation CoffeeLake-H GT2 [UHD Graphics 630]
01:00.0 3D controller: NVIDIA Corporation GP107M [GeForce GTX 1050 Mobile] (rev a1)
(I omitted here irrelevant hardware elements, pasted only the video cards).
Most recypes tell we have to use the modesetting driver for the intel iGPU, but I wanted to have the VIRTUAL display option, which is provided by the intel dirver, but not by the modesetting driver. So despit it is told I must use modesetting in this case, I use the intel…
If you plan to use the modesetting, as it is recommended, change the line
Driver "intel"
to
Driver "modesetting"
Now when X starts, LightDM has to launch a displaysetup script, here I followed the Debian Wiki
https://wiki.debian.org/NVIDIA%20Optimus
with a slight modification:
So put into /etc/lightdm/displaysetup.sh :
#!/bin/sh
xrandr --setprovideroutputsource NVIDIA-G0 Intel
xrandr --auto
xrandr --dpi 96
And make this executable (chmod +x…)
I get the parameters of the first line by exeecuting xrandr --listproviders
I write this because the recype tells about NVIDIA-0 and intel, note that I needed NVIDIA-G0 and Intel.
This may be different for you…
This makes reveres prime functional, and whenever I plug in the external monitor, it is recognized, and desktop is extended…
To make lightdm really run this script, edit /etc/lightdm.conf, so that the first lines look like:
[Seat:*]
display-setup-script=/etc/lightdm/display_setup.sh
If I run a program, it will run on the iGPU by default.
This is wrong for Davinci Resolve, it must run on the nvidia, so I modify it’s desktop file to launch its executable.
__NV_PRIME_RENDER_OFFLOAD=1 __GLX_VENDOR_LIBRARY_NAME=nvidia /opt/resolve/bin/resolve
So the desktop files content looks like this:
[Desktop Entry]
Version=1.0
Encoding=UTF-8
Type=Application
Name=DaVinci Resolve
Path=/opt/resolve
Exec=env __NV_PRIME_RENDER_OFFLOAD=1 __GLX_VENDOR_LIBRARY_NAME=nvidia /opt/resolve/bin/resolve
Icon=/opt/resolve/graphics/DV_Resolve.png
Terminal=false
MimeType=application/x-resolveproj;
StartupNotify=true
Categories=AudioVideo
Comment=
Theoretically I could run any program, game or such on nvidia, but usually I need only Davinci to run there.
I hope this helps you somewhat to fight your optimus related problem!
Let me take the opportunity to whish you a well blessed and happy Easter!