6

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?

srghma
  • 4,770
  • 2
  • 38
  • 54

1 Answers1

5

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.

Robert Hensing
  • 6,708
  • 18
  • 23
  • Very helpful! Couldn't find where is `fetchGit` is coming from, turned out its primop, lol) – srghma Apr 14 '18 at 17:42
  • 2
    There's a powerful search tool for the Nix source at [search.nix.gsc.io](https://search.nix.gsc.io) ― here's a query looking for definitions of `writeScript =`: https://search.nix.gsc.io/?q=writeScript%20%3D&i=nope&files=&repos= – Robert K. Bell Jul 24 '20 at 09:29