无法在堆栈项目中使用 nix 构建 hzk (haskell zookeeper)

Can't build hzk (haskell zookeeper) with nix in stack project

我正在尝试将我雇主的大型 haskell 堆栈项目移植到 nix,但它正在吐 zookeeper_mt。在 stack.yaml 中,我启用了 nix 构建,并且已经添加了一些 nix 包,如 zlib 来修复构建错误,但即使使用 zookeeper_mt,我得到:

hzk                             > configure
hzk                             > Configuring hzk-2.1.0...
hzk                             > Warning: 'ghc-options: -rtsopts' has no effect for libraries. It should only
hzk                             > be used for executables.
hzk                             > build
hzk                             > Preprocessing library for hzk-2.1.0..
hzk                             > /run/user/1000/stack-4c469d140762b8b0/hzk-2.1.0/CApi.hsc:76:10: fatal error: zookeeper.h: No such file or directory
hzk                             > compilation terminated.
hzk                             > compiling .stack-work/dist/x86_64-linux-nix/Cabal-3.0.1.0/build/Database/Zookeeper/CApi_hsc_make.c failed (exit code 1)
hzk                             > command was: /nix/store/za1rr3fxmgfbfl6nbigqi819m3m7nkgi-gcc-wrapper-9.3.0/bin/cc -c .stack-work/dist/x86_64-linux-nix/Cabal-3.0.1.0/build/Database/Zookeeper/CApi_hsc_make.c -o .stack-work/dist/x86_64-linux-nix/Cabal-3.0.1.0/build/Database/Zookeeper/CApi_hsc_make.o -fuse-ld=gold -D__GLASGOW_HASKELL__=808 -Dlinux_BUILD_OS=1 -Dx86_64_BUILD_ARCH=1 -Dlinux_HOST_OS=1 -Dx86_64_HOST_ARCH=1 -I/usr/include/zookeeper -I/nix/store/fn0kwfwaf53638xm37wl94la8dxlbjaq-zlib-1.2.11-dev/include -I/nix/store/in9vak3zphkx9c635iz639iabgrib2fl-postgresql-11.8/include -I/nix/store/qsarmp3rsyvhb7m22xl0fdpsnaf3lv6b-pcre-8.44-dev/include -I/nix/store/87w0n6hix0had2lp58i5rgmrr5dn3ghf-pkg-config-wrapper-0.29.2/include -I/nix/store/v1v3flnwxs3n5lhfrfrs16rcygjzzhmy-zookeeper-3.4.13/include -I/nix/store/grng8b5wmrppx2rmcsqkmjyq23bj1j38-zookeeper_mt-3.4.13/include -I/nix/store/qqra09ff0q8vwqicjs20fn1rvn00k6fy-ghc-8.8.3/include -I/nix/store/pj43dfd5k4zb0ivafkyqpr1blzd616ry-git-2.27.0/include -I/nix/store/za1rr3fxmgfbfl6nbigqi819m3m7nkgi-gcc-wrapper-9.3.0/include -I/nix/store/00lmw45jlcm9ln2vi1rxxdz7cblwk3y6-gmp-6.2.0-dev/include -I/usr/include/zookeeper -I/nix/store/fn0kwfwaf53638xm37wl94la8dxlbjaq-zlib-1.2.11-dev/include -I/nix/store/in9vak3zphkx9c635iz639iabgrib2fl-postgresql-11.8/include -I/nix/store/qsarmp3rsyvhb7m22xl0fdpsnaf3lv6b-pcre-8.44-dev/include -I/nix/store/87w0n6hix0had2lp58i5rgmrr5dn3ghf-pkg-config-wrapper-0.29.2/include -I/nix/store/v1v3flnwxs3n5lhfrfrs16rcygjzzhmy-zookeeper-3.4.13/include -I/nix/store/grng8b5wmrppx2rmcsqkmjyq23bj1j38-zookeeper_mt-3.4.13/include -I/nix/store/qqra09ff0q8vwqicjs20fn1rvn00k6fy-ghc-8.8.3/include -I/nix/store/pj43dfd5k4zb0ivafkyqpr1blzd616ry-git-2.27.0/include -I/nix/store/za1rr3fxmgfbfl6nbigqi819m3m7nkgi-gcc-wrapper-9.3.0/include -I/nix/store/00lmw45jlcm9ln2vi1rxxdz7cblwk3y6-gmp-6.2.0-dev/include -I.stack-work/dist/x86_64-linux-nix/Cabal-3.0.1.0/build/autogen -I.stack-work/dist/x86_64-linux-nix/Cabal-3.0.1.0/build/global-autogen -include .stack-work/dist/x86_64-linux-nix/Cabal-3.0.1.0/build/autogen/cabal_macros.h -I/nix/store/fn0kwfwaf53638xm37wl94la8dxlbjaq-zlib-1.2.11-dev/include -I/nix/store/in9vak3zphkx9c635iz639iabgrib2fl-postgresql-11.8/include -I/nix/store/qsarmp3rsyvhb7m22xl0fdpsnaf3lv6b-pcre-8.44-dev/include -I/nix/store/87w0n6hix0had2lp58i5rgmrr5dn3ghf-pkg-config-wrapper-0.29.2/include -I/nix/store/qqra09ff0q8vwqicjs20fn1rvn00k6fy-ghc-8.8.3/include -I/nix/store/pj43dfd5k4zb0ivafkyqpr1blzd616ry-git-2.27.0/include -I/nix/store/za1rr3fxmgfbfl6nbigqi819m3m7nkgi-gcc-wrapper-9.3.0/include -I/nix/store/00lmw45jlcm9ln2vi1rxxdz7cblwk3y6-gmp-6.2.0-dev/include -I/nix/store/fn0kwfwaf53638xm37wl94la8dxlbjaq-zlib-1.2.11-dev/include -I/nix/store/in9vak3zphkx9c635iz639iabgrib2fl-postgresql-11.8/include -I/nix/store/qsarmp3rsyvhb7m22xl0fdpsnaf3lv6b-pcre-8.44-dev/include -I/nix/store/87w0n6hix0had2lp58i5rgmrr5dn3ghf-pkg-config-wrapper-0.29.2/include -I/nix/store/qqra09ff0q8vwqicjs20fn1rvn00k6fy-ghc-8.8.3/include -I/nix/store/pj43dfd5k4zb0ivafkyqpr1blzd616ry-git-2.27.0/include -I/nix/store/za1rr3fxmgfbfl6nbigqi819m3m7nkgi-gcc-wrapper-9.3.0/include -I/nix/store/00lmw45jlcm9ln2vi1rxxdz7cblwk3y6-gmp-6.2.0-dev/include -I/home/ad/.stack/snapshots/x86_64-linux-nix/dd8fbb08ccfb9e77e9ffdb798576edd8f70b7edca799105b5bc2de70a44535a1/8.8.3/lib/x86_64-linux-ghc-8.8.3/vector-0.12.1.2-AWRYcz9jfa25Avs2q9Jg9V/include -I/nix/store/qqra09ff0q8vwqicjs20fn1rvn00k6fy-ghc-8.8.3/lib/ghc-8.8.3/time-1.9.3/include -I/nix/store/fn0kwfwaf53638xm37wl94la8dxlbjaq-zlib-1.2.11-dev/include -I/nix/store/in9vak3zphkx9c635iz639iabgrib2fl-postgresql-11.8/include -I/nix/store/qqra09ff0q8vwqicjs20fn1rvn00k6fy-ghc-8.8.3/include -I/nix/store/pj43dfd5k4zb0ivafkyqpr1blzd616ry-git-2.27.0/include -I/nix/store/za1rr3fxmgfbfl6nbigqi819m3m7nkgi-gcc-wrapper-9.3.0/include -I/nix/store/00lmw45jlcm9ln2vi1rxxdz7cblwk3y6-gmp-6.2.0-dev/include -I/home/ad/.stack/snapshots/x86_64-linux-nix/dd8fbb08ccfb9e77e9ffdb798576edd8f70b7edca799105b5bc2de70a44535a1/8.8.3/lib/x86_64-linux-ghc-8.8.3/primitive-0.7.0.1-CN4ea3Jrhra6yENuqrDg51/include -I/nix/store/fn0kwfwaf53638xm37wl94la8dxlbjaq-zlib-1.2.11-dev/include -I/nix/store/in9vak3zphkx9c635iz639iabgrib2fl-postgresql-11.8/include -I/nix/store/qsarmp3rsyvhb7m22xl0fdpsnaf3lv6b-pcre-8.44-dev/include -I/nix/store/87w0n6hix0had2lp58i5rgmrr5dn3ghf-pkg-config-wrapper-0.29.2/include -I/nix/store/qqra09ff0q8vwqicjs20fn1rvn00k6fy-ghc-8.8.3/include -I/nix/store/pj43dfd5k4zb0ivafkyqpr1blzd616ry-git-2.27.0/include -I/nix/store/za1rr3fxmgfbfl6nbigqi819m3m7nkgi-gcc-wrapper-9.3.0/include -I/nix/store/00lmw45jlcm9ln2vi1rxxdz7cblwk3y6-gmp-6.2.0-dev/include -I/nix/store/fn0kwfwaf53638xm37wl94la8dxlbjaq-zlib-1.2.11-dev/include -I/nix/store/in9vak3zphkx9c635iz639iabgrib2fl-postgresql-11.8/include -I/nix/store/qsarmp3rsyvhb7m22xl0fdpsnaf3lv6b-pcre-8.44-dev/include -I/nix/store/87w0n6hix0had2lp58i5rgmrr5dn3ghf-pkg-config-wrapper-0.29.2/include -I/nix/store/qqra09ff0q8vwqicjs20fn1rvn00k6fy-ghc-8.8.3/include -I/nix/store/pj43dfd5k4zb0ivafkyqpr1blzd616ry-git-2.27.0/include -I/nix/store/za1rr3fxmgfbfl6nbigqi819m3m7nkgi-gcc-wrapper-9.3.0/include -I/nix/store/00lmw45jlcm9ln2vi1rxxdz7cblwk3y6-gmp-6.2.0-dev/include -I/nix/store/fn0kwfwaf53638xm37wl94la8dxlbjaq-zlib-1.2.11-dev/include -I/nix/store/in9vak3zphkx9c635iz639iabgrib2fl-postgresql-11.8/include -I/nix/store/qsarmp3rsyvhb7m22xl0fdpsnaf3lv6b-pcre-8.44-dev/include -I/nix/store/87w0n6hix0had2lp58i5rgmrr5dn3ghf-pkg-config-wrapper-0.29.2/include -I/nix/store/qqra09ff0q8vwqicjs20fn1rvn00k6fy-ghc-8.8.3/include -I/nix/store/pj43dfd5k4zb0ivafkyqpr1blzd616ry-git-2.27.0/include -I/nix/store/za1rr3fxmgfbfl6nbigqi819m3m7nkgi-gcc-wrapper-9.3.0/include -I/nix/store/00lmw45jlcm9ln2vi1rxxdz7cblwk3y6-gmp-6.2.0-dev/include -I/nix/store/fn0kwfwaf53638xm37wl94la8dxlbjaq-zlib-1.2.11-dev/include -I/nix/store/qqra09ff0q8vwqicjs20fn1rvn00k6fy-ghc-8.8.3/include -I/nix/store/pj43dfd5k4zb0ivafkyqpr1blzd616ry-git-2.27.0/include -I/nix/store/za1rr3fxmgfbfl6nbigqi819m3m7nkgi-gcc-wrapper-9.3.0/include -I/nix/store/00lmw45jlcm9ln2vi1rxxdz7cblwk3y6-gmp-6.2.0-dev/include -I/nix/store/qqra09ff0q8vwqicjs20fn1rvn00k6fy-ghc-8.8.3/lib/ghc-8.8.3/bytestring-0.10.10.0/include -I/nix/store/qqra09ff0q8vwqicjs20fn1rvn00k6fy-ghc-8.8.3/include -I/nix/store/pj43dfd5k4zb0ivafkyqpr1blzd616ry-git-2.27.0/include -I/nix/store/za1rr3fxmgfbfl6nbigqi819m3m7nkgi-gcc-wrapper-9.3.0/include -I/nix/store/00lmw45jlcm9ln2vi1rxxdz7cblwk3y6-gmp-6.2.0-dev/include -I/nix/store/qqra09ff0q8vwqicjs20fn1rvn00k6fy-ghc-8.8.3/lib/ghc-8.8.3/base-4.13.0.0/include -I/nix/store/00lmw45jlcm9ln2vi1rxxdz7cblwk3y6-gmp-6.2.0-dev/include -I/nix/store/qqra09ff0q8vwqicjs20fn1rvn00k6fy-ghc-8.8.3/lib/ghc-8.8.3/integer-gmp-1.0.2.0/include -I/nix/store/qqra09ff0q8vwqicjs20fn1rvn00k6fy-ghc-8.8.3/lib/ghc-8.8.3/include -I/nix/store/l9xqrgxn4z795jivmrmjyanlb6jjq66k-libffi-3.3-dev/include -I/nix/store/qqra09ff0q8vwqicjs20fn1rvn00k6fy-ghc-8.8.3/lib/ghc-8.8.3/include/

