Clone of https://github.com/NixOS/nixpkgs.git (to stress-test knotserver)
1{ lib
2, stdenv
3, fetchFromGitLab
4, fetchFromGitHub
5, fetchurl
6, substituteAll
7, coreutils
8, curl
9, glxinfo
10, gnugrep
11, gnused
12, xdg-utils
13, dbus
14, hwdata
15, mangohud32
16, addOpenGLRunpath
17, appstream
18, glslang
19, mako
20, meson
21, ninja
22, pkg-config
23, unzip
24, libXNVCtrl
25, wayland
26, libX11
27, nlohmann_json
28, spdlog
29, glew
30, glfw
31, xorg
32, gamescopeSupport ? true # build mangoapp and mangohudctl
33, lowerBitnessSupport ? stdenv.hostPlatform.is64bit # Support 32 bit on 64bit
34, nix-update-script
35}:
36
37let
38 # Derived from subprojects/cmocka.wrap
39 cmocka = rec {
40 version = "1.81";
41 src = fetchFromGitLab {
42 owner = "cmocka";
43 repo = "cmocka";
44 rev = "59dc0013f9f29fcf212fe4911c78e734263ce24c";
45 hash = "sha256-IbAZOC0Q60PrKlKVWsgg/PFDV0PLb/yy+Iz/4Iziny0=";
46 };
47 };
48
49 # Derived from subprojects/imgui.wrap
50 imgui = rec {
51 version = "1.81";
52 src = fetchFromGitHub {
53 owner = "ocornut";
54 repo = "imgui";
55 rev = "refs/tags/v${version}";
56 hash = "sha256-rRkayXk3xz758v6vlMSaUu5fui6NR8Md3njhDB0gJ18=";
57 };
58 patch = fetchurl {
59 url = "https://wrapdb.mesonbuild.com/v2/imgui_${version}-1/get_patch";
60 hash = "sha256-bQC0QmkLalxdj4mDEdqvvOFtNwz2T1MpTDuMXGYeQ18=";
61 };
62 };
63
64 # Derived from subprojects/vulkan-headers.wrap
65 vulkan-headers = rec {
66 version = "1.2.158";
67 src = fetchFromGitHub {
68 owner = "KhronosGroup";
69 repo = "Vulkan-Headers";
70 rev = "v${version}";
71 hash = "sha256-5uyk2nMwV1MjXoa3hK/WUeGLwpINJJEvY16kc5DEaks=";
72 };
73 patch = fetchurl {
74 url = "https://wrapdb.mesonbuild.com/v2/vulkan-headers_${version}-2/get_patch";
75 hash = "sha256-hgNYz15z9FjNHoj4w4EW0SOrQh1c4uQSnsOOrt2CDhc=";
76 };
77 };
78in
79stdenv.mkDerivation (finalAttrs: {
80 pname = "mangohud";
81 version = "0.6.9-1";
82
83 src = fetchFromGitHub {
84 owner = "flightlessmango";
85 repo = "MangoHud";
86 rev = "refs/tags/v${finalAttrs.version}";
87 fetchSubmodules = true;
88 hash = "sha256-AX4m1XZ+yXp74E3slFGyI3CGu2eYU+eXNN2EY+ivdfk=";
89 };
90
91 outputs = [ "out" "doc" "man" ];
92
93 # Unpack subproject sources
94 postUnpack = ''(
95 cd "$sourceRoot/subprojects"
96 ${lib.optionalString finalAttrs.doCheck ''
97 cp -R --no-preserve=mode,ownership ${cmocka.src} cmocka
98 ''}
99 cp -R --no-preserve=mode,ownership ${imgui.src} imgui-${imgui.version}
100 cp -R --no-preserve=mode,ownership ${vulkan-headers.src} Vulkan-Headers-${vulkan-headers.version}
101 )'';
102
103 patches = [
104 # Add @libraryPath@ template variable to fix loading the preload
105 # library and @dataPath@ to support overlaying Vulkan apps without
106 # requiring MangoHud to be installed
107 ./preload-nix-workaround.patch
108
109 # Hard code dependencies. Can't use makeWrapper since the Vulkan
110 # layer can be used without the mangohud executable by setting MANGOHUD=1.
111 (substituteAll {
112 src = ./hardcode-dependencies.patch;
113
114 path = lib.makeBinPath [
115 coreutils
116 curl
117 glxinfo
118 gnugrep
119 gnused
120 xdg-utils
121 ];
122
123 libdbus = dbus.lib;
124 inherit hwdata;
125 })
126 ];
127
128 postPatch = ''
129 substituteInPlace bin/mangohud.in \
130 --subst-var-by libraryPath ${lib.makeSearchPath "lib/mangohud" ([
131 (placeholder "out")
132 ] ++ lib.optionals lowerBitnessSupport [
133 mangohud32
134 ])} \
135 --subst-var-by dataDir ${placeholder "out"}/share
136
137 (
138 cd subprojects
139 unzip ${imgui.patch}
140 unzip ${vulkan-headers.patch}
141 )
142 '';
143
144 mesonFlags = [
145 "-Dwith_wayland=enabled"
146 "-Duse_system_spdlog=enabled"
147 "-Dtests=${if finalAttrs.doCheck then "enabled" else "disabled"}"
148 ] ++ lib.optionals gamescopeSupport [
149 "-Dmangoapp=true"
150 "-Dmangoapp_layer=true"
151 "-Dmangohudctl=true"
152 ];
153
154 nativeBuildInputs = [
155 addOpenGLRunpath
156 glslang
157 mako
158 meson
159 ninja
160 pkg-config
161 unzip
162
163 # Only the headers are used from these packages
164 # The corresponding libraries are loaded at runtime from the app's runpath
165 libXNVCtrl
166 wayland
167 libX11
168 ];
169
170 buildInputs = [
171 dbus
172 nlohmann_json
173 spdlog
174 ] ++ lib.optionals gamescopeSupport [
175 glew
176 glfw
177 xorg.libXrandr
178 ];
179
180 doCheck = true;
181
182 nativeCheckInputs = [
183 appstream
184 ];
185
186 # Support 32bit Vulkan applications by linking in 32bit Vulkan layers
187 # This is needed for the same reason the 32bit preload workaround is needed.
188 postInstall = lib.optionalString lowerBitnessSupport ''
189 ln -s ${mangohud32}/share/vulkan/implicit_layer.d/MangoHud.x86.json \
190 "$out/share/vulkan/implicit_layer.d"
191
192 ${lib.optionalString gamescopeSupport ''
193 ln -s ${mangohud32}/share/vulkan/implicit_layer.d/libMangoApp.x86.json \
194 "$out/share/vulkan/implicit_layer.d"
195 ''}
196 '';
197
198 postFixup = let
199 archMap = {
200 "x86_64-linux" = "x86_64";
201 "i686-linux" = "x86";
202 };
203 layerPlatform = archMap."${stdenv.hostPlatform.system}" or null;
204 # We need to give the different layers separate names or else the loader
205 # might try the 32-bit one first, fail and not attempt to load the 64-bit
206 # layer under the same name.
207 in lib.optionalString (layerPlatform != null) ''
208 substituteInPlace $out/share/vulkan/implicit_layer.d/MangoHud.${layerPlatform}.json \
209 --replace "VK_LAYER_MANGOHUD_overlay" "VK_LAYER_MANGOHUD_overlay_${toString stdenv.hostPlatform.parsed.cpu.bits}"
210 '' + ''
211 # Add OpenGL driver path to RUNPATH to support NVIDIA cards
212 addOpenGLRunpath "$out/lib/mangohud/libMangoHud.so"
213 '' + lib.optionalString gamescopeSupport ''
214 addOpenGLRunpath "$out/bin/mangoapp"
215 '' + lib.optionalString finalAttrs.doCheck ''
216 # libcmocka.so is only used for tests
217 rm "$out/lib/libcmocka.so"
218 '';
219
220 passthru.updateScript = nix-update-script { };
221
222 meta = with lib; {
223 description = "A Vulkan and OpenGL overlay for monitoring FPS, temperatures, CPU/GPU load and more";
224 homepage = "https://github.com/flightlessmango/MangoHud";
225 platforms = platforms.linux;
226 license = licenses.mit;
227 maintainers = with maintainers; [ kira-bruneau zeratax ];
228 };
229})