mirror of
https://codeberg.org/mhwombat/nix-book.git
synced 2026-01-10 07:42:35 +08:00
better flake style
This commit is contained in:
parent
f5d2bd2c97
commit
e9565b0878
9 changed files with 38769 additions and 30553 deletions
547
index.html
547
index.html
|
|
@ -208,7 +208,7 @@ pre.pygments .tok-il { color: #666666 } /* Literal.Number.Integer.Long */
|
|||
<li><a href="#genAttrs">3.1. <code>lib.genAttrs</code></a></li>
|
||||
<li><a href="#getExe">3.2. <code>lib.getExe</code> and <code>lib.getExe'</code></a></li>
|
||||
<li><a href="#flakeExposed">3.3. <code>lib.systems.flakeExposed</code></a></li>
|
||||
<li><a href="#mkShell">3.4. <code>pkgs.mkShell</code></a></li>
|
||||
<li><a href="#mkShell">3.4. <code>pkgs.mkShell</code> and <code>pkgs.mkShellNoCC</code></a></li>
|
||||
<li><a href="#mkDerivation">3.5. <code>stdenv.mkDerivation</code></a></li>
|
||||
</ul>
|
||||
</li>
|
||||
|
|
@ -225,14 +225,21 @@ pre.pygments .tok-il { color: #666666 } /* Literal.Number.Integer.Long */
|
|||
</ul>
|
||||
</li>
|
||||
<li><a href="#_a_generic_flake">7. A generic flake</a></li>
|
||||
<li><a href="#_another_look_at_hello_flake">8. Another look at hello-flake</a></li>
|
||||
<li><a href="#_another_look_at_hello_flake">8. Another look at hello-flake</a>
|
||||
<ul class="sectlevel2">
|
||||
<li><a href="#standard-environment">8.1. The Nix standard environment</a></li>
|
||||
</ul>
|
||||
</li>
|
||||
<li><a href="#_a_new_flake_from_scratch">9. A new flake from scratch</a>
|
||||
<ul class="sectlevel2">
|
||||
<li><a href="#_bash">9.1. Bash</a>
|
||||
<ul class="sectlevel3">
|
||||
<li><a href="#_defining_the_development_environment">9.1.1. Defining the development environment</a></li>
|
||||
<li><a href="#_defining_the_package">9.1.2. Defining the package</a></li>
|
||||
<li><a href="#multi-arch">9.1.3. Supporting multiple architectures</a></li>
|
||||
<li><a href="#_a_simple_bash_script">9.1.1. A simple Bash script</a></li>
|
||||
<li><a href="#_defining_the_development_environment">9.1.2. Defining the development environment</a></li>
|
||||
<li><a href="#define-package">9.1.3. Defining the package</a></li>
|
||||
<li><a href="#config-nixpkgs">9.1.4. Configuring Nixpkgs</a></li>
|
||||
<li><a href="#multi-arch">9.1.5. Supporting multiple architectures</a></li>
|
||||
<li><a href="#_a_few_more_improvements">9.1.6. A few more improvements</a></li>
|
||||
</ul>
|
||||
</li>
|
||||
<li><a href="#_haskell">9.2. Haskell</a>
|
||||
|
|
@ -343,7 +350,7 @@ and as a downloadable
|
|||
<a href="https://codeberg.org/mhwombat/nix-book/raw/branch/pages/wombats-book-of-nix.pdf">PDF</a>.</p>
|
||||
</div>
|
||||
<div class="paragraph">
|
||||
<p><em>Last updated 2025-10-12 at 22:10:36 IST</em>.</p>
|
||||
<p><em>Last updated 2025-10-13 at 19:56:23 IST</em>.</p>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
|
@ -2543,7 +2550,7 @@ It is an alias for <code>lib.meta.getExe'</code>.</p>
|
|||
</div>
|
||||
</div>
|
||||
<div class="sect2">
|
||||
<h3 id="mkShell">3.4. <code>pkgs.mkShell</code></h3>
|
||||
<h3 id="mkShell">3.4. <code>pkgs.mkShell</code> and <code>pkgs.mkShellNoCC</code></h3>
|
||||
<div class="paragraph">
|
||||
<p>The function
|
||||
<a href="https://nixos.org/manual/nixpkgs/stable/#sec-pkgs-mkShell"><code>pkgs.mkShell</code></a>
|
||||
|
|
@ -2590,6 +2597,9 @@ along with all attributes of <code>stdenv.mkDerivation</code>.</p>
|
|||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
<div class="paragraph">
|
||||
<p>If you don’t need a C compiler, you can use <code>mkShellNoCC</code> instead.</p>
|
||||
</div>
|
||||
</div>
|
||||
<div class="sect2">
|
||||
<h3 id="mkDerivation">3.5. <code>stdenv.mkDerivation</code></h3>
|
||||
|
|
@ -2625,7 +2635,7 @@ Some of the commonly used attributes are listed below.</p>
|
|||
</tr>
|
||||
<tr>
|
||||
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>unpackPhase</code></p></td>
|
||||
<td class="tableblock halign-left valign-top"><p class="tableblock">Bash command to copy/unpack the source files. If set to <code>"true"</code></p></td>
|
||||
<td class="tableblock halign-left valign-top"><p class="tableblock">Bash command to copy/unpack the source files. If set to <code>"true"</code>, copies/unpacks all files in <code>src</code>, including subdirectories.</p></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>buildPhase</code></p></td>
|
||||
|
|
@ -3552,6 +3562,8 @@ install phases, the only commands available are those provided by the
|
|||
Nix standard environment and the external dependencies identified in the
|
||||
<code>inputs</code> section of the flake.</p>
|
||||
</div>
|
||||
<div class="sect2">
|
||||
<h3 id="standard-environment">8.1. The Nix standard environment</h3>
|
||||
<div class="paragraph">
|
||||
<p>I’ve mentioned the Nix standard environment before, but I didn’t explain
|
||||
what it is. The standard environment, or <code>stdenv</code>, refers to the
|
||||
|
|
@ -3648,6 +3660,7 @@ manual</a>.</p>
|
|||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="sect1">
|
||||
<h2 id="_a_new_flake_from_scratch">9. A new flake from scratch</h2>
|
||||
<div class="sectionbody">
|
||||
|
|
@ -3678,6 +3691,8 @@ $ git init
|
|||
Initialized empty Git repository in /home/amy/codeberg/nix-book/source/new-flake/bash-flake/my-project/.git/</pre>
|
||||
</div>
|
||||
</div>
|
||||
<div class="sect3">
|
||||
<h4 id="_a_simple_bash_script">9.1.1. A simple Bash script</h4>
|
||||
<div class="paragraph">
|
||||
<p>This will be a very simple development project,
|
||||
so that we can focus on how to use Nix.
|
||||
|
|
@ -3695,7 +3710,8 @@ cowsay<span class="tok-w"> </span><span class="tok-s2">"Hello from your fla
|
|||
</div>
|
||||
</div>
|
||||
<div class="paragraph">
|
||||
<p>Add the script to your directory and we will test it.</p>
|
||||
<p>Add the script to your directory and make it executable.
|
||||
Let’s test it.</p>
|
||||
</div>
|
||||
<div class="literalblock">
|
||||
<div class="content">
|
||||
|
|
@ -3742,8 +3758,9 @@ as shown later in <a href="#shebang">Section 10.3, “Scripts”</a>;
|
|||
then the script would run in any Nix environment.
|
||||
But for this exercise, we will package it as a "proper" development project.</p>
|
||||
</div>
|
||||
</div>
|
||||
<div class="sect3">
|
||||
<h4 id="_defining_the_development_environment">9.1.1. Defining the development environment</h4>
|
||||
<h4 id="_defining_the_development_environment">9.1.2. Defining the development environment</h4>
|
||||
<div class="paragraph">
|
||||
<p>We want to define the development environment we need for this project,
|
||||
so that we can recreate it at any time.
|
||||
|
|
@ -3752,7 +3769,33 @@ the same development environment we used.
|
|||
This avoids complaints of "but it works on my machine!"</p>
|
||||
</div>
|
||||
<div class="paragraph">
|
||||
<p>Create the file <code>flake.nix</code> as shown below.
|
||||
<p>We can use a function from Nixpkgs to create the environment,
|
||||
but we have to configure the package set for the system architecture.
|
||||
The function <code>nixpkgs</code> takes an attribute set as input,
|
||||
and returns a configured Nix package set that we can use.
|
||||
The only attribute we need to specify at this time is the system architecture name.
|
||||
For example, the following code creates a package set for the <code>x86_64-linux</code> architecture.</p>
|
||||
</div>
|
||||
<div class="listingblock">
|
||||
<div class="content">
|
||||
<pre class="pygments highlight nowrap"><code data-lang="nix"><span></span><span class="tok-ss">pkgs</span> <span class="tok-o">=</span> <span class="tok-nb">import</span> nixpkgs <span class="tok-p">{</span> <span class="tok-ss">system</span> <span class="tok-o">=</span> <span class="tok-s2">"x86_64-linux"</span><span class="tok-p">;</span> <span class="tok-p">};</span></code></pre>
|
||||
</div>
|
||||
</div>
|
||||
<div class="paragraph">
|
||||
<p>In <a href="#mkShell">Section 3.4, “<code>pkgs.mkShell</code> and <code>pkgs.mkShellNoCC</code>”</a> we learned that
|
||||
Nix provides the function called <code>mkShell</code>, which defines a Bash environment.
|
||||
We need to specify that the environment should provide <code>cowsay</code>.</p>
|
||||
</div>
|
||||
<div class="listingblock">
|
||||
<div class="content">
|
||||
<pre class="pygments highlight nowrap"><code data-lang="nix"><span></span>pkgs<span class="tok-o">.</span>mkShell <span class="tok-p">{</span>
|
||||
<span class="tok-ss">packages</span> <span class="tok-o">=</span> <span class="tok-p">[</span> pkgs<span class="tok-o">.</span>cowsay <span class="tok-p">];</span>
|
||||
<span class="tok-p">};</span></code></pre>
|
||||
</div>
|
||||
</div>
|
||||
<div class="paragraph">
|
||||
<p>Now we’re ready to write the flake.
|
||||
Create the file <code>flake.nix</code> as shown below.
|
||||
If you’re not on an <code>x86_64-linux</code> system, modify the highlighted lines accordingly.</p>
|
||||
</div>
|
||||
<div class="admonitionblock note">
|
||||
|
|
@ -3797,7 +3840,8 @@ run the following command.</p>
|
|||
<span class="normal">17</span>
|
||||
<span class="normal">18</span>
|
||||
<span class="normal">19</span>
|
||||
<span class="normal">20</span></pre></div></td><td class="code"><div><pre><span></span><span class="tok-p">{</span>
|
||||
<span class="normal">20</span>
|
||||
<span class="normal">21</span></pre></div></td><td class="code"><div><pre><span></span><span class="tok-p">{</span>
|
||||
<span class="tok-ss">description</span> <span class="tok-o">=</span> <span class="tok-s2">"what does the cow say"</span><span class="tok-p">;</span>
|
||||
|
||||
<span class="tok-ss">inputs</span> <span class="tok-o">=</span> <span class="tok-p">{</span>
|
||||
|
|
@ -3806,13 +3850,14 @@ run the following command.</p>
|
|||
|
||||
<span class="tok-ss">outputs</span> <span class="tok-o">=</span> <span class="tok-p">{</span> self<span class="tok-p">,</span> nixpkgs <span class="tok-p">}:</span> <span class="tok-p">{</span>
|
||||
|
||||
<span class="hll"> <span class="tok-ss">devShells</span> <span class="tok-o">=</span> <span class="tok-p">{</span>
|
||||
</span> x86_64-linux<span class="tok-o">.</span><span class="tok-ss">default</span> <span class="tok-o">=</span>
|
||||
<span class="hll"> nixpkgs<span class="tok-o">.</span>legacyPackages<span class="tok-o">.</span>x86_64-linux<span class="tok-o">.</span>mkShell <span class="tok-p">{</span>
|
||||
</span> <span class="tok-ss">packages</span> <span class="tok-o">=</span> <span class="tok-p">[</span>
|
||||
<span class="hll"> nixpkgs<span class="tok-o">.</span>legacyPackages<span class="tok-o">.</span>x86_64-linux<span class="tok-o">.</span>cowsay
|
||||
</span> <span class="tok-p">];</span>
|
||||
<span class="tok-p">};</span> <span class="tok-c1"># mkShell</span>
|
||||
<span class="tok-ss">devShells</span> <span class="tok-o">=</span> <span class="tok-p">{</span>
|
||||
<span class="hll"> x86_64-linux<span class="tok-o">.</span><span class="tok-ss">default</span> <span class="tok-o">=</span>
|
||||
</span> <span class="tok-k">let</span>
|
||||
<span class="hll"> <span class="tok-ss">pkgs</span> <span class="tok-o">=</span> <span class="tok-nb">import</span> nixpkgs <span class="tok-p">{</span> <span class="tok-ss">system</span> <span class="tok-o">=</span> <span class="tok-s2">"x86_64-linux"</span><span class="tok-p">;</span> <span class="tok-p">};</span>
|
||||
</span> <span class="tok-k">in</span>
|
||||
pkgs<span class="tok-o">.</span>mkShell <span class="tok-p">{</span>
|
||||
<span class="tok-ss">packages</span> <span class="tok-o">=</span> <span class="tok-p">[</span> pkgs<span class="tok-o">.</span>cowsay <span class="tok-p">];</span>
|
||||
<span class="tok-p">};</span>
|
||||
<span class="tok-p">};</span> <span class="tok-c1"># devShells</span>
|
||||
|
||||
<span class="tok-p">};</span> <span class="tok-c1"># outputs</span>
|
||||
|
|
@ -3823,31 +3868,13 @@ run the following command.</p>
|
|||
<div class="paragraph">
|
||||
<p>The <code>description</code> part is just a short description of the package.
|
||||
The <code>inputs</code> section should be familiar from <a href="#flake-inputs">Section 6.1, “Inputs”</a>.
|
||||
If we ignore parts of the long package names, the <code>outputs</code> section looks like this:</p>
|
||||
</div>
|
||||
<div class="listingblock">
|
||||
<div class="content">
|
||||
<pre class="pygments highlight nowrap"><code> devShells = {
|
||||
x86_64-linux.default =
|
||||
<em>blahblah</em>.mkShell {
|
||||
packages = [
|
||||
<em>blahblah</em>.cowsay
|
||||
];
|
||||
}; # mkShell
|
||||
}; # devShells</code></pre>
|
||||
</div>
|
||||
So far, the <code>outputs</code> section only defines a development environment
|
||||
(we’ll add to it in <a href="#define-package">Section 9.1.3, “Defining the package”</a>).</p>
|
||||
</div>
|
||||
<div class="paragraph">
|
||||
<p>This says, in effect, that to create a <code>default</code> shell for the <code>x86_64-linux</code> architecture,
|
||||
call the <code>mkShell</code> command and tell it you need the <code>cowsay</code> package.</p>
|
||||
</div>
|
||||
<div class="paragraph">
|
||||
<p>The code is rather wordy, with all the <em>this.that.the.other.thing</em> stuff.
|
||||
Also, the name <code>legacyPackages</code> suggests that we’re not following current best practices.
|
||||
(In fact, it could in some circumstances result in duplicate instances of nixpkgs.)
|
||||
In <a href="#multi-arch">Section 9.1.3, “Supporting multiple architectures”</a> we will refactor the code to eliminate some duplication, make it more readable,
|
||||
and eliminate the references to <code>legacyPackages</code>.
|
||||
For now, we will stick with the ugly, but straightforward, version.</p>
|
||||
<p>The repetition of <code>x86_64-linux</code> is undesirable.
|
||||
In <a href="#multi-arch">Section 9.1.5, “Supporting multiple architectures”</a> we will refactor the code to eliminate some duplication and make it more readable.
|
||||
For now, we will stick with the straightforward version.</p>
|
||||
</div>
|
||||
<div class="paragraph">
|
||||
<p>Let’s enter the shell.</p>
|
||||
|
|
@ -3874,7 +3901,7 @@ $ nix develop
|
|||
warning: Git tree '/home/amy/codeberg/nix-book/source/new-flake/bash-flake/my-project' is dirty
|
||||
warning: creating lock file '"/home/amy/codeberg/nix-book/source/new-flake/bash-flake/my-project/flake.lock"':
|
||||
• Added input 'nixpkgs':
|
||||
'github:NixOS/nixpkgs/1b5c1881789eb8c86c655caeff3c918fb76fbfe6?narHash=sha256-5oeX7NvYHNslymyCmX9mLEmLp07a8ai522G8J4VrDrs%3D' (2025-10-12)
|
||||
'github:NixOS/nixpkgs/81a6b38af7d047072d87ff0f0bc75f2b563c5f36?narHash=sha256-9XWnHg2JYVLKB2wDobsLLKXZ/wpzgA6wlWm7HuAFato%3D' (2025-10-13)
|
||||
warning: Git tree '/home/amy/codeberg/nix-book/source/new-flake/bash-flake/my-project' is dirty</pre>
|
||||
</div>
|
||||
</div>
|
||||
|
|
@ -3899,11 +3926,42 @@ We can see that <code>cowsay</code> is now available, and our script runs.</p>
|
|||
</div>
|
||||
</div>
|
||||
<div class="sect3">
|
||||
<h4 id="_defining_the_package">9.1.2. Defining the package</h4>
|
||||
<h4 id="define-package">9.1.3. Defining the package</h4>
|
||||
<div class="paragraph">
|
||||
<p>We created an appropriate development environment, and tested our script.
|
||||
Now we are ready to package it.
|
||||
Add the new lines below to <code>flake.nix</code>.
|
||||
Now we are ready to package it.</p>
|
||||
</div>
|
||||
<div class="paragraph">
|
||||
<p>In <a href="#mkDerivation">Section 3.5, “<code>stdenv.mkDerivation</code>”</a> we learned that the function <code>pkgs.std.mkDerivation</code>
|
||||
provides a way to create a derivation by specifying the steps that need to be performed in each phase
|
||||
We specify the location of the source files.
|
||||
and use the standard unpack phase, which makes our source files available in <code>$src</code>
|
||||
during the build and installation.
|
||||
(We described the environment available during build and installation in <a href="#standard-environment">Section 8.1, “The Nix standard environment”</a>.)
|
||||
We don’t need to do anything in the build phase.
|
||||
In the install phase, we copy the script from <code>$src</code> to the output directory, <code>$out</code>,
|
||||
and make it executable.
|
||||
As with the development environment, we specify that <code>cowsay</code> is required.</p>
|
||||
</div>
|
||||
<div class="listingblock">
|
||||
<div class="content">
|
||||
<pre class="pygments highlight nowrap"><code data-lang="nix"><span></span>pkgs<span class="tok-o">.</span>stdenv<span class="tok-o">.</span>mkDerivation <span class="tok-p">{</span>
|
||||
<span class="tok-ss">name</span> <span class="tok-o">=</span> <span class="tok-s2">"cow-hello.sh"</span><span class="tok-p">;</span>
|
||||
<span class="tok-ss">src</span> <span class="tok-o">=</span> <span class="tok-l">./.</span><span class="tok-p">;</span>
|
||||
<span class="tok-ss">unpackPhase</span> <span class="tok-o">=</span> <span class="tok-s2">"true"</span><span class="tok-p">;</span>
|
||||
<span class="tok-ss">buildPhase</span> <span class="tok-o">=</span> <span class="tok-s2">":"</span><span class="tok-p">;</span>
|
||||
<span class="tok-ss">installPhase</span> <span class="tok-o">=</span>
|
||||
<span class="tok-s tok-s-Multiline">''</span>
|
||||
<span class="tok-s tok-s-Multiline"> mkdir -p $out/bin</span>
|
||||
<span class="tok-s tok-s-Multiline"> cp $src/cow-hello.sh $out/bin</span>
|
||||
<span class="tok-s tok-s-Multiline"> chmod +x $out/bin/cow-hello.sh</span>
|
||||
<span class="tok-s tok-s-Multiline"> ''</span><span class="tok-p">;</span>
|
||||
<span class="tok-ss">buildInputs</span> <span class="tok-o">=</span> <span class="tok-p">[</span> pkgs<span class="tok-o">.</span>cowsay <span class="tok-p">];</span>
|
||||
<span class="tok-p">};</span> <span class="tok-c1"># mkDerivation</span></code></pre>
|
||||
</div>
|
||||
</div>
|
||||
<div class="paragraph">
|
||||
<p>To update the flake, add the new lines below to <code>flake.nix</code>.
|
||||
Again, change <code>x86_64-linux</code> if needed to match your system architecture.</p>
|
||||
</div>
|
||||
<div class="listingblock">
|
||||
|
|
@ -3947,7 +4005,9 @@ Again, change <code>x86_64-linux</code> if needed to match your system architect
|
|||
<span class="normal">36</span>
|
||||
<span class="normal">37</span>
|
||||
<span class="normal">38</span>
|
||||
<span class="normal">39</span></pre></div></td><td class="code"><div><pre><span></span><span class="tok-p">{</span>
|
||||
<span class="normal">39</span>
|
||||
<span class="normal">40</span>
|
||||
<span class="normal">41</span></pre></div></td><td class="code"><div><pre><span></span><span class="tok-p">{</span>
|
||||
<span class="tok-ss">description</span> <span class="tok-o">=</span> <span class="tok-s2">"what does the cow say"</span><span class="tok-p">;</span>
|
||||
|
||||
<span class="tok-ss">inputs</span> <span class="tok-o">=</span> <span class="tok-p">{</span>
|
||||
|
|
@ -3958,30 +4018,32 @@ Again, change <code>x86_64-linux</code> if needed to match your system architect
|
|||
|
||||
<span class="tok-ss">devShells</span> <span class="tok-o">=</span> <span class="tok-p">{</span>
|
||||
x86_64-linux<span class="tok-o">.</span><span class="tok-ss">default</span> <span class="tok-o">=</span>
|
||||
nixpkgs<span class="tok-o">.</span>legacyPackages<span class="tok-o">.</span>x86_64-linux<span class="tok-o">.</span>mkShell <span class="tok-p">{</span>
|
||||
<span class="tok-ss">packages</span> <span class="tok-o">=</span> <span class="tok-p">[</span>
|
||||
nixpkgs<span class="tok-o">.</span>legacyPackages<span class="tok-o">.</span>x86_64-linux<span class="tok-o">.</span>cowsay
|
||||
<span class="tok-p">];</span>
|
||||
<span class="tok-p">};</span> <span class="tok-c1"># mkShell</span>
|
||||
<span class="tok-k">let</span>
|
||||
<span class="tok-ss">pkgs</span> <span class="tok-o">=</span> <span class="tok-nb">import</span> nixpkgs <span class="tok-p">{</span> <span class="tok-ss">system</span> <span class="tok-o">=</span> <span class="tok-s2">"x86_64-linux"</span><span class="tok-p">;</span> <span class="tok-p">};</span>
|
||||
<span class="tok-k">in</span>
|
||||
pkgs<span class="tok-o">.</span>mkShell <span class="tok-p">{</span>
|
||||
<span class="tok-ss">packages</span> <span class="tok-o">=</span> <span class="tok-p">[</span> pkgs<span class="tok-o">.</span>cowsay <span class="tok-p">];</span>
|
||||
<span class="tok-p">};</span>
|
||||
<span class="tok-p">};</span> <span class="tok-c1"># devShells</span>
|
||||
|
||||
<span class="hll"> <span class="tok-ss">packages</span> <span class="tok-o">=</span> <span class="tok-p">{</span>
|
||||
</span><span class="hll"> x86_64-linux<span class="tok-o">.</span><span class="tok-ss">default</span> <span class="tok-o">=</span>
|
||||
</span><span class="hll"> nixpkgs<span class="tok-o">.</span>legacyPackages<span class="tok-o">.</span>x86_64-linux<span class="tok-o">.</span>stdenv<span class="tok-o">.</span>mkDerivation <span class="tok-p">{</span>
|
||||
</span><span class="hll"> <span class="tok-ss">name</span> <span class="tok-o">=</span> <span class="tok-s2">"cow-hello.sh"</span><span class="tok-p">;</span>
|
||||
</span><span class="hll"> <span class="tok-ss">src</span> <span class="tok-o">=</span> <span class="tok-l">./.</span><span class="tok-p">;</span>
|
||||
</span><span class="hll"> <span class="tok-ss">unpackPhase</span> <span class="tok-o">=</span> <span class="tok-s2">"true"</span><span class="tok-p">;</span>
|
||||
</span><span class="hll"> <span class="tok-ss">buildPhase</span> <span class="tok-o">=</span> <span class="tok-s2">":"</span><span class="tok-p">;</span>
|
||||
</span><span class="hll"> <span class="tok-ss">installPhase</span> <span class="tok-o">=</span>
|
||||
</span><span class="hll"> <span class="tok-s tok-s-Multiline">''</span>
|
||||
</span><span class="hll"><span class="tok-s tok-s-Multiline"> mkdir -p $out/bin</span>
|
||||
</span><span class="hll"><span class="tok-s tok-s-Multiline"> cp $src/cow-hello.sh $out/bin</span>
|
||||
</span><span class="hll"><span class="tok-s tok-s-Multiline"> chmod +x $out/bin/cow-hello.sh</span>
|
||||
</span><span class="hll"><span class="tok-s tok-s-Multiline"> ''</span><span class="tok-p">;</span>
|
||||
</span><span class="hll"> <span class="tok-ss">buildInputs</span> <span class="tok-o">=</span> <span class="tok-p">[</span>
|
||||
</span><span class="hll"> nixpkgs<span class="tok-o">.</span>legacyPackages<span class="tok-o">.</span>x86_64-linux<span class="tok-o">.</span>cowsay
|
||||
</span><span class="hll"> <span class="tok-p">];</span>
|
||||
</span><span class="hll"> <span class="tok-p">};</span> <span class="tok-c1"># mkDerivation</span>
|
||||
</span><span class="hll"> <span class="tok-k">let</span>
|
||||
</span><span class="hll"> <span class="tok-ss">pkgs</span> <span class="tok-o">=</span> <span class="tok-nb">import</span> nixpkgs <span class="tok-p">{</span> <span class="tok-ss">system</span> <span class="tok-o">=</span> <span class="tok-s2">"x86_64-linux"</span><span class="tok-p">;</span> <span class="tok-p">};</span>
|
||||
</span><span class="hll"> <span class="tok-k">in</span>
|
||||
</span><span class="hll"> pkgs<span class="tok-o">.</span>stdenv<span class="tok-o">.</span>mkDerivation <span class="tok-p">{</span>
|
||||
</span><span class="hll"> <span class="tok-ss">name</span> <span class="tok-o">=</span> <span class="tok-s2">"cow-hello.sh"</span><span class="tok-p">;</span>
|
||||
</span><span class="hll"> <span class="tok-ss">src</span> <span class="tok-o">=</span> <span class="tok-l">./.</span><span class="tok-p">;</span>
|
||||
</span><span class="hll"> <span class="tok-ss">unpackPhase</span> <span class="tok-o">=</span> <span class="tok-s2">"true"</span><span class="tok-p">;</span>
|
||||
</span><span class="hll"> <span class="tok-ss">buildPhase</span> <span class="tok-o">=</span> <span class="tok-s2">":"</span><span class="tok-p">;</span>
|
||||
</span><span class="hll"> <span class="tok-ss">installPhase</span> <span class="tok-o">=</span>
|
||||
</span><span class="hll"> <span class="tok-s tok-s-Multiline">''</span>
|
||||
</span><span class="hll"><span class="tok-s tok-s-Multiline"> mkdir -p $out/bin</span>
|
||||
</span><span class="hll"><span class="tok-s tok-s-Multiline"> cp $src/cow-hello.sh $out/bin</span>
|
||||
</span><span class="hll"><span class="tok-s tok-s-Multiline"> chmod +x $out/bin/cow-hello.sh</span>
|
||||
</span><span class="hll"><span class="tok-s tok-s-Multiline"> ''</span><span class="tok-p">;</span>
|
||||
</span><span class="hll"> <span class="tok-ss">buildInputs</span> <span class="tok-o">=</span> <span class="tok-p">[</span> pkgs<span class="tok-o">.</span>cowsay <span class="tok-p">];</span>
|
||||
</span><span class="hll"> <span class="tok-p">};</span> <span class="tok-c1"># mkDerivation</span>
|
||||
</span><span class="hll"> <span class="tok-p">};</span> <span class="tok-c1"># packages</span>
|
||||
</span>
|
||||
<span class="tok-p">};</span> <span class="tok-c1"># outputs</span>
|
||||
|
|
@ -4012,11 +4074,15 @@ we would have an error about the file being missing.</p>
|
|||
</div>
|
||||
</div>
|
||||
<div class="sect3">
|
||||
<h4 id="multi-arch">9.1.3. Supporting multiple architectures</h4>
|
||||
<h4 id="config-nixpkgs">9.1.4. Configuring Nixpkgs</h4>
|
||||
|
||||
</div>
|
||||
<div class="sect3">
|
||||
<h4 id="multi-arch">9.1.5. Supporting multiple architectures</h4>
|
||||
<div class="paragraph">
|
||||
<p>Congratulations!
|
||||
Our package is popular, and people want to run it on <code>aarch64-linux</code> systems.
|
||||
So now we need to add an entry for that to <code>packages</code>.
|
||||
So now we need to add an entry to <code>packages</code>.
|
||||
Of course we want to test it on the new architecture,
|
||||
so we’ll add an entry to <code>devShells</code> as well.</p>
|
||||
</div>
|
||||
|
|
@ -4085,7 +4151,11 @@ so we’ll add an entry to <code>devShells</code> as well.</p>
|
|||
<span class="normal">60</span>
|
||||
<span class="normal">61</span>
|
||||
<span class="normal">62</span>
|
||||
<span class="normal">63</span></pre></div></td><td class="code"><div><pre><span></span><span class="tok-p">{</span>
|
||||
<span class="normal">63</span>
|
||||
<span class="normal">64</span>
|
||||
<span class="normal">65</span>
|
||||
<span class="normal">66</span>
|
||||
<span class="normal">67</span></pre></div></td><td class="code"><div><pre><span></span><span class="tok-p">{</span>
|
||||
<span class="tok-ss">description</span> <span class="tok-o">=</span> <span class="tok-s2">"what does the cow say"</span><span class="tok-p">;</span>
|
||||
|
||||
<span class="tok-ss">inputs</span> <span class="tok-o">=</span> <span class="tok-p">{</span>
|
||||
|
|
@ -4096,54 +4166,58 @@ so we’ll add an entry to <code>devShells</code> as well.</p>
|
|||
|
||||
<span class="tok-ss">devShells</span> <span class="tok-o">=</span> <span class="tok-p">{</span>
|
||||
x86_64-linux<span class="tok-o">.</span><span class="tok-ss">default</span> <span class="tok-o">=</span>
|
||||
nixpkgs<span class="tok-o">.</span>legacyPackages<span class="tok-o">.</span>x86_64-linux<span class="tok-o">.</span>mkShell <span class="tok-p">{</span>
|
||||
<span class="tok-ss">packages</span> <span class="tok-o">=</span> <span class="tok-p">[</span>
|
||||
nixpkgs<span class="tok-o">.</span>legacyPackages<span class="tok-o">.</span>x86_64-linux<span class="tok-o">.</span>cowsay
|
||||
<span class="tok-p">];</span>
|
||||
<span class="tok-p">};</span> <span class="tok-c1"># mkShell</span>
|
||||
<span class="tok-k">let</span>
|
||||
<span class="tok-ss">pkgs</span> <span class="tok-o">=</span> <span class="tok-nb">import</span> nixpkgs <span class="tok-p">{</span> <span class="tok-ss">system</span> <span class="tok-o">=</span> <span class="tok-s2">"x86_64-linux"</span><span class="tok-p">;</span> <span class="tok-p">};</span>
|
||||
<span class="tok-k">in</span>
|
||||
pkgs<span class="tok-o">.</span>mkShell <span class="tok-p">{</span>
|
||||
<span class="tok-ss">packages</span> <span class="tok-o">=</span> <span class="tok-p">[</span> pkgs<span class="tok-o">.</span>cowsay <span class="tok-p">];</span>
|
||||
<span class="tok-p">};</span>
|
||||
|
||||
<span class="hll"> aarch64-linux<span class="tok-o">.</span><span class="tok-ss">default</span> <span class="tok-o">=</span>
|
||||
</span><span class="hll"> nixpkgs<span class="tok-o">.</span>legacyPackages<span class="tok-o">.</span>aarch64-linux<span class="tok-o">.</span>mkShell <span class="tok-p">{</span>
|
||||
</span><span class="hll"> <span class="tok-ss">packages</span> <span class="tok-o">=</span> <span class="tok-p">[</span>
|
||||
</span><span class="hll"> nixpkgs<span class="tok-o">.</span>legacyPackages<span class="tok-o">.</span>aarch64-linux<span class="tok-o">.</span>cowsay
|
||||
</span><span class="hll"> <span class="tok-p">];</span>
|
||||
</span><span class="hll"> <span class="tok-p">};</span> <span class="tok-c1"># mkShell</span>
|
||||
</span><span class="hll"> <span class="tok-k">let</span>
|
||||
</span><span class="hll"> <span class="tok-ss">pkgs</span> <span class="tok-o">=</span> <span class="tok-nb">import</span> nixpkgs <span class="tok-p">{</span> <span class="tok-ss">system</span> <span class="tok-o">=</span> <span class="tok-s2">"aarch64-linux"</span><span class="tok-p">;</span> <span class="tok-p">};</span>
|
||||
</span><span class="hll"> <span class="tok-k">in</span>
|
||||
</span><span class="hll"> pkgs<span class="tok-o">.</span>mkShell <span class="tok-p">{</span>
|
||||
</span><span class="hll"> <span class="tok-ss">packages</span> <span class="tok-o">=</span> <span class="tok-p">[</span> pkgs<span class="tok-o">.</span>cowsay <span class="tok-p">];</span>
|
||||
</span><span class="hll"> <span class="tok-p">};</span>
|
||||
</span> <span class="tok-p">};</span> <span class="tok-c1"># devShells</span>
|
||||
|
||||
<span class="tok-ss">packages</span> <span class="tok-o">=</span> <span class="tok-p">{</span>
|
||||
x86_64-linux<span class="tok-o">.</span><span class="tok-ss">default</span> <span class="tok-o">=</span>
|
||||
nixpkgs<span class="tok-o">.</span>legacyPackages<span class="tok-o">.</span>x86_64-linux<span class="tok-o">.</span>stdenv<span class="tok-o">.</span>mkDerivation <span class="tok-p">{</span>
|
||||
<span class="tok-ss">name</span> <span class="tok-o">=</span> <span class="tok-s2">"cow-hello.sh"</span><span class="tok-p">;</span>
|
||||
<span class="tok-ss">src</span> <span class="tok-o">=</span> <span class="tok-l">./.</span><span class="tok-p">;</span>
|
||||
<span class="tok-ss">unpackPhase</span> <span class="tok-o">=</span> <span class="tok-s2">"true"</span><span class="tok-p">;</span>
|
||||
<span class="tok-ss">buildPhase</span> <span class="tok-o">=</span> <span class="tok-s2">":"</span><span class="tok-p">;</span>
|
||||
<span class="tok-ss">installPhase</span> <span class="tok-o">=</span>
|
||||
<span class="tok-s tok-s-Multiline">''</span>
|
||||
<span class="tok-s tok-s-Multiline"> mkdir -p $out/bin</span>
|
||||
<span class="tok-s tok-s-Multiline"> cp $src/cow-hello.sh $out/bin</span>
|
||||
<span class="tok-s tok-s-Multiline"> chmod +x $out/bin/cow-hello.sh</span>
|
||||
<span class="tok-s tok-s-Multiline"> ''</span><span class="tok-p">;</span>
|
||||
<span class="tok-ss">buildInputs</span> <span class="tok-o">=</span> <span class="tok-p">[</span>
|
||||
nixpkgs<span class="tok-o">.</span>legacyPackages<span class="tok-o">.</span>x86_64-linux<span class="tok-o">.</span>cowsay
|
||||
<span class="tok-p">];</span>
|
||||
<span class="tok-p">};</span> <span class="tok-c1"># mkDerivation</span>
|
||||
<span class="tok-k">let</span>
|
||||
<span class="tok-ss">pkgs</span> <span class="tok-o">=</span> <span class="tok-nb">import</span> nixpkgs <span class="tok-p">{</span> <span class="tok-ss">system</span> <span class="tok-o">=</span> <span class="tok-s2">"x86_64-linux"</span><span class="tok-p">;</span> <span class="tok-p">};</span>
|
||||
<span class="tok-k">in</span>
|
||||
pkgs<span class="tok-o">.</span>stdenv<span class="tok-o">.</span>mkDerivation <span class="tok-p">{</span>
|
||||
<span class="tok-ss">name</span> <span class="tok-o">=</span> <span class="tok-s2">"cow-hello.sh"</span><span class="tok-p">;</span>
|
||||
<span class="tok-ss">src</span> <span class="tok-o">=</span> <span class="tok-l">./.</span><span class="tok-p">;</span>
|
||||
<span class="tok-ss">unpackPhase</span> <span class="tok-o">=</span> <span class="tok-s2">"true"</span><span class="tok-p">;</span>
|
||||
<span class="tok-ss">buildPhase</span> <span class="tok-o">=</span> <span class="tok-s2">":"</span><span class="tok-p">;</span>
|
||||
<span class="tok-ss">installPhase</span> <span class="tok-o">=</span>
|
||||
<span class="tok-s tok-s-Multiline">''</span>
|
||||
<span class="tok-s tok-s-Multiline"> mkdir -p $out/bin</span>
|
||||
<span class="tok-s tok-s-Multiline"> cp $src/cow-hello.sh $out/bin</span>
|
||||
<span class="tok-s tok-s-Multiline"> chmod +x $out/bin/cow-hello.sh</span>
|
||||
<span class="tok-s tok-s-Multiline"> ''</span><span class="tok-p">;</span>
|
||||
<span class="tok-ss">buildInputs</span> <span class="tok-o">=</span> <span class="tok-p">[</span> pkgs<span class="tok-o">.</span>cowsay <span class="tok-p">];</span>
|
||||
<span class="tok-p">};</span> <span class="tok-c1"># mkDerivation</span>
|
||||
|
||||
<span class="hll"> aarch64-linux<span class="tok-o">.</span><span class="tok-ss">default</span> <span class="tok-o">=</span>
|
||||
</span><span class="hll"> nixpkgs<span class="tok-o">.</span>legacyPackages<span class="tok-o">.</span>aarch64-linux<span class="tok-o">.</span>stdenv<span class="tok-o">.</span>mkDerivation <span class="tok-p">{</span>
|
||||
</span><span class="hll"> <span class="tok-ss">name</span> <span class="tok-o">=</span> <span class="tok-s2">"cow-hello.sh"</span><span class="tok-p">;</span>
|
||||
</span><span class="hll"> <span class="tok-ss">src</span> <span class="tok-o">=</span> <span class="tok-l">./.</span><span class="tok-p">;</span>
|
||||
</span><span class="hll"> <span class="tok-ss">unpackPhase</span> <span class="tok-o">=</span> <span class="tok-s2">"true"</span><span class="tok-p">;</span>
|
||||
</span><span class="hll"> <span class="tok-ss">buildPhase</span> <span class="tok-o">=</span> <span class="tok-s2">":"</span><span class="tok-p">;</span>
|
||||
</span><span class="hll"> <span class="tok-ss">installPhase</span> <span class="tok-o">=</span>
|
||||
</span><span class="hll"> <span class="tok-s tok-s-Multiline">''</span>
|
||||
</span><span class="hll"><span class="tok-s tok-s-Multiline"> mkdir -p $out/bin</span>
|
||||
</span><span class="hll"><span class="tok-s tok-s-Multiline"> cp $src/cow-hello.sh $out/bin</span>
|
||||
</span><span class="hll"><span class="tok-s tok-s-Multiline"> chmod +x $out/bin/cow-hello.sh</span>
|
||||
</span><span class="hll"><span class="tok-s tok-s-Multiline"> ''</span><span class="tok-p">;</span>
|
||||
</span><span class="hll"> <span class="tok-ss">buildInputs</span> <span class="tok-o">=</span> <span class="tok-p">[</span>
|
||||
</span><span class="hll"> nixpkgs<span class="tok-o">.</span>legacyPackages<span class="tok-o">.</span>aarch64-linux<span class="tok-o">.</span>cowsay
|
||||
</span><span class="hll"> <span class="tok-p">];</span>
|
||||
</span><span class="hll"> <span class="tok-p">};</span> <span class="tok-c1"># mkDerivation</span>
|
||||
</span><span class="hll"> <span class="tok-k">let</span>
|
||||
</span><span class="hll"> <span class="tok-ss">pkgs</span> <span class="tok-o">=</span> <span class="tok-nb">import</span> nixpkgs <span class="tok-p">{</span> <span class="tok-ss">system</span> <span class="tok-o">=</span> <span class="tok-s2">"aarch64-linux"</span><span class="tok-p">;</span> <span class="tok-p">};</span>
|
||||
</span><span class="hll"> <span class="tok-k">in</span>
|
||||
</span><span class="hll"> pkgs<span class="tok-o">.</span>stdenv<span class="tok-o">.</span>mkDerivation <span class="tok-p">{</span>
|
||||
</span><span class="hll"> <span class="tok-ss">name</span> <span class="tok-o">=</span> <span class="tok-s2">"cow-hello.sh"</span><span class="tok-p">;</span>
|
||||
</span><span class="hll"> <span class="tok-ss">src</span> <span class="tok-o">=</span> <span class="tok-l">./.</span><span class="tok-p">;</span>
|
||||
</span><span class="hll"> <span class="tok-ss">unpackPhase</span> <span class="tok-o">=</span> <span class="tok-s2">"true"</span><span class="tok-p">;</span>
|
||||
</span><span class="hll"> <span class="tok-ss">buildPhase</span> <span class="tok-o">=</span> <span class="tok-s2">":"</span><span class="tok-p">;</span>
|
||||
</span><span class="hll"> <span class="tok-ss">installPhase</span> <span class="tok-o">=</span>
|
||||
</span><span class="hll"> <span class="tok-s tok-s-Multiline">''</span>
|
||||
</span><span class="hll"><span class="tok-s tok-s-Multiline"> mkdir -p $out/bin</span>
|
||||
</span><span class="hll"><span class="tok-s tok-s-Multiline"> cp $src/cow-hello.sh $out/bin</span>
|
||||
</span><span class="hll"><span class="tok-s tok-s-Multiline"> chmod +x $out/bin/cow-hello.sh</span>
|
||||
</span><span class="hll"><span class="tok-s tok-s-Multiline"> ''</span><span class="tok-p">;</span>
|
||||
</span><span class="hll"> <span class="tok-ss">buildInputs</span> <span class="tok-o">=</span> <span class="tok-p">[</span> pkgs<span class="tok-o">.</span>cowsay <span class="tok-p">];</span>
|
||||
</span><span class="hll"> <span class="tok-p">};</span> <span class="tok-c1"># mkDerivation</span>
|
||||
</span> <span class="tok-p">};</span> <span class="tok-c1"># packages</span>
|
||||
|
||||
<span class="tok-p">};</span> <span class="tok-c1"># outputs</span>
|
||||
|
|
@ -4209,7 +4283,8 @@ For the second argument, we used a function that "pretended" to generate definit
|
|||
<p>What if we wrote a function which, given the name of the system architecture,
|
||||
would generate the development shell definition for us,
|
||||
and another function that would do the same for the package definition?
|
||||
Applying <code>lib.genAttrs</code> and the list of system architecture names would give us
|
||||
Applying <code>lib.genAttrs</code> and the list of system architecture names
|
||||
to those functions would give us
|
||||
all the definitions we need for the outputs section.</p>
|
||||
</div>
|
||||
<div class="paragraph">
|
||||
|
|
@ -4258,25 +4333,189 @@ We can make the flake more readable with the following definitions.</p>
|
|||
<div class="listingblock">
|
||||
<div class="content">
|
||||
<pre class="pygments highlight nowrap"><code data-lang="nix"><span></span><span class="tok-ss">supportedSystems</span> <span class="tok-o">=</span> <span class="tok-p">[</span> <span class="tok-s2">"x86_64-linux"</span> <span class="tok-s2">"aarch64-linux"</span> <span class="tok-p">];</span>
|
||||
|
||||
<span class="tok-ss">forAllSupportedSystems</span> <span class="tok-o">=</span> nixpkgs<span class="tok-o">.</span>lib<span class="tok-o">.</span>genAttrs supportedSystems<span class="tok-p">;</span></code></pre>
|
||||
<span class="tok-ss">forEachSystem</span> <span class="tok-o">=</span> nixpkgs<span class="tok-o">.</span>lib<span class="tok-o">.</span>genAttrs supportedSystems<span class="tok-p">;</span></code></pre>
|
||||
</div>
|
||||
</div>
|
||||
<div class="paragraph">
|
||||
<p>Now let’s examine the definition of <code>nixpkgsFor.${system}</code>.</p>
|
||||
<p>So <code>forEachSystem</code> is a function which takes one argument.
|
||||
That argument should be a function that, given the system name,
|
||||
generates the appropriate definition for that system,
|
||||
Now let’s examine the definition of <code>nixpkgsFor.${system}</code>.</p>
|
||||
</div>
|
||||
<div class="listingblock">
|
||||
<div class="content">
|
||||
<pre class="pygments highlight nowrap"><code data-lang="nix"><span></span><span class="tok-ss">nixpkgsFor</span> <span class="tok-o">=</span> forAllSupportedSystems <span class="tok-p">(</span>system<span class="tok-p">:</span> <span class="tok-nb">import</span> nixpkgs <span class="tok-p">{</span> <span class="tok-k">inherit</span> system<span class="tok-p">;</span> <span class="tok-p">});</span></code></pre>
|
||||
<pre class="pygments highlight nowrap"><code data-lang="nix"><span></span><span class="tok-ss">nixpkgsFor</span> <span class="tok-o">=</span> forEachSystem <span class="tok-p">(</span>system<span class="tok-p">:</span> <span class="tok-nb">import</span> nixpkgs <span class="tok-p">{</span> <span class="tok-k">inherit</span> system<span class="tok-p">;</span> <span class="tok-p">});</span></code></pre>
|
||||
</div>
|
||||
</div>
|
||||
<div class="paragraph">
|
||||
<p>Here we’re using <code>forEachSystem</code> to access the appropriate nixpkgs for a system.</p>
|
||||
</div>
|
||||
<div class="paragraph">
|
||||
<p>Putting everything together, we have a shiny new flake.
|
||||
You may want to compare it carefully to the original version,
|
||||
in order to reassure yourself that the definitions are equivalent.</p>
|
||||
</div>
|
||||
<div class="listingblock">
|
||||
<div class="title">flake.nix (version 3)</div>
|
||||
<div class="title">flake.nix (version 4)</div>
|
||||
<div class="content">
|
||||
<pre class="pygments highlight nowrap"><code data-lang="nix"><div class="lineno"><table class="linenotable"><tr><td class="linenos"><div class="linenodiv"><pre><span class="normal"> 1</span>
|
||||
<span class="normal"> 2</span>
|
||||
<span class="normal"> 3</span>
|
||||
<span class="normal"> 4</span>
|
||||
<span class="normal"> 5</span>
|
||||
<span class="normal"> 6</span>
|
||||
<span class="normal"> 7</span>
|
||||
<span class="normal"> 8</span>
|
||||
<span class="normal"> 9</span>
|
||||
<span class="normal">10</span>
|
||||
<span class="normal">11</span>
|
||||
<span class="normal">12</span>
|
||||
<span class="normal">13</span>
|
||||
<span class="normal">14</span>
|
||||
<span class="normal">15</span>
|
||||
<span class="normal">16</span>
|
||||
<span class="normal">17</span>
|
||||
<span class="normal">18</span>
|
||||
<span class="normal">19</span>
|
||||
<span class="normal">20</span>
|
||||
<span class="normal">21</span>
|
||||
<span class="normal">22</span>
|
||||
<span class="normal">23</span>
|
||||
<span class="normal">24</span>
|
||||
<span class="normal">25</span>
|
||||
<span class="normal">26</span>
|
||||
<span class="normal">27</span>
|
||||
<span class="normal">28</span>
|
||||
<span class="normal">29</span>
|
||||
<span class="normal">30</span>
|
||||
<span class="normal">31</span>
|
||||
<span class="normal">32</span>
|
||||
<span class="normal">33</span>
|
||||
<span class="normal">34</span>
|
||||
<span class="normal">35</span>
|
||||
<span class="normal">36</span>
|
||||
<span class="normal">37</span>
|
||||
<span class="normal">38</span>
|
||||
<span class="normal">39</span>
|
||||
<span class="normal">40</span>
|
||||
<span class="normal">41</span></pre></div></td><td class="code"><div><pre><span></span><span class="tok-p">{</span>
|
||||
<span class="tok-ss">description</span> <span class="tok-o">=</span> <span class="tok-s2">"what does the cow say"</span><span class="tok-p">;</span>
|
||||
|
||||
<span class="tok-ss">inputs</span> <span class="tok-o">=</span> <span class="tok-p">{</span>
|
||||
nixpkgs<span class="tok-o">.</span><span class="tok-ss">url</span> <span class="tok-o">=</span> <span class="tok-s2">"github:NixOS/nixpkgs"</span><span class="tok-p">;</span>
|
||||
<span class="tok-p">};</span>
|
||||
|
||||
<span class="tok-ss">outputs</span> <span class="tok-o">=</span> <span class="tok-p">{</span> self<span class="tok-p">,</span> nixpkgs <span class="tok-p">}:</span>
|
||||
<span class="tok-k">let</span>
|
||||
<span class="hll"> <span class="tok-ss">supportedSystems</span> <span class="tok-o">=</span> <span class="tok-p">[</span> <span class="tok-s2">"x86_64-linux"</span> <span class="tok-s2">"aarch64-linux"</span> <span class="tok-p">];</span>
|
||||
</span><span class="hll"> <span class="tok-ss">forEachSystem</span> <span class="tok-o">=</span> nixpkgs<span class="tok-o">.</span>lib<span class="tok-o">.</span>genAttrs supportedSystems<span class="tok-p">;</span>
|
||||
</span><span class="hll"> <span class="tok-ss">nixpkgsFor</span> <span class="tok-o">=</span> forEachSystem <span class="tok-p">(</span>system<span class="tok-p">:</span> <span class="tok-nb">import</span> nixpkgs <span class="tok-p">{</span> <span class="tok-k">inherit</span> system<span class="tok-p">;</span> <span class="tok-p">});</span>
|
||||
</span>
|
||||
<span class="tok-k">in</span> <span class="tok-p">{</span>
|
||||
|
||||
<span class="hll"> <span class="tok-ss">devShells</span> <span class="tok-o">=</span> forEachSystem <span class="tok-p">(</span>system<span class="tok-p">:</span>
|
||||
</span><span class="hll"> <span class="tok-k">let</span> <span class="tok-ss">pkgs</span> <span class="tok-o">=</span> nixpkgsFor<span class="tok-o">.</span><span class="tok-si">${</span>system<span class="tok-si">}</span><span class="tok-p">;</span> <span class="tok-k">in</span> <span class="tok-p">{</span>
|
||||
</span> <span class="tok-ss">default</span> <span class="tok-o">=</span> pkgs<span class="tok-o">.</span>mkShell <span class="tok-p">{</span>
|
||||
<span class="tok-ss">packages</span> <span class="tok-o">=</span> <span class="tok-p">[</span> pkgs<span class="tok-o">.</span>cowsay <span class="tok-p">];</span>
|
||||
<span class="tok-p">};</span>
|
||||
<span class="tok-p">});</span>
|
||||
|
||||
<span class="hll"> <span class="tok-ss">packages</span> <span class="tok-o">=</span> forEachSystem <span class="tok-p">(</span>system<span class="tok-p">:</span>
|
||||
</span><span class="hll"> <span class="tok-k">let</span> <span class="tok-ss">pkgs</span> <span class="tok-o">=</span> nixpkgsFor<span class="tok-o">.</span><span class="tok-si">${</span>system<span class="tok-si">}</span><span class="tok-p">;</span> <span class="tok-k">in</span> <span class="tok-p">{</span>
|
||||
</span> <span class="tok-ss">default</span> <span class="tok-o">=</span> pkgs<span class="tok-o">.</span>stdenv<span class="tok-o">.</span>mkDerivation <span class="tok-p">{</span>
|
||||
<span class="tok-ss">name</span> <span class="tok-o">=</span> <span class="tok-s2">"cow-hello.sh"</span><span class="tok-p">;</span>
|
||||
<span class="tok-ss">src</span> <span class="tok-o">=</span> <span class="tok-l">./.</span><span class="tok-p">;</span>
|
||||
<span class="tok-ss">unpackPhase</span> <span class="tok-o">=</span> <span class="tok-s2">"true"</span><span class="tok-p">;</span>
|
||||
<span class="tok-ss">buildPhase</span> <span class="tok-o">=</span> <span class="tok-s2">":"</span><span class="tok-p">;</span>
|
||||
<span class="tok-ss">installPhase</span> <span class="tok-o">=</span>
|
||||
<span class="tok-s tok-s-Multiline">''</span>
|
||||
<span class="tok-s tok-s-Multiline"> mkdir -p $out/bin</span>
|
||||
<span class="tok-s tok-s-Multiline"> cp $src/cow-hello.sh $out/bin</span>
|
||||
<span class="tok-s tok-s-Multiline"> chmod +x $out/bin/cow-hello.sh</span>
|
||||
<span class="tok-s tok-s-Multiline"> ''</span><span class="tok-p">;</span>
|
||||
<span class="tok-ss">buildInputs</span> <span class="tok-o">=</span> <span class="tok-p">[</span> pkgs<span class="tok-o">.</span>cowsay <span class="tok-p">];</span>
|
||||
<span class="tok-p">};</span> <span class="tok-c1"># mkDerivation</span>
|
||||
<span class="tok-p">});</span> <span class="tok-c1"># packages</span>
|
||||
|
||||
<span class="tok-p">};</span> <span class="tok-c1"># outputs</span>
|
||||
<span class="tok-p">}</span>
|
||||
</pre></div></td></tr></table></div></code></pre>
|
||||
</div>
|
||||
</div>
|
||||
<div class="paragraph">
|
||||
<p>Let’s verify that it runs on our system.</p>
|
||||
</div>
|
||||
<div class="literalblock">
|
||||
<div class="content">
|
||||
<pre class="nowrap">$ git commit -am "refactored the flake"
|
||||
[master (root-commit) cdb1858] refactored the flake
|
||||
3 files changed, 70 insertions(+)
|
||||
create mode 100755 cow-hello.sh
|
||||
create mode 100644 flake.lock
|
||||
create mode 100644 flake.nix
|
||||
$ nix run
|
||||
________________________
|
||||
< Hello from your flake! >
|
||||
------------------------
|
||||
\ ^__^
|
||||
\ (oo)\_______
|
||||
(__)\ )\/\
|
||||
||----w |
|
||||
|| ||</pre>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="sect3">
|
||||
<h4 id="_a_few_more_improvements">9.1.6. A few more improvements</h4>
|
||||
<div class="paragraph">
|
||||
<p>I took some shortcuts in the flake definitions up to this point, just to keep it simple.
|
||||
Normally a flake has both a <code>packages</code> section and an <code>apps</code> section.
|
||||
The <code>apps</code> section is where we specify executable programs.
|
||||
If there is no <code>apps</code> section, then <code>nix run</code> will default to using the package, but that’s not ideal.</p>
|
||||
</div>
|
||||
<div class="paragraph">
|
||||
<p>A typical flake might have multiple packages and multiple apps.
|
||||
(We could even have multiple development environments.)
|
||||
Normally we would specify a default package and a default app.
|
||||
The command <code>nix run ` <em>flakeurl</em></code>#<code><em>appname</em> will run the app named <em>appname</em> from the `apps</code> section of <em>flakeurl</em>.
|
||||
If we don’t specify <em>appname</em>, the default app is run.</p>
|
||||
</div>
|
||||
<div class="paragraph">
|
||||
<p>To define an app, we specify the type and the path to the executable.
|
||||
We can re-use the definition from the <code>packages</code> section as shown below.</p>
|
||||
</div>
|
||||
<div class="listingblock">
|
||||
<div class="content">
|
||||
<pre class="pygments highlight nowrap"><code data-lang="nix"><span></span><span class="tok-ss">hello</span> <span class="tok-o">=</span> <span class="tok-p">{</span>
|
||||
<span class="tok-ss">type</span> <span class="tok-o">=</span> <span class="tok-s2">"app"</span><span class="tok-p">;</span>
|
||||
<span class="tok-ss">program</span> <span class="tok-o">=</span> pkgs<span class="tok-o">.</span>lib<span class="tok-o">.</span>getExe self<span class="tok-o">.</span>packages<span class="tok-o">.</span><span class="tok-si">${</span>system<span class="tok-si">}</span><span class="tok-o">.</span>hello<span class="tok-p">;</span>
|
||||
<span class="tok-p">};</span></code></pre>
|
||||
</div>
|
||||
</div>
|
||||
<div class="paragraph">
|
||||
<p>Later we might want to add overlays or some configuration options to <code>nixpkgs</code> in our flake.
|
||||
We can include the scaffolding for it with the following change.</p>
|
||||
</div>
|
||||
<div class="listingblock">
|
||||
<div class="content">
|
||||
<pre class="pygments highlight nowrap"><code data-lang="nix"><span></span><span class="tok-ss">nixpkgsFor</span> <span class="tok-o">=</span> forEachSystem <span class="tok-p">(</span>system<span class="tok-p">:</span> <span class="tok-nb">import</span> nixpkgs <span class="tok-p">{</span>
|
||||
<span class="tok-k">inherit</span> system<span class="tok-p">;</span>
|
||||
<span class="tok-ss">config</span> <span class="tok-o">=</span> <span class="tok-p">{</span> <span class="tok-p">};</span>
|
||||
<span class="tok-ss">overlays</span> <span class="tok-o">=</span> <span class="tok-p">[</span> <span class="tok-p">];</span>
|
||||
<span class="tok-p">});</span></code></pre>
|
||||
</div>
|
||||
</div>
|
||||
<div class="paragraph">
|
||||
<p>The Nix manual has more information on
|
||||
<a href="https://nixos.org/manual/nixpkgs/stable/#sec-config-options-reference">config options</a>
|
||||
and
|
||||
<a href="https://nixos.org/manual/nixpkgs/stable/#chap-overlays">overlays</a>.</p>
|
||||
</div>
|
||||
<div class="paragraph">
|
||||
<p>Putting everything together, we have:</p>
|
||||
</div>
|
||||
<div class="listingblock">
|
||||
<div class="title">flake.nix (version 5)</div>
|
||||
<div class="content">
|
||||
<pre class="pygments highlight nowrap"><code data-lang="nix"><div class="lineno"><table class="linenotable"><tr><td class="linenos"><div class="linenodiv"><pre><span class="normal"> 1</span>
|
||||
<span class="normal"> 2</span>
|
||||
|
|
@ -4321,7 +4560,22 @@ in order to reassure yourself that the definitions are equivalent.</p>
|
|||
<span class="normal">41</span>
|
||||
<span class="normal">42</span>
|
||||
<span class="normal">43</span>
|
||||
<span class="normal">44</span></pre></div></td><td class="code"><div><pre><span></span><span class="tok-p">{</span>
|
||||
<span class="normal">44</span>
|
||||
<span class="normal">45</span>
|
||||
<span class="normal">46</span>
|
||||
<span class="normal">47</span>
|
||||
<span class="normal">48</span>
|
||||
<span class="normal">49</span>
|
||||
<span class="normal">50</span>
|
||||
<span class="normal">51</span>
|
||||
<span class="normal">52</span>
|
||||
<span class="normal">53</span>
|
||||
<span class="normal">54</span>
|
||||
<span class="normal">55</span>
|
||||
<span class="normal">56</span>
|
||||
<span class="normal">57</span>
|
||||
<span class="normal">58</span>
|
||||
<span class="normal">59</span></pre></div></td><td class="code"><div><pre><span></span><span class="tok-p">{</span>
|
||||
<span class="tok-ss">description</span> <span class="tok-o">=</span> <span class="tok-s2">"what does the cow say"</span><span class="tok-p">;</span>
|
||||
|
||||
<span class="tok-ss">inputs</span> <span class="tok-o">=</span> <span class="tok-p">{</span>
|
||||
|
|
@ -4333,23 +4587,27 @@ in order to reassure yourself that the definitions are equivalent.</p>
|
|||
|
||||
<span class="tok-ss">supportedSystems</span> <span class="tok-o">=</span> <span class="tok-p">[</span> <span class="tok-s2">"x86_64-linux"</span> <span class="tok-s2">"aarch64-linux"</span> <span class="tok-p">];</span>
|
||||
|
||||
<span class="tok-ss">forAllSupportedSystems</span> <span class="tok-o">=</span> nixpkgs<span class="tok-o">.</span>lib<span class="tok-o">.</span>genAttrs supportedSystems<span class="tok-p">;</span>
|
||||
<span class="tok-ss">forEachSystem</span> <span class="tok-o">=</span> nixpkgs<span class="tok-o">.</span>lib<span class="tok-o">.</span>genAttrs supportedSystems<span class="tok-p">;</span>
|
||||
|
||||
<span class="tok-ss">nixpkgsFor</span> <span class="tok-o">=</span> forAllSupportedSystems <span class="tok-p">(</span>system<span class="tok-p">:</span> <span class="tok-nb">import</span> nixpkgs <span class="tok-p">{</span> <span class="tok-k">inherit</span> system<span class="tok-p">;</span> <span class="tok-p">});</span>
|
||||
|
||||
<span class="tok-k">in</span> <span class="tok-p">{</span>
|
||||
<span class="hll">
|
||||
</span><span class="hll"> <span class="tok-ss">devShells</span> <span class="tok-o">=</span> forAllSupportedSystems <span class="tok-p">(</span>system<span class="tok-p">:</span>
|
||||
</span><span class="hll"> <span class="tok-k">let</span> <span class="tok-ss">pkgs</span> <span class="tok-o">=</span> nixpkgsFor<span class="tok-o">.</span><span class="tok-si">${</span>system<span class="tok-si">}</span><span class="tok-p">;</span> <span class="tok-k">in</span> <span class="tok-p">{</span>
|
||||
</span><span class="hll"> <span class="tok-ss">default</span> <span class="tok-o">=</span> pkgs<span class="tok-o">.</span>mkShell <span class="tok-p">{</span>
|
||||
</span><span class="hll"> <span class="tok-ss">packages</span> <span class="tok-o">=</span> <span class="tok-p">[</span> pkgs<span class="tok-o">.</span>cowsay <span class="tok-p">];</span>
|
||||
</span><span class="hll"> <span class="tok-p">};</span>
|
||||
<span class="tok-ss">nixpkgsFor</span> <span class="tok-o">=</span> forEachSystem <span class="tok-p">(</span>system<span class="tok-p">:</span> <span class="tok-nb">import</span> nixpkgs <span class="tok-p">{</span>
|
||||
<span class="tok-k">inherit</span> system<span class="tok-p">;</span>
|
||||
<span class="hll"> <span class="tok-ss">config</span> <span class="tok-o">=</span> <span class="tok-p">{</span> <span class="tok-p">};</span>
|
||||
</span><span class="hll"> <span class="tok-ss">overlays</span> <span class="tok-o">=</span> <span class="tok-p">[</span> <span class="tok-p">];</span>
|
||||
</span> <span class="tok-p">});</span>
|
||||
|
||||
<span class="tok-ss">packages</span> <span class="tok-o">=</span> forAllSupportedSystems <span class="tok-p">(</span>system<span class="tok-p">:</span>
|
||||
<span class="tok-k">in</span> <span class="tok-p">{</span>
|
||||
|
||||
<span class="tok-ss">devShells</span> <span class="tok-o">=</span> forEachSystem <span class="tok-p">(</span>system<span class="tok-p">:</span>
|
||||
<span class="tok-k">let</span> <span class="tok-ss">pkgs</span> <span class="tok-o">=</span> nixpkgsFor<span class="tok-o">.</span><span class="tok-si">${</span>system<span class="tok-si">}</span><span class="tok-p">;</span> <span class="tok-k">in</span> <span class="tok-p">{</span>
|
||||
<span class="tok-ss">default</span> <span class="tok-o">=</span> pkgs<span class="tok-o">.</span>stdenv<span class="tok-o">.</span>mkDerivation <span class="tok-p">{</span>
|
||||
<span class="tok-ss">name</span> <span class="tok-o">=</span> <span class="tok-s2">"cow-hello.sh"</span><span class="tok-p">;</span>
|
||||
<span class="tok-ss">default</span> <span class="tok-o">=</span> pkgs<span class="tok-o">.</span>mkShell <span class="tok-p">{</span>
|
||||
<span class="tok-ss">packages</span> <span class="tok-o">=</span> <span class="tok-p">[</span> pkgs<span class="tok-o">.</span>cowsay <span class="tok-p">];</span>
|
||||
<span class="tok-p">};</span>
|
||||
<span class="tok-p">});</span>
|
||||
|
||||
<span class="tok-ss">packages</span> <span class="tok-o">=</span> forEachSystem <span class="tok-p">(</span>system<span class="tok-p">:</span>
|
||||
<span class="hll"> <span class="tok-k">let</span> <span class="tok-ss">pkgs</span> <span class="tok-o">=</span> nixpkgsFor<span class="tok-o">.</span><span class="tok-si">${</span>system<span class="tok-si">}</span><span class="tok-p">;</span> <span class="tok-k">in</span> <span class="tok-k">rec</span> <span class="tok-p">{</span>
|
||||
</span><span class="hll"> <span class="tok-ss">hello</span> <span class="tok-o">=</span> pkgs<span class="tok-o">.</span>stdenv<span class="tok-o">.</span>mkDerivation <span class="tok-p">{</span>
|
||||
</span> <span class="tok-ss">name</span> <span class="tok-o">=</span> <span class="tok-s2">"cow-hello.sh"</span><span class="tok-p">;</span>
|
||||
<span class="tok-ss">src</span> <span class="tok-o">=</span> <span class="tok-l">./.</span><span class="tok-p">;</span>
|
||||
<span class="tok-ss">unpackPhase</span> <span class="tok-o">=</span> <span class="tok-s2">"true"</span><span class="tok-p">;</span>
|
||||
<span class="tok-ss">buildPhase</span> <span class="tok-o">=</span> <span class="tok-s2">":"</span><span class="tok-p">;</span>
|
||||
|
|
@ -4361,11 +4619,22 @@ in order to reassure yourself that the definitions are equivalent.</p>
|
|||
<span class="tok-s tok-s-Multiline"> ''</span><span class="tok-p">;</span>
|
||||
<span class="tok-ss">buildInputs</span> <span class="tok-o">=</span> <span class="tok-p">[</span> pkgs<span class="tok-o">.</span>cowsay <span class="tok-p">];</span>
|
||||
<span class="tok-p">};</span> <span class="tok-c1"># mkDerivation</span>
|
||||
<span class="tok-p">});</span> <span class="tok-c1"># packages</span>
|
||||
|
||||
<span class="tok-p">};</span> <span class="tok-c1"># outputs</span>
|
||||
<span class="hll"><span class="tok-p">}</span>
|
||||
</span></pre></div></td></tr></table></div></code></pre>
|
||||
<span class="hll"> <span class="tok-ss">default</span> <span class="tok-o">=</span> hello<span class="tok-p">;</span>
|
||||
</span> <span class="tok-p">});</span> <span class="tok-c1"># packages</span>
|
||||
|
||||
<span class="hll"> <span class="tok-ss">apps</span> <span class="tok-o">=</span> forEachSystem <span class="tok-p">(</span>system<span class="tok-p">:</span>
|
||||
</span><span class="hll"> <span class="tok-k">let</span> <span class="tok-ss">pkgs</span> <span class="tok-o">=</span> nixpkgsFor<span class="tok-o">.</span><span class="tok-si">${</span>system<span class="tok-si">}</span><span class="tok-p">;</span> <span class="tok-k">in</span> <span class="tok-k">rec</span> <span class="tok-p">{</span>
|
||||
</span><span class="hll"> <span class="tok-ss">hello</span> <span class="tok-o">=</span> <span class="tok-p">{</span>
|
||||
</span><span class="hll"> <span class="tok-ss">type</span> <span class="tok-o">=</span> <span class="tok-s2">"app"</span><span class="tok-p">;</span>
|
||||
</span><span class="hll"> <span class="tok-ss">program</span> <span class="tok-o">=</span> pkgs<span class="tok-o">.</span>lib<span class="tok-o">.</span>getExe self<span class="tok-o">.</span>packages<span class="tok-o">.</span><span class="tok-si">${</span>system<span class="tok-si">}</span><span class="tok-o">.</span>hello<span class="tok-p">;</span>
|
||||
</span><span class="hll"> <span class="tok-p">};</span>
|
||||
</span><span class="hll">
|
||||
</span><span class="hll"> <span class="tok-ss">default</span> <span class="tok-o">=</span> hello<span class="tok-p">;</span>
|
||||
</span><span class="hll"> <span class="tok-p">});</span>
|
||||
</span> <span class="tok-p">};</span> <span class="tok-c1"># outputs</span>
|
||||
<span class="tok-p">}</span>
|
||||
</pre></div></td></tr></table></div></code></pre>
|
||||
</div>
|
||||
</div>
|
||||
<div class="paragraph">
|
||||
|
|
@ -4374,12 +4643,10 @@ in order to reassure yourself that the definitions are equivalent.</p>
|
|||
<div class="literalblock">
|
||||
<div class="content">
|
||||
<pre class="nowrap">$ git commit -am "refactored the flake"
|
||||
[master (root-commit) a69e4f5] refactored the flake
|
||||
3 files changed, 73 insertions(+)
|
||||
create mode 100755 cow-hello.sh
|
||||
create mode 100644 flake.lock
|
||||
create mode 100644 flake.nix
|
||||
[master ba19983] refactored the flake
|
||||
1 file changed, 21 insertions(+), 3 deletions(-)
|
||||
$ nix run
|
||||
evaluation warning: getExe: Package "cow-hello.sh" does not have the meta.mainProgram attribute. We'll assume that the main program has the same name for now, but this behavior is deprecated, because it leads to surprising errors when the assumption does not hold. If the package has a main program, please set `meta.mainProgram` in its definition to make this warning go away. Otherwise, if the package does not have a main program, or if you don't control its definition, use getExe' to specify the name to the program, such as lib.getExe' foo "bar".
|
||||
________________________
|
||||
< Hello from your flake! >
|
||||
------------------------
|
||||
|
|
@ -6850,7 +7117,7 @@ Hello from your flake!</pre>
|
|||
</div>
|
||||
<div id="footer">
|
||||
<div id="footer-text">
|
||||
Last updated 2025-10-12 22:10:36 IST
|
||||
Last updated 2025-10-13 19:56:23 IST
|
||||
</div>
|
||||
</div>
|
||||
</body>
|
||||
|
|
|
|||
|
|
@ -104,6 +104,9 @@ install phases, the only commands available are those provided by the
|
|||
Nix standard environment and the external dependencies identified in the
|
||||
`inputs` section of the flake.
|
||||
|
||||
[#standard-environment]
|
||||
== The Nix standard environment
|
||||
|
||||
I've mentioned the Nix standard environment before, but I didn't explain
|
||||
what it is. The standard environment, or `stdenv`, refers to the
|
||||
functionality that is available during the build and install phases of a
|
||||
|
|
|
|||
|
|
@ -9,30 +9,32 @@
|
|||
|
||||
devShells = {
|
||||
x86_64-linux.default =
|
||||
nixpkgs.legacyPackages.x86_64-linux.mkShell {
|
||||
packages = [
|
||||
nixpkgs.legacyPackages.x86_64-linux.cowsay
|
||||
];
|
||||
}; # mkShell
|
||||
let
|
||||
pkgs = import nixpkgs { system = "x86_64-linux"; };
|
||||
in
|
||||
pkgs.mkShell {
|
||||
packages = [ pkgs.cowsay ];
|
||||
};
|
||||
}; # devShells
|
||||
|
||||
packages = {
|
||||
x86_64-linux.default =
|
||||
nixpkgs.legacyPackages.x86_64-linux.stdenv.mkDerivation {
|
||||
name = "cow-hello.sh";
|
||||
src = ./.;
|
||||
unpackPhase = "true";
|
||||
buildPhase = ":";
|
||||
installPhase =
|
||||
''
|
||||
mkdir -p $out/bin
|
||||
cp $src/cow-hello.sh $out/bin
|
||||
chmod +x $out/bin/cow-hello.sh
|
||||
'';
|
||||
buildInputs = [
|
||||
nixpkgs.legacyPackages.x86_64-linux.cowsay
|
||||
];
|
||||
}; # mkDerivation
|
||||
let
|
||||
pkgs = import nixpkgs { system = "x86_64-linux"; };
|
||||
in
|
||||
pkgs.stdenv.mkDerivation {
|
||||
name = "cow-hello.sh";
|
||||
src = ./.;
|
||||
unpackPhase = "true";
|
||||
buildPhase = ":";
|
||||
installPhase =
|
||||
''
|
||||
mkdir -p $out/bin
|
||||
cp $src/cow-hello.sh $out/bin
|
||||
chmod +x $out/bin/cow-hello.sh
|
||||
'';
|
||||
buildInputs = [ pkgs.cowsay ];
|
||||
}; # mkDerivation
|
||||
}; # packages
|
||||
|
||||
}; # outputs
|
||||
|
|
|
|||
|
|
@ -9,54 +9,58 @@
|
|||
|
||||
devShells = {
|
||||
x86_64-linux.default =
|
||||
nixpkgs.legacyPackages.x86_64-linux.mkShell {
|
||||
packages = [
|
||||
nixpkgs.legacyPackages.x86_64-linux.cowsay
|
||||
];
|
||||
}; # mkShell
|
||||
let
|
||||
pkgs = import nixpkgs { system = "x86_64-linux"; };
|
||||
in
|
||||
pkgs.mkShell {
|
||||
packages = [ pkgs.cowsay ];
|
||||
};
|
||||
|
||||
aarch64-linux.default =
|
||||
nixpkgs.legacyPackages.aarch64-linux.mkShell {
|
||||
packages = [
|
||||
nixpkgs.legacyPackages.aarch64-linux.cowsay
|
||||
];
|
||||
}; # mkShell
|
||||
let
|
||||
pkgs = import nixpkgs { system = "aarch64-linux"; };
|
||||
in
|
||||
pkgs.mkShell {
|
||||
packages = [ pkgs.cowsay ];
|
||||
};
|
||||
}; # devShells
|
||||
|
||||
packages = {
|
||||
x86_64-linux.default =
|
||||
nixpkgs.legacyPackages.x86_64-linux.stdenv.mkDerivation {
|
||||
name = "cow-hello.sh";
|
||||
src = ./.;
|
||||
unpackPhase = "true";
|
||||
buildPhase = ":";
|
||||
installPhase =
|
||||
''
|
||||
mkdir -p $out/bin
|
||||
cp $src/cow-hello.sh $out/bin
|
||||
chmod +x $out/bin/cow-hello.sh
|
||||
'';
|
||||
buildInputs = [
|
||||
nixpkgs.legacyPackages.x86_64-linux.cowsay
|
||||
];
|
||||
}; # mkDerivation
|
||||
let
|
||||
pkgs = import nixpkgs { system = "x86_64-linux"; };
|
||||
in
|
||||
pkgs.stdenv.mkDerivation {
|
||||
name = "cow-hello.sh";
|
||||
src = ./.;
|
||||
unpackPhase = "true";
|
||||
buildPhase = ":";
|
||||
installPhase =
|
||||
''
|
||||
mkdir -p $out/bin
|
||||
cp $src/cow-hello.sh $out/bin
|
||||
chmod +x $out/bin/cow-hello.sh
|
||||
'';
|
||||
buildInputs = [ pkgs.cowsay ];
|
||||
}; # mkDerivation
|
||||
|
||||
aarch64-linux.default =
|
||||
nixpkgs.legacyPackages.aarch64-linux.stdenv.mkDerivation {
|
||||
name = "cow-hello.sh";
|
||||
src = ./.;
|
||||
unpackPhase = "true";
|
||||
buildPhase = ":";
|
||||
installPhase =
|
||||
''
|
||||
mkdir -p $out/bin
|
||||
cp $src/cow-hello.sh $out/bin
|
||||
chmod +x $out/bin/cow-hello.sh
|
||||
'';
|
||||
buildInputs = [
|
||||
nixpkgs.legacyPackages.aarch64-linux.cowsay
|
||||
];
|
||||
}; # mkDerivation
|
||||
let
|
||||
pkgs = import nixpkgs { system = "aarch64-linux"; };
|
||||
in
|
||||
pkgs.stdenv.mkDerivation {
|
||||
name = "cow-hello.sh";
|
||||
src = ./.;
|
||||
unpackPhase = "true";
|
||||
buildPhase = ":";
|
||||
installPhase =
|
||||
''
|
||||
mkdir -p $out/bin
|
||||
cp $src/cow-hello.sh $out/bin
|
||||
chmod +x $out/bin/cow-hello.sh
|
||||
'';
|
||||
buildInputs = [ pkgs.cowsay ];
|
||||
}; # mkDerivation
|
||||
}; # packages
|
||||
|
||||
}; # outputs
|
||||
|
|
|
|||
|
|
@ -7,23 +7,20 @@
|
|||
|
||||
outputs = { self, nixpkgs }:
|
||||
let
|
||||
|
||||
supportedSystems = [ "x86_64-linux" "aarch64-linux" ];
|
||||
|
||||
forAllSupportedSystems = nixpkgs.lib.genAttrs supportedSystems;
|
||||
|
||||
nixpkgsFor = forAllSupportedSystems (system: import nixpkgs { inherit system; });
|
||||
forEachSystem = nixpkgs.lib.genAttrs supportedSystems;
|
||||
nixpkgsFor = forEachSystem (system: import nixpkgs { inherit system; });
|
||||
|
||||
in {
|
||||
|
||||
devShells = forAllSupportedSystems (system:
|
||||
devShells = forEachSystem (system:
|
||||
let pkgs = nixpkgsFor.${system}; in {
|
||||
default = pkgs.mkShell {
|
||||
packages = [ pkgs.cowsay ];
|
||||
};
|
||||
});
|
||||
|
||||
packages = forAllSupportedSystems (system:
|
||||
packages = forEachSystem (system:
|
||||
let pkgs = nixpkgsFor.${system}; in {
|
||||
default = pkgs.stdenv.mkDerivation {
|
||||
name = "cow-hello.sh";
|
||||
|
|
|
|||
|
|
@ -9,11 +9,12 @@
|
|||
|
||||
devShells = {
|
||||
x86_64-linux.default =
|
||||
nixpkgs.legacyPackages.x86_64-linux.mkShell {
|
||||
packages = [
|
||||
nixpkgs.legacyPackages.x86_64-linux.cowsay
|
||||
];
|
||||
}; # mkShell
|
||||
let
|
||||
pkgs = import nixpkgs { system = "x86_64-linux"; };
|
||||
in
|
||||
pkgs.mkShell {
|
||||
packages = [ pkgs.cowsay ];
|
||||
};
|
||||
}; # devShells
|
||||
|
||||
}; # outputs
|
||||
|
|
|
|||
|
|
@ -10,6 +10,8 @@ $ cd my-project
|
|||
$ git init
|
||||
....
|
||||
|
||||
== A simple Bash script
|
||||
|
||||
This will be a very simple development project,
|
||||
so that we can focus on how to use Nix.
|
||||
We want to package the following script.
|
||||
|
|
@ -24,7 +26,8 @@ $ cp ../cow-hello.sh .
|
|||
$# cat cow-hello.sh
|
||||
....
|
||||
|
||||
Add the script to your directory and we will test it.
|
||||
Add the script to your directory and make it executable.
|
||||
Let's test it.
|
||||
|
||||
....
|
||||
$ chmod +x cow-hello.sh
|
||||
|
|
@ -63,10 +66,35 @@ Furthermore, anyone else who wants to work on our project will be able to recrea
|
|||
the same development environment we used.
|
||||
This avoids complaints of "but it works on my machine!"
|
||||
|
||||
We can use a function from Nixpkgs to create the environment,
|
||||
but we have to configure the package set for the system architecture.
|
||||
The function `nixpkgs` takes an attribute set as input,
|
||||
and returns a configured Nix package set that we can use.
|
||||
The only attribute we need to specify at this time is the system architecture name.
|
||||
For example, the following code creates a package set for the `x86_64-linux` architecture.
|
||||
|
||||
[source,nix]
|
||||
....
|
||||
pkgs = import nixpkgs { system = "x86_64-linux"; };
|
||||
....
|
||||
|
||||
In <<#mkShell>> we learned that
|
||||
Nix provides the function called `mkShell`, which defines a Bash environment.
|
||||
We need to specify that the environment should provide `cowsay`.
|
||||
|
||||
[source,nix]
|
||||
....
|
||||
pkgs.mkShell {
|
||||
packages = [ pkgs.cowsay ];
|
||||
};
|
||||
....
|
||||
|
||||
|
||||
////
|
||||
$ cp ../flake.nix .
|
||||
////
|
||||
|
||||
Now we're ready to write the flake.
|
||||
Create the file `flake.nix` as shown below.
|
||||
If you're not on an `x86_64-linux` system, modify the highlighted lines accordingly.
|
||||
|
||||
|
|
@ -78,7 +106,7 @@ run the following command.
|
|||
nix eval --impure --raw --expr 'builtins.currentSystem'
|
||||
====
|
||||
|
||||
[source,nix,linenums,highlight="10,12,14"]
|
||||
[source,nix,linenums,highlight="11,13"]
|
||||
.flake.nix (version 1)
|
||||
....
|
||||
$# cat flake.nix
|
||||
|
|
@ -86,29 +114,12 @@ $# cat flake.nix
|
|||
|
||||
The `description` part is just a short description of the package.
|
||||
The `inputs` section should be familiar from <<#flake-inputs>>.
|
||||
If we ignore parts of the long package names, the `outputs` section looks like this:
|
||||
So far, the `outputs` section only defines a development environment
|
||||
(we'll add to it in <<#define-package>>).
|
||||
|
||||
[source,subs=quotes]
|
||||
....
|
||||
devShells = {
|
||||
x86_64-linux.default =
|
||||
_blahblah_.mkShell {
|
||||
packages = [
|
||||
_blahblah_.cowsay
|
||||
];
|
||||
}; # mkShell
|
||||
}; # devShells
|
||||
....
|
||||
|
||||
This says, in effect, that to create a `default` shell for the `x86_64-linux` architecture,
|
||||
call the `mkShell` command and tell it you need the `cowsay` package.
|
||||
|
||||
The code is rather wordy, with all the _this.that.the.other.thing_ stuff.
|
||||
Also, the name `legacyPackages` suggests that we're not following current best practices.
|
||||
(In fact, it could in some circumstances result in duplicate instances of nixpkgs.)
|
||||
In <<#multi-arch>> we will refactor the code to eliminate some duplication, make it more readable,
|
||||
and eliminate the references to `legacyPackages`.
|
||||
For now, we will stick with the ugly, but straightforward, version.
|
||||
The repetition of `x86_64-linux` is undesirable.
|
||||
In <<#multi-arch>> we will refactor the code to eliminate some duplication and make it more readable.
|
||||
For now, we will stick with the straightforward version.
|
||||
|
||||
Let's enter the shell.
|
||||
|
||||
|
|
@ -136,18 +147,48 @@ We can see that `cowsay` is now available, and our script runs.
|
|||
$ ./cow-hello.sh # Succeeds
|
||||
....
|
||||
|
||||
[#define-package]
|
||||
== Defining the package
|
||||
|
||||
We created an appropriate development environment, and tested our script.
|
||||
Now we are ready to package it.
|
||||
Add the new lines below to `flake.nix`.
|
||||
|
||||
In <<#mkDerivation>> we learned that the function `pkgs.std.mkDerivation`
|
||||
provides a way to create a derivation by specifying the steps that need to be performed in each phase
|
||||
We specify the location of the source files.
|
||||
and use the standard unpack phase, which makes our source files available in `$src`
|
||||
during the build and installation.
|
||||
(We described the environment available during build and installation in <<#standard-environment>>.)
|
||||
We don't need to do anything in the build phase.
|
||||
In the install phase, we copy the script from `$src` to the output directory, `$out`,
|
||||
and make it executable.
|
||||
As with the development environment, we specify that `cowsay` is required.
|
||||
|
||||
[source,nix]
|
||||
....
|
||||
pkgs.stdenv.mkDerivation {
|
||||
name = "cow-hello.sh";
|
||||
src = ./.;
|
||||
unpackPhase = "true";
|
||||
buildPhase = ":";
|
||||
installPhase =
|
||||
''
|
||||
mkdir -p $out/bin
|
||||
cp $src/cow-hello.sh $out/bin
|
||||
chmod +x $out/bin/cow-hello.sh
|
||||
'';
|
||||
buildInputs = [ pkgs.cowsay ];
|
||||
}; # mkDerivation
|
||||
....
|
||||
|
||||
To update the flake, add the new lines below to `flake.nix`.
|
||||
Again, change `x86_64-linux` if needed to match your system architecture.
|
||||
|
||||
////
|
||||
$ cp ../flake-2.nix flake.nix
|
||||
////
|
||||
|
||||
[source,nix,linenums,highlight="19..36"]
|
||||
[source,nix,linenums,highlight="20..38"]
|
||||
.flake.nix (version 2)
|
||||
....
|
||||
$# cat flake.nix
|
||||
|
|
@ -162,13 +203,15 @@ $ nix run
|
|||
If we hadn't added `cow-hello.sh` to the git repository,
|
||||
we would have an error about the file being missing.
|
||||
|
||||
[#config-nixpkgs]
|
||||
== Configuring Nixpkgs
|
||||
|
||||
[#multi-arch]
|
||||
== Supporting multiple architectures
|
||||
|
||||
Congratulations!
|
||||
Our package is popular, and people want to run it on `aarch64-linux` systems.
|
||||
So now we need to add an entry for that to `packages`.
|
||||
So now we need to add an entry to `packages`.
|
||||
Of course we want to test it on the new architecture,
|
||||
so we'll add an entry to `devShells` as well.
|
||||
|
||||
|
|
@ -176,7 +219,7 @@ so we'll add an entry to `devShells` as well.
|
|||
$ cp ../flake-3.nix flake.nix
|
||||
////
|
||||
|
||||
[source,nix,linenums,highlight="18..23,44..59"]
|
||||
[source,nix,linenums,highlight="19..25,47..63"]
|
||||
.flake.nix (version 3)
|
||||
....
|
||||
$# cat flake.nix
|
||||
|
|
@ -223,7 +266,8 @@ For the second argument, we used a function that "pretended" to generate definit
|
|||
What if we wrote a function which, given the name of the system architecture,
|
||||
would generate the development shell definition for us,
|
||||
and another function that would do the same for the package definition?
|
||||
Applying `lib.genAttrs` and the list of system architecture names would give us
|
||||
Applying `lib.genAttrs` and the list of system architecture names
|
||||
to those functions would give us
|
||||
all the definitions we need for the outputs section.
|
||||
|
||||
The following function will generate a development shell definition.
|
||||
|
|
@ -269,17 +313,21 @@ We can make the flake more readable with the following definitions.
|
|||
[source,nix]
|
||||
....
|
||||
supportedSystems = [ "x86_64-linux" "aarch64-linux" ];
|
||||
|
||||
forAllSupportedSystems = nixpkgs.lib.genAttrs supportedSystems;
|
||||
forEachSystem = nixpkgs.lib.genAttrs supportedSystems;
|
||||
....
|
||||
|
||||
So `forEachSystem` is a function which takes one argument.
|
||||
That argument should be a function that, given the system name,
|
||||
generates the appropriate definition for that system,
|
||||
Now let's examine the definition of `nixpkgsFor.${system}`.
|
||||
|
||||
[source,nix]
|
||||
....
|
||||
nixpkgsFor = forAllSupportedSystems (system: import nixpkgs { inherit system; });
|
||||
nixpkgsFor = forEachSystem (system: import nixpkgs { inherit system; });
|
||||
....
|
||||
|
||||
Here we're using `forEachSystem` to access the appropriate nixpkgs for a system.
|
||||
|
||||
Putting everything together, we have a shiny new flake.
|
||||
You may want to compare it carefully to the original version,
|
||||
in order to reassure yourself that the definitions are equivalent.
|
||||
|
|
@ -288,8 +336,8 @@ in order to reassure yourself that the definitions are equivalent.
|
|||
$ cp ../flake-4.nix flake.nix
|
||||
////
|
||||
|
||||
[source,nix,linenums,highlight="18..23,44..59"]
|
||||
.flake.nix (version 3)
|
||||
[source,nix,linenums,highlight="10..12,16,17,23,24"]
|
||||
.flake.nix (version 4)
|
||||
....
|
||||
$# cat flake.nix
|
||||
....
|
||||
|
|
@ -301,9 +349,65 @@ $ git commit -am "refactored the flake"
|
|||
$ nix run
|
||||
....
|
||||
|
||||
// TODO In packages, add cow-hello and set default = cow-hello. Explain why.
|
||||
== A few more improvements
|
||||
|
||||
// TODO Add an apps section and explain why.
|
||||
I took some shortcuts in the flake definitions up to this point, just to keep it simple.
|
||||
Normally a flake has both a `packages` section and an `apps` section.
|
||||
The `apps` section is where we specify executable programs.
|
||||
If there is no `apps` section, then `nix run` will default to using the package, but that's not ideal.
|
||||
|
||||
A typical flake might have multiple packages and multiple apps.
|
||||
(We could even have multiple development environments.)
|
||||
Normally we would specify a default package and a default app.
|
||||
The command `nix run ` _flakeurl_`#`_appname_ will run the app named _appname_ from the `apps` section of _flakeurl_.
|
||||
If we don't specify _appname_, the default app is run.
|
||||
|
||||
To define an app, we specify the type and the path to the executable.
|
||||
We can re-use the definition from the `packages` section as shown below.
|
||||
|
||||
[source,nix]
|
||||
....
|
||||
hello = {
|
||||
type = "app";
|
||||
program = pkgs.lib.getExe self.packages.${system}.hello;
|
||||
};
|
||||
....
|
||||
|
||||
Later we might want to add overlays or some configuration options to `nixpkgs` in our flake.
|
||||
We can include the scaffolding for it with the following change.
|
||||
|
||||
[source,nix]
|
||||
....
|
||||
nixpkgsFor = forEachSystem (system: import nixpkgs {
|
||||
inherit system;
|
||||
config = { };
|
||||
overlays = [ ];
|
||||
});
|
||||
....
|
||||
|
||||
The Nix manual has more information on
|
||||
https://nixos.org/manual/nixpkgs/stable/#sec-config-options-reference[config options]
|
||||
and
|
||||
https://nixos.org/manual/nixpkgs/stable/#chap-overlays[overlays].
|
||||
|
||||
Putting everything together, we have:
|
||||
|
||||
////
|
||||
$ cp ../flake-5.nix flake.nix
|
||||
////
|
||||
|
||||
[source,nix,linenums,highlight="17,18,31,32,46,49..57"]
|
||||
.flake.nix (version 5)
|
||||
....
|
||||
$# cat flake.nix
|
||||
....
|
||||
|
||||
Let's verify that it runs on our system.
|
||||
|
||||
....
|
||||
$ git commit -am "refactored the flake"
|
||||
$ nix run
|
||||
....
|
||||
|
||||
////
|
||||
Good adoc0 scripts clean up after themselves.
|
||||
|
|
|
|||
|
|
@ -136,7 +136,7 @@ This attribute is considered experimental and is subject to change.
|
|||
====
|
||||
|
||||
[#mkShell]
|
||||
== `pkgs.mkShell`
|
||||
== `pkgs.mkShell` and `pkgs.mkShellNoCC`
|
||||
|
||||
The function
|
||||
https://nixos.org/manual/nixpkgs/stable/#sec-pkgs-mkShell[`pkgs.mkShell`]
|
||||
|
|
@ -167,6 +167,7 @@ along with all attributes of `stdenv.mkDerivation`.
|
|||
|`""`
|
||||
|===
|
||||
|
||||
If you don't need a C compiler, you can use `mkShellNoCC` instead.
|
||||
|
||||
[#mkDerivation]
|
||||
== `stdenv.mkDerivation`
|
||||
|
|
@ -190,7 +191,7 @@ Some of the commonly used attributes are listed below.
|
|||
|location of the source files
|
||||
|
||||
|`unpackPhase`
|
||||
|Bash command to copy/unpack the source files. If set to `"true"`
|
||||
|Bash command to copy/unpack the source files. If set to `"true"`, copies/unpacks all files in `src`, including subdirectories.
|
||||
|
||||
|`buildPhase`
|
||||
|Bash commands to build the package. If no action is required, use the no-op `":"` command.
|
||||
|
|
|
|||
68443
wombats-book-of-nix.pdf
68443
wombats-book-of-nix.pdf
File diff suppressed because it is too large
Load diff
Loading…
Add table
Add a link
Reference in a new issue