Create a simple haskell library using nix

I was interested in Nix for a while, and I thought that I would finally try to use it to launch a new haskell project.

I started with the directory structure

project.cabal src/Lib.hs 

If the cabal file has the following contents:

 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 

and lib.hs has

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

As you can see, this is pretty simple. When I do cabal build , it seems happy. Please note that I am not a haskell expert in any way, so I can make a beginner mistake here.

To build this with Nix, I read https://github.com/Gabriel439/haskell-nix to get the info. I performed cabal2nix . > default.nix cabal2nix . > default.nix to get the Nix version of my cabal file. Then I created a release.nix file to create it. The contents of these two files are as follows:

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 { } 

After that I executed nix-build release.nix and returned

 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 

Which, of course, is bad. What mistake am I making here? I succeeded in a similar attempt, which consisted of creating an executable file instead of a library, so I suspect this has something to do with it. The gitub repo I followed was also using an executable.

+5
source share
1 answer

I believe that by default, nix, unlike a simple cabal, will try to build any Haskell project using the shared object function, for each tutorial on bondage :

- enable split OBJS

Use the GHC function -split-objs when creating the library. This reduces the final size of the executables that use the library, allowing them to communicate only with the bits that they use, and not with the whole library. The disadvantage is that building a library takes longer and uses significantly more memory.

I'm not sure why this might crash on your system, but depending on your version, nixpkgs can be disabled by adding one of:

enableSplitObjs = false;

enableDeadCodeElimination = false;

to the conclusion.

A list of other other attributes / options can be found at https://github.com/NixOS/nixpkgs/blob/master/pkgs/development/haskell-modules/generic-builder.nix Unfortunately, I do not know any official documentation. describing in more detail.

0
source

Source: https://habr.com/ru/post/1266800/


All Articles