nix-prefetch-url(1)
β¨ nix-prefetch-url(1) β General Commands Manual β¨
okay bestie, sit down, put your phone on do-not-disturb, because today we're learning about the little command that will save you from downloading the same tarball twice like some kind of Victorian peasant. it's from the General Commands Manual, it's one page, and it's iconic. π
π NAME π
nix-prefetch-url β copy a file from a URL into the store and print its hash
π¦ SYNOPSIS π¦
nix-prefetch-url url [hash] [--type hashAlgo] [--print-path] [--unpack]
[--name name]
πΈ DESCRIPTION πΈ
so what does this thing actually do?? the command nix-prefetch-url downloads the file that the URL url points at, prints out its cryptographic hash, and copies it into the Nix store. the file name it lands under in the store is hash-baseName, where baseName is literally everything after the final slash in url. no cap.
now, the tea: this command is just a convenience for Nix expression writers. bestie, you know the vibe β a Nix expression often fetches some source distribution from the network using the fetchurl expression in Nixpkgs. but plot twist, fetchurl needs a cryptographic hash. and if you don't know the hash?? you'd have to download the file first, and then fetchurl would download it again when you build your Nix expression. wasting bandwidth?? in this economy?? π
the slay, however, is this: since fetchurl uses the same name for the downloaded file as nix-prefetch-url does, that whole redundant download can be avoided. we love a collaborative queen. π«Ά
if hash is specified, then a download is not performed when the Nix store already contains a file with the same hash and base name. no redundant nothing. otherwise, the file does get downloaded, and an error is signaled if the actual hash of the file doesn't match the hash you specified. (yes, it will absolutely catch you lying. π«’)
this command prints the hash on standard output. the hash is printed using Nix32 unless --type md5 is specified, in which case it's printed using base-16. and additionally, if the option --print-path is used, the path of the downloaded file in the Nix store is also printed. two-for-one, main character energy. π
π OPTIONS π
--type hashAlgoβ use the specified cryptographic hash algorithm, which can be one ofblake3,md5,sha1,sha256, andsha512. the default issha256. pick your fighter. β--print-pathβ print the store path of the downloaded file on standard output.--unpackβ unpack the archive (which must be a tarball or zip file) and add the result to the Nix store. the resulting hash can be used with functions such as Nixpkgs'sfetchziporfetchFromGitHub. π--executableβ set the executable bit on the downloaded file.--name nameβ override the name of the file in the Nix store. by default, this ishash-basename, wherebasenameis the last component ofurl. overriding the name is necessary whenbasenamecontains characters that are not allowed in Nix store paths. (nix store paths are picky, respect their boundaries bestie. π«)
π₯ EXAMPLES π₯
the moment you've all been waiting for. watch and learn:
$ nix-prefetch-url ftp://ftp.gnu.org/pub/gnu/hello/hello-2.10.tar.gz
0ssi1wpaf7plaswqqjwigppsg5fyh99vdlb9kzl7c9lng89ndq1i
$ nix-prefetch-url --print-path mirror://gnu/hello/hello-2.10.tar.gz
0ssi1wpaf7plaswqqjwigppsg5fyh99vdlb9kzl7c9lng89ndq1i
/nix/store/8alrpdaasjd1x6g1fczchmzbpqm936a3-hello-2.10.tar.gz
$ nix-prefetch-url --unpack --print-path https://github.com/NixOS/patchelf/archive/0.8.tar.gz
079agjlv0hrv7fxnx9ngipx14gyncbkllxrp9cccnh3a50fxcmy7
/nix/store/19zrmhm3m40xxaw81c8cqm6aljgrnwj2-0.8.tar.gz
see how the hash comes out and the path just shows up when you ask nicely with --print-path?? obsessed. π
fr fr, if you pass a hash and it doesn't match what you actually got, it errors out. don't try to slip a fake hash past it, it's not the one. π
aaaand that's the page!! π go forth and prefetch, you gorgeous Nix wizard you. this has been nix-prefetch-url(1) from the General Commands Manual β thank u for coming to my ted talk, mwah. πβ¨πΈ
π¦ where this page comes from
- fedora 44: nix-legacy 2.35.3-2.fc44 Β· LGPL-2.1-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms