{ lib, stdenv, buildPythonPackage, common-updater-scripts, curl, fetchFromGitHub, nix, nix-update, nanobind_3, perl, writeShellApplication, # build-system cmake, setuptools, typing-extensions, # buildInputs fmt, nlohmann_json, # linux-only openblasCompat, # tests callPackage, numpy, pytestCheckHook, python, sysctl, }: let # static dependencies included directly during compilation gguf-tools = fetchFromGitHub { owner = "antirez"; repo = "gguf-tools"; rev = "8fa6eb65236618e28fd7710a0fba565f7faa1848"; hash = "sha256-15FvyPOFqTOr5vdWQoPnZz+mYH919++EtghjozDlnSA="; }; in buildPythonPackage (finalAttrs: { pname = "mlx"; version = "0.32.4"; pyproject = true; __structuredAttrs = true; src = fetchFromGitHub { owner = "ml-explore"; repo = "mlx"; tag = "v${finalAttrs.version}"; hash = "sha256-Bs8c1+GvJuUsrB/KrW6Sjfe8noS8P+M7dwBQY0XJtaU="; }; patches = [ # Use nix packages instead of fetching their sources ./dont-fetch-json.patch ]; postPatch = '' substituteInPlace mlx/backend/cpu/jit_compiler.cpp \ --replace-fail "g++" "${lib.getExe' stdenv.cc "c++"}" ''; dontUseCmakeConfigure = true; enableParallelBuilding = true; # Allows multiple cores to be used in Python builds. postUnpack = '' export MAKEFLAGS+="''${enableParallelBuilding:+-j$NIX_BUILD_CORES}" ''; env = { PYPI_RELEASE = 1; CMAKE_ARGS = toString [ # NOTE The `metal` command-line utility used to build the Metal kernels is not open-source. # To build mlx with Metal support in Nix, you'd need to use one of the sandbox escape # hatches which let you interact with a native install of Xcode, such as `composeXcodeWrapper` # or by changing the upstream (e.g., https://github.com/zed-industries/zed/discussions/7016). (lib.cmakeBool "MLX_BUILD_METAL" false) (lib.cmakeBool "USE_SYSTEM_FMT" true) (lib.cmakeOptionType "filepath" "FETCHCONTENT_SOURCE_DIR_GGUFLIB" "${gguf-tools}") (lib.cmakeOptionType "filepath" "FETCHCONTENT_SOURCE_DIR_NANOBIND" "${nanobind_3.src}") (lib.cmakeFeature "CMAKE_CXX_FLAGS" "-I${lib.getDev nlohmann_json}/include/nlohmann") ]; }; build-system = [ cmake setuptools typing-extensions ]; buildInputs = [ fmt nlohmann_json ] ++ lib.optionals stdenv.hostPlatform.isLinux [ # MLX passes 32-bit integers to LAPACK. openblasCompat ]; pythonImportsCheck = [ "mlx" ]; # Run the mlx Python test suite. nativeCheckInputs = [ numpy pytestCheckHook sysctl ]; enabledTestPaths = [ "python/tests/" ]; disabledTests = lib.optionals stdenv.hostPlatform.isDarwin [ # M1 Accelerate bug, to be fixed in macOS: # https://github.com/ml-explore/mlx/issues/3200 # https://github.com/ml-explore/mlx/pull/3563#issuecomment-4784288696 "test_gather_qmm_sorted" ] ++ lib.optionals (stdenv.hostPlatform.isLinux && stdenv.hostPlatform.isx86_64) [ # AssertionError: 1.049041748046875e-05 not less than 1e-05 "test_1bit_quantize_dequantize" ]; # patchelf is only available on Linux and no patching is needed on darwin. # Otherwise mlx/core.cpython-313-x86_64-linux-gnu.so contains a reference to # /build/source/build/temp.linux-x86_64-cpython-313/mlx.core/libmlx.so in its rpath. postInstall = lib.optionalString stdenv.hostPlatform.isLinux '' patchelf --replace-needed \ libmlx.so \ $out/${python.sitePackages}/mlx/lib64/libmlx.so \ $out/${python.sitePackages}/mlx/core.cpython-*.so ''; # Additional testing by executing the example Python scripts supplied with mlx # using the version of the library we've built. passthru = { inherit gguf-tools; tests.mlxTest = (callPackage ./tests.nix { mlx = finalAttrs.finalPackage; inherit (finalAttrs) src; }).mlxTest; updateScript = lib.getExe (writeShellApplication { name = "mlx-update"; runtimeInputs = [ common-updater-scripts curl nix nix-update perl ]; text = builtins.readFile ./update.sh; }); }; meta = { homepage = "https://github.com/ml-explore/mlx"; description = "Array framework for Apple silicon"; changelog = "https://github.com/ml-explore/mlx/releases/tag/${finalAttrs.src.tag}"; license = lib.licenses.mit; maintainers = with lib.maintainers; [ booxter cameronyule viraptor ]; }; })