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 instead of 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 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 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 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 / are executed, and if it’ll switch OFF and the nvidia module will be unloaded once / 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, 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.

Building with KMS and UVM
Users who want to use CUDA or offloading with their NVIDIA GPU will want to use KMS and UVM. To do this, first follow the guide above to install without KMS and UVM. This provides a good starting point because if it doesn't work with that setup, it will not work with this one.

First, stop the bumblebee daemon:

Then unmount the module and remount  module:

Next, change your use flags to include  and  :

and update your set:

Next, enable the overlay with :

and install the patched version of bumblebee (which is capable of removing all of the NVIDIA drivers):

Edit the to unload the relevant drivers in the right order:

Change your kernel module parameter in your file to

Now reboot your system.

Using Bumblebee with Steam Games
In order to run a Steam game with or, you need to
 * right click on the game within steam
 * select "properties"
 * "Set Launch Options"
 * add either  or   to the text field.

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

First add the bumblebee overlay via layman:

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.

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).

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. 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.

GPU doesn't automatically turn off for the first time
Some users may experience their bumblebee setup working in every respect except for that the GPU remains on until the first time a or  command is run. A workaround for this is to add  to your  or other initialising config file (like your i3 config file).

System Hangs on any PCI call when the GPU is off
Some users may find that their setup works well but will experience random kernel crashes characterized by a complete system hang with no errors in any log files generated. If this is the case, it may be necessary to trick the acpi kernel module into thinking that the system is an earlier version of Windows by editing your grub configuration:

The parameters,  , and   may also be necessary.

Then regenerate your grub config file:

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
 * Dell XPS 15 9560