`nix shell` equivalent of `nix shell -p ARBITRARY_EXPRESSION`

With nix-shell, I often jump into an environment with a derivation created from an arbitrary Nix expression using values from Nixpkgs.

For instance, here is an example using haskellPackages.ghcWithPackages from Nixpkgs:

$ nix-shell -p 'haskellPackages.ghcWithPackages (p: [ p.turtle ])'

This drops you into a shell with ghc available. ghc has a package database with the Haskell package turtle.

The neat thing is that nix-shell -p accepts any arbitrary expression, and everything from the top-level in Nixpkgs is in-scope. This is quite convenient.


How would I do this with nix shell ? It looks like nix shell has an --expr flag, but this is a lot more work than nix-shell -p.

Here’s a similar thing with nix shell:

$ nix shell --impure --expr 'with import <nixpkgs> {}; haskellPackages.ghcWithPackages (p: [ p.turtle ])'

Here’s what’s annoying with this:

  • I have to remember the --impure flag. There’s also no short-flag for this:

    $ nix shell --expr 'with import <nixpkgs> {}; haskellPackages.ghcWithPackages (p: [ p.turtle ])'
    error: cannot look up '<nixpkgs>' in pure evaluation mode (use '--impure' to override)
    
  • There is no short flag for --expr (unlike nix-shell which has -E):

    $ nix shell --impure -E 'with import <nixpkgs> {}; haskellPackages.ghcWithPackages (p: [ p.turtle ])'
    error: unrecognised flag '-E'
    
  • I need the explicit import <nixpkgs> {}, otherwise nothing is in scope:

    $ nix shell --impure --expr 'haskellPackages.ghcWithPackages (p: [ p.turtle ])'
    error: undefined variable 'haskellPackages'
    
4 Likes

according to edolstra, the fact that -p worked for this was a bug [Question] How to use <pkg>.override with nix-build? · Issue #1401 · NixOS/nix · GitHub

1 Like