Nix Language: Tips & Tricks: Difference between revisions

imported>Toraritte
Moved from "Overview of Nix Expression Language"
 
DoggoBit (talk | contribs)
m propose merge
 
(6 intermediate revisions by 4 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 90: Line 101:
Note that this requires <code>./.</code> to refer to the current directory, but also importantly requires the leading slash on the quoted-string-path part.
Note that this requires <code>./.</code> to refer to the current directory, but also importantly requires the leading slash on the quoted-string-path part.


== Writing update scripts / Referencing a relative path as string ===
== Writing update scripts / Referencing a relative path as string ==


Nix has relative path syntax that describes files relative to the current nix file, for example
Nix has relative path syntax that describes files relative to the current nix file, for example
Line 133: Line 144:
in writeScript "update-foo.sh" ''
in writeScript "update-foo.sh" ''
   echo "updating foo.txt!"
   echo "updating foo.txt!"
   cat "additional new data" >> ${lib.escapeShellArg textdata}
   echo "additional new data" >> ${lib.escapeShellArg textdata}
''
''
</syntaxHighlight>
</syntaxHighlight>
Line 159: Line 170:
We can use this trick to update the sources of nix expressions (for example by generating a script which updates a json file with the software’s hashes).
We can use this trick to update the sources of nix expressions (for example by generating a script which updates a json file with the software’s hashes).


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