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

2

7

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
Cargill answered 10/10, 2019 at 15:54 Comment(0)
L
4

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 ];
} ""
Lugo answered 30/5, 2020 at 23:52 Comment(0)
S
4

I got it to work by creating a GHC environment using a haskell+nix tutorial. Below is my release.nix and default.nix is generated via cabal2nix . > default.nix : (note the zlib as a part of the haskell packages to include in the ghcWithPackages call)

let
  config = 
  {
    packageOverrides = pkgs: 
    {
      haskellPackages = pkgs.haskellPackages.override 
      {
        overrides = new: old:
        {
          interview = new.callPackage ./default.nix;
        };
      };
    };
  };

  pkgs = import <nixpkgs> { inherit config; };

  ghc = pkgs.haskellPackages.ghcWithPackages (hpkgs: with hpkgs;
    [ cabal-install
      interview
      zlib
    ]
  );
in

with pkgs;

mkShell 
{ buildInputs = [ ghc ];
}

Skintight answered 17/10, 2019 at 17:12 Comment(0)
L
4

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 ];
} ""
Lugo answered 30/5, 2020 at 23:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.