on e94de40 help sign in
haskell.nix/lib/import-and-filter-project.nix
47 lines 2.4 kB history raw
1# This function takes the output of `callCabalProjectToNix` or
2# `callStackToNix`. It imports the projectNix that was produced
3# and combines it with the original source.
4{ pkgs, haskellLib }:
5{ projectNix, sourceRepos, src }:
6let
7 # Full source including possible relative paths form the
8 # project directory.
9 srcRoot =
10 if haskellLib.canCleanSource src
11 then haskellLib.cleanSourceWith {
12 name = if src ? name then "${src.name}-root" else "source-root";
13 src = src.origSrc or src;
14 filter = src.filter or (_: _: true);
15 }
16 else src.origSrc or src;
17 # The sub directory containing the cabal.project or stack.yaml file
18 projectSubDir' = src.origSubDir or ""; # With leading /
19 projectSubDir = pkgs.lib.strings.removePrefix "/" projectSubDir'; # Without /
20 projectSubDir'' = if projectSubDir == "" then "" else projectSubDir + "/"; # With trailing /
21 project = import "${projectNix}${projectSubDir'}";
22in project // {
23 extras = hackage: let old = (project.extras hackage).packages; in {
24 packages = pkgs.lib.attrsets.mapAttrs (name: value:
25 if builtins.isFunction value
26 then value
27 else {...}@args:
28 let oldPkg = import value args;
29 packageSrc = if !pkgs.lib.strings.hasPrefix (toString projectNix) (toString oldPkg.src.content)
30 then toString oldPkg.src.content
31 else let
32 subDir = pkgs.lib.strings.removePrefix "/" (
33 pkgs.lib.strings.removePrefix (toString projectNix)
34 (toString oldPkg.src.content));
35 srcRepoPrefix = projectSubDir'' + ".source-repository-packages/";
36 in if pkgs.lib.strings.hasPrefix srcRepoPrefix subDir
37 then
38 pkgs.lib.lists.elemAt sourceRepos (
39 pkgs.lib.strings.toInt (pkgs.lib.strings.removePrefix srcRepoPrefix subDir))
40 else if haskellLib.canCleanSource srcRoot
41 then haskellLib.cleanSourceWith { src = srcRoot; inherit subDir; }
42 else srcRoot + (if subDir == "" then "" else "/" + subDir);
43 in oldPkg // {
44 src = (pkgs.lib).mkDefault packageSrc;
45 }) old;
46 };
47 }