# Building GNU hello with custom version

**URL:** https://discourse.nixos.org/t/building-gnu-hello-with-custom-version/8432
**Category:** Help
**Created:** [August 3, 2020, 12:35pm UTC](https://discourse.nixos.org/t/building-gnu-hello-with-custom-version/8432 "2020-08-03T12:35:54Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![jheidbrink](https://discourse.nixos.org/letter_avatar_proxy/v4/letter/j/f1d935/32.png) [@jheidbrink](https://discourse.nixos.org/u/jheidbrink)
#### Post date: [August 3, 2020, 12:35pm UTC](https://discourse.nixos.org/t/building-gnu-hello-with-custom-version/8432/1 "2020-08-03T12:35:54Z")

</div>

I’m trying to build GNU hello with version 2.9 (nixpkgs defaults to 2.10). I used the following nix expression for a nix-shell:

```auto
let pkgs = import <nixpkgs> {};
    hello29 = pkgs.hello.overrideAttrs (_oldAttrs: rec { version = "2.9"; });
in
  pkgs.mkShell { buildInputs = [hello29]; }

```

While this causes nix to compile GNU hello, it still uses the sources for 2.10, so I’m still running hello 2.10 in the shell.

Maybe the `src` attribute is not automatically recalculated when `version` changes? Can I somehow enforce that?

---

<div class="post-metadata">

### Author: ![voidless](https://discourse.nixos.org/user_avatar/discourse.nixos.org/voidless/32/30296_2.png) [@voidless](https://discourse.nixos.org/u/voidless)
#### Post date: [August 3, 2020, 1:33pm UTC](https://discourse.nixos.org/t/building-gnu-hello-with-custom-version/8432/2 "2020-08-03T13:33:55Z")

</div>

> Maybe the `src` attribute is not automatically recalculated when `version` changes? Can I somehow enforce that?

That is indeed true. The src attribute is not used for generating building instructions, but rather a meta information, which should be provided in packages.

If you want to build an older version of the package, you could either override the src statement( with something like fetchurl or fetchFromGithub and an older version of the gnu hello source files) or you could check out an older version of the nixpkgs repo and use that:

```auto
nix-shell -I nixpkgs=path-to-your-local-nixpkgs-repo -p pkgs.your-package

```

---

<div class="post-metadata">

### Author: ![danieldk](https://discourse.nixos.org/user_avatar/discourse.nixos.org/danieldk/32/31405_2.png) [@danieldk](https://discourse.nixos.org/u/danieldk)
#### Post date: [August 3, 2020, 1:40pm UTC](https://discourse.nixos.org/t/building-gnu-hello-with-custom-version/8432/3 "2020-08-03T13:40:19Z")

</div>

> [@jheidbrink](#):
>
> Maybe the `src` attribute is not automatically recalculated when `version` changes? Can I somehow enforce that?

That’s (unfortunately) correct. You also have to override the `src` attribute to ensure an older version is fetched.

See [Avoid rec expresions in nixpkgs? - #4 by jtojnar](https://discourse.nixos.org/t/avoid-rec-expresions-in-nixpkgs/8293/4) onwards for a recent technical discussion.

---

<div class="post-metadata">

### Author: ![fzakaria](https://discourse.nixos.org/user_avatar/discourse.nixos.org/fzakaria/32/2643_2.png) [@fzakaria](https://discourse.nixos.org/u/fzakaria)
#### Post date: [August 3, 2020, 4:27pm UTC](https://discourse.nixos.org/t/building-gnu-hello-with-custom-version/8432/4 "2020-08-03T16:27:35Z")

</div>

Can someone go into more technical detail on this?

here is the relevant hello nix derivation

```nix
{ stdenv, fetchurl }:

stdenv.mkDerivation rec {
  pname = "hello";
  version = "2.10";

  src = fetchurl {
    url = "mirror://gnu/hello/${pname}-${version}.tar.gz";
    sha256 = "0ssi1wpaf7plaswqqjwigppsg5fyh99vdlb9kzl7c9lng89ndq1i";
  };

```

One would suspect that since _fetchUrl_ uses **version** it should now point to a different version.  
(The _sha256_ would be incorrect but that’s another point all together)

I guess because it only overrides “attributes”; and the attribute at that point is **src**

Here is a little demo I was playing with:

```nix
let pkgs = import <nixpkgs> {};
in
let my-pkg = pkgs.stdenv.mkDerivation rec {
          name = "my-pkg";
          version = "1.3";
          src = builtins.fetchurl {
          		url = "http://ftp.gnu.org/gnu/hello/hello-${version}.tar.gz";
          		sha256 = "1xi3jv67fq3mx9wlqba2hpxbcj4m5lms04hi0mh1f366zdhw5vi1";
          	};
          };
    override-pkg = my-pkg.overrideAttrs (_oldAttrs: rec {
    		version = "2.0";
    	});
in
	pkgs.lib.traceVal "${override-pkg.version} ${override-pkg.src}"

```

---

<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: [August 3, 2020, 5:18pm UTC](https://discourse.nixos.org/t/building-gnu-hello-with-custom-version/8432/5 "2020-08-03T17:18:49Z")

</div>

`overrideAttrs` just calls `mkDerivation` again with the same calculated arguments, only replacing the arguments that were returned by the function passed to `overrideAttrs`:

> <https://github.com/NixOS/nixpkgs/blob/6e4d33a0019282c1d3c1bcd5c2fb2a0dba43bb46/pkgs/stdenv/generic/make-derivation.nix#L339>

Since you did not return a new `src`, the overridden derivation used the old one, which used the `version` value that was in scope when it was created (line 5 in your example).

---

<div class="post-metadata">

### Author: ![fzakaria](https://discourse.nixos.org/user_avatar/discourse.nixos.org/fzakaria/32/2643_2.png) [@fzakaria](https://discourse.nixos.org/u/fzakaria)
#### Post date: [August 3, 2020, 5:55pm UTC](https://discourse.nixos.org/t/building-gnu-hello-with-custom-version/8432/6 "2020-08-03T17:55:48Z")

</div>

Nix needs to be even _lazier_ 😉  
Moar lazy please.
