1
0

flake.nix 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. # The flake interface to llama.cpp's Nix expressions. The flake is used as a
  2. # more discoverable entry-point, as well as a way to pin the dependencies and
  3. # expose default outputs, including the outputs built by the CI.
  4. # For more serious applications involving some kind of customization you may
  5. # want to consider consuming the overlay, or instantiating `llamaPackages`
  6. # directly:
  7. #
  8. # ```nix
  9. # pkgs.callPackage ${llama-cpp-root}/.devops/nix/scope.nix { }`
  10. # ```
  11. # Cf. https://jade.fyi/blog/flakes-arent-real/ for a more detailed exposition
  12. # of the relation between Nix and the Nix Flakes.
  13. {
  14. description = "Port of Facebook's LLaMA model in C/C++";
  15. inputs = {
  16. nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
  17. flake-parts.url = "github:hercules-ci/flake-parts";
  18. };
  19. # There's an optional binary cache available. The details are below, but they're commented out.
  20. #
  21. # Why? The terrible experience of being prompted to accept them on every single Nix command run.
  22. # Plus, there are warnings shown about not being a trusted user on a default Nix install
  23. # if you *do* say yes to the prompts.
  24. #
  25. # This experience makes having `nixConfig` in a flake a persistent UX problem.
  26. #
  27. # To make use of the binary cache, please add the relevant settings to your `nix.conf`.
  28. # It's located at `/etc/nix/nix.conf` on non-NixOS systems. On NixOS, adjust the `nix.settings`
  29. # option in your NixOS configuration to add `extra-substituters` and `extra-trusted-public-keys`,
  30. # as shown below.
  31. #
  32. # ```
  33. # nixConfig = {
  34. # extra-substituters = [
  35. # # A development cache for nixpkgs imported with `config.cudaSupport = true`.
  36. # # Populated by https://hercules-ci.com/github/SomeoneSerge/nixpkgs-cuda-ci.
  37. # # This lets one skip building e.g. the CUDA-enabled openmpi.
  38. # # TODO: Replace once nix-community obtains an official one.
  39. # "https://cuda-maintainers.cachix.org"
  40. # ];
  41. #
  42. # # Verify these are the same keys as published on
  43. # # - https://app.cachix.org/cache/cuda-maintainers
  44. # extra-trusted-public-keys = [
  45. # "cuda-maintainers.cachix.org-1:0dq3bujKpuEPMCX6U4WylrUDZ9JyUG0VpVZa7CNfq5E="
  46. # ];
  47. # };
  48. # ```
  49. # For inspection, use `nix flake show github:ggml-org/llama.cpp` or the nix repl:
  50. #
  51. # ```bash
  52. # ❯ nix repl
  53. # nix-repl> :lf github:ggml-org/llama.cpp
  54. # Added 13 variables.
  55. # nix-repl> outputs.apps.x86_64-linux.quantize
  56. # { program = "/nix/store/00000000000000000000000000000000-llama.cpp/bin/llama-quantize"; type = "app"; }
  57. # ```
  58. outputs =
  59. { self, flake-parts, ... }@inputs:
  60. let
  61. # We could include the git revisions in the package names but those would
  62. # needlessly trigger rebuilds:
  63. # llamaVersion = self.dirtyShortRev or self.shortRev;
  64. # Nix already uses cryptographic hashes for versioning, so we'll just fix
  65. # the fake semver for now:
  66. llamaVersion = "0.0.0";
  67. in
  68. flake-parts.lib.mkFlake { inherit inputs; }
  69. {
  70. imports = [
  71. .devops/nix/nixpkgs-instances.nix
  72. .devops/nix/apps.nix
  73. .devops/nix/devshells.nix
  74. .devops/nix/jetson-support.nix
  75. ];
  76. # An overlay can be used to have a more granular control over llama-cpp's
  77. # dependencies and configuration, than that offered by the `.override`
  78. # mechanism. Cf. https://nixos.org/manual/nixpkgs/stable/#chap-overlays.
  79. #
  80. # E.g. in a flake:
  81. # ```
  82. # { nixpkgs, llama-cpp, ... }:
  83. # let pkgs = import nixpkgs {
  84. # overlays = [ (llama-cpp.overlays.default) ];
  85. # system = "aarch64-linux";
  86. # config.allowUnfree = true;
  87. # config.cudaSupport = true;
  88. # config.cudaCapabilities = [ "7.2" ];
  89. # config.cudaEnableForwardCompat = false;
  90. # }; in {
  91. # packages.aarch64-linux.llamaJetsonXavier = pkgs.llamaPackages.llama-cpp;
  92. # }
  93. # ```
  94. #
  95. # Cf. https://nixos.org/manual/nix/unstable/command-ref/new-cli/nix3-flake.html?highlight=flake#flake-format
  96. flake.overlays.default = (
  97. final: prev: {
  98. llamaPackages = final.callPackage .devops/nix/scope.nix { inherit llamaVersion; };
  99. inherit (final.llamaPackages) llama-cpp;
  100. }
  101. );
  102. systems = [
  103. "aarch64-darwin"
  104. "aarch64-linux"
  105. "x86_64-darwin" # x86_64-darwin isn't tested (and likely isn't relevant)
  106. "x86_64-linux"
  107. ];
  108. perSystem =
  109. {
  110. config,
  111. lib,
  112. system,
  113. pkgs,
  114. pkgsCuda,
  115. pkgsRocm,
  116. ...
  117. }:
  118. {
  119. # For standardised reproducible formatting with `nix fmt`
  120. formatter = pkgs.nixfmt-rfc-style;
  121. # Unlike `.#packages`, legacyPackages may contain values of
  122. # arbitrary types (including nested attrsets) and may even throw
  123. # exceptions. This attribute isn't recursed into by `nix flake
  124. # show` either.
  125. #
  126. # You can add arbitrary scripts to `.devops/nix/scope.nix` and
  127. # access them as `nix build .#llamaPackages.${scriptName}` using
  128. # the same path you would with an overlay.
  129. legacyPackages = {
  130. llamaPackages = pkgs.callPackage .devops/nix/scope.nix { inherit llamaVersion; };
  131. llamaPackagesWindows = pkgs.pkgsCross.mingwW64.callPackage .devops/nix/scope.nix {
  132. inherit llamaVersion;
  133. };
  134. llamaPackagesCuda = pkgsCuda.callPackage .devops/nix/scope.nix { inherit llamaVersion; };
  135. llamaPackagesRocm = pkgsRocm.callPackage .devops/nix/scope.nix { inherit llamaVersion; };
  136. };
  137. # We don't use the overlay here so as to avoid making too many instances of nixpkgs,
  138. # cf. https://zimbatm.com/notes/1000-instances-of-nixpkgs
  139. packages =
  140. {
  141. default = config.legacyPackages.llamaPackages.llama-cpp;
  142. vulkan = config.packages.default.override { useVulkan = true; };
  143. windows = config.legacyPackages.llamaPackagesWindows.llama-cpp;
  144. python-scripts = config.legacyPackages.llamaPackages.python-scripts;
  145. }
  146. // lib.optionalAttrs pkgs.stdenv.isLinux {
  147. cuda = config.legacyPackages.llamaPackagesCuda.llama-cpp;
  148. mpi-cpu = config.packages.default.override { useMpi = true; };
  149. mpi-cuda = config.packages.default.override { useMpi = true; };
  150. }
  151. // lib.optionalAttrs (system == "x86_64-linux") {
  152. rocm = config.legacyPackages.llamaPackagesRocm.llama-cpp;
  153. };
  154. # Packages exposed in `.#checks` will be built by the CI and by
  155. # `nix flake check`.
  156. #
  157. # We could test all outputs e.g. as `checks = confg.packages`.
  158. #
  159. # TODO: Build more once https://github.com/ggml-org/llama.cpp/issues/6346 has been addressed
  160. checks = {
  161. inherit (config.packages) default vulkan;
  162. };
  163. };
  164. };
  165. }