# Debug a Nix expression with debug/trace statements?

**URL:** https://discourse.nixos.org/t/debug-a-nix-expression-with-debug-trace-statements/691
**Category:** Help
**Created:** [August 18, 2018, 11:36am UTC](https://discourse.nixos.org/t/debug-a-nix-expression-with-debug-trace-statements/691 "2018-08-18T11:36:47Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![ivanbrennan](https://discourse.nixos.org/user_avatar/discourse.nixos.org/ivanbrennan/32/197_2.png) [@ivanbrennan](https://discourse.nixos.org/u/ivanbrennan)
#### Post date: [August 18, 2018, 11:36am UTC](https://discourse.nixos.org/t/debug-a-nix-expression-with-debug-trace-statements/691/1 "2018-08-18T11:36:48Z")

</div>

Is it possible to print debug/trace statements from a Nix expression? This could be useful when trying to fix a broken derivation.

For example, I was trying to write a [shell.nix](https://discourse.nixos.org/t/jqr-rpackage-error/685/5) that overrides an R package, and part of my file contained:

```nix
rPackageOverrides = old: {
  jqr = old.jqr.overrideDerivation (attrs: {
    preConfigure = "patchShebangs configure";
  });
};
pkgs = import <nixpkgs> { config = { inherit rPackageOverrides; }; };

```

Running nix-shell would complain that `old` had no `jqr` attribute: `error: attribute 'jqr' missing`.

It would be helpful if I could inspect some of `old`’s attributes. Is there a way to do that?

---

<div class="post-metadata">

### Author: ![earvstedt](https://discourse.nixos.org/user_avatar/discourse.nixos.org/earvstedt/32/310_2.png) [@earvstedt](https://discourse.nixos.org/u/earvstedt)
#### Post date: [August 19, 2018, 9:09am UTC](https://discourse.nixos.org/t/debug-a-nix-expression-with-debug-trace-statements/691/2 "2018-08-19T09:09:50Z")

</div>

Use `builtins.trace (string to output) (expr to return)`.  
You probably also don’t want to inherit `rPackageOverrides`, but use `packageOverrides` instead.

```auto
nix-build --no-out-link - <<'EOF'
let
  packageOverrides = old: {
    jqr = builtins.trace ''jq: ${old.jq}, jqr: ${old.jqr or "unavailable"}'' old.hello;
  };
  pkgs = import <nixpkgs> { config = { inherit packageOverrides; }; };
in
  pkgs.jqr
EOF

```

What are you looking for in the old attributes?

---

<div class="post-metadata">

### Author: ![bhipple](https://discourse.nixos.org/user_avatar/discourse.nixos.org/bhipple/32/201_2.png) [@bhipple](https://discourse.nixos.org/u/bhipple)
#### Post date: [August 19, 2018, 3:08pm UTC](https://discourse.nixos.org/t/debug-a-nix-expression-with-debug-trace-statements/691/3 "2018-08-19T15:08:41Z")

</div>

builtins.trace looks quite helpful, thanks earvstedt! Is there something like a builtins.breakpoint that drops you into nix repl when it evaluates?

---

<div class="post-metadata">

### Author: ![ivanbrennan](https://discourse.nixos.org/user_avatar/discourse.nixos.org/ivanbrennan/32/197_2.png) [@ivanbrennan](https://discourse.nixos.org/u/ivanbrennan)
#### Post date: [August 19, 2018, 3:24pm UTC](https://discourse.nixos.org/t/debug-a-nix-expression-with-debug-trace-statements/691/4 "2018-08-19T15:24:40Z")

</div>

Thanks, `builtins.trace` is exactly what I was looking for.

I just wanted to poke around to see if `old` was the top-level pkgs set, or something else.

I’m using `rPackageOverrides` because I’m trying to modify the jqr derivation produced by r-modules by leveraging the `overrides` argument that r-modules/default.nix accepts. In top-level/all-packages.nix, `rPackages` is set like:

```nix
rPackages = callPackage ../development/r-modules {
  overrides = (config.rPackageOverrides or (p: {})) pkgs;
};

```

I can’t figure out how to actually accomplish this (referencing `old.rPackages.jqr` leads to infinite recursion), but `builtins.trace` is an invaluable lesson. Thanks!

---

<div class="post-metadata">

### Author: ![earvstedt](https://discourse.nixos.org/user_avatar/discourse.nixos.org/earvstedt/32/310_2.png) [@earvstedt](https://discourse.nixos.org/u/earvstedt)
#### Post date: [August 19, 2018, 6:07pm UTC](https://discourse.nixos.org/t/debug-a-nix-expression-with-debug-trace-statements/691/5 "2018-08-19T18:07:32Z")

</div>

No, that’s not yet possible.

---

<div class="post-metadata">

### Author: ![Mic92](https://discourse.nixos.org/user_avatar/discourse.nixos.org/mic92/32/5265_2.png) [@Mic92](https://discourse.nixos.org/u/Mic92)
#### Post date: [August 28, 2018, 10:26am UTC](https://discourse.nixos.org/t/debug-a-nix-expression-with-debug-trace-statements/691/6 "2018-08-28T10:26:14Z")

</div>

Related helpers can be found in [https://github.com/NixOS/nixpkgs/blob/master/lib/debug.nix](https://github.com/NixOS/nixpkgs/blob/master/lib/debug.nix)
