4

I'm using the splendid Haskell library Miso, which recommends using Nix. The README walks us through a simple project which can be built with nix-build. The documentation for Miso hints that I can do:

nix-shell -A env
cabal configure --ghcjs
cabal build

which also builds the project, although it places the result in a different place.

Are nix-build and cabal build within nix-shell guaranteed to produce the same output? More generally, given a .nix expression how would I workout what steps (such as cabal configure) are required to replicate it's behavior?

Shersh
  • 9,019
  • 3
  • 33
  • 61

1 Answers1

6

In general, you are not guaranteed that the output of nix-build and the same steps in a nix-shell will equivalent. Some causes of differences:

  • nix-build can run the build in a sandbox, whereas nix-shell will not.
  • nix-shell can be invoked without the --pure flag, so that many more environment variables will be set (this lets you use GUI applications for example)
  • Although nix-shell sets the $out environment variable, it is not writable
  • A process in nix-shell runs as your user, whereas nix-build with the daemon enabled will typically run it as nixbld<n>, even with sandboxing disabled

And there's probably more I didn't think of right now.

For cabal-install and plain ghc in particular, a consequence of running without the sandbox is that it can access your user package db. This requires some care when using these tools in a nix-shell; see this answer.

Robert Hensing
  • 6,708
  • 18
  • 23