Skip to content

Commit 8403bf2

Browse files
committed
flake.nix: avoid re-evaluating nixpkgs too many times
1 parent 6157b36 commit 8403bf2

File tree

3 files changed

+38
-22
lines changed

3 files changed

+38
-22
lines changed

.devops/nix/overlay.nix

Lines changed: 0 additions & 5 deletions
This file was deleted.

.devops/nix/scope.nix

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{ lib, newScope }:
2+
3+
lib.makeScope newScope (self: { llama-cpp = self.callPackage ./package.nix { }; })

flake.nix

Lines changed: 35 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,23 +17,42 @@
1717
in
1818

1919
{
20-
# These define the various ways to build the llama.cpp project.
21-
# Integrate them into your flake.nix configuration by adding this overlay to nixpkgs.overlays.
22-
overlays.default = import ./.devops/nix/overlay.nix;
20+
# An overlay can be used to have a more granular control over llama-cpp's
21+
# dependencies and configuration, than that offered by the `.override`
22+
# mechanism. Cf. https://nixos.org/manual/nixpkgs/stable/#chap-overlays.
23+
#
24+
# E.g. in a flake:
25+
# ```
26+
# { nixpkgs, llama-cpp, ... }:
27+
# let pkgs = import nixpkgs {
28+
# overlays = [ (llama-cpp.overlays.default) ];
29+
# system = "aarch64-linux";
30+
# config.allowUnfree = true;
31+
# config.cudaSupport = true;
32+
# config.cudaCapabilities = [ "7.2" ];
33+
# config.cudaEnableForwardCompat = false;
34+
# }; in {
35+
# packages.aarch64-linux.llamaJetsonXavier = pkgs.llamaPackages.llama-cpp;
36+
# }
37+
# ```
38+
#
39+
# Cf. https://nixos.org/manual/nix/unstable/command-ref/new-cli/nix3-flake.html?highlight=flake#flake-format
40+
overlays.default = (final: prev: { llamaPackages = final.callPackage .devops/nix/scope.nix { }; });
2341

2442
# These use the package definition from `./.devops/nix/package.nix`.
2543
# There's one per backend that llama-cpp uses. Add more as needed!
2644
packages = eachSystem (
2745
system:
2846
let
29-
defaultConfig = {
47+
# Avoid re-evaluation for the nixpkgs instance,
48+
# cf. https://zimbatm.com/notes/1000-instances-of-nixpkgs
49+
pkgs = nixpkgs.legacyPackages.${system};
50+
51+
# Ensure dependencies use CUDA consistently (e.g. that openmpi, ucc,
52+
# and ucx are built with CUDA support)
53+
pkgsCuda = import nixpkgs {
3054
inherit system;
31-
overlays = [ self.overlays.default ];
32-
};
33-
pkgs = import nixpkgs defaultConfig;
3455

35-
# Let's not make a big deal about getting the CUDA bits.
36-
cudaConfig = defaultConfig // {
3756
config.cudaSupport = true;
3857
config.allowUnfreePredicate =
3958
p:
@@ -48,19 +67,18 @@
4867
)
4968
(p.meta.licenses or [ p.meta.license ]);
5069
};
51-
pkgsCuda = import nixpkgs cudaConfig;
5270

53-
# Let's make sure to turn on ROCm support across the whole package ecosystem.
54-
rocmConfig = defaultConfig // {
71+
# Ensure dependencies use ROCm consistently
72+
pkgsRocm = import nixpkgs {
73+
inherit system;
5574
config.rocmSupport = true;
5675
};
57-
pkgsRocm = import nixpkgs rocmConfig;
5876
in
5977
{
60-
default = pkgs.llama-cpp;
61-
opencl = pkgs.llama-cpp.override { useOpenCL = true; };
62-
cuda = pkgsCuda.llama-cpp;
63-
rocm = pkgsRocm.llama-cpp;
78+
default = (pkgs.callPackage .devops/nix/scope.nix { }).llama-cpp;
79+
opencl = self.packages.${system}.default.override { useOpenCL = true; };
80+
cuda = (pkgsCuda.callPackage .devops/nix/scope.nix { }).llama-cpp;
81+
rocm = (pkgsRocm.callPackage .devops/nix/scope.nix { }).llama-cpp;
6482
}
6583
);
6684

0 commit comments

Comments
 (0)