haskell - 如何提供 C 库以在 NixOS 上进行堆栈?

标签 haskell compiler-errors haskell-stack pkg-config nixos

我有一个基于堆栈的项目,它依赖于几个 C 库。这些 C 库之一 zlib 可从 native NixOS 包中获取,我可以将其放入 stack.yaml 的 nix 部分:

nix:
  enable: true
  packages:
    - "zlib"

另一个不是 nixpkgs 的一部分。堆栈文档建议使用 stack.yaml 中的 nix 部分的替代方法是“编写 shell.nix”,无需详细说明。

所以我写了一个,以 zlib 为例:

{ pkgs ? import <nixpkgs> { } }:
  pkgs.mkShell {
    buildInputs = [
      pkgs.pkgconfig
      pkgs.zlib
      pkgs.stack
    ];
  }

这为我提供了 zlib 的有效 pkg 配置:

[nix-shell:~/Work/PrivateStorage/PaymentServer]$ pkg-config --modversion zlib
1.2.11

但是,它似乎无法使堆栈能够找到该库:

[nix-shell:~/Work/PrivateStorage/PaymentServer]$ stack build
zlib-0.6.2: configure
Progress 1/7

--  While building package zlib-0.6.2 using:
      /home/exarkun/.stack/setup-exe-cache/x86_64-linux-nix/Cabal-simple_mPHDZzAJ_2.4.0.1_ghc-8.6.5 --builddir=.stack-work/dist/x86_64-linux-nix/Cabal-2.4.0.1 configure --with-ghc=/nix/store/zfpm9bai9gj8vs09s2i2gkhvgsjkx13z-ghc-8.6.5/bin/ghc --with-ghc-pkg=/nix/store/zfpm9bai9gj8vs09s2i2gkhvgsjkx13z-ghc-8.6.5/bin/ghc-pkg --user --package-db=clear --package-db=global --package-db=/home/exarkun/.stack/snapshots/x86_64-linux-nix/lts-14.1/8.6.5/pkgdb --libdir=/home/exarkun/.stack/snapshots/x86_64-linux-nix/lts-14.1/8.6.5/lib --bindir=/home/exarkun/.stack/snapshots/x86_64-linux-nix/lts-14.1/8.6.5/bin --datadir=/home/exarkun/.stack/snapshots/x86_64-linux-nix/lts-14.1/8.6.5/share --libexecdir=/home/exarkun/.stack/snapshots/x86_64-linux-nix/lts-14.1/8.6.5/libexec --sysconfdir=/home/exarkun/.stack/snapshots/x86_64-linux-nix/lts-14.1/8.6.5/etc --docdir=/home/exarkun/.stack/snapshots/x86_64-linux-nix/lts-14.1/8.6.5/doc/zlib-0.6.2 --htmldir=/home/exarkun/.stack/snapshots/x86_64-linux-nix/lts-14.1/8.6.5/doc/zlib-0.6.2 --haddockdir=/home/exarkun/.stack/snapshots/x86_64-linux-nix/lts-14.1/8.6.5/doc/zlib-0.6.2 --dependency=base=base-4.12.0.0 --dependency=bytestring=bytestring-0.10.8.2 --extra-include-dirs=/nix/store/a54skdf3xksiqvcvr75bjpdl1jx8dgbk-gmp-6.1.2-dev/include --extra-include-dirs=/nix/store/br7kq0kvbn73rhzl17js0w3pprphhzv1-git-2.19.2/include --extra-include-dirs=/nix/store/ghzg4kg0sjif58smj2lfm2bdvjwim85y-gcc-wrapper-7.4.0/include --extra-include-dirs=/nix/store/zfpm9bai9gj8vs09s2i2gkhvgsjkx13z-ghc-8.6.5/include --extra-lib-dirs=/nix/store/br7kq0kvbn73rhzl17js0w3pprphhzv1-git-2.19.2/lib --extra-lib-dirs=/nix/store/ghzg4kg0sjif58smj2lfm2bdvjwim85y-gcc-wrapper-7.4.0/lib --extra-lib-dirs=/nix/store/kggcrzpa5hd41b7v60wa7xjkgjs43xsl-gmp-6.1.2/lib --extra-lib-dirs=/nix/store/zfpm9bai9gj8vs09s2i2gkhvgsjkx13z-ghc-8.6.5/lib
    Process exited with code: ExitFailure 1
    Logs have been written to: /home/exarkun/Work/PrivateStorage/PaymentServer/.stack-work/logs/zlib-0.6.2.log

    Configuring zlib-0.6.2...
    Cabal-simple_mPHDZzAJ_2.4.0.1_ghc-8.6.5: Missing dependency on a foreign
    library:
    * Missing (or bad) header file: zlib.h
    * Missing (or bad) C library: z
    This problem can usually be solved by installing the system package that
    provides this library (you may need the "-dev" version). If the library is
    already installed but in a non-standard location then you can use the flags
    --extra-include-dirs= and --extra-lib-dirs= to specify where it is.If the
    library file does exist, it may contain errors that are caught by the C
    compiler at the preprocessing stage. In this case you can re-run configure
    with the verbosity flag -v3 to see the error messages.
    If the header file does exist, it may contain errors that are caught by the C
    compiler at the preprocessing stage. In this case you can re-run configure
    with the verbosity flag -v3 to see the error messages.

对于 zlib,没有特别需要走这条路,但据我所知,我无法将非 nixpkgs 包放入 stack.yaml 中的 nix.packages 列表中。

如何通过堆栈找到这些库?

最佳答案

显然是一天结束时文档阅读失败的情况。今天早上,没花多长时间就找到了解释如何使用自定义 shell.nix 的堆栈文档。 The stack docs do elaborate on how this works ,包括一个示例,表明它根本不像我所期望的 shell:

{ghc}:
with (import <nixpkgs> {});

haskell.lib.buildStackProject {
  inherit ghc;
  name = "myEnv";
  buildInputs = [ glpk pcre ];
}

调整我的 shell 以使用 buildStackProject 而不是 mkShell:

{ ghc }:
with (import <nixpkgs> { overlays = [ (import ./overlay.nix) ]; });

haskell.lib.buildStackProject {
  inherit ghc;
  name = "PrivacyPass";
  # extra-library made available via the overlay.
  # overlay probably not strictly necessary here, either.
  buildInputs = [ zlib extra-library ];
}

然后在 stack.yaml 中将 stack 指向它:

nix:
  enable: true
  shell-file: "stack-shell.nix"

构建成功。

关于haskell - 如何提供 C 库以在 NixOS 上进行堆栈?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57822288/

相关文章:

types - BISON/FLEX C-Basic没有声明类型错误

c# - 为什么在这种情况下编译器不提示?

haskell - 分发 Haskell 应用程序和更新它的最佳实践

c - Haskell:Arch Linux 上缺少的 C 库在 Ubuntu 上也能正常工作

Haskell:在没有函数依赖的情况下洗牌数据

Guards 中的 Haskell 类型解构

c++ - 使用 g++ 用一个命令预处理多个源文件

haskell - 使用 stack 安装 shuffle 库时出错

haskell - 使用 tls-extra 进行简单的 smtp

haskell - 在atom中使用haskell - ghc mod错误