Low-level derivations: Difference between revisions
Remove wip and add link back to derivations |
m Switched more args to named |
||
(11 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 65: | Line 66: | ||
Building the package again, we'll come across another surprising error: | Building the package again, we'll come across another surprising error: | ||
{{code| | {{code|line=no|lang=console|highlight=8| | ||
$ nix-build example.nix | $ nix-build example.nix | ||
this derivation will be built: | this derivation will be built: | ||
Line 80: | Line 81: | ||
Turns out <code>chmod</code> is not part of our build environment either; that's how minimal the low-level derivation environment is! However, before we fix the script in a similar manner, let's observe another important detail: the store path. Compare the build location of this example, and the previous one; the SHAs are different! That is because our derivation has different inputs to the one before, therefore it's built under a different location in the Nix store. And to highlight this, without fixing the script, let's build it again: | Turns out <code>chmod</code> is not part of our build environment either; that's how minimal the low-level derivation environment is! However, before we fix the script in a similar manner, let's observe another important detail: the store path. Compare the build location of this example, and the previous one; the SHAs are different! That is because our derivation has different inputs to the one before, therefore it's built under a different location in the Nix store. And to highlight this, without fixing the script, let's build it again: | ||
{{code| | {{code|line=no|lang=console|highlight=3| | ||
$ nix-build example.nix | $ nix-build example.nix | ||
this derivation will be built: | this derivation will be built: | ||
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, 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= | {{File|example.nix|nix|highlight=3| | ||
<nowiki> | <nowiki> | ||
{ pkgs ? import </nowiki><<nowiki>nixpkgs</nowiki>><nowiki> {} }: | { pkgs ? import </nowiki><<nowiki>nixpkgs</nowiki>><nowiki> {} }: | ||
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| | ||
{ pkgs ? import <nixpkgs> {} }: | { pkgs ? import <nixpkgs> {} }: | ||
Line 166: | Line 167: | ||
'' | '' | ||
}} | }} | ||
{{references}} |