当前我的default.nix看起来像:
{
sources ? import ./nix/sources.nix
, compiler ? "ghc865" } :
let
niv = import sources.nixpkgs {
overlays = [
(_ : _ : { niv = import sources.niv {}; })
] ;
config = {};
};
pkgs = niv.pkgs;
myHaskellPackages = pkgs.haskell.packages.${compiler}.override {
overrides = self: super: {
mkDerivation = args: super.mkDerivation (args // {
enableLibraryProfiling = true;
});
};
};
in
(myHaskellPackages.callCabal2nix "moscoviumorange" (./.) {})
我想为我的应用程序及其依赖项启用Haskell分析。对于我的应用程序,我可以使用hpack处理:
executables:
moscoviumorange-p:
ghc-options: -main-is Main -prof -fprof-auto
source-dirs: src
main: Main.hs
如何对依赖库执行相同的操作?
最佳答案
...
(myHaskellPackages.callCabal2nixWithOptions "moscoviumorange" (./.) "--enable-profiling" {})
以下是我的计算方法:
使用nix repl,我通过以下方式找到了该函数的路径:
nix-repl> pkgs.haskell.packages.ghc865.callCabal2nix
«lambda @ /nix/store/54fafxcq7b6ac2hvs8qzp2bgqzgpiiga-nixos-19.09.1019.c5aabb0d603/nixos/pkgs/development/haskell-modules/make-package-set.nix:213:21»
然后,我检出nixpkgs存储库(确保它与我在default.nix中引用的存储库处于同一提交),并导航到该文件+行,我发现:
# Creates a Haskell package from a source package by calling cabal2nix on the source.
callCabal2nixWithOptions = name: src: extraCabal2nixOptions: args:
let
filter = path: type:
pkgs.lib.hasSuffix "${name}.cabal" path ||
baseNameOf path == "package.yaml";
expr = self.haskellSrc2nix {
inherit name extraCabal2nixOptions;
src = if pkgs.lib.canCleanSource src
then pkgs.lib.cleanSourceWith { inherit src filter; }
else src;
};
in overrideCabal (callPackageKeepDeriver expr args) (orig: {
inherit src;
});
callCabal2nix = name: src: args: self.callCabal2nixWithOptions name src "" args;
进一步缩小到
haskellSrc2nix
: haskellSrc2nix = { name, src, sha256 ? null, extraCabal2nixOptions ? "" }:
let
sha256Arg = if sha256 == null then "--sha256=" else ''--sha256="${sha256}"'';
in pkgs.buildPackages.stdenv.mkDerivation {
name = "cabal2nix-${name}";
nativeBuildInputs = [ pkgs.buildPackages.cabal2nix ];
preferLocalBuild = true;
allowSubstitutes = false;
phases = ["installPhase"];
LANG = "en_US.UTF-8";
LOCALE_ARCHIVE = pkgs.lib.optionalString (buildPlatform.libc == "glibc") "${buildPackages.glibcLocales}/lib/locale/locale-archive";
installPhase = ''
export HOME="$TMP"
mkdir -p "$out"
cabal2nix --compiler=${self.ghc.haskellCompilerName} --system=${hostPlatform.config} ${sha256Arg} "${src}" ${extraCabal2nixOptions} > "$out/default.nix"
'';
};
啊哈!用谷歌做cabal2nix分析-似乎支持带有命令行参数的选项。
https://github.com/NixOS/cabal2nix/blob/650d5300c6bb5632bbab6fcb2d939fb476e469c5/src/Cabal2nix.hs#L92