Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add builtins.hashFile #2792

Merged
merged 1 commit into from May 7, 2019
Merged

Conversation

JohnAZoidberg
Copy link
Member

@JohnAZoidberg JohnAZoidberg commented May 3, 2019

For text files it is possible to do it like so:
builtins.hashString "sha256" (builtins.readFile /tmp/a)
but that doesn't work for binary files.
With builtins.hashFile any kind of file can be conveniently hashed.

Fixes: #1835

I added some tests and also tested it manually in the repl:

nix-repl> builtins.readFile ./foo
"blah"

nix-repl> builtins.hashString "sha256" "blah"
"8b7df143d91c716ecfa5fc1730022f6b421b05cedee8fd52b1fc65a96030ad52"

nix-repl> builtins.hashFile "sha256" ./foo
"8b7df143d91c716ecfa5fc1730022f6b421b05cedee8fd52b1fc65a96030ad52"

Also works on binary files:

nix-repl> builtins.hashFile "sha256" ./inst/bin/nix
"bad8124620c1c5538b3a3317fb10cbe7d1bd1b65b692bc3717ae98933022f992"

$ sha256sum ./inst/bin/nix
bad8124620c1c5538b3a3317fb10cbe7d1bd1b65b692bc3717ae98933022f992  ./inst/bin/nix

I'm not sure why my test in tests/lang/eval-fail-hashfile-missing.nix isn't working. It evaluates but it shouldn't. In the repl it doesn't evaluate.

This is my first edit in the Nix source so please be kind if I forgot something ;)


By the way: I found it really weird that EvalState::coerceToPath and EvalState::forceString take the same arguments but in a different order. Is that intentional or is that something which we should fix?

src/libexpr/primops.cc Outdated Show resolved Hide resolved
let
paths = [ ./this-file-is-definitely-not-there-7392097 "/and/neither/is/this/37293620" ];
in
builtins.concatLists (map (hash: map (builtins.hashFile hash) paths) ["md5" "sha1" "sha256" "sha512"])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Having more than one test here is probably pointless since evaluation will fail on the first one, if I'm not mistaken.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right. But if you run make installcheck the test will fail i.e. the evaluation succeeds. But I think something else is wrong which makes the test fail.

For text files it is possible to do it like so:
`builtins.hashString "sha256" (builtins.readFile /tmp/a)`
but that doesn't work for binary files.

With builtins.hashFile any kind of file can be conveniently hashed.
@edolstra edolstra merged commit 7becb1b into NixOS:master May 7, 2019
@Ekleog
Copy link
Member

Ekleog commented May 7, 2019

I have no objection to this change, but just wanted to suggest that we might want to have changes to the nix language (including adding builtins) go through an RFC in the future, so that more eyes could view the changes and think of the overall consistency of the nix language than there are people watching the nix repo :)

@JohnAZoidberg
Copy link
Member Author

I have no objection to this change, but just wanted to suggest that we might want to have changes to the nix language (including adding builtins) go through an RFC in the future, so that more eyes could view the changes and think of the overall consistency of the nix language than there are people watching the nix repo :)

Yes, good idea. Didn't think about it for long because it's not a big change.

@edolstra wait! Sorry, I should've probably called the PR WIP. The test that should fail, succeeds. At least on my system. Can you confirm?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

builtins.hashFile
3 participants