Skip to content

Commit 0327c07

Browse files
committed
refactor: consolidate in one place
1 parent bacecfc commit 0327c07

File tree

3 files changed

+37
-35
lines changed

3 files changed

+37
-35
lines changed

nix/build-haskell-package.nix

Lines changed: 34 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
}:
1313

1414
let
15-
mkNewStorePath = name: src:
15+
mkNewStorePath' = name: src:
1616
# Since 'src' may be a subdirectory of a store path
1717
# (in string form, which means that it isn't automatically
1818
# copied), the purpose of cleanSourceWith here is to create a
@@ -23,29 +23,38 @@ let
2323
inherit src;
2424
};
2525

26-
in
26+
# Avoid rebuilding because of changes in parent directories
27+
mkNewStorePath = name: src:
28+
let newSrc = mkNewStorePath' name src;
29+
in log.traceDebug "${name}.mkNewStorePath ${newSrc}" newSrc;
30+
31+
callCabal2nix = name: src:
32+
let pkg = self.callCabal2nix name src { };
33+
in log.traceDebug "${name}.callCabal2nix src=${src} deriver=${pkg.cabal2nixDeriver.outPath}" pkg;
2734

28-
name: root: cabal2NixFile:
29-
lib.pipe root
30-
[
31-
# Avoid rebuilding because of changes in parent directories
32-
(mkNewStorePath "source-${name}")
33-
(x: log.traceDebug "${name}.mkNewStorePath ${x.outPath}" x)
35+
# Use cached cabal2nix generated nix expression if present, otherwise use IFD (callCabal2nix)
36+
callCabal2NixUnlessCached = name: src: cabal2nixFile:
37+
let path = "${src}/${cabal2nixFile}";
38+
in
39+
if builtins.pathExists path
40+
then
41+
callPackage name path
42+
else
43+
callCabal2nix name src { };
44+
45+
callPackage = name: nixFilePath:
46+
let pkg = self.callPackage nixFilePath { };
47+
in log.traceDebug "${name}.callPackage[cabal2nix] ${nixFilePath}" pkg;
48+
49+
callHackage = name: version:
50+
let pkg = self.callHackage name version { };
51+
in log.traceDebug "${name}.callHackage ver=${version}" pkg;
52+
in
3453

35-
(root:
36-
let path = "${root}/${cabal2NixFile}";
37-
in
38-
# Check if cached cabal2nix generated nix expression is present,
39-
# if present use it with callPackage
40-
# to avoid IFD
41-
if builtins.pathExists path
42-
then
43-
(log.traceDebug "${name}.callPackage[cabal2nix] ${path}")
44-
(self.callPackage path { })
45-
else
46-
lib.pipe (self.callCabal2nix name root { })
47-
[
48-
(pkg: log.traceDebug "${name}.callCabal2nix root=${root} deriver=${pkg.cabal2nixDeriver.outPath}" pkg)
49-
]
50-
)
51-
]
54+
name: cfg:
55+
# If 'source' is a path, we treat it as such. Otherwise, we assume it's a version (from hackage).
56+
if lib.types.path.check cfg.source
57+
then
58+
callCabal2NixUnlessCached name (mkNewStorePath name cfg.source) cfg.cabal2NixFile
59+
else
60+
callHackage name cfg.source

nix/modules/project/packages/default.nix

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -58,15 +58,8 @@ in
5858
build-haskell-package = import ../../../build-haskell-package.nix {
5959
inherit pkgs lib self log;
6060
};
61-
getOrMkPackage = name: cfg:
62-
if lib.types.path.check cfg.source
63-
then
64-
(build-haskell-package name cfg.source cfg.cabal2NixFile)
65-
else
66-
log.traceDebug "${name}.callHackage ${cfg.source}"
67-
(self.callHackage name cfg.source { });
6861
in
69-
lib.mapAttrs getOrMkPackage project.config.packages;
62+
lib.mapAttrs build-haskell-package project.config.packages;
7063
};
7164
};
7265
}

nix/modules/project/packages/package.nix

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ in
2626
cabal2NixFile = lib.mkOption {
2727
type = lib.types.str;
2828
description = ''
29-
The Nix file which contains cached (pre-generated) `cabal2nix` expressions.
29+
Filename of the cabal2nix generated nix expression.
3030
31-
By default, it refers to `cabal.nix` file.
31+
This gets used if it exists instead of using IFD (callCabal2nix).
3232
'';
3333
default = "cabal.nix";
3434
};

0 commit comments

Comments
 (0)