9

I have a software application that can be built and installed with stack. I would like to offer a binary package as well for Linux and Mac. For this purpose I'm considering nix, since, among other things, it can be used in Linux and Mac. This will save me the trouble of having to maintain two package types.

After reading about how nix packages are defined, I would expect that a stack based project could be built with a configuration that would look like:

{ stdenv, fetchurl, stack }: # we need to depend on stack

stdenv.mkDerivation { 
  name = "some-haskell-package-0.1"; 
  builder = ./builder.sh; # here we would call `stack install` 
  src = fetchurl {  # ...
  };
}

Looking at the resources available online, I cannot find any description of how this could be done. I don't know if this means that stack and nix are not intended to be used in this way.

The only thing I could find in the manual is how stack can use nix, and a stack to nix conversion tool.

I'm also open to alternatives for multi-platform packaging.

Damian Nadales
  • 4,907
  • 1
  • 21
  • 34
  • 1
    The Nix Manual has a section called "How to build a Haskell project using Stack" https://nixos.org/nixpkgs/manual/#how-to-build-a-haskell-project-using-stack . Use `haskell.lib.buildStackProject` instead of `stdenv.mkDerivation`. – MarcDefiant Oct 27 '17 at 14:21

1 Answers1

5

With this recent merge, I am able to build and installed a simple stack project using following steps:

  1. Create new hello project with stack new hello
  2. Add following lines to stack.yaml

    nix:
      enable: true
      shell-file: default.nix
    
  3. Create default.nix

    with (import <nixpkgs> { });
    
    haskell.lib.buildStackProject {
      name = "hello";
      src = ./.;
    }
    

At this point, I am able to run nix-build to build the project and result will be at ./result/bin/hello-exe

If I want to install, I would run nix-env -i -f default.nix


Side note: nix-build will download stack's build plan every time it run.

I want to incrementally build my project while making changes so I mainly just drop in the shell and call stack build there.

wizzup
  • 2,361
  • 20
  • 34
  • Any ideas on how to use the latest `nix` (that includes those changes)? – Damian Nadales Nov 07 '17 at 09:19
  • 1
    @DamianNadales You could use `-I nixpkgs=/path/to/repo` flag with most `nix-*` command. If you don't want to clone [nixpkgs](https://github.com/NixOS/nixpkgs) you can just use the snapshot url provided by github like this `nix-shell -I nixpkgs=https://github.com/NixOS/nixpkgs/archive/master.tar.gz -p stack` – wizzup Nov 08 '17 at 05:49
  • I get the following error: derivation '/nix/store/xxx.drv' has '__noChroot' set, but that's not allowed when 'sandbox' is 'true' – luochen1990 Nov 03 '21 at 10:02