Jamie Ballingall
Jamie Ballingall

Reputation: 289

Can I replicate what nix-build does with nix-shell and cabal build?

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?

Upvotes: 4

Views: 303

Answers (1)

Robert Hensing
Robert Hensing

Reputation: 7359

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.

Upvotes: 6

Related Questions