How to get cabal and nix work together

∥☆過路亽.° 提交于 2019-11-28 15:38:54

问题


As far as I understood, Nix is alternative for cabal sandbox. I finally managed to install Nix, but I still don't understand how it can replace a sandbox.

I understand you don't need cabal using Nix and the wrapped version of GHC; however if you want to publish a package you'll need at some point to package it using cabal. Therefore, you need to be able to write and test your cabal configuration within NIX. How do you do that?

Ideally, I would like an environment similar to cabal sandbox but "contained" within NIX, is that possible? In fact, what I really would like is the equivalent of nested sandboxes — as I usually work on projects made of multiple packages.

Update about my current workflow

At the moment I work on 2 or 3 independent projects (P1, P2, P3) which are each composed of 2 or 3 cabal modules/packages, let's say for P1: L11, L12 (libraries) and E11 (executables). E11 depends on L12 which depends on L11. I mainly split the executables from the library because they are private and kept on a private git repo.

In theory, each project could have this own sandbox (shared between its submodules). I tried that (having a common sandbox for L11 L12 and E11), but it's quickly annoying because, if you modify L11, you can't rebuild it because E11 depends on it, so I have to uninstall E11 first to recompile L11. It might be no exactly the case, but I encounter the similar problem. This would be fine if I were occasionally modifying L11, but in practice, I changed it more that E11.

As the shared sandbox doesn't work, so I went back to the one sandbox for every package solution. It's working but is less than ideal. The main problem is if I modify L11, I need to compile it twice (once in L11, and then again in E11). Also, each time I'm starting a new sandbox, as everybody knows, I need to wait a while to get everything package downloaded and recompiled.

So by using Nix, I'm hopping to be able to set up separate cabal "environments" per project, which solves all the issue aboves.

Hope this is clearer.


回答1:


I do all my development using Nix and cabal these days, and I can happily say that they work in harmony very well. My current workflow is very new, in that it relies on features in nixpkgs that have only just reached the master branch. As such, the first thing you'll need to do is clone nixpkgs from Github:

cd ~
git clone git://github.com/nixos/nixpkgs

(In the future this won't be necessary, but right now it is).

Single Project Usage

Now that we have a nixpkgs clone, we can start using the haskellng package set. haskellng is a rewrite of how we package things in Nix, and is of interest to us for being more predictable (package names match Hackage package names) and more configurable. First, we'll install the cabal2nix tool, which can automate some things for us, and we'll also install cabal-install to provide the cabal executable:

nix-env -f ~/nixpkgs -i -A haskellngPackages.cabal2nix -A haskellngPackages.cabal-install

From this point, it's all pretty much clear sailing.

If you're starting a new project, you can just call cabal init in a new directory, as you would normally. When you're ready to build, you can turn this .cabal file into a development environment:

cabal init
# answer the questions
cabal2nix --shell my-project.cabal > shell.nix

This gives you a shell.nix file, which can be used with nix-shell. You don't need to use this very often though - the only time you'll usually use it is with cabal configure:

nix-shell -I ~ --command 'cabal configure'

cabal configure caches absolute paths to everything, so now when you want to build you just use cabal build as normal:

cabal build

Whenever your .cabal file changes you'll need to regenerate shell.nix - just run the command above, and then cabal configure afterwards.

Multiple Project Usage

The approach scales nicely to multiple projects, but it requires a little bit more manual work to "glue" everything together. To demonstrate how this works, lets consider my socket-io library. This library depends on engine-io, and I usually develop both at the same time.

The first step to Nix-ifying this project is to generate default.nix expressions along side each individual .cabal file:

cabal2nix engine-io/engine-io.cabal > engine-io/default.nix
cabal2nix socket-io/socket-io.cabal > socket-io/default.nix

These default.nix expressions are functions, so we can't do much right now. To call the functions, we write our own shell.nix file that explains how to combine everything. For engine-io/shell.nix, we don't have to do anything particularly clever:

with (import <nixpkgs> {}).pkgs;
(haskellngPackages.callPackage ./. {}).env

For socket-io, we need to depend on engine-io:

with (import <nixpkgs> {}).pkgs;
let modifiedHaskellPackages = haskellngPackages.override {
      overrides = self: super: {
        engine-io = self.callPackage ../engine-io {};
        socket-io = self.callPackage ./. {};
      };
    };
in modifiedHaskellPackages.socket-io.env

Now we have shell.nix in each environment, so we can use cabal configure as before.

The key observation here is that whenever engine-io changes, we need to reconfigure socket-io to detect these changes. This is as simple as running

cd socket-io; nix-shell -I ~ --command 'cabal configure'

Nix will notice that ../engine-io has changed, and rebuild it before running cabal configure.



来源:https://stackoverflow.com/questions/27968909/how-to-get-cabal-and-nix-work-together

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!