2017-04-17 93 views
5

我一直對Nix感興趣,我想我最終會嘗試使用它來啓動一個新的haskell項目。使用nix構建簡單的haskell庫

我開始了與目錄結構

project.cabal 
src/Lib.hs 

凡陰謀文件具有以下內容:

name: project 
version: 0.1.0.0 
build-type: Simple 
license: MIT 
cabal-version: >= 1.18 

library 
    exposed-modules: Lib 
    build-depends: base < 5 
    hs-source-dirs: src 
    default-language: Haskell2010 

和Lib.hs具有

module Lib where 

hello :: Int -> IO() 
hello x = putStrLn (show x) 

正如你所看到的,這很簡單。當我執行cabal build時,它似乎很高興。請注意,我不是任何方式的haskell專家,所以我可能會在這裏犯一些初學者的錯誤。

要與尼克斯建立這個,我一直在閱讀https://github.com/Gabriel439/haskell-nix以獲取我的信息。我執行cabal2nix . > default.nix以獲得我的cabal文件的Nix版本。然後我創建了一個release.nix文件來實際構建它。是這兩個文件的內容如下:

default.nix

{ mkDerivation, base, stdenv }: 
mkDerivation { 
    pname = "project"; 
    version = "0.1.0.0"; 
    src = ./.; 
    libraryHaskellDepends = [ base ]; 
    license = stdenv.lib.licenses.mit; 
} 

release.nix

let 
    pkgs = import <nixpkgs> { }; 
in 
    pkgs.haskellPackages.callPackage ./default.nix { } 

這樣做後,我執行nix-build release.nix和回來

these derivations will be built: 
    /nix/store/p481alkpm89712n3hnwai0nxhmjrm8b2-project-0.1.0.0.drv 
building path(s) ‘/nix/store/yszy2a6wd88pf6zlw0nw99l5wzvc0s9x-project-0.1.0.0’ 
setupCompilerEnvironmentPhase 
Build with /nix/store/d5w12a8bprd2518xnqp1cwh3rbjiagyx-ghc-8.0.1. 
unpacking sources 
unpacking source archive /nix/store/fsn4b9w54h2jdpv546nwvy82vnkszl1w-project 
source root is project 
patching sources 
compileBuildDriverPhase 
setupCompileFlags: -package-db=/tmp/nix-build-project-0.1.0.0.drv-0/package.conf.d -j4 -threaded 
[1 of 1] Compiling Main    (/nix/store/4mdp8nhyfddh7bllbi7xszz7k9955n79-Setup.hs, /tmp/nix-build-project-0.1.0.0.drv-0/Main.o) 
Linking Setup ... 
... 
... 
Building project-0.1.0.0... 
Preprocessing library project-0.1.0.0... 
dist/build/Lib_o_split: getDirectoryContents: does not exist (No such file or 
directory) 
builder for ‘/nix/store/p481alkpm89712n3hnwai0nxhmjrm8b2-project-0.1.0.0.drv’ failed with exit code 1 
error: build of ‘/nix/store/p481alkpm89712n3hnwai0nxhmjrm8b2-project-0.1.0.0.drv’ failed 

當然哪個不好。我在這裏犯了什麼錯誤?我在一個類似的嘗試中取得了成功,那就是構建一個可執行文件而不是一個庫,所以我懷疑它與此有關。我跟隨的github回購也使用了可執行文件。

+0

在黑暗中一槍的位,但確實添加'enableSplitObjs = false;'到你的default.nix文件的幫助呢?如果錯誤:'assertion'失敗,那麼你可以嘗試'enableDeadCodeElimination = false;'而不是? – ppb

+0

@ppb'enableSplitObjs = false;'使它正確構建。爲什麼會修復這些問題?有沒有一個地方可以記錄所有的haskell nix選項?另外,如果你讓這個答案,我會接受它。 – phil

回答

0

我相信,在默認情況下尼克斯,不像普通的陰謀,將嘗試使用拆分對象特徵來構建任何Haskell的項目,per cabal's manual

--enable-split-objs

Use the GHC -split-objs feature when building the library. This reduces the final size of the executables that use the library by allowing them to link with only the bits that they use rather than the entire library. The downside is that building the library takes longer and uses considerably more memory.

我也不太清楚,爲什麼可以上你的失敗系統,但根據您的nixpkgs版本可以通過添加一個被禁用:

enableSplitObjs = false;

enableDeadCodeElimination = false;

來推導。

有關其他屬性/選項的列表,您可以參考https://github.com/NixOS/nixpkgs/blob/master/pkgs/development/haskell-modules/generic-builder.nix不幸的是,我不知道任何官方文檔更詳細地描述這些屬性。