Error: file 'nixpkgs' was not found in the Nix search path

I’m getting started at nix and I wanted to use nix-shell to sandbox all the dependencies of a project I am running - Rust, IPFS and Postgres.

I used this for my shell.nix, (copy/pasted from a blog post I found, just to get started):

let
  moz_overlay = import (builtins.fetchTarball https://github.com/mozilla/nixpkgs-mozilla/archive/master.tar.gz);
  nixpkgs = import <nixpkgs> {
    overlays = [ moz_overlay ];
  };
  ruststable = (nixpkgs.latest.rustChannels.stable.rust.override {
    extensions = [ "rust-src" "rust-analysis" ];}
  );
in
  with nixpkgs;
  stdenv.mkDerivation {
    name = "rust";
    buildInputs = [ rustup ruststable ];
  }

and then when I run nix-shell in the directory, it tells me this:

error: file 'nixpkgs' was not found in the Nix search path (add it using $NIX_PATH or -I), at /Users/realisation/Code/graph-node/shell.nix:3:20
I’m getting started at nix and I wanted to use nix-shell to sandbox all the dependencies of a project I am running - Rust, IPFS and Postgres.

I used this for my shell.nix, (copy/pasted from a blog post I found, just to get started):

let
  moz_overlay = import (builtins.fetchTarball https://github.com/mozilla/nixpkgs-mozilla/archive/master.tar.gz);
  nixpkgs = import <nixpkgs> {
    overlays = [ moz_overlay ];
  };
  ruststable = (nixpkgs.latest.rustChannels.stable.rust.override {
    extensions = [ "rust-src" "rust-analysis" ];}
  );
in
  with nixpkgs;
  stdenv.mkDerivation {
    name = "rust";
    buildInputs = [ rustup ruststable ];
  }

and then when I run nix-shell in the directory, it tells me this:

error: file 'nixpkgs' was not found in the Nix search path (add it using $NIX_PATH or -I), at /Users/realisation/Code/graph-node/shell.nix:3:20

What am I doing wrong here?

No feedback? Where can I learn about what is going wrong and about how to make nix-shell environments to setup the dependencies for my projects?

I received the same error trying to set up a nix-shell in another project.

error: while evaluating anonymous function at /Users/realisation/Desktop/test/shell.nix:1:1, called from undefined position:

file 'nixpkgs' was not found in the Nix search path (add it using $NIX_PATH or -I), at /Users/realisation/Desktop/test/shell.nix:1:17

Aha, it looks like this happens when I run the command with sudo - but that quite possibly isn’t the right thing to do - I just did it because I was getting an error about not being able to download a derivation when I did not run it with sudo.

That error is this:

warning: unable to download 'http://tarballs.nixos.org/sha256/4e321a7bf05f382437dee9f10d727affe25244466690ab0e180f653a0d2f17a2': Couldn't resolve host name (6); retrying in 279 ms
warning: unable to download 'http://tarballs.nixos.org/sha256/4e321a7bf05f382437dee9f10d727affe25244466690ab0e180f653a0d2f17a2': Couldn't resolve host name (6); retrying in 532 ms
warning: unable to download 'http://tarballs.nixos.org/sha256/4e321a7bf05f382437dee9f10d727affe25244466690ab0e180f653a0d2f17a2': Couldn't resolve host name (6); retrying in 1152 ms
warning: unable to download 'http://tarballs.nixos.org/sha256/4e321a7bf05f382437dee9f10d727affe25244466690ab0e180f653a0d2f17a2': Couldn't resolve host name (6); retrying in 2782 ms
warning: unable to download 'http://tarballs.nixos.org/stdenv-darwin/x86_64/d5bdfcbfe6346761a332918a267e82799ec954d2/bootstrap-tools.cpio.bz2': Couldn't resolve host name (6); retrying in 299 ms
warning: unable to download 'http://tarballs.nixos.org/stdenv-darwin/x86_64/d5bdfcbfe6346761a332918a267e82799ec954d2/bootstrap-tools.cpio.bz2': Couldn't resolve host name (6); retrying in 610 ms
warning: unable to download 'http://tarballs.nixos.org/stdenv-darwin/x86_64/d5bdfcbfe6346761a332918a267e82799ec954d2/bootstrap-tools.cpio.bz2': Couldn't resolve host name (6); retrying in 1265 ms
warning: unable to download 'http://tarballs.nixos.org/stdenv-darwin/x86_64/d5bdfcbfe6346761a332918a267e82799ec954d2/bootstrap-tools.cpio.bz2': Couldn't resolve host name (6); retrying in 2411 ms
error: unable to download 'http://tarballs.nixos.org/stdenv-darwin/x86_64/d5bdfcbfe6346761a332918a267e82799ec954d2/bootstrap-tools.cpio.bz2': Couldn't resolve host name (6)
builder for '/nix/store/qbr6qqs9gi96r5dpamalb76725zyyvj3-bootstrap-tools.cpio.bz2.drv' failed with exit code 1