Haskell / Stack / Nix 构建失败,需要 pkg-config >= 0.9.0 和 cairo >= 1.2.0,但最新版本是 pkg-config 0.29.2 和 cairo 1.15.4

Haskell / Stack / Nix build fails, requiring pkg-config >= 0.9.0 and cairo >= 1.2.0, but latest versions are pkg-config 0.29.2 and cairo 1.15.4

我正在使用 Stack 和 Nix 构建一个 Haskell 项目,并依赖于来自 Hackage 的 cairo 库。

当我构建项目时,出现错误:无法找到 pkg-config >= 0.9.0cairo >= 1.2.0。然而,根据他们的网站,最新版本是 pkg-config 0.29.2cairo-1.15.4,这也反映在 Nix 包管理器中。

另一方面,有 this article from 2006 announcing the release of Cairo 1.2.0,这进一步混淆了问题。

问题

  1. Why are the expected versions so wildly different from the published versions on nixpkgs and the pkg-config / cairo websites?

  2. What advice do you have to get cairo to build on macOS (best case scenario: using Nix for system packages and Stack for Haskell packages)

重现步骤:

> stack new cairo-test simple && cd cairo-test

# Now, to get Cairo
> stack install cairo

cairo-0.13.3.1: configure ...
Process exited with code: ExitFailure 1
Configuring cairo-0.13.3.1...
setup: The program 'pkg-config' version >=0.9.0 is required but it could not be found.

# This version doesn't seem to exist (not on the pkg-config website, either).
> nix-env -qaP pkg-config
nixpkgs.pkgconfig          pkg-config-0.29.2
nixpkgs.pkgconfigUpstream  pkg-config-0.29.2

# However, if installed, a new error:
> nix-env -i pkg-config
installing ‘pkg-config-0.29.2’
building path(s) ‘/nix/store/m4ks2si7b78757c1rc43r0833pxkvjb3-user-environment’
created 102 symlinks in user environment
> stack install cairo
setup: The pkg-config package 'cairo' version >=1.2.0 is required but 
it could not be found.

# Again, this version doesn't seem to exist, either on the site on in `nixpkgs`
> nix-env -qaP cairo
nixpkgs.cairo  cairo-1.14.8

# Installing it anyway, to see what the next error is
> nix-env -i cairo
installing ‘cairo-1.14.8’
building path(s) ‘/nix/store/dcx0in96wcd7yd8q71y93jd5306vag8g-user-environment’
created 112 symlinks in user environment

# Get the same version error now that Cairo is installed
setup: The pkg-config package 'cairo' version >=1.2.0 is required but it could not be found.

通过安装 Homebrew 然后使用 brew install cairo 开罗,我能够构建项目。

但是,这安装了 Cairo 版本 1.14 — 与 Nix 安装的相同!当我使用 brew info cairo 检查时,我看到了更多信息:

cairo: stable 1.14.8 (bottled), devel 1.15.4, HEAD

==> Dependencies
Build: pkg-config ✘
Required: freetype ✔, fontconfig ✔, libpng ✔, pixman ✔, glib ✔

我尝试使用 Nix 安装这些依赖项,然后卸载 Homebrew。运气不好——出现大量链接错误。所以我重新安装了 Homebrew,它又可以工作了。

这不是我一直在寻找的好的、干净的解决方案(必须安装外部包管理器),但它确实解决了现在的问题。

问题不是库版本,1.14.8 大于 1.2.0。问题是 cairo 开发文件没有链接到您的环境中,也就是说,$PKG_CONFIG_PATH 没有设置。

三种解决方案:

  1. /nix/store找到cairo-dev目录,添加到PKG_CONFIG_PATH。例如,

     $ cairodev=$(nix-store --query --outputs $(nix-instantiate '<nixpkgs>' -A cairo) | grep dev$)
     $ export PKG_CONFIG_PATH=$cairodev:$PKG_CONFIG_PATH
    
  2. 运行 从 nix shell, nix-shell -p pkgconfig cairo.

  3. 中堆叠
  4. 使用堆栈的 nix 支持,将以下内容添加到 stack.yml

    nix:
      enable: true
      packages: [pkgconfig, cairo]