flake.nix 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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) isAarch64 isDarwin;
  10. inherit (pkgs.lib) optionals;
  11. isM1 = isAarch64 && isDarwin;
  12. osSpecific = if isM1 then
  13. with pkgs.darwin.apple_sdk_11_0.frameworks; [
  14. Accelerate
  15. MetalKit
  16. MetalPerformanceShaders
  17. MetalPerformanceShadersGraph
  18. ]
  19. else if isDarwin then
  20. with pkgs.darwin.apple_sdk.frameworks; [
  21. Accelerate
  22. CoreGraphics
  23. CoreVideo
  24. ]
  25. else
  26. [ ];
  27. pkgs = import nixpkgs { inherit system; };
  28. llama-python =
  29. pkgs.python310.withPackages (ps: with ps; [ numpy sentencepiece ]);
  30. in {
  31. packages.default = pkgs.stdenv.mkDerivation {
  32. name = "llama.cpp";
  33. src = ./.;
  34. postPatch = if isM1 then ''
  35. substituteInPlace ./ggml-metal.m \
  36. --replace '[bundle pathForResource:@"ggml-metal" ofType:@"metal"];' "@\"$out/bin/ggml-metal.metal\";"
  37. '' else
  38. "";
  39. nativeBuildInputs = with pkgs; [ cmake ];
  40. buildInputs = osSpecific;
  41. cmakeFlags = [ "-DLLAMA_BUILD_SERVER=ON" ] ++ (optionals isM1 [
  42. "-DCMAKE_C_FLAGS=-D__ARM_FEATURE_DOTPROD=1"
  43. "-DLLAMA_METAL=ON"
  44. ]);
  45. installPhase = ''
  46. runHook preInstall
  47. mkdir -p $out/bin
  48. mv bin/* $out/bin/
  49. mv $out/bin/main $out/bin/llama
  50. mv $out/bin/server $out/bin/llama-server
  51. echo "#!${llama-python}/bin/python" > $out/bin/convert.py
  52. cat ${./convert.py} >> $out/bin/convert.py
  53. chmod +x $out/bin/convert.py
  54. runHook postInstall
  55. '';
  56. meta.mainProgram = "llama";
  57. };
  58. apps.llama-server = {
  59. type = "app";
  60. program = "${self.packages.${system}.default}/bin/llama-server";
  61. };
  62. apps.llama-embedding = {
  63. type = "app";
  64. program = "${self.packages.${system}.default}/bin/embedding";
  65. };
  66. apps.llama = {
  67. type = "app";
  68. program = "${self.packages.${system}.default}/bin/llama";
  69. };
  70. apps.default = self.apps.${system}.llama;
  71. devShells.default = pkgs.mkShell {
  72. packages = with pkgs; [ cmake llama-python ] ++ osSpecific;
  73. };
  74. });
  75. }