Nouveau & nvidia-drivers switching

Switching between nVidia's binary driver and the open source nouveau driver can be tricky, but definitely doable. This article will detail how to go about switching between them at different stages (i.e. at boot, in a runlevel, etc.)

On Boot
This method assumes the following:
 * Two kernels, one with the nouveau driver enabled and one with the nouveau driver disabled (or built as a module).
 * The nouveau-less kernel needs to have a -nvidia suffix.
 * The GRUB bootloader.

It is assumed you already have a nouveau kernel and want to build the one that will use the nvidia driver. Begin with appending the -nvidia suffix to the kernel name:

Now make sure the nouveau driver is disabled or built as a module:

Now recompile and install the kernel. Make sure you update GRUB to take the new kernel into account. Note that kernels with the same version but with different names are considered unique. So if you want to emerge an out-of-tree kernel module to be used with the nouveau kernel you'll first need to copy over the nouveau kernels config file to.

Blacklist the nvidia and nouveau modules as you don't want udev to try and autoload them.

And finally use the local.d script below to switch graphics driver depending on what kernel you booted with:

Copy it to and set the executable bit:

Switching using a single kernel and hprofile
Another method is switching between two profiles with hprofile, using a single kernel: has to be modified adding a few lines for vga switching. Warning: hprofile shall be added in boot runlevel.

Switching using a single kernel and systemd
If you have your system configured to use systemd, you can control setting nvidia and nouveau by using the functionality provided by the kernel command line.

Within the grub.cfg file you need to duplicate the desired menu item and then edit the two versions to specify which modules with be blacklisted, and to pass an environment variable to systemd. If you previously blacklisted nvidia or nouveau modules in /etc/modprobe.d then you need to remove them since that will now be controlled directly from the kernel command line.

Next we are going to use a small script similar to the one shown in the "Switching using two kernels" section. It is very similar, but slightly modified. For the sake on continuity we will place it in the same location. The main difference is that is does not use the kernel's name to determine which profile to use, but rather it uses a variable set in the environment by systemd from the kernel command line.

If you have any software that uses opencl, then it is recommend you use this alternate version of the script that also switches the opencl implementation between mesa and nvidia. If software tries to use opencl under the nouveau driver, but the opencl is still set to nvidia, then bad things happen. You can have the system change to Mesa's Clover Gallium OpenCL state tracker, but you must first ensure that mesa has been built with the opencl USE flag enabled. If that dependency has been taken care of then use the following version.

As mentioned in the previous section, don't forget to set the executable bit.

To have this script get called during the systemd init process, you can create a service file for it, but it's just as effective to integrate it into the currently existing xdm | gdm | kdm | other(?) display manager service file. Edit the desired service file located in /usr/lib/systemd/system and under the "[Service]" section, add an entry for ExecStartPre. This is used to specify any command to be run before the main command of the service file is executed. The example shown below for demonstration is a modified version of the default kdm service file, but the same modification can be made to any display manager service file.