Skip to content

Commit

Permalink
refactor: consolidate in one place
Browse files Browse the repository at this point in the history
  • Loading branch information
srid committed Dec 23, 2024
1 parent bacecfc commit f47c91a
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 29 deletions.
51 changes: 30 additions & 21 deletions nix/build-haskell-package.nix
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
}:

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

in

name: root: cabal2NixFile:
lib.pipe root
[
# Avoid rebuilding because of changes in parent directories
(mkNewStorePath "source-${name}")
(x: log.traceDebug "${name}.mkNewStorePath ${x.outPath}" x)
mkNewStorePath = name: src:
let newSrc = mkNewStorePath' name src;
in log.traceDebug "${name}.mkNewStorePath ${newSrc}" newSrc;

(root:
let path = "${root}/${cabal2NixFile}";
callCabal2nix = name: src:
let pkg = self.callCabal2nix name src { };
in log.traceDebug "${name}.callCabal2nix src=${src} deriver=${pkg.cabal2nixDeriver.outPath}" pkg;

# Use cached cabal2nix generated nix expression if present, otherwise use IFD (callCabal2nix)
callCabal2NixUnlessCached = name: src: cabal2nixFile:
let path = "${src}/${cabal2nixFile}";
in
# Check if cached cabal2nix generated nix expression is present,
# if present use it with callPackage
# to avoid IFD
if builtins.pathExists path
then
(log.traceDebug "${name}.callPackage[cabal2nix] ${path}")
(self.callPackage path { })
callPackage name path
else
lib.pipe (self.callCabal2nix name root { })
[
(pkg: log.traceDebug "${name}.callCabal2nix root=${root} deriver=${pkg.cabal2nixDeriver.outPath}" pkg)
]
)
]
callCabal2nix name src { };

callPackage = name: nixFilePath:
let pkg = self.callPackage nixFilePath { };
in log.traceDebug "${name}.callPackage[cabal2nix] ${nixFilePath}" pkg;

callHackage = name: version:
let pkg = self.callHackage name version { };
in log.traceDebug "${name}.callHackage ver=${version}" pkg;
in

name: cfg:
# If 'source' is a path, we treat it as such. Otherwise, we assume it's a version (from hackage).
if lib.types.path.check cfg.source
then
callCabal2NixUnlessCached name (mkNewStorePath name cfg.source) cfg.cabal2NixFile
else
callHackage name cfg.source
9 changes: 1 addition & 8 deletions nix/modules/project/packages/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,8 @@ in
build-haskell-package = import ../../../build-haskell-package.nix {
inherit pkgs lib self log;
};
getOrMkPackage = name: cfg:
if lib.types.path.check cfg.source
then
(build-haskell-package name cfg.source cfg.cabal2NixFile)
else
log.traceDebug "${name}.callHackage ${cfg.source}"
(self.callHackage name cfg.source { });
in
lib.mapAttrs getOrMkPackage project.config.packages;
lib.mapAttrs build-haskell-package project.config.packages;
};
};
}

0 comments on commit f47c91a

Please sign in to comment.