16 enableStatic ? stdenv.hostPlatform.isStatic,
17 # MoltenVK supports using private APIs to implement some Vulkan functionality.
18 # Applications that use private APIs can’t be distributed on the App Store,
19 # but that’s not really a concern for nixpkgs, so use them by default.
20 # See: https://github.com/KhronosGroup/MoltenVK/blob/main/README.md#metal_private_api
21 enablePrivateAPIUsage ? true,
24 stdenv.mkDerivation (finalAttrs: {
40 nativeBuildInputs = [ xcbuildHook ];
48 src = fetchFromGitHub {
49 owner = "KhronosGroup";
51 rev = "v${finalAttrs.version}";
52 hash = "sha256-24qQnJ0RnJP2M4zSlSlQ4c4dVZtHutNiCvjrsCDw6wY=";
56 # Cherry-pick patch to fix build failure due to a hardcoded SPIRV-Cross namespace.
57 # This can be dropped for MoltenVK 1.2.12.
59 url = "https://github.com/KhronosGroup/MoltenVK/commit/856c8237ac3b32178caae3408effc35bedfdffa1.patch?full_index=1";
60 hash = "sha256-dVTop8sU19Swdb3ajbI+6S715NaxTqd7d0yQ/FDqxqY=";
65 # Move `mvkGitRevDerived.h` to a stable location
66 substituteInPlace Scripts/gen_moltenvk_rev_hdr.sh \
67 --replace-fail '$'''{BUILT_PRODUCTS_DIR}' "$NIX_BUILD_TOP/$sourceRoot/build/include" \
68 --replace-fail '$(git rev-parse HEAD)' ${finalAttrs.src.rev}
70 # Modify MoltenVK Xcode projects to build with xcbuild and dependencies from nixpkgs.
71 for proj in MoltenVK MoltenVKShaderConverter; do
72 # Remove xcframework dependencies from the Xcode projects. The basic format is:
73 # (children|files) = (
74 # DCFD7F822A45BDA0007BBBF7 /* SPIRVCross.xcframework in Frameworks */,
77 # This regex will only remove lines matching `xcframework` that are in these blocks
78 # to avoid accidentally corrupting the project.
79 sed -E -e '/(children|files) = /,/;/{/xcframework/d}' \
80 -i "$proj/$proj.xcodeproj/project.pbxproj"
81 # Ensure the namespace used is consistent with the spirv-cross package in nixpkgs.
82 substituteInPlace "$proj/$proj.xcodeproj/project.pbxproj" \
83 --replace-fail SPIRV_CROSS_NAMESPACE_OVERRIDE=MVK_spirv_cross SPIRV_CROSS_NAMESPACE_OVERRIDE=spirv_cross
85 substituteInPlace MoltenVKShaderConverter/MoltenVKShaderConverter.xcodeproj/project.pbxproj \
86 --replace-fail MetalGLShaderConverterTool MoltenVKShaderConverter \
87 --replace-fail MetalGLShaderConverter-macOS MoltenVKShaderConverter
89 # Don’t try to build `xcframework`s because `xcbuild` can’t build them.
90 sed -e '/xcframework/d' -i Scripts/package_all.sh
92 # Remove vendored dependency links.
93 find . -lname '*/External/*' -delete
95 # The library will be linked in the install phase regardless of version,
96 # so truncate it if it exists to avoid link failures.
97 test -f Scripts/create_dylib.sh && truncate --size 0 Scripts/create_dylib.sh
99 # Link glslang source because MoltenVK needs non-public headers to build.
100 mkdir -p build/include
101 ln -s "${glslang.src}" "build/include/glslang"
104 env.NIX_CFLAGS_COMPILE = toString (
106 "-isystem ${lib.getDev libcxx}/include/c++/v1"
107 "-I${lib.getDev spirv-cross}/include/spirv_cross"
108 "-I${lib.getDev spirv-headers}/include/spirv/unified1"
110 # MoltenVK prints a lot of verbose output to the console out of
111 # the box; we adjust this to match Homebrew’s default log level.
112 "-DMVK_CONFIG_LOG_LEVEL=MVK_CONFIG_LOG_LEVEL_NONE"
114 ++ lib.optional enablePrivateAPIUsage "-DMVK_USE_METAL_PRIVATE_API=1"
117 env.NIX_LDFLAGS = toString [
125 "-lspirv-cross-reflect"
129 NIX_CFLAGS_COMPILE+=" \
130 -I$NIX_BUILD_TOP/$sourceRoot/build/include \
131 -I$NIX_BUILD_TOP/$sourceRoot/Common"
138 "MoltenVKPackaging.xcodeproj"
140 "MoltenVK Package (macOS only)"
142 "generic/platform=macOS"
144 stdenv.hostPlatform.darwinArch
150 mkdir -p Package/Release/MoltenVK/static
151 cp Products/Release/libMoltenVK.a Package/Release/MoltenVK/static
155 # MoltenVK’s Xcode project builds the dylib, but it doesn’t seem to work with
156 # xcbuild. This is based on the script versions prior to 1.2.8 used.
157 mkdir -p Package/Release/MoltenVK/dynamic/dylib
158 clang++ -Wl,-all_load -Wl,-w \
160 -compatibility_version 1.0.0 -current_version 1.0.0 \
163 -framework CoreGraphics \
164 -framework Foundation \
166 -framework IOSurface \
168 -framework QuartzCore \
170 -lMoltenVKShaderConverter \
171 -lspirv-cross-reflect \
172 -install_name "$out/lib/libMoltenVK.dylib" \
173 -o Package/Release/MoltenVK/dynamic/dylib/libMoltenVK.dylib \
174 -force_load Products/Release/libMoltenVK.a
180 libraryExtension=${if enableStatic then ".a" else ".dylib"}
181 packagePath=${if enableStatic then "static" else "dynamic/dylib"}
183 mkdir -p "$out/lib" "$out/share/vulkan/icd.d" "$bin/bin" "$dev"
185 cp Package/Release/MoltenVKShaderConverter/Tools/MoltenVKShaderConverter "$bin/bin"
186 cp -r Package/Release/MoltenVK/include "$dev"
187 cp Package/Release/MoltenVK/$packagePath/libMoltenVK$libraryExtension "$out/lib"
189 # Install ICD definition for use with vulkan-loader.
190 install -m644 MoltenVK/icd/MoltenVK_icd.json \
191 "$out/share/vulkan/icd.d/MoltenVK_icd.json"
192 substituteInPlace "$out/share/vulkan/icd.d/MoltenVK_icd.json" \
193 --replace-fail ./libMoltenVK.dylib "$out/lib/libMoltenVK.dylib"
198 __structuredAttrs = true;
200 passthru.updateScript = gitUpdater {
202 ignoredVersions = ".*-(beta|rc).*";
206 description = "Vulkan Portability implementation built on top of Apple’s Metal API";
207 homepage = "https://github.com/KhronosGroup/MoltenVK";
208 changelog = "https://github.com/KhronosGroup/MoltenVK/releases";
209 maintainers = [ lib.maintainers.reckenrode ];
210 license = lib.licenses.asl20;
211 platforms = lib.platforms.darwin;