Generate a file hash similar to the one output by nix-prefetch-url

Suppose i’ve got a zip file available under some URL. I need to get its hash, which should be identical to the one output by nix-prefetch-url --unpack <URL> , but without a working Nix installation. How can one do it?

I tried to understand the C++ implementation of nix-prefetch-url in https://github.com/NixOS/nix/blob/027f6a735fa72b65825eb9fa870affed045c8f1d/src/nix/prefetch.cc#L45, but it’s a bit complicated.

I’m pretty sure that it just adds it to the nix store, which won’t work without a nix setup: https://github.com/NixOS/nix/blob/027f6a735fa72b65825eb9fa870affed045c8f1d/src/nix/prefetch.cc#L118

The nix manual has a good explanation in nix-hash though: nix-hash - Nix Reference Manual

The nix pills cover the topic, too: Nix Pills | Nix & NixOS

It’s a little more complex because it needs to deal with directory trees as well as just raw files.

If you don’t want to use nix-hash either, the implementation of the nix-hash hashing stuff is in https://github.com/NixOS/nix/blob/0b2ea0023c81e8631df549dce996833ab5213a4a/src/libutil/hash.cc. it just uses a sha256sum to my knowledge though, at least once you’ve figured out how to structure a NAR.

Thanks a lot @TLATER for a detailed response! It explains a lot. Provided links are very helpful.
So no easy way to simulate it - i’ll find a workaround then.

Regards,
Vitaliy.

You may try with GitHub - DavHau/nix-portable: Nix - Static, Permissionless, Installation-free, Pre-configured to do fetching & hashing without a proper nix store.

1 Like