mirror of
https://github.com/NixOS/nix-pills
synced 2024-09-19 04:00:13 -04:00
parent
ba16e06742
commit
eff2de0555
|
@ -60,8 +60,7 @@
|
|||
|
||||
<para>
|
||||
Launch <literal>nix repl</literal>. First of all, Nix supports basic arithmetic operations:
|
||||
<literal>+</literal>, <literal>-</literal>, and <literal>*</literal>. Integer division can be
|
||||
done with <literal>builtins.div</literal>.
|
||||
<literal>+</literal>, <literal>-</literal>, <literal>*</literal> and <literal>/</literal>.
|
||||
(To exit <literal>nix repl</literal>, use the command <literal>:q</literal>.
|
||||
Help is available through the <literal>:?</literal> command.)
|
||||
</para>
|
||||
|
@ -69,16 +68,23 @@
|
|||
<screen><xi:include href="./04/basics.txt" parse="text" /></screen>
|
||||
|
||||
<para>
|
||||
You might wonder why Nix doesn't have basic operations such as integer division. The answer is
|
||||
because it's not needed for creating packages. Nix is not a general purpose
|
||||
language, it's a domain-specific language for writing packages.
|
||||
Attempting to perform division in Nix can lead to some surprises.
|
||||
</para>
|
||||
|
||||
<screen><xi:include href="./04/relative-path.txt" parse="text" /></screen>
|
||||
|
||||
<para>
|
||||
Just think - <literal>builtins.div</literal> is not used in the whole of the
|
||||
nixpkgs repository: it's not actually useful if you are writing package expressions.
|
||||
What happened? Recall that Nix is not a general purpose language, it's a
|
||||
domain-specific language for writing packages. Integer division isn't
|
||||
actually that useful when writing package expressions. Nix parsed
|
||||
<literal>6/3</literal> as a relative path to the current directory. To get
|
||||
Nix to perform division instead, leave a space after the
|
||||
<literal>/</literal>. Alternatively, you can use
|
||||
<literal>builtins.div</literal>.
|
||||
</para>
|
||||
|
||||
<screen><xi:include href="./04/division.txt" parse="text" /></screen>
|
||||
|
||||
<para>
|
||||
Other operators are <literal>||</literal>, <literal>&&</literal> and <literal>!</literal>
|
||||
for booleans, and relational
|
||||
|
@ -101,15 +107,10 @@
|
|||
</para>
|
||||
|
||||
<para>
|
||||
Try to use <literal>/</literal> between two numbers:
|
||||
</para>
|
||||
|
||||
<screen><xi:include href="./04/relative-path.txt" parse="text" /></screen>
|
||||
|
||||
<para>
|
||||
Nix parsed <literal>2/3</literal> as a relative path to the current directory. Expressions will
|
||||
be parsed as paths as long as there's a slash. Therefore to specify the current
|
||||
directory, use <literal>./.</literal> In addition, Nix also parses urls specially.
|
||||
As demonstrated above, expressions will
|
||||
be parsed as paths as long as there's a slash not followed by a space.
|
||||
Therefore to specify the current directory, use <literal>./.</literal>
|
||||
In addition, Nix also parses urls specially.
|
||||
</para>
|
||||
|
||||
<para>
|
||||
|
|
|
@ -1,4 +1,8 @@
|
|||
nix-repl> 1+3
|
||||
4
|
||||
nix-repl> builtins.div 6 3
|
||||
2
|
||||
|
||||
nix-repl> 7-4
|
||||
3
|
||||
|
||||
nix-repl> 3*2
|
||||
6
|
||||
|
|
5
pills/04/division.txt
Normal file
5
pills/04/division.txt
Normal file
|
@ -0,0 +1,5 @@
|
|||
nix-repl> 6/ 3
|
||||
2
|
||||
|
||||
nix-repl> builtins.div 6 3
|
||||
2
|
|
@ -1,2 +1,2 @@
|
|||
nix-repl> 2/3
|
||||
/home/nix/2/3
|
||||
nix-repl> 6/3
|
||||
/home/nix/6/3
|
||||
|
|
|
@ -231,6 +231,7 @@
|
|||
<screen><xi:include href="./08/set-union.txt" parse="text" /></screen>
|
||||
|
||||
<para>
|
||||
<emphasis role="bold">Exercise:</emphasis>
|
||||
Complete the new <filename>builder.sh</filename> by adding
|
||||
<code>$baseInputs</code> in the <code>for</code> loop together with
|
||||
<code>$buildInputs</code>. As you noticed, we passed that new variable in
|
||||
|
|
|
@ -208,7 +208,8 @@
|
|||
<para>
|
||||
That is, for each file we run <command>patchelf --shrink-rpath</command>
|
||||
and <command>strip</command>. Note that we used two new commands here,
|
||||
<command>find</command> and <command>patchelf</command>. These two
|
||||
<command>find</command> and <command>patchelf</command>.
|
||||
<emphasis role="bold">Exercise:</emphasis> These two
|
||||
deserve a place in <code>baseInputs</code> of
|
||||
<filename>autotools.nix</filename> as <command>findutils</command> and
|
||||
<command>patchelf</command>.
|
||||
|
|
|
@ -129,11 +129,15 @@
|
|||
</para>
|
||||
|
||||
<para>
|
||||
The codebase is becoming a little long. You can find all the files in this
|
||||
<link xlink:href="https://gist.github.com/lethalman/97fae227329b442267bc">nixpill10 gist</link>.
|
||||
Here is our modified <filename>autotools.nix</filename>.
|
||||
Noteworthy is the <code>setup = ./setup.sh;</code> attribute in the
|
||||
derivation, which adds <filename>setup.sh</filename> to the nix store and
|
||||
as usual, adds a <code>$setup</code> environment variable in the builder.
|
||||
</para>
|
||||
|
||||
<programlisting><xi:include href="./10/autotools-nix.txt" parse="text" /></programlisting>
|
||||
|
||||
<para>
|
||||
Thanks to that, we can split <filename>builder.sh</filename> into
|
||||
<filename>setup.sh</filename> and <filename>builder.sh</filename>. What
|
||||
<filename>builder.sh</filename> does is sourcing <code>$setup</code> and
|
||||
|
@ -141,6 +145,24 @@
|
|||
some bash changes.
|
||||
</para>
|
||||
|
||||
<para>
|
||||
Here is the modified <filename>builder.sh</filename>.
|
||||
</para>
|
||||
|
||||
<programlisting><xi:include href="./10/builder-sh.txt" parse="text" /></programlisting>
|
||||
|
||||
<para>
|
||||
Here is the modified <filename>setup.sh</filename>.
|
||||
</para>
|
||||
|
||||
<programlisting><xi:include href="./10/setup-sh.txt" parse="text" /></programlisting>
|
||||
|
||||
<para>
|
||||
Finally, here is <filename>hello.nix</filename>.
|
||||
</para>
|
||||
|
||||
<programlisting><xi:include href="./10/hello-nix.txt" parse="text" /></programlisting>
|
||||
|
||||
<para>
|
||||
Now back to nix-shell:
|
||||
</para>
|
||||
|
|
12
pills/10/autotools-nix.txt
Normal file
12
pills/10/autotools-nix.txt
Normal file
|
@ -0,0 +1,12 @@
|
|||
pkgs: attrs:
|
||||
with pkgs;
|
||||
let defaultAttrs = {
|
||||
builder = "${bash}/bin/bash";
|
||||
args = [ ./builder.sh ];
|
||||
setup = ./setup.sh;
|
||||
baseInputs = [ gnutar gzip gnumake gcc binutils-unwrapped coreutils gawk gnused gnugrep patchelf findutils ];
|
||||
buildInputs = [];
|
||||
system = builtins.currentSystem;
|
||||
};
|
||||
in
|
||||
derivation (defaultAttrs // attrs)
|
3
pills/10/builder-sh.txt
Normal file
3
pills/10/builder-sh.txt
Normal file
|
@ -0,0 +1,3 @@
|
|||
set -e
|
||||
source $setup
|
||||
genericBuild
|
7
pills/10/hello-nix.txt
Normal file
7
pills/10/hello-nix.txt
Normal file
|
@ -0,0 +1,7 @@
|
|||
let
|
||||
pkgs = import <nixpkgs> {};
|
||||
mkDerivation = import ./autotools.nix pkgs;
|
||||
in mkDerivation {
|
||||
name = "hello";
|
||||
src = ./hello-2.10.tar.gz;
|
||||
}
|
39
pills/10/setup-sh.txt
Normal file
39
pills/10/setup-sh.txt
Normal file
|
@ -0,0 +1,39 @@
|
|||
unset PATH
|
||||
for p in $baseInputs $buildInputs; do
|
||||
export PATH=$p/bin${PATH:+:}$PATH
|
||||
done
|
||||
|
||||
function unpackPhase() {
|
||||
tar -xzf $src
|
||||
|
||||
for d in *; do
|
||||
if [ -d "$d" ]; then
|
||||
cd "$d"
|
||||
break
|
||||
fi
|
||||
done
|
||||
}
|
||||
|
||||
function configurePhase() {
|
||||
./configure --prefix=$out
|
||||
}
|
||||
|
||||
function buildPhase() {
|
||||
make
|
||||
}
|
||||
|
||||
function installPhase() {
|
||||
make install
|
||||
}
|
||||
|
||||
function fixupPhase() {
|
||||
find $out -type f -exec patchelf --shrink-rpath '{}' \; -exec strip '{}' \; 2>/dev/null
|
||||
}
|
||||
|
||||
function genericBuild() {
|
||||
unpackPhase
|
||||
configurePhase
|
||||
buildPhase
|
||||
installPhase
|
||||
fixupPhase
|
||||
}
|
Loading…
Reference in a new issue