From 0588baf2e97aad78e354aeada1c3abd121fa534f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Amy=20de=20Buitl=C3=A9ir?= Date: Sun, 23 Dec 2018 22:37:23 +0000 Subject: [PATCH 01/11] Can now divide using / --- pills/04-basics-of-language.xml | 32 +++++++++++++++++--------------- pills/04/basics.txt | 8 ++++++-- pills/04/division.txt | 5 +++++ pills/04/relative-path.txt | 3 ++- 4 files changed, 30 insertions(+), 18 deletions(-) create mode 100644 pills/04/division.txt diff --git a/pills/04-basics-of-language.xml b/pills/04-basics-of-language.xml index cae9826..42e6478 100644 --- a/pills/04-basics-of-language.xml +++ b/pills/04-basics-of-language.xml @@ -60,8 +60,7 @@ Launch nix repl. First of all, Nix supports basic arithmetic operations: - +, -, and *. Integer division can be - done with builtins.div. + +, -, * and /. (To exit nix repl, use the command :q. Help is available through the :? command.) @@ -69,16 +68,24 @@ - 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. + + - Just think - builtins.div 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 6/3 as a relative path to the current directory. + To get Nix to perform division instead, leave a space after the /. + Alternatively, you can use builtins.div. + + Other operators are ||, && and ! for booleans, and relational @@ -101,14 +108,9 @@ - Try to use / between two numbers: - - - - - - Nix parsed 2/3 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 + 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 ./. In addition, Nix also parses urls specially. diff --git a/pills/04/basics.txt b/pills/04/basics.txt index 42f21eb..8cd4664 100644 --- a/pills/04/basics.txt +++ b/pills/04/basics.txt @@ -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 diff --git a/pills/04/division.txt b/pills/04/division.txt new file mode 100644 index 0000000..36e768b --- /dev/null +++ b/pills/04/division.txt @@ -0,0 +1,5 @@ +nix-repl> 6/ 3 +2 + +nix-repl> builtins.div 6 3 +2 diff --git a/pills/04/relative-path.txt b/pills/04/relative-path.txt index e9bc749..95ec7a6 100644 --- a/pills/04/relative-path.txt +++ b/pills/04/relative-path.txt @@ -1,2 +1,3 @@ -nix-repl> 2/3 +nix-repl> 6/3 /home/nix/2/3 + From f103d25dca12969f97a25af8984617e159ee589e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Amy=20de=20Buitl=C3=A9ir?= Date: Sun, 23 Dec 2018 22:43:19 +0000 Subject: [PATCH 02/11] Emphasised an exercise for the reader --- pills/08-generic-builders.xml | 1 + 1 file changed, 1 insertion(+) diff --git a/pills/08-generic-builders.xml b/pills/08-generic-builders.xml index 0a4061b..d9fa1dd 100644 --- a/pills/08-generic-builders.xml +++ b/pills/08-generic-builders.xml @@ -231,6 +231,7 @@ + Exercise: Complete the new builder.sh by adding $baseInputs in the for loop together with $buildInputs. As you noticed, we passed that new variable in From c509fdc241ad204d26288a70fffcb4b0c9df30bf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Amy=20de=20Buitl=C3=A9ir?= Date: Sun, 23 Dec 2018 23:23:13 +0000 Subject: [PATCH 03/11] Emphasised an exercise for the reader --- pills/09-automatic-runtime.xml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pills/09-automatic-runtime.xml b/pills/09-automatic-runtime.xml index 196ca02..f3b84dc 100644 --- a/pills/09-automatic-runtime.xml +++ b/pills/09-automatic-runtime.xml @@ -208,7 +208,8 @@ That is, for each file we run patchelf --shrink-rpath and strip. Note that we used two new commands here, - find and patchelf. These two + find and patchelf. + Exercise: These two deserve a place in baseInputs of autotools.nix as findutils and patchelf. From 6349bfe6ba85714ceb4e37584e37a9a2c3334341 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Amy=20de=20Buitl=C3=A9ir?= Date: Sun, 23 Dec 2018 23:27:36 +0000 Subject: [PATCH 04/11] Reference hello-2.10.tar.gz as in pill 08 --- pills/10/hello-nix.txt | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 pills/10/hello-nix.txt diff --git a/pills/10/hello-nix.txt b/pills/10/hello-nix.txt new file mode 100644 index 0000000..c216fb1 --- /dev/null +++ b/pills/10/hello-nix.txt @@ -0,0 +1,7 @@ +let + pkgs = import {}; + mkDerivation = import ./autotools.nix pkgs; +in mkDerivation { + name = "hello"; + src = ./hello-2.10.tar.gz; +} From 01e7a6008bce14018f9ce24dceac5017f01e1e73 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Amy=20de=20Buitl=C3=A9ir?= Date: Sun, 23 Dec 2018 23:28:16 +0000 Subject: [PATCH 05/11] Using binutils-unwrapped for ar command --- pills/10/autotools-nix.txt | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 pills/10/autotools-nix.txt diff --git a/pills/10/autotools-nix.txt b/pills/10/autotools-nix.txt new file mode 100644 index 0000000..4473dc2 --- /dev/null +++ b/pills/10/autotools-nix.txt @@ -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) From 0f9e22dc6068a4a711f80e7213523ded44fb4355 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Amy=20de=20Buitl=C3=A9ir?= Date: Sun, 23 Dec 2018 23:28:47 +0000 Subject: [PATCH 06/11] Migrated code from gist --- pills/10-developing-with-nix-shell.xml | 26 +++++++++++++++-- pills/10/builder-sh.txt | 3 ++ pills/10/setup-sh.txt | 39 ++++++++++++++++++++++++++ 3 files changed, 66 insertions(+), 2 deletions(-) create mode 100644 pills/10/builder-sh.txt create mode 100644 pills/10/setup-sh.txt diff --git a/pills/10-developing-with-nix-shell.xml b/pills/10-developing-with-nix-shell.xml index 74d14e1..89a2bb0 100644 --- a/pills/10-developing-with-nix-shell.xml +++ b/pills/10-developing-with-nix-shell.xml @@ -129,11 +129,15 @@ - The codebase is becoming a little long. You can find all the files in this - nixpill10 gist. + Here is our modified autotools.nix. Noteworthy is the setup = ./setup.sh; attribute in the derivation, which adds setup.sh to the nix store and as usual, adds a $setup environment variable in the builder. + + + + + Thanks to that, we can split builder.sh into setup.sh and builder.sh. What builder.sh does is sourcing $setup and @@ -141,6 +145,24 @@ some bash changes. + + Here is the modified builder.sh. + + + + + + Here is the modified setup.sh. + + + + + + Finally, here is hello.nix. + + + + Now back to nix-shell: diff --git a/pills/10/builder-sh.txt b/pills/10/builder-sh.txt new file mode 100644 index 0000000..91b081f --- /dev/null +++ b/pills/10/builder-sh.txt @@ -0,0 +1,3 @@ +set -e +source $setup +genericBuild diff --git a/pills/10/setup-sh.txt b/pills/10/setup-sh.txt new file mode 100644 index 0000000..90e9840 --- /dev/null +++ b/pills/10/setup-sh.txt @@ -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 +} From c79175ea7d29a73ddf660498f27982c9f804b4fd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Amy=20de=20Buitl=C3=A9ir?= Date: Mon, 24 Dec 2018 23:58:49 +0000 Subject: [PATCH 07/11] Made result consistent. --- pills/04/relative-path.txt | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/pills/04/relative-path.txt b/pills/04/relative-path.txt index 95ec7a6..7ff07b2 100644 --- a/pills/04/relative-path.txt +++ b/pills/04/relative-path.txt @@ -1,3 +1,2 @@ nix-repl> 6/3 -/home/nix/2/3 - +/home/nix/6/3 From 063c568fb4175a9f2b427f72c5de02bb6f1d57ef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Amy=20de=20Buitl=C3=A9ir?= Date: Mon, 24 Dec 2018 23:59:09 +0000 Subject: [PATCH 08/11] Fixed indentation --- pills/04-basics-of-language.xml | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/pills/04-basics-of-language.xml b/pills/04-basics-of-language.xml index 42e6478..be49942 100644 --- a/pills/04-basics-of-language.xml +++ b/pills/04-basics-of-language.xml @@ -74,14 +74,13 @@ - 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 6/3 as a relative path to the current directory. - To get Nix to perform division instead, leave a space after the /. - Alternatively, you can use builtins.div. + 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 + 6/3 as a relative path to the current directory. To get + Nix to perform division instead, leave a space after the + /. Alternatively, you can use + builtins.div. @@ -110,8 +109,8 @@ 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 ./. In addition, Nix also parses urls specially. + Therefore to specify the current directory, use ./. + In addition, Nix also parses urls specially. From c923db7cca0cbd32a387fa18d62212d894d7377f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Amy=20de=20Buitl=C3=A9ir?= Date: Mon, 31 Dec 2018 02:25:14 +0000 Subject: [PATCH 09/11] Use note formatting --- pills/02-install-on-your-running.xml | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/pills/02-install-on-your-running.xml b/pills/02-install-on-your-running.xml index 16f167b..e269181 100644 --- a/pills/02-install-on-your-running.xml +++ b/pills/02-install-on-your-running.xml @@ -15,11 +15,14 @@ Now we'll install Nix on our running system and understand what changed in our system after the installation. - If you're using NixOS, Nix is already installed; - you can skip to the next pill. + + If you're using NixOS, Nix is already installed; + you can skip to the next pill. + + Installing Nix is as easy as installing any other package. From cd558279536f426b199badb9dcf9c8074dbb5514 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Amy=20de=20Buitl=C3=A9ir?= Date: Mon, 31 Dec 2018 02:25:36 +0000 Subject: [PATCH 10/11] Use note formatting --- pills/02-install-on-your-running.xml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pills/02-install-on-your-running.xml b/pills/02-install-on-your-running.xml index e269181..d472a5f 100644 --- a/pills/02-install-on-your-running.xml +++ b/pills/02-install-on-your-running.xml @@ -17,11 +17,11 @@ changed in our system after the installation. - + If you're using NixOS, Nix is already installed; you can skip to the next pill. - + Installing From a451a48fb2827a30ed99bdcd68a0bf4230371ab0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Amy=20de=20Buitl=C3=A9ir?= Date: Mon, 31 Dec 2018 02:31:03 +0000 Subject: [PATCH 11/11] Use note formatting --- pills/03-enter-environment.xml | 8 ++++---- pills/04-basics-of-language.xml | 7 +++++-- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/pills/03-enter-environment.xml b/pills/03-enter-environment.xml index 4387bbd..49684c4 100644 --- a/pills/03-enter-environment.xml +++ b/pills/03-enter-environment.xml @@ -16,10 +16,10 @@
Enter the environment - - If you're using NixOS, you can skip to the next step. - + + If you're using NixOS, you can skip to the next step. + In the previous article we created a Nix user, so let's start by switching diff --git a/pills/04-basics-of-language.xml b/pills/04-basics-of-language.xml index be49942..6a6605c 100644 --- a/pills/04-basics-of-language.xml +++ b/pills/04-basics-of-language.xml @@ -61,10 +61,13 @@ Launch nix repl. First of all, Nix supports basic arithmetic operations: +, -, * and /. - (To exit nix repl, use the command :q. - Help is available through the :? command.) + + To exit nix repl, use the command :q. + Help is available through the :? command. + +