如何提供 C 库以在 NixOS 上堆叠?

Posted

技术标签:

【中文标题】如何提供 C 库以在 NixOS 上堆叠?【英文标题】:How do I supply a C library to stack on NixOS? 【发布时间】:2020-01-09 08:21:17 【问题描述】:

我有一个基于堆栈的项目,它依赖于几个 C 库。其中一个 C 库 zlib 可从本机 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-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 列表中。

如何获取堆栈以找到这些库?

【问题讨论】:

【参考方案1】:

显然是一天结束的文档阅读失败的情况。今天早上,很快就找到了解释如何使用自定义 shell.nix 的堆栈文档。 The stack docs do elaborate on how this works,包括一个例子,它表明它根本不像我认为预期的外壳:

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"

导致构建成功。

【讨论】:

以上是关于如何提供 C 库以在 NixOS 上堆叠?的主要内容,如果未能解决你的问题,请参考以下文章

如何使用R将多列堆叠成一列[重复]

Catalyst 3750堆叠

如何在R中堆叠数据框[重复]

华为交换机S5735S如何堆叠iStack

华为交换机S5735S如何堆叠iStack

Appcelerator:垂直堆叠视图是不是可行?