首页 > 解决方案 > 无法在 NixOS 上构建 haskell 包 mysql

问题描述

我正在尝试使用 构建最基本的 haskell 项目cabal-3.2.0.0ghc-8.2.2该项目使用mysql-0.1.7.3.

我使用cabal init添加mysql ^>= 0.1.7.cabal文件中来设置项目。然后我还添加了这个shell.nix文件以将库依赖项纳入范围:

所以我添加了一个shell.nix文件来明确地将所有内容纳入范围:(这是从这里派生的)

let 
    oldpkgs=import (fetchTarball https://github.com/NixOS/nixpkgs/archive/83b35508c6491103cd16a796758e07417a28698b.tar.gz) {};
    pkgs=import <nixpkgs>{};
    ghc = oldpkgs.haskell.compiler.ghc822;
in pkgs.haskell.lib.buildStackProject {
    inherit ghc;
    name = "myEnv";
    buildInputs = (with oldpkgs; [ zlib mysql openssl ]);
}

我进入nix-shell并尝试使用cabal build --with-compiler ghc-8.2.2. 但是,我得到了这个错误而不是构建成功:

[...]
Failed to build mysql-0.1.7.3.
Build log (
/home/faebl/.cabal/logs/ghc-8.2.2/mysql-0.1.7.3-7b08cf393638283e9449be836b946cd61b6404d2e6bc263d439034d2df46d924.log
):
[1 of 1] Compiling Main             ( /home/faebl/dev/mysqlTest/dist-newstyle/tmp/src-1662897/mysql-0.1.7.3/dist/setup/setup.hs, /home/faebl/dev/mysqlTest/dist-newstyle/tmp/src-1662897/mysql-0.1.7.3/dist/setup/Main.o )
Linking /home/faebl/dev/mysqlTest/dist-newstyle/tmp/src-1662897/mysql-0.1.7.3/dist/setup/setup ...
Configuring mysql-0.1.7.3...
Preprocessing library for mysql-0.1.7.3..
In file included from C.hsc:73:0:
include/mysql_signals.h:9:19: fatal error: mysql.h: No such file or directory
 #include "mysql.h"
                   ^
compilation terminated.
[...]

(所以实际上找到了库)

用于构建部分 mysql 库的命令是这样的:(标记为更好的可读性)

/nix/store/6s3dl13ingr21z93dy6zxcc652wdlcrh-gcc-wrapper-6.4.0/bin/cc
-c
dist/build/Database/MySQL/Base/C_hsc_make.c
-o
dist/build/Database/MySQL/Base/C_hsc_make.o
-fno-stack-protector
-fno-stack-protector
-D__GLASGOW_HASKELL__=802
-Dlinux_BUILD_OS=1
-Dx86_64_BUILD_ARCH=1
-Dlinux_HOST_OS=1
-Dx86_64_HOST_ARCH=1
->>>>I/nix/store/6kl22pij3n5qppcmlg3a6m8ny19yylqj-mariadb-10.2.12//nix/store/6kl22pij3n5qppcmlg3a6m8ny19yylqj-mariadb-10.2.12/include/mysql
->>>>I/nix/store/6kl22pij3n5qppcmlg3a6m8ny19yylqj-mariadb-10.2.12//nix/store/6kl22pij3n5qppcmlg3a6m8ny19yylqj-mariadb-10.2.12/include/mysql/mysql
-Iinclude
-Idist/build/autogen
-Idist/build/global-autogen
-include
dist/build/autogen/cabal_macros.h
-I/nix/store/75hyhwqmh2zh2wflffn1qgrrhy5nl8vg-ghc-8.2.2/lib/ghc-8.2.2/bytestring-0.10.8.2/include
-I/nix/store/75hyhwqmh2zh2wflffn1qgrrhy5nl8vg-ghc-8.2.2/lib/ghc-8.2.2/base-4.10.1.0/include
-I/nix/store/zi1rbssjvyyddjd4s8jmbardz11akd0s-gmp-6.1.2-dev/include
-I/nix/store/75hyhwqmh2zh2wflffn1qgrrhy5nl8vg-ghc-8.2.2/lib/ghc-8.2.2/integer-gmp-1.0.1.0/include
-I/nix/store/75hyhwqmh2zh2wflffn1qgrrhy5nl8vg-ghc-8.2.2/lib/ghc-8.2.2/include
-I/nix/store/75hyhwqmh2zh2wflffn1qgrrhy5nl8vg-ghc-8.2.2/lib/ghc-8.2.2/include/

假设它与链接和 nixos 的非 FHS 一致性有关,我将其添加到文件中的可执行.cabal文件中:

extra-lib-dirs: /nix/store/6kl22pij3n5qppcmlg3a6m8ny19yylqj-mariadb-10.2.1/include/server
extra-libraries: mysql

构建结果没有变化。

这是我的完整.cabal文件:

cabal-version:      2.4
name:               mysqlTest
version:            0.1.0.0

executable mysqlTest
    main-is:          Main.hs
    build-depends: base         >= 4.9 && < 4.13
                 , mysql       ^>= 0.1.7

    hs-source-dirs:   app
    default-language: Haskell2010
    extra-lib-dirs: /nix/store/6kl22pij3n5qppcmlg3a6m8ny19yylqj-mariadb-10.2.1/include/server -- (from `which mysql` in `nix-shell`)
    extra-libraries: mysql

我还尝试将库包含在LIBRARY_PATH=[path/to/nix/store/mysql/lib]:$LIBRARY_PATH cabal build --with-compiler ghc-8.2.2从这里

并通过cabal 文件将文件复制mysql.h[cabal-project/libs]并包含在其中;但是,行为也没有任何改变extra-lib-dirsextra-include-dirs

因为这是一个更大的项目的一部分,所以我需要使用它ghc-8.2.2cabal作为构建工具。这是否需要一些调整nix-ld(如果需要如何调整?)还是有更简单的解决方案?

标签: mysqlhaskellbuildcabalnixos

解决方案


推荐阅读