flake.nix 2.5 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) 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. mkdir -p $out/bin
  47. mv bin/* $out/bin/
  48. mv $out/bin/main $out/bin/llama
  49. mv $out/bin/server $out/bin/llama-server
  50. echo "#!${llama-python}/bin/python" > $out/bin/convert.py
  51. cat ${./convert.py} >> $out/bin/convert.py
  52. chmod +x $out/bin/convert.py
  53. '';
  54. meta.mainProgram = "llama";
  55. };
  56. apps.llama-server = {
  57. type = "app";
  58. program = "${self.packages.${system}.default}/bin/llama-server";
  59. };
  60. apps.llama-embedding = {
  61. type = "app";
  62. program = "${self.packages.${system}.default}/bin/embedding";
  63. };
  64. apps.llama = {
  65. type = "app";
  66. program = "${self.packages.${system}.default}/bin/llama";
  67. };
  68. apps.default = self.apps.${system}.llama;
  69. devShells.default = pkgs.mkShell {
  70. packages = with pkgs; [ cmake llama-python ] ++ osSpecific;
  71. };
  72. });
  73. }