Skip to content

Commit d59f32c

Browse files
committed
Automatically generate cache
1 parent f3f6cce commit d59f32c

File tree

2 files changed

+51
-4
lines changed

2 files changed

+51
-4
lines changed

lib/stack-cache-generator.nix

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Generate cache entries for dependencies of package defined in `src`
2+
3+
{ pkgs }:
4+
{ src, stackYaml ? "stack.yaml" }:
5+
let
6+
s2n = import "${pkgs.fetchFromGitHub {
7+
owner = "serokell";
8+
repo = "stack-to-nix";
9+
rev = "28e690d3eddd47c59982c7fbf4f950320ff7ff69";
10+
sha256 = "1xnx5baj3k29iy8ccppn28ayf4483zddrvq6fikfpvblzp5zrnaj";
11+
}}/lib.nix" pkgs;
12+
13+
deps = (s2n.importYAML "${src}/${stackYaml}").extra-deps or [ ];
14+
hashPath = path:
15+
builtins.readFile (pkgs.runCommand "hash-path" { preferLocalBuild = true; }
16+
"echo -n $(${pkgs.nix}/bin/nix-hash --type sha256 --base32 ${path}) > $out");
17+
in with pkgs.lib;
18+
concatMap (dep:
19+
if !builtins.isAttrs dep then
20+
[ ]
21+
else
22+
let
23+
pkgsrc = builtins.fetchGit {
24+
url = dep.git;
25+
ref = "*";
26+
rev = dep.commit;
27+
};
28+
in map (subdir:
29+
{
30+
name = s2n.cabalPackageName "${pkgsrc}/${subdir}";
31+
rev = dep.commit;
32+
url = dep.git;
33+
sha256 = hashPath pkgsrc;
34+
} // (optionalAttrs (subdir != "") { inherit subdir; }))
35+
(dep.subdirs or [ "" ])) deps

overlays/haskell.nix

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,10 @@ self: super: {
240240
text = self.buildPackages.lib.concatMapStringsSep "\n" f repos;
241241
};
242242

243+
genStackCache = import ../lib/stack-cache-generator.nix {
244+
inherit (self.buildPackages) pkgs;
245+
};
246+
243247
mkCacheModule = cache:
244248
# for each item in the `cache`, set
245249
# packages.$name.src = fetchgit ...
@@ -337,13 +341,21 @@ self: super: {
337341

338342
stackProject' =
339343
{ ... }@args:
340-
let stack = importAndFilterProject (callStackToNix args);
344+
let stack = importAndFilterProject (callStackToNix ({ inherit cache; } // args));
345+
cache = if args ? cache
346+
then builtins.trace
347+
"warning: passing `cache' to `stackProject' is deprecated. See #335 #358"
348+
args.cache
349+
else genStackCache {
350+
inherit (args) src;
351+
stackYaml = args.stackYaml or "stack.yaml";
352+
};
341353
in let pkg-set = mkStackPkgSet
342354
{ stack-pkgs = stack.pkgs;
343355
pkg-def-extras = (args.pkg-def-extras or []);
344-
modules = (args.modules or [])
345-
++ self.lib.optional (args ? ghc) { ghc.package = args.ghc; }
346-
++ self.lib.optional (args ? cache) (mkCacheModule args.cache);
356+
modules = self.lib.singleton (mkCacheModule cache)
357+
++ (args.modules or [])
358+
++ self.lib.optional (args ? ghc) { ghc.package = args.ghc; };
347359
};
348360
in { inherit (pkg-set.config) hsPkgs; stack-nix = stack.nix; };
349361

0 commit comments

Comments
 (0)