我可以在 nix-shell -p zookeeper_mt 中看到 zookeeper.h 最终出现在 ${zookeeper}/include/zookeeper/ 中,但这就像 ubuntu 包一样,它也有 [=23] 的这个 zookeeper 子目录=],并且该项目在安装了 ubuntu 包的非 nix 堆栈构建中运行良好。

我在那个错误消息 -I/nix/store/grng8b5wmrppx2rmcsqkmjyq23bj1j38-zookeeper_mt-3.4.13/include 中看到没有额外的 /zookeeper 但我不确定系统的哪一部分对此负责或如何修复它因为我是新手尼克斯。是 nix 包的错误、堆栈的错误还是其他原因?

hzk.cabal 有这样的行

  include-dirs:        /usr/include/zookeeper

这对于符合 FHS 的系统是一个很好的帮助,只要系统分发包括 zookeeper 库。 /usr/local/opt/* 中的本地安装会导致同样的问题。

Nixpkgs 是这样解决的https://github.com/NixOS/nixpkgs/blob/a8a0dfa047d9a96f832ff2be30d004ad5544bd7e/pkgs/development/haskell-modules/configuration-nix.nix#L117-L120,但这并没有转化为 Stack。

为了让它在堆栈中工作,我们可以使用它的 nix-shell 集成。

stack.yaml:

nix:
  packages: [] # Note that specifying both packages: and a shell-file: results in an error.
  shell-file: stack-shell.nix

stack-shell.nix:

{ghc}:
let pkgs = import <nixpkgs> {};
# (side note)
# If you're pinning nixpkgs, You should probably set
# NIX_PATH=nixpkgs=... in your real `shell.nix`
# because that's where stack gets its ghc.
in
pkgs.haskell.lib.buildStackProject {
  inherit ghc;
  name = "myEnv";
  buildInputs = [ pkgs.zookeeper_mt ];
  shellHook = ''
    NIX_CFLAGS_COMPILE="-I ${pkgs.zookeeper_mt}/include/zookeeper $NIX_CFLAGS_COMPILE"
  '';
}