NVIDIA/Bumblebee

This is a guide to configuring a Gentoo system to make use of Hybrid Graphics using:
 * The proprietary Nvidia graphics drivers
 * The Intel iGPU drivers
 * Bumblebee
 * bbswitch for hardware switching
 * Primus for bridging

This configuration prefers Primus over Optirun/VGL.

NVIDIA Optimus Support
This is pretty much straightforward. If you have an intel processor (2nd Gen +) and a NVIDIA gpu, then you probably have optimus on your laptop. To check for sure, run

and look for VGA or 3D controllers to see your present GPUs. If you don't have an optimus system, this guide will do nothing for you.

Nvidia Drivers
First configure your system to use Nvidia's proprietary drivers over the open source Nouveau Drivers and install the drivers. This is already detailed elsewhere on the wiki and is not repeated here.

Intel Drivers
Also configure and install the intel drivers according to the wiki. DDX or modesetting both ought to work.

Kernel
Ensure that you system can load and unload modules:

Video Cards
Set your video card variables like so:

USE Flags
In order to allow easy enabling and disabling of the nvidia drivers, we need to disable the  and   USE flags for the nvidia drivers:

Now update your @world set:

Then reboot your system

Installing Bumblebee, bbswitch, Primus
Emerge the packages:

User access to Bumblebee
Make sure to add your user to the  and   groups to allow use of bumblebee:

(Replacing USER with your username)

Configuring /etc/init.d/bumblebee
By default, the OpenRC bumblebee module is set to depend on VGL and XDM. We are using primus over VGL. The XDM dependancy can cause problems if this configuration is being setup prior to the x server. Remove the following lines (which should be near the start of the file):

Your final file should look something like this:

The important part is that it no longer has any dependencies.

Configuring /etc/bumblebee/bumblebee.conf
The following settings need to be present in the /etc/bumblebee/bumblebee.conf file.

OpenRC
Now enable bumblebe to load at runlevel

And once again, reboot the system:

Configuring /etc/modprobe.d/nvidia-rmmod.conf
Earlier, we disabled the  and   USE flags for the Nvidia drivers. This means we should only have the module  present and not ,  , or. In order that  functions correctly, we need it to remove only   and nothing else. Remove all other modules from /etc/modprobe.d/nvidia-rmmod.conf so that it changes from looking like this:

to this:

OpenGL
We need to ensure OpenGl is set to use xorg-x11 instead of nvidia:

Using glxgears
Install the  package to access  :

Before we run our test, make sure the  is NOT loaded:

And check the status of bbswitch using:

As you can see:
 * bbswitch is loaded
 * nvidia is NOT loaded
 * the nvidia GPU (at PCI location 01:00.0) is OFF

Now let’s check if the NVIDIA card will be switched ON and the nvidia module will be loaded once optirun/primusrun are executed, and if it’ll switch OFF and the nvidia module will be unloaded once optirun/primusrun finish executing. From within an Xorg Session:

or (but not both at once):

A window showing glxgears should open. While it's running, check:

This shows that the Nvidia GPU is now on, its module loaded, and openGL is using it as expected. Once you end the  or   command and exit glxgears, the previous checks run again should show the GPU off and the module unloaded.

Using glxinfo
Alternatively,  can be used to see if bumblebee is working:

As you can see, running the command without  runs with the iGPU but with   runs with the Nvidia GPU.

bbswitch refuses to turn off card
Try adding:

to your kernel command-line (this has worked for a couple of users, especially those with Thinkpads and Lenovo laptops).

Using the Layman Overlay
Some users cannot get this configuration to work and it is suggested to use the Layman Overlay to emerge bumblebee and primus.

First install the Layman Overlay then add bumblebee:

Then unmask bumblebee and primus:

And mask the official Portage Tree:

Then emerge Primus:

Configuring Xorg
Some systems require Xorg to be configured to use the Intel Drivers. Do this according to the Gentoo Intel Wiki Page.

Building with KMS
If users want to build their system using  and/or   on their Nvidia driver, they must edit their nvida-rmmod.conf to unload the relevant driver:

And they must use a patched version of bumblebee, an example of which can be found on the Seden Overlay.

Manual Unloading
If the nvidia drivers are buildt with  and/or   and a patched version of bumblebee then the bumblebee module will not be able to unload the   module as it will be in use by the   module which is itself in use by the   module. However, the modules cat be unloaded manually (using ) and the relevant changes to /etc/modprobe.d/rmmod-nvidia.conf. The GPU can then be turned off using. Some users have these two commands in a script accessible through a key command in their WM.

System Freezes when Starting Xorg Server
On some systems, it may be necessary to start Xorg with the  module loaded and the discrete GPU turned on or else the system will hang. The module can then be unloaded and the GPU switched off. In order to manually load the module, add  to your modules configuration file:

The  module must not be blacklisted.

Bumblebee Daemon Doesn't Start On Boot
First check that bumblebee is configured in OpenRC: Now enable bumblebe to load at runlevel

If you still get errors indicating that the Bumblebee Daemon is not running when you attempt  and   commands, it may be necessary to start the daemon manually on log-in. Put into a suitable configuration file so that it will be automatically run when you start your X server. This might be in your  or window manager (such as i3) config file depending on your setup.

Blacklisting the Nvidia Modules
On some systems it may be necessary to blacklist the  modules. Edit your blacklist file as necessary:

Specific Use-Cases
The following Wiki Pages make reference to Bumblebee and/or Bumblebee configurations:
 * Lenovo ThinkPad T440s
 * Clevo P650HS-G
 * Dell XPS 15z
 * Toshiba Satellite P70-A