Skip to content

Commit f04cab0

Browse files
committed
Automatically generate cache
1 parent f5d1f82 commit f04cab0

File tree

3 files changed

+55
-6
lines changed

3 files changed

+55
-6
lines changed

changelog.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
This file contains a summary of changes to Haskell.nix and `nix-tools`
22
that will impact users.
33

4+
## December 10, 2019
5+
* Now the `cache` attribute of `stackProject` is not required and is going to be deprecated.
6+
47
## November 18, 2019
58
* Changed the `cleanSourceHaskell` to accept an attrset of `src` and
69
(optional) `name` parameters. This allows you to keep the source

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: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,10 @@ self: super: {
300300
text = self.buildPackages.lib.concatMapStringsSep "\n" mkCacheLine repos;
301301
};
302302

303+
genStackCache = import ../lib/stack-cache-generator.nix {
304+
inherit (self.buildPackages) pkgs;
305+
};
306+
303307
mkCacheModule = cache:
304308
# for each item in the `cache`, set
305309
# packages.$name.src = fetchgit ...
@@ -315,8 +319,6 @@ self: super: {
315319
# src value.
316320
#
317321
# TODO: this should be moved into `call-stack-to-nix`
318-
# it should be automatic and not the burden of
319-
# the end user to work around nix peculiarities.
320322
{ packages =
321323
let
322324
repoToAttr = { name, url, rev, ref ? null, sha256 ? null, subdir ? null, is-private ? false, ... }: {
@@ -334,6 +336,7 @@ self: super: {
334336
cacheMap = builtins.map repoToAttr cache;
335337
in
336338
builtins.foldl' (x: y: x // y) {} cacheMap;
339+
337340
};
338341

339342
# Takes a haskell src directory runs cabal new-configure and plan-to-nix.
@@ -408,13 +411,21 @@ self: super: {
408411

409412
stackProject' =
410413
{ ... }@args:
411-
let stack = importAndFilterProject (callStackToNix args);
414+
let stack = importAndFilterProject (callStackToNix ({ inherit cache; } // args));
415+
cache = if args ? cache
416+
then builtins.trace
417+
"warning: passing `cache' to `stackProject' is deprecated. See #335 #358"
418+
args.cache
419+
else genStackCache {
420+
inherit (args) src;
421+
stackYaml = args.stackYaml or "stack.yaml";
422+
};
412423
in let pkg-set = mkStackPkgSet
413424
{ stack-pkgs = stack.pkgs;
414425
pkg-def-extras = (args.pkg-def-extras or []);
415-
modules = (args.modules or [])
416-
++ self.lib.optional (args ? ghc) { ghc.package = args.ghc; }
417-
++ self.lib.optional (args ? cache) (mkCacheModule args.cache);
426+
modules = self.lib.singleton (mkCacheModule cache)
427+
++ (args.modules or [])
428+
++ self.lib.optional (args ? ghc) { ghc.package = args.ghc; };
418429
};
419430
in { inherit (pkg-set.config) hsPkgs; stack-nix = stack.nix; };
420431

0 commit comments

Comments
 (0)