haskellnixcabal-installnix-shell

Equivalent of passing `-p zlib` argument to `nix-shell` in `shell.nix`


If I build my Haskell project under nix-shell it gives error about missing zlib.

If I build the project in nix-shell using nix-shell -p zlib then the project sees zlib and builds successfully.

How can I add the zlib package to shell.nix file so that passing -p zlib is no longer needed?

Note: the builds were done using cabal v2-build

For building with stack I had to do the following

This is how my shell.nix is currently defined:

{ nixpkgs ? import <nixpkgs> {}, compiler ? "default", doBenchmark ? false }:

let

  inherit (nixpkgs) pkgs;

  f = { mkDerivation, base, directory, stdenv, text, turtle, zlib }:
      mkDerivation {
        pname = "haskell-editor-setup";
        version = "0.1.0.0";
        src = ./.;
        isLibrary = false;
        isExecutable = true;
        executableHaskellDepends = [ base directory text turtle zlib ];
        description = "Terminal program that will set up Haskell environment with a target selected editor or IDE";
        license = stdenv.lib.licenses.gpl3;
      };

  haskellPackages = if compiler == "default"
                       then pkgs.haskellPackages
                       else pkgs.haskell.packages.${compiler};

  variant = if doBenchmark then pkgs.haskell.lib.doBenchmark else pkgs.lib.id;

  drv = variant (haskellPackages.callPackage f {});

in

  if pkgs.lib.inNixShell then drv.env else drv

Solution

  • You want to create a derivation which has those as dependencies. There's a trivial builder called runCommand which runs a script you give it. For shell.nix the builder won't actually be used, it's only for providing a shell, so you can give this an empty script:

    with import <nixpkgs> { };
    
    runCommand "my-shell" {
      buildInputs = [ zlib ];
    } ""