Nix (language): Difference between revisions
imported>Lheckemann m Formatting |
imported>Profpatsch add tips & trick section; convert string to path |
||
Line 376: | Line 376: | ||
Nix language has decent syntax highlighting (SH) support among popular code editors, but refactoring/autocomplete is still rare. | Nix language has decent syntax highlighting (SH) support among popular code editors, but refactoring/autocomplete is still rare. | ||
== Tips & Tricks == | |||
=== Convert a string to an (importable) path === | |||
<syntaxHighlight lang=nix> | |||
nix-repl> "/home/bernd/folder" | |||
"/home/bernd/folder" | |||
nix-repl> :t "/home/bernd/folder" | |||
a string | |||
nix-repl> builtins.toPath "/home/bernd/folder" | |||
"/home/bernd/folder" | |||
nix-repl> :t builtins.toPath "/home/bernd/folder" | |||
a string | |||
nix-repl> /. + builtins.toPath "/home/bernd/folder" | |||
/home/bernd/folder | |||
nix-repl> :t /. + builtins.toPath "/home/bernd/folder" | |||
a path | |||
</syntaxHighlight> | |||
In contrast to what [https://nixos.org/nix/manual/#builtin-toPath <code>builtins.toPath</code>] suggests, it does not result in a path, but only checks whether the string is an absolute path, and normalizes it. The trick is to prepend the <code>/.</code> (“root”) path literal, which converts the result to a nix path (that will be copied to the store when used in a derivation). | |||
Be careful not to confuse it with <code>./.</code>, which is the “directory of the current nix file” path literal, and will result in something like <code>/my/scripts/folder/home/bernd/folder</code> (provided you are in <code>/my/scripts/folder</code>). | |||
This trick might be helpful in combination with [https://nixos.org/nix/manual/#builtin-getEnv <code>builtins.getEnv</code>], which returns a string (which might be a path). Be careful, depending on environment variables introduces heavy non-determinism and might lead to rebuilds! | |||
Reference: [[Editor Modes for Nix Files]] | Reference: [[Editor Modes for Nix Files]] |