I’m trying to create a flake to build the simplest possible Haskell project. It will be another tutorial in my nix-for-numbskulls series.
I’ve seen the example using cabal2nix, but I’d prefer to use the much simpler haskellPackages.developPackage, if possible. Here’s what I’ve got so far:
$ nix build
error: cannot look up '<nixpkgs>' in pure evaluation mode (use '--impure' to override)
at /nix/store/w7sbcj28gkzps7x1ysvzn4sd3g9pnb1i-source/flake.nix:11:23:
10| let
11| pkgs = import <nixpkgs> { };
| ^
12| in
(use '--show-trace' to show detailed location information)
So I tried…
$ nix build --impure
error: builder for '/nix/store/1n0lk1cd5w5nnandy6d29rfq6rrwc8gz-hello-haskell-flake-0.1.0.0.drv' failed with exit code 1;
last 10 log lines:
> $, called at libraries/Cabal/Cabal/Distribution/Simple/Configure.hs:1024:20 in Cabal-3.2.1.0:Distribution.Simple.Configure
> configureFinalizedPackage, called at libraries/Cabal/Cabal/Distribution/Simple/Configure.hs:477:12 in Cabal-3.2.1.0:Distribution.Simple.Configure
> configure, called at libraries/Cabal/Cabal/Distribution/Simple.hs:625:20 in Cabal-3.2.1.0:Distribution.Simple
> confHook, called at libraries/Cabal/Cabal/Distribution/Simple/UserHooks.hs:65:5 in Cabal-3.2.1.0:Distribution.Simple.UserHooks
> configureAction, called at libraries/Cabal/Cabal/Distribution/Simple.hs:180:19 in Cabal-3.2.1.0:Distribution.Simple
> defaultMainHelper, called at libraries/Cabal/Cabal/Distribution/Simple.hs:116:27 in Cabal-3.2.1.0:Distribution.Simple
> defaultMain, called at /nix/store/4mdp8nhyfddh7bllbi7xszz7k9955n79-Setup.hs:2:8 in main:Main
> Setup: Encountered missing or private dependencies:
> hello-haskell-flake -any
>
For full logs, run 'nix log /nix/store/1n0lk1cd5w5nnandy6d29rfq6rrwc8gz-hello-haskell-flake-0.1.0.0.drv'.
You are importing <nixpkgs> (which does a lookup in NIX_PATH and is completely independent from the nixpkgs variable in scope) instead of plain nixpkgs.
There seems to have been a recent thread on the topic of legacyPackages/import:
I’ve also just realised that the cabal2nix approach is much easier than I thought, and should meet my needs. At first, I was put off by the mere mention of cabal2nix because when I used it (back in ye olden days), you had to run it every time you modified your cabal file. That’s why I switched to developPackage in the first place. Reading more carefully, I now understand that the flake will call cabal2nix for me; I just set it up and forget it.
Well this is no different from running it manually, you can just use nix (via import from derivation) to run cabal2nix in a derivation for you. That possibility has been around for quite some time (in fact it’s used by developPackage internally), but comes with its own disadvantages (mainly negatively impacting eval time).