flake.nix 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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 '[[NSBundle mainBundle] 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. devShells.default = pkgs.mkShell {
  51. packages = with pkgs; [
  52. cmake
  53. llama-python
  54. ] ++ osSpecific;
  55. };
  56. }
  57. );
  58. }