Creating a Nix Package from a Stack Project

I have a software application that can be created and installed using stack . I would also like to offer a binary package for Linux and Mac. For this, I am considering nix because, by the way, it can be used on Linux and Mac. This will save me from having to support two types of packages.

After reading about how nix packages are defined, I expect that a stack based project can be built with a configuration that looks like this:

 { 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 available resources on the Internet, I cannot find any description of how this can be done. I don't know if that means stack and nix not meant to be used that way.

The only thing I could find in the guide: how stack can use nix , and stack to nix .

I am also open to alternatives for multi-platform packaging.

+5
source share
1 answer

With this recent merge , I can build and install a simple stack project using the following steps:

  • Create a new hello project with stack new hello
  • Add the following lines to stack.yaml

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

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

At this point, I can run nix-build to create the project, and the result will be in ./result/bin/hello-exe

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


Side note: nix-build will load the stack build plan every time it starts.

I want to gradually build up my project as I make changes, so I basically just drop the shell and call stack build there.

+2
source

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


All Articles