Nix Language: Tips & Tricks: Difference between revisions

imported>Mateusauler
m To wrtie text to a file by redirecting the output of a shell utility, `echo` should be used, instead of `cat`.
DoggoBit (talk | contribs)
m propose merge
 
(2 intermediate revisions by 2 users not shown)
Line 1: Line 1:
{{Merge|Nix (language)}}
== Finding the definition of a function or package ==
== Finding the definition of a function or package ==


Line 17: Line 19:
</pre>
</pre>
This doesn't work for non-functions or builtin functions, which show <code>«primop»</code>. It will always find the actual lambda, not an attribute that reexports a partial application, for example.
This doesn't work for non-functions or builtin functions, which show <code>«primop»</code>. It will always find the actual lambda, not an attribute that reexports a partial application, for example.
Also in the REPL, you may use the <code>:edit</code> command (or its abbreviation <code>:e</code>) on an expression to open your editor (detected from the <code>$EDITOR</code> environment variable) at the location where an attribute was defined.
<syntaxhighlight lang="bash">
$ nix repl --expr '{ pkgs = import <nixpkgs> { }; }'
nix-repl> :e pkgs.lib.strings.makeBinPath
</syntaxhighlight>
The position information is not always perfectly accurate, but the above sequence of commands ''should'' open your editor to somewhere close to https://github.com/NixOS/nixpkgs/blob/b6ed1c5ee1470faf835024587c25fafb03693cbe/lib/strings.nix#L228.


== Convert a string to an (<code>import</code>-able) path ==
== Convert a string to an (<code>import</code>-able) path ==
Line 160: Line 171:


== Relevant pages ==
== Relevant pages ==
* [[Overview of the Nix Expression Language]]
* [[Overview of the Nix Language]]
* [[Editor Modes for Nix Files]]
* [[Editor Modes for Nix Files]]
* [[Nix Expression Language: Learning resources|Learning resources]]
* [[Nix Expression Language: Learning resources|Learning resources]]