# Why wouldn't the sha256 hash change when fetching from a different URL (when updating a package)?

**URL:** https://discourse.nixos.org/t/why-wouldnt-the-sha256-hash-change-when-fetching-from-a-different-url-when-updating-a-package/11745
**Category:** Help
**Created:** [March 2, 2021, 5:33am UTC](https://discourse.nixos.org/t/why-wouldnt-the-sha256-hash-change-when-fetching-from-a-different-url-when-updating-a-package/11745 "2021-03-02T05:33:19Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![toraritte](https://discourse.nixos.org/user_avatar/discourse.nixos.org/toraritte/32/6777_2.png) [@toraritte](https://discourse.nixos.org/u/toraritte)
#### Post date: [March 2, 2021, 5:33am UTC](https://discourse.nixos.org/t/why-wouldnt-the-sha256-hash-change-when-fetching-from-a-different-url-when-updating-a-package/11745/1 "2021-03-02T05:33:19Z")

</div>

I’m trying to do my first pull request to Nixpkgs to upgrade the `docbook5` package from `5.0.1` to `5.1`, and when I changed in its [`default.nix`](https://github.com/NixOS/nixpkgs/blob/84cf00f98031e93f389f1eb93c4a7374a33cc0a9/pkgs/data/sgml%2Bxml/schemas/docbook-5.0/default.nix) the `version` line to `5.1`,

```nix
stdenv.mkDerivation rec {
  pname = "docbook5";
# version = "5.0.1";
  version = "5.1";

  src = fetchurl {
    url = "http://www.docbook.org/xml/${version}/docbook-${version}.zip";
    sha256 = "1iz3hq1lqgnshvlz4j9gvh4jy1ml74qf90vqf2ikbq0h4i2xzybs";
  };

```

I expected `nix-build -A docbook5` to fail by showing the correct hash but it succeeded without errors instead.

---

<div class="post-metadata">

### Author: ![toraritte](https://discourse.nixos.org/user_avatar/discourse.nixos.org/toraritte/32/6777_2.png) [@toraritte](https://discourse.nixos.org/u/toraritte)
#### Post date: [March 2, 2021, 5:35am UTC](https://discourse.nixos.org/t/why-wouldnt-the-sha256-hash-change-when-fetching-from-a-different-url-when-updating-a-package/11745/2 "2021-03-02T05:35:54Z")

</div>

Well, of course it didn’t fail because it automatically tried the cached path based on the current `sha256` hash, and it built the same package, but with a different name.

```plaintext
trying http://tarballs.nixos.org/sha256/1iz3hq1lqgnshvlz4j9gvh4jy1ml74qf90vqf2ikbq0h4i2xzybs
  % Total % Received % Xferd Average Speed Time Time Time Current
                                 Dload Upload Total Spent Left Speed
  0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0
100 816k 100 816k 0 0 664k 0 0:00:01 0:00:01 --:--:-- 2234k

```

Replaced the 52 characters of the `sha256` hash with zeroes, and now it worked as expected.

---

<div class="post-metadata">

### Author: ![anon31783435](https://discourse.nixos.org/letter_avatar_proxy/v4/letter/a/e0b2c6/32.png) [@anon31783435](https://discourse.nixos.org/u/anon31783435)
#### Post date: [March 2, 2021, 7:44am UTC](https://discourse.nixos.org/t/why-wouldnt-the-sha256-hash-change-when-fetching-from-a-different-url-when-updating-a-package/11745/3 "2021-03-02T07:44:18Z")

</div>

`sha256` hash with zeroes › that’s why `lib.fakeSha256` exists

And because of that not easy workflow (at leas for newbies as me),  
I always add a comment above hashes:

```auto
# How to obtain hash:
# nix-prefetch-url http://www.docbook.org/xml/<version>/docbook-<version>.zip

```

But I think something simpler should exist if possible (auto-upgrade check, …).

---

<div class="post-metadata">

### Author: ![jacg](https://discourse.nixos.org/user_avatar/discourse.nixos.org/jacg/32/2653_2.png) [@jacg](https://discourse.nixos.org/u/jacg)
#### Post date: [March 2, 2021, 8:48am UTC](https://discourse.nixos.org/t/why-wouldnt-the-sha256-hash-change-when-fetching-from-a-different-url-when-updating-a-package/11745/4 "2021-03-02T08:48:39Z")

</div>

> [@toraritte](#):
>
> Well, of course it didn’t fail because it automatically tried the cached path based on the current `sha256` hash, and it built the same package, but with a different name.

Yup, and these go by the name of _fixed-output derivations_ (though I’m struggling to find this term defined in the _official_ documentation right now … Nix Pills mentions it in passing.)

> [@anon31783435](#):
>
> `sha256` hash with zeroes › that’s why `lib.fakeSha256` exists

Yup, but whenever I want to use it I forget whether its camel case or something else, forget about the differences between `fakeSha256` and `fakeHash`, forget whether it’s in `lib` or somewhere else … so in the end I just use `sha256 = ""` which IIRC works by design.

---

<div class="post-metadata">

### Author: ![NobbZ](https://discourse.nixos.org/user_avatar/discourse.nixos.org/nobbz/32/7022_2.png) [@NobbZ](https://discourse.nixos.org/u/NobbZ)
#### Post date: [March 2, 2021, 8:53am UTC](https://discourse.nixos.org/t/why-wouldnt-the-sha256-hash-change-when-fetching-from-a-different-url-when-updating-a-package/11745/5 "2021-03-02T08:53:32Z")

</div>

> [@toraritte](#):
>
> of course it didn’t fail because it automatically tried the cached path based on the current `sha256` hash, and it built the same package, but with a different name.

There was no different name involved. `fetchurl` and most other fetchers use `source` as the default name.

I started using a generated name that includes the version for the fetchers to have an easier time invalidating hashes.

---

<div class="post-metadata">

### Author: ![toraritte](https://discourse.nixos.org/user_avatar/discourse.nixos.org/toraritte/32/6777_2.png) [@toraritte](https://discourse.nixos.org/u/toraritte)
#### Post date: [March 2, 2021, 2:32pm UTC](https://discourse.nixos.org/t/why-wouldnt-the-sha256-hash-change-when-fetching-from-a-different-url-when-updating-a-package/11745/6 "2021-03-02T14:32:55Z")

</div>

> [@anon31783435](#):
>
> `sha256` hash with zeroes › that’s why `lib.fakeSha256` exists

Thanks! Haven’t heard of this function yet.

> [@jacg](#):
>
> these go by the name of _fixed-output derivations_ (though I’m struggling to find this term defined in the _official_ documentation right now …

Thanks! I just [found it in the Nix manual](https://nixos.org/manual/nix/stable/#fixed-output-drvs) ([archive](https://web.archive.org/web/20210227145624/https://nixos.org/manual/nix/stable/#fixed-output-drvs)) but I also haven’t remembered it being there to be honest.

> [@jacg](#):
>
> so in the end I just use `sha256 = ""`

Again, this is what I remembered as well, until a couple days ago I had to update another package, and it didn’t work (neither did using a random value). I found the _52 zeroes_ solution suggested by [a Stackoverflow comment](https://stackoverflow.com/questions/31659527/what-is-the-meaning-of-sha256-in-nixpkgs-fetchgit-where-does-the-value-come-fro#comment-84270480).

```plaintext
$ nix-build -A docbook5
error: hash '' has wrong length for hash type 'sha256'

$ nix-build -A docbook5
error: hash 'random' has wrong length for hash type 'sha256'

$ nix-build -A docbook5
error: invalid base-32 hash '7777777777777777777777777777777777777777777777777777'

$ nix-build -A docbook5
these derivations will be built:
  /nix/store/6x7fbvxhqr4pmcz48fzamavfyhhgmn1c-docbook-5.0.1.zip.drv
  /nix/store/9a9cd090fzwq4g03mbghphsq68f3n142-docbook5-5.0.1.drv
building '/nix/store/6x7fbvxhqr4pmcz48fzamavfyhhgmn1c-docbook-5.0.1.zip.drv'...

trying http://www.docbook.org/xml/5.0.1/docbook-5.0.1.zip
  % Total % Received % Xferd Average Speed Time Time Time Current
                                 Dload Upload Total Spent Left Speed
100 67 100 67 0 0 172 0 --:--:-- --:--:-- --:--:-- 172
100 63 100 63 0 0 116 0 --:--:-- --:--:-- --:--:-- 116
100 816k 100 816k 0 0 535k 0 0:00:01 0:00:01 --:--:-- 1399k
hash mismatch in fixed-output derivation '/nix/store/zzp8gm0h65ln7b594b8h3iyismr22s83-docbook-5.0.1.zip':
  wanted: sha256:0000000000000000000000000000000000000000000000000000
  got: sha256:1iz3hq1lqgnshvlz4j9gvh4jy1ml74qf90vqf2ikbq0h4i2xzybs
cannot build derivation '/nix/store/9a9cd090fzwq4g03mbghphsq68f3n142-docbook5-5.0.1.drv': 1 dependencies couldn't be built
error: build of '/nix/store/9a9cd090fzwq4g03mbghphsq68f3n142-docbook5-5.0.1.drv' failed

```

Probably the `nix-prefetch-url` method is the best (but I’ll probably forget it…).

> [@NobbZ](#):
>
> There was no different name involved. `fetchurl` and most other fetchers use `source` as the default name.

Would you expand on this? I may have been misusing the term _name_ then, but the output path was certainly different, and the name of the derivation can be generated from `pname` and `version` since [RFC-35](https://github.com/Synthetica9/rfcs/blob/565700f94ddc712ad92d6ac3a42fd5f861adce0e/rfcs/0035-default-name-from-pname.md) ([PR](https://github.com/NixOS/rfcs/pull/35)). Then again, whenever I get confident that I understand something NIx-related, I quickly get a reality check…

> [@NobbZ](#):
>
> I started using a generated name that includes the version for the fetchers to have an easier time invalidating hashes.

Is this something akin to [RFC-35](https://github.com/Synthetica9/rfcs/blob/565700f94ddc712ad92d6ac3a42fd5f861adce0e/rfcs/0035-default-name-from-pname.md)?

---

<div class="post-metadata">

### Author: ![NobbZ](https://discourse.nixos.org/user_avatar/discourse.nixos.org/nobbz/32/7022_2.png) [@NobbZ](https://discourse.nixos.org/u/NobbZ)
#### Post date: [March 2, 2021, 2:43pm UTC](https://discourse.nixos.org/t/why-wouldnt-the-sha256-hash-change-when-fetching-from-a-different-url-when-updating-a-package/11745/7 "2021-03-02T14:43:38Z")

</div>

> [@toraritte](#):
>
> Would you expand on this? I may have been misusing the term _name_ then, but the output path was certainly different, and the name of the derivation can be generated from `pname` and `version` since [RFC-35](https://github.com/Synthetica9/rfcs/blob/565700f94ddc712ad92d6ac3a42fd5f861adce0e/rfcs/0035-default-name-from-pname.md) ([PR](https://github.com/NixOS/rfcs/pull/35)). Then again, whenever I get confident that I understand something NIx-related, I quickly get a reality check…

When you do `fetchurl { url = …; sha256 = …; }` then the name of the derivation is “`source`”. You need to provide an explicit name argument, eg: `fetchurl { name = "${name}-${version}-source"; url = …; sha256 = …; }`, to make it aware of changes in the outer derivations version, without having to remember to invalidate the hash manually.

> [@toraritte](#):
>
> Is this something akin to [RFC-35](https://github.com/Synthetica9/rfcs/blob/565700f94ddc712ad92d6ac3a42fd5f861adce0e/rfcs/0035-default-name-from-pname.md)?

That was about `mkDerivation` and its variants, though I am talking about the various `fetch*` helpers to fetch sources.

---

<div class="post-metadata">

### Author: ![jtojnar](https://discourse.nixos.org/user_avatar/discourse.nixos.org/jtojnar/32/70_2.png) [@jtojnar](https://discourse.nixos.org/u/jtojnar)
#### Post date: [March 2, 2021, 2:58pm UTC](https://discourse.nixos.org/t/why-wouldnt-the-sha256-hash-change-when-fetching-from-a-different-url-when-updating-a-package/11745/8 "2021-03-02T14:58:05Z")

</div>

> [@toraritte](#):
>
> > [@jacg](#):
> >
> > so in the end I just use `sha256 = ""`
> 
> Again, this is what I remembered as well, until a couple days ago I had to update another package, and it didn’t work (neither did using a random value). I found the _52 zeroes_ solution suggested by [a Stackoverflow comment](https://stackoverflow.com/questions/31659527/what-is-the-meaning-of-sha256-in-nixpkgs-fetchgit-where-does-the-value-come-fro#comment-84270480). Probably the `nix-prefetch-url` method is the best (but I’ll probably forget it…).

The support for empty hashes is [relatively recent](https://github.com/NixOS/nix/pull/3674) and only works with `nixUnstable` at the moment. I would recommend using empty hash on unstable Nix, and changing the first character of the hash between 0 and 1 in base32 hashes (or `lib.fakeSha256`) on stable Nix.

The issue with `nix-prefetch-url` is that it only works for plain `fetchurl`. If you prefetch an archive and pass its hash to `fetchzip`, it will get confused since it expects an extracted directory tree with some post-processing, not an archive.
