Replace nixpkgs fetchers with nix builtins fetchers?

Nix provides some builtins to fetch source code. It seems that we could use those instead of the nixpkgs fetchers.

Does it make sense to replace nixpkgs fetchers with the Nix fetchers?

It also seems that the builtin fetchers are doing the right thing:

$ nix-store -r $(nix-instantiate '<nixpkgs>' -A fetchgit --argstr url "https://github.com/NixOS/nix" --argstr hash "sha256-MtVatZVsV+dtjdD4AC4bztrnDFas+WZYHzQMt41FwzU=" --argstr rev "refs/tags/2.13.2")
/nix/store/clcvzin5nbaw8df7p3fqxicqsv135bwx-nix

$ nix-store -r $(nix-instantiate '<nixpkgs>' -A fetchgit --argstr url "https://github.com/NixOS/nix" --argstr hash "sha256-MtVatZVsV+dtjdD4AC4bztrnDFas+WZYHzQMt41FwzU=" --argstr rev "4935cf4d9b74405c753942197c9f328075793502")
/nix/store/kvjczzxhy5xvihyz301cwsamss51r56j-nix-4935cf4

$ nix-store -r $(nix-instantiate '<nixpkgs>' -A fetchzip --argstr url "https://github.com/NixOS/nix/archive/refs/tags/2.13.2.tar.gz" --argstr hash "sha256-MtVatZVsV+dtjdD4AC4bztrnDFas+WZYHzQMt41FwzU=")
/nix/store/7ncnkh5irwaii8gv028bb0cylb5y57x5-source

vs

$ nix eval --expr '(builtins.fetchGit { url= "https://github.com/NixOS/nix"; ref="refs/tags/2.13.2"; allRefs=true; narHash="sha256-MtVatZVsV+dtjdD4AC4bztrnDFas+WZYHzQMt41FwzU=";}).outPath'
/nix/store/7ncnkh5irwaii8gv028bb0cylb5y57x5-source

$ nix eval --expr '(builtins.fetchGit { url= "https://github.com/NixOS/nix"; rev = "4935cf4d9b74405c753942197c9f328075793502"; allRefs=true; narHash="sha256-MtVatZVsV+dtjdD4AC4bztrnDFas+WZYHzQMt41FwzU=";}).outPath'
/nix/store/7ncnkh5irwaii8gv028bb0cylb5y57x5-source

$ nix eval --expr '(builtins.fetchTarball {url="https://github.com/NixOS/nix/archive/refs/tags/2.13.2.tar.gz"; sha256="sha256-MtVatZVsV+dtjdD4AC4bztrnDFas+WZYHzQMt41FwzU=";})'
/nix/store/7ncnkh5irwaii8gv028bb0cylb5y57x5-source

The hash is always the same. We get 3 different store paths with the nixpkgs fetchers, but with the builtin fetchers we get always the same store path.

Not saying we shouldn’t do that, but the main reason that we don’t use builtins fetchers is because they run at eval time and return paths instead of derivations, and therefore can’t be served as binary cache.

1 Like

You can also get the same store paths from the nixpkgs fetchers by adding --argstr name source.

1 Like

Built-in fetchers block the evaluator and return a valid path when it’s been downloaded.

Fixed-output derivation based fetchers (ie nixpkgs) return a string with context to represent the downloaded path, so that it doesn’t block the evaluator. This is why you can evaluate a package without having to wait for a thousand irrelevant source dependencies to download, one by one, sequentially.

9 Likes