Difference between nix-shell and "nix run"?

@jayesh.bhoot I think a better breakdown would look like this:

use old command new command
start a new shell that has ripgrep available nix-shell -p ripgrep nix shell nixpkgs#ripgrep
start a new shell that has the dependencies required for building ripgrep available (like gcc and rustc) nix-shell -A ripgrep nix develop nixpkgs#ripgrep
build ripgrep completely within Nix. Put the resulting binary in a result/ symlink nix-build -A ripgrep nix build nixpkgs#ripgrep
run an application defined in a flake nothing similar? nix run nixpkgs#ripgrep

A couple things to keep in mind:

  • nix build, nix develop, and nix run have special support for flakes. For instance, if there is a flake.nix in the current directory, then:

    • nix build with no arguments will try to build the outputs.defaultPackage.
    • nix develop with no arguments will load the shell defined in outputs.devShell.
    • nix run with no arugments will run the command defined in outputs.defaultApp.
  • You might be able to make the argument that nix run is similar to nix-shell -p ripgrep --run 'ripgrep', but this is somewhat of a stretch.

  • nix shell may be renamed: Rename 'nix shell' · Issue #4715 · NixOS/nix · GitHub

11 Likes