1{ config, lib, pkgs, pkgs_i686, ... }:
2
3with lib;
4
5let
6
7 cfg = config.hardware.opengl;
8
9 kernelPackages = config.boot.kernelPackages;
10
11 videoDrivers = config.services.xserver.videoDrivers;
12
13 makePackage = p: pkgs.buildEnv {
14 name = "mesa-drivers+txc-${p.mesa_drivers.version}";
15 paths =
16 [ p.mesa_drivers
17 p.mesa_noglu # mainly for libGL
18 (if cfg.s3tcSupport then p.libtxc_dxtn else p.libtxc_dxtn_s2tc)
19 ];
20 };
21
22 package = pkgs.buildEnv {
23 name = "opengl-drivers";
24 paths = [ cfg.package ] ++ cfg.extraPackages;
25 };
26
27 package32 = pkgs.buildEnv {
28 name = "opengl-drivers-32bit";
29 paths = [ cfg.package32 ] ++ cfg.extraPackages32;
30 };
31
32in
33
34{
35 options = {
36 hardware.opengl.enable = mkOption {
37 description = "Whether this configuration requires OpenGL.";
38 type = types.bool;
39 default = false;
40 internal = true;
41 };
42
43 hardware.opengl.driSupport = mkOption {
44 type = types.bool;
45 default = true;
46 description = ''
47 Whether to enable accelerated OpenGL rendering through the
48 Direct Rendering Interface (DRI).
49 '';
50 };
51
52 hardware.opengl.driSupport32Bit = mkOption {
53 type = types.bool;
54 default = false;
55 description = ''
56 On 64-bit systems, whether to support Direct Rendering for
57 32-bit applications (such as Wine). This is currently only
58 supported for the <literal>nvidia</literal> and
59 <literal>ati_unfree</literal> drivers, as well as
60 <literal>Mesa</literal>.
61 '';
62 };
63
64 hardware.opengl.s3tcSupport = mkOption {
65 type = types.bool;
66 default = false;
67 description = ''
68 Make S3TC(S3 Texture Compression) via libtxc_dxtn available
69 to OpenGL drivers instead of the patent-free S2TC replacement.
70
71 Using this library may require a patent license depending on your location.
72 '';
73 };
74
75 hardware.opengl.package = mkOption {
76 type = types.package;
77 internal = true;
78 description = ''
79 The package that provides the OpenGL implementation.
80 '';
81 };
82
83 hardware.opengl.package32 = mkOption {
84 type = types.package;
85 internal = true;
86 description = ''
87 The package that provides the 32-bit OpenGL implementation on
88 64-bit systems. Used when <option>driSupport32Bit</option> is
89 set.
90 '';
91 };
92
93 hardware.opengl.extraPackages = mkOption {
94 type = types.listOf types.package;
95 default = [];
96 example = literalExample "with pkgs; [ vaapiIntel libvdpau-va-gl vaapiVdpau ]";
97 description = ''
98 Additional packages to add to OpenGL drivers. This can be used
99 to add additional VA-API/VDPAU drivers.
100 '';
101 };
102
103 hardware.opengl.extraPackages32 = mkOption {
104 type = types.listOf types.package;
105 default = [];
106 example = literalExample "with pkgs; [ vaapiIntel libvdpau-va-gl vaapiVdpau ]";
107 description = ''
108 Additional packages to add to 32-bit OpenGL drivers on
109 64-bit systems. Used when <option>driSupport32Bit</option> is
110 set. This can be used to add additional VA-API/VDPAU drivers.
111 '';
112 };
113
114 };
115
116 config = mkIf cfg.enable {
117
118 assertions = lib.singleton {
119 assertion = cfg.driSupport32Bit -> pkgs.stdenv.isx86_64;
120 message = "Option driSupport32Bit only makes sense on a 64-bit system.";
121 };
122
123 system.activationScripts.setup-opengl =
124 ''
125 ln -sfn ${package} /run/opengl-driver
126 ${if pkgs.stdenv.isi686 then ''
127 ln -sfn opengl-driver /run/opengl-driver-32
128 '' else if cfg.driSupport32Bit then ''
129 ln -sfn ${package32} /run/opengl-driver-32
130 '' else ''
131 rm -f /run/opengl-driver-32
132 ''}
133 '';
134
135 environment.sessionVariables.LD_LIBRARY_PATH =
136 [ "/run/opengl-driver/lib" "/run/opengl-driver-32/lib" ];
137
138 hardware.opengl.package = mkDefault (makePackage pkgs);
139 hardware.opengl.package32 = mkDefault (makePackage pkgs_i686);
140
141 boot.extraModulePackages = optional (elem "virtualbox" videoDrivers) kernelPackages.virtualboxGuestAdditions;
142 };
143}