Reputation: 359
I want to write a shell.nix
file that will provide me a development environment which includes developer tools + haskell dependencies. I would like to keep my nix expressions separate - for now that means one file containing information about my editor, and one file containing information about my haskell project.
I have tried to set up a really basic environment, but can't get it to work. I have two files which I can create a nix-shell
from already:
# my-haskell-shell.nix - generated by cabal2nix --shell
{ nixpkgs ? import <nixpkgs> {}, compiler ? "default", doBenchmark ? false }:
let
inherit (nixpkgs) pkgs;
f = { mkDerivation, base, cabal-install, hpack, stdenv }:
mkDerivation {
pname = "hello-world";
version = "0.1.0.0";
src = ./.;
isLibrary = false;
isExecutable = true;
libraryToolDepends = [ hpack ];
executableHaskellDepends = [ base ];
executableToolDepends = [ cabal-install hpack ];
preConfigure = "hpack";
license = stdenv.lib.licenses.mit;
};
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
# my-neovim-shell.nix
with import <nixpkgs> {};
let
my-neovim = neovim.override { vimAlias = false; };
in
stdenv.mkDerivation {
name = "neovim-dev-env";
buildInputs = [
my-neovim
];
}
Both of these produce useful shell environments! (That is, nix-shell my-haskell-env.nix
and nix-shell my-neovim-env.nix
give me cabal and neovim respectively, but not both).
My attempt at producing a shell.nix
which will provide an environment with both neovim and cabal available is this:
# shell.nix
with import <nixpkgs> {};
let
my-neovim-shell-env = import ./my-neovim-shell.nix;
my-haskell-shell-env = import ./my-haskell-shell.nix {};
in
stdenv.mkDerivation {
name = "my-new-env";
buildInputs = [
my-neovim-shell-env
my-haskell-shell-env
];
}
This does not work however. It seems to try to build the neovim environment:
$ nix-shell
these derivations will be built:
/nix/store/k9ygid1wl75vf2nq7jzfh32mv5f8i956-neovim-dev-env.drv
building '/nix/store/k9ygid1wl75vf2nq7jzfh32mv5f8i956-neovim-dev-env.drv'...
unpacking sources
variable $src or $srcs should point to the source
builder for '/nix/store/k9ygid1wl75vf2nq7jzfh32mv5f8i956-neovim-dev-env.drv' failed with exit code 1
error: build of '/nix/store/k9ygid1wl75vf2nq7jzfh32mv5f8i956-neovim-dev-env.drv' failed
I don't know how to fix this; what am I doing wrong? I suspect maybe I should not be using mkDerivation?
Upvotes: 3
Views: 2260
Reputation: 359
Managed to find a solution. Maybe there is a better way, but this works:
Suppose you have a package.yaml, then
$ cabal2nix . > app.nix
# shell.nix
let
p = import <nixpkgs> {};
app = p.haskellPackages.callPackage (./app.nix) {};
lib = p.haskell.lib;
in
lib.overrideCabal app (old: { buildTools = [p.my-nvim] ++ (old.buildTools or []); })
(where I've added my-nvim
to my ~/.config/nixpkgs/overlays/
)
Now I have access to the my-nvim package and ghc, etc:
$ nix-shell
$ nvim # works
$ ghc # works
Inspiration from https://github.com/p-implies-q/nix-util
Upvotes: 2