How to fetch Nixpkgs with an empty NIX PATH

From NixOS Wiki
Revision as of 20:21, 30 October 2024 by SigmaSquadron (talk | contribs) (Forgot "also")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

The NIX_PATH environment variable is not the only way to fetch a Nixpkgs instance. You can also use a pure, declarative Nixpkgs source by fetching its tarball:

default.nix
let
  nixpkgs = builtins.fetchTarball {
    url    = "https://github.com/NixOS/nixpkgs/archive/4a817d2083d6cd7068dc55511fbf90f84653b301.tar.gz";
    sha256 = "";
  };

  pkgs = import nixpkgs { config = {}; };

in
  ...

Remember to replace the example git revision (4a817d2...) with the desired revision of Nixpkgs. When building such a file for the very first time, Nix will refuse to evaluate a tarball that has an invalid or missing hash. Fortunately, the error message that will be printed contains the real hash to be used; simply replace sha256 = ""; with sha256 = "hash that Nix will give you";, and rebuild the file.

You can also use the nix-prefetch-url utility to obtain the correct SHA256 hash without needing to build a spurious derivation.

$ nix-prefetch-url --unpack "https://github.com/NixOS/nixpkgs/archive/4a817d2083d6cd7068dc55511fbf90f84653b301.tar.gz"