[英]How can I enable Haskell profiling when using Nix's callCabal2nix
[英]With Nix, how can I specify Haskell dependencies with profiling enabled?
我最初是这样尝试的:
nix-shell -p "haskell.packages.ghc821.ghcWithPackages (p: with p; [text hspec lens])" -j4 --run 'ghc Main.hs -prof
然后GHC告诉我
Main.hs:4:1: error:
Could not find module ‘Control.Lens’
Perhaps you haven't installed the profiling libraries for package ‘lens-4.15.4’?
Use -v to see a list of the files searched for.
在网上搜索我发现了这个: https : //github.com/NixOS/nixpkgs/issues/22340
所以我似乎无法从缓存中下载。 但是没关系,如果至少我可以在本地构建配置文件变体。
我可以通过简单地修改给予-p
的nix表达式来实现吗?
然后在写这个问题的这一点上,我记得这个资源: https : //github.com/NixOS/nixpkgs/blob/bd6ba7/pkgs/development/haskell-modules/lib.nix
我在哪里找到了enableLibraryProfiling
。 所以我尝试过:
nix-shell -p "haskell.packages.ghc821.ghcWithPackages (p: with p; [text hspec (haskell.lib.enableLibraryProfiling lens)])" -j4 --run 'ghc Main.hs -prof'
这让我遇到了一个新错误:
src/Control/Lens/Internal/Getter.hs:26:1: error:
Could not find module ‘Data.Functor.Contravariant’
Perhaps you haven't installed the profiling libraries for package ‘contravariant-1.4’?
Use -v to see a list of the files searched for.
|
26 | import Data.Functor.Contravariant
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
因此,如果我可以将所有包映射到enableLibraryProfiling
上,那么我想这可行。 但是我的nix知识目前并没有完全扩展到那么远。 我怎么能这样做? 这甚至是正确的追求途径吗?
通过进一步窥探nix-repl
和来自ElvishJerricco的一些有用的指针来自FreeNode的#reflex-frp,我能够构建这个,这似乎有效:
$ nix-shell -p "(haskell.packages.ghc821.extend (self: super: {mkDerivation = expr: super.mkDerivation (expr // { enableLibraryProfiling = true; });})).ghcWithPackages (p: with p; [text hspec lens])" -j4 --run 'ghc Main.hs -prof'
我想出了一个简单的方法,我正在使用Nix进行关于Haskell开发的最大博客文章。 现在,这里只是分析部分的文本:
尼克斯相当容易。 首先,我们将以下内容添加到~/.config/nixpkgs/config.nix
:
{
packageOverrides = super: let self = super.pkgs; in
{
profiledHaskellPackages = self.haskellPackages.override {
overrides = self: super: {
mkDerivation = args: super.mkDerivation (args // {
enableLibraryProfiling = true;
});
};
};
};
}
现在在我们想要profiling-shell.nix
的项目中,我们创建了一个新的profiling-shell.nix
:
let nixpkgs = import <nixpkgs> {};
orig = nixpkgs.pkgs.profiledHaskellPackages.callPackage ./default.nix {};
in (nixpkgs.pkgs.haskell.lib.doBenchmark orig).env
除了使用我们刚刚全局定义的profiledHaskellPackages
之外,几乎与我们的普通shell.nix
相同。 现在,调用nix-shell profiling-shell.nix
将重建项目中的每个依赖项,并启用分析。 第一次这样做需要很长时间。 幸运的是,这并没有破坏我们的Nix商店 - 一个vanilla nix-shell
似乎确实再次呈现我们的常规依赖,没有重新下载或重建。
警告: nix-collect-garbage -d
将从我们的Nix Store中删除所有自定义库,如果需要,我们必须再次构建它们。
如果我们正在编写一个库,那么我们可以分析的最接近的可执行文件将是我们的基准套件。 要做到这一点:
-prof
和-fprof-auto
添加到我们的基准GHC选项中 default.nix
nix-shell profiling-shell.nix
cabal configure --enable-library-profiling --enable-benchmarks
cabal build
dist/build/projname/projname-bench +RTS -p
projname-bench.prof
文件 根据结果,我们可以在正常的Nix Shell中进行代码更改,删除分析选项,重新生成default.nix
和基准。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.