srghma
srghma

Reputation: 5353

How to show where function was defined in nix?

Are there any way in nix to check where the function was defined, to see its source code?

For example, I have function writeScript, how to check from where it came from?

Upvotes: 6

Views: 1410

Answers (1)

Robert Hensing
Robert Hensing

Reputation: 7389

Nix will report it in the 'description' of a lambda term. I will assume Nix 2.0 here.

$ nix --version
nix (Nix) 2.0
$ nix repl '<nixpkgs>'
Welcome to Nix version 2.0. Type :? for help.

Loading '<nixpkgs>'...
Added 8606 variables.

nix-repl> writeScript
«lambda @ /nix/store/5m86fk1jw1pcrysckb6kam77jdxh3kgd-nixos-18.03.131802.4b4bbce199d/nixos/pkgs/build-support/trivial-builders.nix:57:17»

nix-repl> 

This location may be different from what you expect sometimes, because it doesn't take you to the location where the name was defined, but to the location where the actual lambda is.

nix-repl> map (x: x)
«primop-app»

nix-repl> l: map (x: x) l
«lambda @ (string):1:1»

The Nix evaluator simply doesn't hold on to that information for every definition for simplicity and performance. So sometimes you will need to search with brute force using grep or similar. Note that github doesn't index large files like pkgs/top-level/all-packages.nix.

Upvotes: 5

Related Questions