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

How do I supply a C library to stack on NixOS?

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

的 nix 部分
nix:
  enable: true
  packages:
    - "zlib"

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

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

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

这为我提供了 zlib 的有效 pkg-config:

[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 中将堆栈指向它:

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

构建成功。