# Is it possible to override cargoSha256 in buildRustPackage?

**URL:** https://discourse.nixos.org/t/is-it-possible-to-override-cargosha256-in-buildrustpackage/4393
**Category:** Help
**Created:** [October 16, 2019, 1:07pm UTC](https://discourse.nixos.org/t/is-it-possible-to-override-cargosha256-in-buildrustpackage/4393 "2019-10-16T13:07:34Z")
**Posts on this page:** 1
**Showing post:** 3

<div class="post-metadata">

### Author: ![aszlig](https://discourse.nixos.org/user_avatar/discourse.nixos.org/aszlig/32/78_2.png) [@aszlig](https://discourse.nixos.org/u/aszlig)
#### Post date: [October 17, 2019, 1:02am UTC](https://discourse.nixos.org/t/is-it-possible-to-override-cargosha256-in-buildrustpackage/4393/3 "2019-10-17T01:02:47Z")

</div>

`overrideAttrs` works on the attributes passed to `stdenv.mkDerivation`, not on the attributes passed to `buildRustPackage`, so at that point you already get the attributes that `buildRustPackage` is passing to `stdenv.mkDerivation`.

In order to get around this you need to look at the [source of `buildRustPackage`](https://github.com/NixOS/nixpkgs/blob/823da4d492b8b4ad46bf812db8421d99ff17a8fc/pkgs/build-support/rust/default.nix) and figure out how the `mkDerivation` attributes came to be.

For `buildRustPackage` the important part here is [`cargoDeps`](https://github.com/NixOS/nixpkgs/blob/823da4d492b8b4ad46bf812db8421d99ff17a8fc/pkgs/build-support/rust/default.nix#L62), which is the only location where `cargoSha256` is used. So let’s say if you have a package `foo` and you’d like to change `src` and `cargoSha256` you could override it like this:

```nix
foo.overrideAttrs (drv: rec {
  src = ...;
  cargoDeps = drv.cargoDeps.overrideAttrs (_: {
    inherit src; # You need to pass "src" here again,
                 # otherwise the old "src" will be used.
    outputHash = "... new cargoSha256 ...";
  });
})

```

The nested `overrideAttrs` is for the [`fetchcargo` function](https://github.com/NixOS/nixpkgs/blob/29b5aaeb5d8282f7bb4ce112aaae44309ec3410c/pkgs/build-support/rust/default.nix#L30-L35) ([source](https://github.com/NixOS/nixpkgs/blob/29b5aaeb5d8282f7bb4ce112aaae44309ec3410c/pkgs/build-support/rust/fetchcargo.nix)), which again overrides `mkDerivation` attributes, so instead of passing `sha256`, you need to pass [`outputHash`](https://github.com/NixOS/nixpkgs/blob/29b5aaeb5d8282f7bb4ce112aaae44309ec3410c/pkgs/build-support/rust/fetchcargo.nix#L75).

To illustrate this with a more concrete (but probably stupid) example, here is how to downgrade [`loc`](https://github.com/NixOS/nixpkgs/blob/29b5aaeb5d8282f7bb4ce112aaae44309ec3410c/pkgs/development/misc/loc/default.nix) to version 0.4.0:

```nix
{ pkgs ? import <nixpkgs> {}, lib ? pkgs.lib }:

pkgs.loc.overrideAttrs (drv: rec {
  name = "loc-${version}";
  version = "0.4.0";

  src = pkgs.fetchFromGitHub {
    owner = "cgag";
    repo = "loc";
    rev = "v${version}";
    sha256 = "0cdcalfb0njvlswwvzbp0s7lwfqx4acxcmlsjw2bkanszbdz10s8";
  };

  cargoDeps = drv.cargoDeps.overrideAttrs (lib.const {
    name = "${name}-vendor";
    inherit src;
    outputHash = "1qiib37qlm1z239mfr5020m4a1ig2abhlnwava7il8dqvrxzsxpl";
  });
})

```

Here I also passed `name` along with the `src` attribute to make sure that the store path actually reflects the version change, so it’s less confusing than having a store path that says something like `0.4.1` but the actual version is `0.4.0`.

---

_[View the full topic](https://discourse.nixos.org/t/is-it-possible-to-override-cargosha256-in-buildrustpackage/4393)._
