Eval the Nix

Published 2021-07-31 on Farid Zakaria's Blog

I’ll keep this post short and sweet. I want to write a little blurb that I’ve been mostly enjoying using nix eval to introspect.

Scenario 1 I wanted to set JAVA_HOME to a particular JDK.

You can use eval to determine the JAVA_HOME. The gotcha here however, is that the JDK may not necessarily be in your _/nix/home.

❯ nix eval nixpkgs.jdk8.home --raw

A more complete solution is the following.

export JAVA_HOME=$(nix build nixpkgs.jdk8 && nix eval nixpkgs.jdk8.home --raw)

Scenario 2 I wanted to discover the calculated /nix/store path for a particular derivation.

❯ nix eval nixpkgs.hello.outPath --raw

💡 You can also do something similar to find out the derivation path.

❯ nix eval nixpkgs.hello.drvPath --raw

Scenario 3 I want a quick one-liner for what I can do with nix repl.

❯ nix eval '(builtins.attrNames (import <nixpkgs>{}).hello)' --json | jq

⚠️ I do find however annoying differences between nix repl and nix eval that I’m not clear on.

For instance the following works on nix repl but fails on nix eval

❯ nix repl
Welcome to Nix version 2.3.12. Type :? for help.

nix-repl> :l <nixpkgs>
Added 14373 variables.

nix-repl> hello
«derivation /nix/store/wqnx5cgcabxkfp771fmjr1nw7mjr9zlm-hello-2.10.drv»
❯ nix eval nixpkgs#hello
error: anonymous function at /nix/store/h7b0frzjk6ylyqq471m667yd9bl9n6fm-source/pkgs/build-support/fetchurl/boot.nix:5:1 called with unexpected argument 'meta'

       at /nix/store/h7b0frzjk6ylyqq471m667yd9bl9n6fm-source/pkgs/build-support/fetchzip/default.nix:18:2:

           18| (fetchurl (let
             |  ^
           19|   basename = baseNameOf (if url != "" then url else builtins.head urls);
(use '--show-trace' to show detailed location information)

If you happen to know why, answer this message on discourse or tweet me.