Low-level derivations: Difference between revisions
include references |
m Switched more args to named |
||
(6 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
{{external|The Nix Reference Manual|§ 5.4.1. {{manual|nix|language/derivations|Derivations}}}} | |||
Writing '''low-level derivations''' without the convenience of the [[standard environment]] requires understanding the core fundamentals of how [[Nix]] works. While writing [[derivations]] in this manner is not common, they can be useful when no utility present in the standard environment matches the required use case, as well as being an essential learning tool for understanding why <code>stdenv.mkDerivation</code> is needed in most cases. | Writing '''low-level derivations''' without the convenience of the [[standard environment]] requires understanding the core fundamentals of how [[Nix]] works. While writing [[derivations]] in this manner is not common, they can be useful when no utility present in the standard environment matches the required use case, as well as being an essential learning tool for understanding why <code>stdenv.mkDerivation</code> is needed in most cases. | ||
Line 7: | Line 8: | ||
{{File|example.sh|bash|#!/bin/bash | {{File|example.sh|bash|#!/bin/bash | ||
echo "Hello, world!"}} | echo "Hello, world!"}} | ||
When running this script (e.g. <code>$ ./example.sh</code>) we'll get exactly the output we want. However, we're trying to build the script using Nix. Our first approach might be to write a simple derivation similar to: | When running this script (e.g. <code>$ ./example.sh</code>) we'll get exactly the output we want. However, we're trying to build the script using Nix. Our first approach might be to write a simple derivation{{cite manual|nix|language/derivations|number=5.4.1|title=Derivations}} similar to: | ||
{{File|example.nix|nix|<nowiki>derivation { | {{File|example.nix|nix|<nowiki>derivation { | ||
name = "hello-world"; | name = "hello-world"; | ||
Line 137: | Line 138: | ||
=== Standard environment === | === Standard environment === | ||
For practicality purposes, let's compare this to the standard environment way of doing this. Using <code>stdenv.mkDerivation</code> gives us access to the common Linux utilities pre-included for us{{cite manual|nixpkgs|sec-tools-of-stdenv|Tools provided by stdenv}}, so we don't have to do the package importing ourselves: | For practicality purposes, let's compare this to the standard environment way of doing this. Using <code>stdenv.mkDerivation</code>{{cite manual|nixpkgs|sec-using-stdenv|title=Using stdenv}} gives us access to the common Linux utilities pre-included for us{{cite manual|nixpkgs|sec-tools-of-stdenv|title=Tools provided by stdenv}}, so we don't have to do the package importing ourselves: | ||
{{File|example.nix|nix|highlight=3| | {{File|example.nix|nix|highlight=3| | ||
Line 157: | Line 158: | ||
=== Nixpkgs utilities === | === Nixpkgs utilities === | ||
Even further up the chain of abstractions, [[Nixpkgs]] contains many pre-built utilities for us that handle some of the configuration involved in the standard environment as well. In our case, we can use the <code>writeShellScript</code>: | Even further up the chain of abstractions, [[Nixpkgs]] contains many pre-built utilities for us that handle some of the configuration involved in the standard environment as well. In our case, we can use the <code>writeShellScript</code>{{cite manual|nixpkgs|trivial-builder-writeShellScript|title=writeShellScript}}: | ||
{{File|example.nix|nix| | {{File|example.nix|nix| |