flake.nix 2.4 KB

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