From 0327c07701bd5c6a4fa6de2baf898818efe60b96 Mon Sep 17 00:00:00 2001 From: Sridhar Ratnakumar Date: Sun, 22 Dec 2024 20:39:25 -0500 Subject: [PATCH] refactor: consolidate in one place --- nix/build-haskell-package.nix | 59 ++++++++++++++---------- nix/modules/project/packages/default.nix | 9 +--- nix/modules/project/packages/package.nix | 4 +- 3 files changed, 37 insertions(+), 35 deletions(-) diff --git a/nix/build-haskell-package.nix b/nix/build-haskell-package.nix index f0da63d2..0b2cf296 100644 --- a/nix/build-haskell-package.nix +++ b/nix/build-haskell-package.nix @@ -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 @@ -23,29 +23,38 @@ let inherit src; }; -in + # Avoid rebuilding because of changes in parent directories + mkNewStorePath = name: src: + let newSrc = mkNewStorePath' name src; + in log.traceDebug "${name}.mkNewStorePath ${newSrc}" newSrc; + + callCabal2nix = name: src: + let pkg = self.callCabal2nix name src { }; + in log.traceDebug "${name}.callCabal2nix src=${src} deriver=${pkg.cabal2nixDeriver.outPath}" pkg; -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) + # Use cached cabal2nix generated nix expression if present, otherwise use IFD (callCabal2nix) + callCabal2NixUnlessCached = name: src: cabal2nixFile: + let path = "${src}/${cabal2nixFile}"; + in + if builtins.pathExists path + then + callPackage name path + else + 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 - (root: - let path = "${root}/${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 { }) - else - lib.pipe (self.callCabal2nix name root { }) - [ - (pkg: log.traceDebug "${name}.callCabal2nix root=${root} deriver=${pkg.cabal2nixDeriver.outPath}" pkg) - ] - ) - ] +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 diff --git a/nix/modules/project/packages/default.nix b/nix/modules/project/packages/default.nix index 6a8b7cd0..6bcd6e6e 100644 --- a/nix/modules/project/packages/default.nix +++ b/nix/modules/project/packages/default.nix @@ -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; }; }; } diff --git a/nix/modules/project/packages/package.nix b/nix/modules/project/packages/package.nix index bbf74e1a..21a5c960 100644 --- a/nix/modules/project/packages/package.nix +++ b/nix/modules/project/packages/package.nix @@ -26,9 +26,9 @@ in cabal2NixFile = lib.mkOption { type = lib.types.str; description = '' - The Nix file which contains cached (pre-generated) `cabal2nix` expressions. + Filename of the cabal2nix generated nix expression. - By default, it refers to `cabal.nix` file. + This gets used if it exists instead of using IFD (callCabal2nix). ''; default = "cabal.nix"; };