How to use nix-shell properly and avoid "dumping very large path"?

Late to the party, but (since I can't comment on niksnut's correct answer) I wanted to mention a means of handling this if you want to add some subset of src to the Nix store, filtering out large/unneeded files.

This approach uses lib.cleanSource and friends from nixpkgs:

# shell.nix
{ pkgs ? import <nixpkgs> {} }:

with pkgs;

let
  cleanVendorFilter = name: type: 
    type == "directory" && baseNameOf (toString name) == "vendor";
  cleanVendor = src: lib.cleanSourceWith { filter = cleanVendorFilter; inherit src; };
  shellSrc = lib.cleanSource (cleanVendor ./.);
in mkShell {
  name = "my-shell";
  shellHook = ''
    printf 1>&2 '# Hello from store path %s!\n' ${shellSrc}
  '';
}

In the above snippet, shellSrc refers to an attribute set representing a store path that contains ./., but without the vendor subdirectory (cleanVendor) and without .git, .svn, files ending in ~, and other editor/VCS-related stuff (cleanSource).

Check out lib/sources.nix for more ways to filter paths.


Probably the src attribute (the current directory) is very big. nix-shell will copy it to the Nix store on each invocation, which is probably not what you want/need. The workaround is to write:

src = if lib.inNixShell then null else ./.;

(where lib comes from Nixpkgs).

This way, ./. will be copied when you invoke nix-build, but not when you run nix-shell.

Tags:

Ocaml

Nix