flake.nix 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. {
  2. inputs = {
  3. nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
  4. flake-utils.url = "github:numtide/flake-utils";
  5. };
  6. outputs = { self, nixpkgs, flake-utils }:
  7. flake-utils.lib.eachDefaultSystem (system:
  8. let
  9. inherit (pkgs.stdenv) isAarch32 isAarch64 isDarwin;
  10. osSpecific = with pkgs; [ openmpi ] ++
  11. (
  12. if isAarch64 && isDarwin then
  13. with pkgs.darwin.apple_sdk_11_0.frameworks; [
  14. Accelerate
  15. MetalKit
  16. MetalPerformanceShaders
  17. MetalPerformanceShadersGraph
  18. ]
  19. else if isAarch32 && isDarwin then
  20. with pkgs.darwin.apple_sdk.frameworks; [
  21. Accelerate
  22. CoreGraphics
  23. CoreVideo
  24. ]
  25. else
  26. with pkgs; [ openblas ]
  27. );
  28. pkgs = import nixpkgs { inherit system; };
  29. nativeBuildInputs = with pkgs; [ cmake pkgconfig ];
  30. llama-python =
  31. pkgs.python3.withPackages (ps: with ps; [ numpy sentencepiece ]);
  32. in {
  33. packages.default = pkgs.stdenv.mkDerivation {
  34. name = "llama.cpp";
  35. src = ./.;
  36. postPatch = ''
  37. substituteInPlace ./ggml-metal.m \
  38. --replace '[bundle pathForResource:@"ggml-metal" ofType:@"metal"];' "@\"$out/bin/ggml-metal.metal\";"
  39. substituteInPlace ./*.py --replace '/usr/bin/env python' '${llama-python}/bin/python'
  40. '';
  41. nativeBuildInputs = nativeBuildInputs;
  42. buildInputs = osSpecific;
  43. cmakeFlags = [ "-DLLAMA_BUILD_SERVER=ON" "-DLLAMA_MPI=ON" "-DBUILD_SHARED_LIBS=ON" "-DCMAKE_SKIP_BUILD_RPATH=ON" ]
  44. ++ (if isAarch64 && isDarwin then [
  45. "-DCMAKE_C_FLAGS=-D__ARM_FEATURE_DOTPROD=1"
  46. "-DLLAMA_METAL=ON"
  47. ] else [
  48. "-DLLAMA_BLAS=ON"
  49. "-DLLAMA_BLAS_VENDOR=OpenBLAS"
  50. ]);
  51. postInstall = ''
  52. mv $out/bin/main $out/bin/llama
  53. mv $out/bin/server $out/bin/llama-server
  54. '';
  55. meta.mainProgram = "llama";
  56. };
  57. apps.llama-server = {
  58. type = "app";
  59. program = "${self.packages.${system}.default}/bin/llama-server";
  60. };
  61. apps.llama-embedding = {
  62. type = "app";
  63. program = "${self.packages.${system}.default}/bin/embedding";
  64. };
  65. apps.llama = {
  66. type = "app";
  67. program = "${self.packages.${system}.default}/bin/llama";
  68. };
  69. apps.default = self.apps.${system}.llama;
  70. devShells.default = pkgs.mkShell {
  71. packages = nativeBuildInputs ++ osSpecific;
  72. };
  73. });
  74. }