about summary refs log tree commit diff
path: root/pkgs/tools/X11/bumblebee/default.nix
blob: 914972d91aa76c8b03f99e68ebe45e2a0751f185 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# The bumblebee package allows a program to be rendered on an
# dedicated video card by spawning an additional X11 server and
# streaming the results via VirtualGL or primus to the primary server.

# The package is rather chaotic; it's also quite recent.
# As it may change a lot, some of the hacks in this nix expression
# will hopefully not be needed in the future anymore.

# To test:
# 1. make sure that the 'bbswitch' kernel module is installed,
# 2. then run 'bumblebeed' as root
# 3. Then either 'optirun glxinfo' or 'primusrun glxinfo' as user.
#
# The glxinfo output should indicate the NVidia driver is being used
# and all expected extensions are supported.
#
# To use at startup, see hardware.bumblebee options.

{ stdenv, lib, fetchurl, fetchpatch, pkg-config, help2man, makeWrapper
, glib, libbsd
, libX11, xorgserver, kmod, xf86videonouveau
, nvidia_x11, virtualgl, libglvnd
, automake111x, autoconf
# The below should only be non-null in a x86_64 system. On a i686
# system the above nvidia_x11 and virtualgl will be the i686 packages.
# TODO: Confusing. Perhaps use "SubArch" instead of i686?
, nvidia_x11_i686 ? null
, libglvnd_i686 ? null
, useDisplayDevice ? false
, extraNvidiaDeviceOptions ? ""
, extraNouveauDeviceOptions ? ""
, useNvidia ? true
}:

let
  nvidia_x11s = [ nvidia_x11 ]
                ++ lib.optional nvidia_x11.useGLVND libglvnd
                ++ lib.optionals (nvidia_x11_i686 != null)
                   ([ nvidia_x11_i686 ] ++ lib.optional nvidia_x11_i686.useGLVND libglvnd_i686);

  nvidiaLibs = lib.makeLibraryPath nvidia_x11s;

  bbdPath = lib.makeBinPath [ kmod xorgserver ];

  xmodules = lib.concatStringsSep "," (map (x: "${x.out or x}/lib/xorg/modules") ([ xorgserver ] ++ lib.optional (!useNvidia) xf86videonouveau));

  modprobePatch = fetchpatch {
    url = "https://github.com/Bumblebee-Project/Bumblebee/commit/1ada79fe5916961fc4e4917f8c63bb184908d986.patch";
    sha256 = "02vq3vba6nx7gglpjdfchws9vjhs1x02a543yvqrxqpvvdfim2x2";
  };
  libkmodPatch = fetchpatch {
    url = "https://github.com/Bumblebee-Project/Bumblebee/commit/deceb14cdf2c90ff64ebd1010a674305464587da.patch";
    sha256 = "00c05i5lxz7vdbv445ncxac490vbl5g9w3vy3gd71qw1f0si8vwh";
  };

in stdenv.mkDerivation rec {
  pname = "bumblebee";
  version = "3.2.1";

  src = fetchurl {
    url = "https://www.bumblebee-project.org/${pname}-${version}.tar.gz";
    sha256 = "03p3gvx99lwlavznrpg9l7jnl1yfg2adcj8jcjj0gxp20wxp060h";
  };

  patches = [
    ./nixos.patch

    modprobePatch
    libkmodPatch
  ];

  # By default we don't want to use a display device
  nvidiaDeviceOptions = lib.optionalString (!useDisplayDevice) ''
    # Disable display device
    Option "UseEDID" "false"
    Option "UseDisplayDevice" "none"
  '' + extraNvidiaDeviceOptions;

  nouveauDeviceOptions = extraNouveauDeviceOptions;

  # the have() function is deprecated and not available to bash completions the
  # way they are currently loaded in NixOS, so use _have. See #10936
  postPatch = ''
    substituteInPlace scripts/bash_completion/bumblebee \
      --replace "have optirun" "_have optirun"
  '';

  preConfigure = ''
    # Don't use a special group, just reuse wheel.
    substituteInPlace configure \
      --replace 'CONF_GID="bumblebee"' 'CONF_GID="wheel"'

    # Apply configuration options
    substituteInPlace conf/xorg.conf.nvidia \
      --subst-var nvidiaDeviceOptions

    substituteInPlace conf/xorg.conf.nouveau \
      --subst-var nouveauDeviceOptions
  '';

  # Build-time dependencies of bumblebeed and optirun.
  # Note that it has several runtime dependencies.
  buildInputs = [ libX11 glib libbsd kmod ];
  nativeBuildInputs = [ makeWrapper pkg-config help2man automake111x autoconf ];

  # The order of LDPATH is very specific: First X11 then the host
  # environment then the optional sub architecture paths.
  #
  # The order for MODPATH is the opposite: First the environment that
  # includes the acceleration driver. As this is used for the X11
  # server, which runs under the host architecture, this does not
  # include the sub architecture components.
  configureFlags = [
    "--with-udev-rules=$out/lib/udev/rules.d"
    # see #10282
    #"CONF_PRIMUS_LD_PATH=${primusLibs}"
  ] ++ lib.optionals useNvidia [
    "CONF_LDPATH_NVIDIA=${nvidiaLibs}"
    "CONF_MODPATH_NVIDIA=${nvidia_x11.bin}/lib/xorg/modules"
  ];

  CFLAGS = [
    "-DX_MODULE_APPENDS=\\\"${xmodules}\\\""
  ];

  postInstall = ''
    wrapProgram "$out/sbin/bumblebeed" \
      --prefix PATH : "${bbdPath}"

    wrapProgram "$out/bin/optirun" \
      --prefix PATH : "${virtualgl}/bin"
  '';

  meta = with lib; {
    description = "Daemon for managing Optimus videocards (power-on/off, spawns xservers)";
    homepage = "https://github.com/Bumblebee-Project/Bumblebee";
    license = licenses.gpl3;
    maintainers = with maintainers; [ abbradar ];
    platforms = platforms.linux;
    # linking fails with multiple error of type:
    # multiple definition of `bb_pm_method_string'; src/module.o:(.bss+0x0): first defined here
    broken = true; # Added 03-08-2022
  };
}