I’d like to implement the logic to generate the nix hash of a directory.
I want to get the hash that
nix hash path /path/to/dir gives you, but without relaying on nix (I want to re-implement that logic in a different programming language)
It looks to me that the C++ implementation is here:
But is that algorithm explained/documented somewhere?
I remember having seen it explained somewhere, though I am not sure if that was in an official documentation.
The gist of what I read was:
- strip all file attributes and permissions and ownerships that can’t exist in the store
- store the folder in a NAR (nix archive)
- Get the hash of that
Then, the next question is how to generate a NAR. I found this gist:
I’m not sure if the details have changed, but nobbz might be thinking of Eelco’s thesis–I think there’s a section in there on the hashing at least: https://edolstra.github.io/pubs/phd-thesis.pdf
I implemented the NAR serialization algorithm from https://edolstra.github.io/pubs/phd-thesis.pdf (Figure 5.2, page 101) in clojure:
The hash from the NAR is the hash for a directory
Thanks to you both @NobbZ @abathur