on 06a67e6 help sign in
haskell.nix/lib/stack-cache-generator.nix
102 lines 3.9 kB history raw
1# Generate cache entries for dependencies of package defined in `src`
3{ pkgs, haskellLib }:
4{ src
5, stackYaml ? "stack.yaml"
6, sha256map ? null
7 # An alternative to adding `# nix-sha256:` comments into the
8 # stack.yaml file:
9 # sha256map =
10 # { "https://github.com/jgm/pandoc-citeproc"."0.17"
11 # = "0dxx8cp2xndpw3jwiawch2dkrkp15mil7pyx7dvd810pwc22pm2q"; };
12, lookupSha256 ?
13 if sha256map != null
14 then { location, tag, ...}: sha256map."${location}"."${tag}"
15 else _: null
16, resolverSha256 ? null
17, nix-tools ? pkgs.haskell-nix.internal-nix-tools # When building stack projects we use the internal nix-tools (compiled with a fixed GHC version)
18, ...
19}:
20let
21 # We only care about the stackYaml file. If src is a local directory
22 # we want to avoid recalculating the cache unless the stack.yaml file
23 # changes.
25 inherit (haskellLib.fetchResolver {
26 inherit src stackYaml resolverSha256;
27 }) resolver fetchedResolver;
29 # Filter just the stack yaml file and any resolver yaml file it points to.
30 maybeCleanedSource =
31 if haskellLib.canCleanSource src
32 then haskellLib.cleanSourceWith {
33 inherit src;
34 filter = path: type:
35 let
36 origSrc = if src ? _isLibCleanSourceWith then src.origSrc else src;
37 origSubDir = if src ? _isLibCleanSourceWithEx then src.origSubDir else "";
38 relPath = pkgs.lib.removePrefix (toString origSrc + origSubDir + "/") path;
40 # checks if path1 is a parent directory for path2
41 isParent = path1: path2: pkgs.lib.hasPrefix "${path1}/" path2;
43 in
44 (relPath == stackYaml)
45 || (resolver != null && (relPath == resolver || isParent relPath resolver))
46 ;
47 }
48 else src;
50 # All repos served via ssh or git protocols are usually private
51 private = url: pkgs.lib.substring 0 4 url != "http";
53 repos = builtins.fromJSON (builtins.readFile (pkgs.runCommand "stack-repos" {
54 buildInputs = [ nix-tools ];
55 } ''
56 TMP=$(mktemp -d)
57 cd $TMP
58 cp -r "${maybeCleanedSource}/." $TMP
59 chmod -R +w $TMP
60 substituteInPlace ${stackYaml} --replace "# nix-sha256:" "nix-sha256:"
61 ${pkgs.lib.optionalString (fetchedResolver != null) ''
62 substituteInPlace ${stackYaml} --replace "${resolver}" "${fetchedResolver}"
63 ''}
64 stack-repos --stack-yaml ${stackYaml}
65 cp repos.json $out
66 ''));
68 cabalName = path: builtins.readFile (pkgs.runCommand "cabal-name" {
69 buildInputs = [ nix-tools ];
70 } ''
71 cabal-name ${path} > $out
72 '');
74 hashPath = path:
75 builtins.readFile (pkgs.runCommand "hash-path" { preferLocalBuild = true; }
76 "echo -n $(${pkgs.nix}/bin/nix-hash --type sha256 --base32 ${path}) > $out");
77in with pkgs.lib;
78concatMap (dep:
79 let
80 is-private = private dep.url;
81 sha256 = if dep.sha256 != null
82 then dep.sha256
83 else lookupSha256 {
84 location = dep.url;
85 tag = dep.rev;
86 };
87 pkgsrc =
88 if !is-private && sha256 != null
89 then pkgs.fetchgit {
90 inherit (dep) url rev;
91 inherit sha256;
92 }
93 else builtins.fetchGit {
94 inherit (dep) url rev;
95 };
96 in map (subdir: {
97 name = cabalName "${pkgsrc}/${subdir}";
98 inherit (dep) url rev;
99 inherit is-private;
100 sha256 = if !is-private then hashPath pkgsrc else null;
101 } // (optionalAttrs (subdir != "") { inherit subdir; }))
102 (dep.subdirs or [ "" ])) repos