From 57dc8ac9218bece83a03ff173cba7621e0ba4126 Mon Sep 17 00:00:00 2001 From: Noam Yorav-Raphael Date: Mon, 8 Apr 2024 23:16:24 +0300 Subject: [PATCH] Transform elements to include content --- pills/01-why-you-should-give-it-try.xml | 4 +- pills/01/which-bash.txt | 2 - pills/02-install-on-your-running.xml | 26 ++- pills/02/ldd-bash.txt | 3 - pills/02/nixpkgs-expressions.txt | 4 - pills/02/profile.xml | 6 - pills/02/user-environment.xml | 4 - pills/03-enter-environment.xml | 81 ++++++-- pills/03/channel-list.txt | 2 - pills/03/generation-3.txt | 2 - pills/03/install-hello.txt | 5 - pills/03/install.txt | 1 - pills/03/list-generations.txt | 3 - pills/03/list-installed-derivations.txt | 3 - pills/03/ls-nix-profile.txt | 4 - pills/03/ls-profile-bin.txt | 8 - pills/03/nix-env-rollback.txt | 1 - pills/03/nix-store-tree.txt | 2 - pills/03/nix-store.txt | 2 - pills/03/references.txt | 3 - pills/03/referrers.txt | 6 - pills/03/rollback.txt | 2 - pills/03/source-nix.txt | 1 - pills/03/uninstall-all.txt | 4 - pills/04-basics-of-language.xml | 126 +++++++++--- pills/04/basics.txt | 8 - pills/04/dash.txt | 4 - pills/04/division.txt | 5 - pills/04/double-quotes.txt | 4 - pills/04/escaping.txt | 4 - pills/04/if.txt | 4 - pills/04/interpolate.txt | 7 - pills/04/lazy.txt | 2 - pills/04/let-basic.txt | 2 - pills/04/let-multiple-assign.txt | 4 - pills/04/let-multiple.txt | 2 - pills/04/let-nested.txt | 2 - pills/04/let-reference.txt | 2 - pills/04/let-scope.txt | 2 - pills/04/lists.txt | 2 - pills/04/relative-path.txt | 2 - pills/04/set-access.txt | 4 - pills/04/set-basics.txt | 3 - pills/04/set-failed.txt | 2 - pills/04/set-recursive.txt | 2 - pills/04/strings-basic.txt | 4 - pills/04/with-basic.txt | 5 - pills/04/with-scope.txt | 4 - pills/05-functions-and-imports.xml | 97 ++++++--- pills/05/a-nix.txt | 1 - pills/05/anon-function.txt | 2 - pills/05/argument-set-error.txt | 5 - pills/05/b-nix.txt | 1 - pills/05/default-values.txt | 5 - pills/05/import.txt | 5 - pills/05/mul-nix.txt | 1 - pills/05/multi-argument-function.txt | 7 - pills/05/named-function.txt | 5 - pills/05/named-set-argument.txt | 3 - pills/05/no-parenthesis.txt | 9 - pills/05/partial-application.txt | 5 - pills/05/set-argument.txt | 6 - pills/05/test-import-2.txt | 3 - pills/05/test-import.txt | 2 - pills/05/test-nix-2.txt | 4 - pills/05/test-nix.txt | 1 - pills/05/variadic-arguments.txt | 2 - pills/06-our-first-derivation.xml | 123 +++++++++-- pills/06/build-derivation.txt | 7 - pills/06/check-drvattrs.txt | 2 - pills/06/coreutils.txt | 6 - pills/06/current-system.txt | 2 - pills/06/drvattrs.txt | 2 - pills/06/examine-build.xml | 25 --- pills/06/fake-system.txt | 3 - pills/06/fix-attribute.txt | 4 - pills/06/inspect-values.txt | 5 - pills/06/interpolate.txt | 4 - pills/06/list-coreutils.txt | 2 - pills/06/outpath.txt | 4 - pills/06/realise-derivation.txt | 1 - pills/06/reference.txt | 2 - pills/06/show-derivation.xml | 21 -- pills/06/test-build.txt | 5 - pills/06/tostring.txt | 4 - pills/06/type-derivation.txt | 2 - pills/07-working-derivation.xml | 116 ++++++++++- pills/07/bash.xml | 5 - pills/07/builder.sh.txt | 2 - pills/07/c-program-derivation.xml | 7 - pills/07/foo.drv.xml | 29 --- pills/07/read-log.xml | 18 -- pills/07/simple-derivation.xml | 6 - pills/07/simple.c.txt | 3 - pills/07/simple.txt | 12 -- pills/07/simple_builder.sh.txt | 3 - pills/07/simple_inherit.txt | 11 - pills/08-generic-builders.xml | 150 +++++++++++++- pills/08/autotools-nix.txt | 21 -- pills/08/generic-builder.txt | 18 -- pills/08/hello-builder.txt | 6 - pills/08/hello-nix-darwin.txt | 21 -- pills/08/hello-nix-rev-1.txt | 21 -- pills/08/hello-nix-rev-2.txt | 8 - pills/08/hello-nix.txt | 21 -- pills/08/set-union.txt | 4 - pills/08/to-string-nixpkgs.txt | 6 - pills/08/to-string.txt | 4 - pills/09-automatic-runtime.xml | 47 ++++- pills/09/build-hello-nix.txt | 5 - pills/09/find.txt | 1 - pills/09/instantiate-hello.txt | 8 - pills/09/instantiate.txt | 15 -- pills/09/ldd-hello.txt | 4 - pills/09/strings.txt | 2 - pills/10-developing-with-nix-shell.xml | 98 ++++++++- pills/10/autotools-nix.txt | 24 --- pills/10/builder-sh.txt | 3 - pills/10/hello-nix.txt | 8 - pills/10/nix-shell-hello.txt | 5 - pills/10/nix-shell-source.txt | 3 - pills/10/setup-sh.txt | 39 ---- pills/10/source-builder.txt | 2 - pills/11-garbage-collector.xml | 50 ++++- pills/11/channel-update.txt | 4 - pills/11/install-bsd-games.txt | 8 - pills/11/ls-gcroots-auto.txt | 5 - pills/11/nix-collect-garbage.txt | 6 - pills/11/remove-bsd-games.txt | 6 - pills/11/remove-gen-9.txt | 9 - pills/12-inputs-design-pattern.xml | 108 +++++++++- pills/12/graphviz-derivation.txt | 8 - pills/12/graphviz-gd-derivation.txt | 13 -- pills/12/graphviz-mkderivation.txt | 14 -- pills/12/nix-env-install-graphviz.txt | 3 - pills/12/repository-mkderivation.txt | 25 --- pills/12/repository-test-nix-build.txt | 4 - pills/12/repository-test-nix-repl.txt | 7 - pills/12/repository.txt | 4 - pills/12/setup-sh.txt | 8 - pills/12/simple-png.txt | 2 - pills/13-callpackage-design-pattern.xml | 61 +++++- pills/13/callpackage-function-call.txt | 4 - pills/13/callpackage-function-overrides.txt | 5 - pills/13/callpackage-function.txt | 5 - pills/13/callpackage-usage.txt | 17 -- pills/13/get-args-function.txt | 3 - pills/13/intersect-attr-values.txt | 5 - pills/13/package-derivation.txt | 2 - pills/13/repository-derivation.txt | 4 - pills/14-override-design-pattern.xml | 57 ++++- pills/14/make-overridable-lib.txt | 8 - pills/14/mygraphviz.txt | 11 - pills/14/nix-repl-make-overridable-test.txt | 11 - pills/14/nix-repl-make-overridable-twice.txt | 9 - pills/14/rec-make-overridable.txt | 8 - pills/15-nix-search-paths.xml | 43 +++- pills/15/mypkgs-graphviz-multiple.txt | 4 - pills/15/mypkgs-install-attr-graphviz.txt | 3 - pills/15/mypkgs-path.txt | 4 - pills/15/mypkgs-query-all.txt | 4 - pills/15/nix-instantiate-ping.txt | 6 - pills/15/nixpkgs-path.txt | 4 - pills/15/ping-custom-path.txt | 4 - pills/16-nixpkgs-parameters.xml | 20 +- pills/16/config-foo-bar.txt | 7 - pills/16/myrelease-nix.txt | 4 - pills/16/pkgs-psmisc.txt | 3 - pills/17-nixpkgs-overriding-packages.xml | 49 ++++- pills/17/build-asciidoc-graphviz-override.txt | 2 - pills/17/config-nix.txt | 8 - pills/17/fix-function.txt | 9 - pills/17/fix-pkgs-function.txt | 4 - pills/17/graphviz-override.txt | 4 - pills/17/newpkgs-override-set.txt | 5 - pills/17/p-graphviz-override.txt | 3 - pills/18-nix-store-paths.xml | 85 +++++++- pills/18/bar-derivation.xml | 12 -- pills/18/derivation-simple-content.xml | 23 --- pills/18/derivation-simple.txt | 3 - pills/18/mycontent-string-representation.txt | 4 - pills/18/mycontent.txt | 5 - pills/18/myfile-final-hash.txt | 2 - pills/18/myfile-hash-alternate.txt | 2 - pills/18/myfile-hash.txt | 2 - pills/18/myfile-string-hash.txt | 3 - pills/18/myout-drv-hash.txt | 5 - pills/18/output-path-replace-empty.txt | 2 - pills/19-fundamentals-of-stdenv.xml | 119 ++++++++++- pills/19/default-builder.txt | 2 - pills/19/hello-derivation.xml | 50 ----- pills/19/stdenv-derivation.txt | 8 - pills/19/stdenv-hello-build.txt | 5 - pills/19/stdenv-hello.txt | 5 - pills/19/stdenv-mkderivation.txt | 11 - pills/19/stdenv-references.txt | 6 - pills/19/stdenv-setup-fake-builder.txt | 9 - pills/19/stdenv-setup-head.txt | 5 - pills/20-basic-dependencies-and-hooks.xml | 194 ++++++++++++++++-- pills/20/build-inputs-0.bash | 4 - pills/20/build-inputs-1.bash | 14 -- pills/20/build-inputs-2.bash | 3 - pills/20/build-inputs-3.bash | 9 - pills/20/build-inputs-4.bash | 1 - pills/20/env-hooks-0.bash | 10 - pills/20/env-hooks-1.bash | 7 - pills/20/propagated-build-inputs-0.bash | 12 -- pills/20/propagated-build-inputs-1.bash | 11 - pills/20/propagated-build-inputs-2.bash | 4 - pills/20/setup-hooks-0.bash | 12 -- pills/20/three-hellos.nix | 46 ----- pills/20/two-hellos.nix | 35 ---- 212 files changed, 1450 insertions(+), 1493 deletions(-) delete mode 100644 pills/01/which-bash.txt delete mode 100644 pills/02/ldd-bash.txt delete mode 100644 pills/02/nixpkgs-expressions.txt delete mode 100644 pills/02/profile.xml delete mode 100644 pills/02/user-environment.xml delete mode 100644 pills/03/channel-list.txt delete mode 100644 pills/03/generation-3.txt delete mode 100644 pills/03/install-hello.txt delete mode 100644 pills/03/install.txt delete mode 100644 pills/03/list-generations.txt delete mode 100644 pills/03/list-installed-derivations.txt delete mode 100644 pills/03/ls-nix-profile.txt delete mode 100644 pills/03/ls-profile-bin.txt delete mode 100644 pills/03/nix-env-rollback.txt delete mode 100644 pills/03/nix-store-tree.txt delete mode 100644 pills/03/nix-store.txt delete mode 100644 pills/03/references.txt delete mode 100644 pills/03/referrers.txt delete mode 100644 pills/03/rollback.txt delete mode 100644 pills/03/source-nix.txt delete mode 100644 pills/03/uninstall-all.txt delete mode 100644 pills/04/basics.txt delete mode 100644 pills/04/dash.txt delete mode 100644 pills/04/division.txt delete mode 100644 pills/04/double-quotes.txt delete mode 100644 pills/04/escaping.txt delete mode 100644 pills/04/if.txt delete mode 100644 pills/04/interpolate.txt delete mode 100644 pills/04/lazy.txt delete mode 100644 pills/04/let-basic.txt delete mode 100644 pills/04/let-multiple-assign.txt delete mode 100644 pills/04/let-multiple.txt delete mode 100644 pills/04/let-nested.txt delete mode 100644 pills/04/let-reference.txt delete mode 100644 pills/04/let-scope.txt delete mode 100644 pills/04/lists.txt delete mode 100644 pills/04/relative-path.txt delete mode 100644 pills/04/set-access.txt delete mode 100644 pills/04/set-basics.txt delete mode 100644 pills/04/set-failed.txt delete mode 100644 pills/04/set-recursive.txt delete mode 100644 pills/04/strings-basic.txt delete mode 100644 pills/04/with-basic.txt delete mode 100644 pills/04/with-scope.txt delete mode 100644 pills/05/a-nix.txt delete mode 100644 pills/05/anon-function.txt delete mode 100644 pills/05/argument-set-error.txt delete mode 100644 pills/05/b-nix.txt delete mode 100644 pills/05/default-values.txt delete mode 100644 pills/05/import.txt delete mode 100644 pills/05/mul-nix.txt delete mode 100644 pills/05/multi-argument-function.txt delete mode 100644 pills/05/named-function.txt delete mode 100644 pills/05/named-set-argument.txt delete mode 100644 pills/05/no-parenthesis.txt delete mode 100644 pills/05/partial-application.txt delete mode 100644 pills/05/set-argument.txt delete mode 100644 pills/05/test-import-2.txt delete mode 100644 pills/05/test-import.txt delete mode 100644 pills/05/test-nix-2.txt delete mode 100644 pills/05/test-nix.txt delete mode 100644 pills/05/variadic-arguments.txt delete mode 100644 pills/06/build-derivation.txt delete mode 100644 pills/06/check-drvattrs.txt delete mode 100644 pills/06/coreutils.txt delete mode 100644 pills/06/current-system.txt delete mode 100644 pills/06/drvattrs.txt delete mode 100644 pills/06/examine-build.xml delete mode 100644 pills/06/fake-system.txt delete mode 100644 pills/06/fix-attribute.txt delete mode 100644 pills/06/inspect-values.txt delete mode 100644 pills/06/interpolate.txt delete mode 100644 pills/06/list-coreutils.txt delete mode 100644 pills/06/outpath.txt delete mode 100644 pills/06/realise-derivation.txt delete mode 100644 pills/06/reference.txt delete mode 100644 pills/06/show-derivation.xml delete mode 100644 pills/06/test-build.txt delete mode 100644 pills/06/tostring.txt delete mode 100644 pills/06/type-derivation.txt delete mode 100644 pills/07/bash.xml delete mode 100644 pills/07/builder.sh.txt delete mode 100644 pills/07/c-program-derivation.xml delete mode 100644 pills/07/foo.drv.xml delete mode 100644 pills/07/read-log.xml delete mode 100644 pills/07/simple-derivation.xml delete mode 100644 pills/07/simple.c.txt delete mode 100644 pills/07/simple.txt delete mode 100644 pills/07/simple_builder.sh.txt delete mode 100644 pills/07/simple_inherit.txt delete mode 100644 pills/08/autotools-nix.txt delete mode 100644 pills/08/generic-builder.txt delete mode 100644 pills/08/hello-builder.txt delete mode 100644 pills/08/hello-nix-darwin.txt delete mode 100644 pills/08/hello-nix-rev-1.txt delete mode 100644 pills/08/hello-nix-rev-2.txt delete mode 100644 pills/08/hello-nix.txt delete mode 100644 pills/08/set-union.txt delete mode 100644 pills/08/to-string-nixpkgs.txt delete mode 100644 pills/08/to-string.txt delete mode 100644 pills/09/build-hello-nix.txt delete mode 100644 pills/09/find.txt delete mode 100644 pills/09/instantiate-hello.txt delete mode 100644 pills/09/instantiate.txt delete mode 100644 pills/09/ldd-hello.txt delete mode 100644 pills/09/strings.txt delete mode 100644 pills/10/autotools-nix.txt delete mode 100644 pills/10/builder-sh.txt delete mode 100644 pills/10/hello-nix.txt delete mode 100644 pills/10/nix-shell-hello.txt delete mode 100644 pills/10/nix-shell-source.txt delete mode 100644 pills/10/setup-sh.txt delete mode 100644 pills/10/source-builder.txt delete mode 100644 pills/11/channel-update.txt delete mode 100644 pills/11/install-bsd-games.txt delete mode 100644 pills/11/ls-gcroots-auto.txt delete mode 100644 pills/11/nix-collect-garbage.txt delete mode 100644 pills/11/remove-bsd-games.txt delete mode 100644 pills/11/remove-gen-9.txt delete mode 100644 pills/12/graphviz-derivation.txt delete mode 100644 pills/12/graphviz-gd-derivation.txt delete mode 100644 pills/12/graphviz-mkderivation.txt delete mode 100644 pills/12/nix-env-install-graphviz.txt delete mode 100644 pills/12/repository-mkderivation.txt delete mode 100644 pills/12/repository-test-nix-build.txt delete mode 100644 pills/12/repository-test-nix-repl.txt delete mode 100644 pills/12/repository.txt delete mode 100644 pills/12/setup-sh.txt delete mode 100644 pills/12/simple-png.txt delete mode 100644 pills/13/callpackage-function-call.txt delete mode 100644 pills/13/callpackage-function-overrides.txt delete mode 100644 pills/13/callpackage-function.txt delete mode 100644 pills/13/callpackage-usage.txt delete mode 100644 pills/13/get-args-function.txt delete mode 100644 pills/13/intersect-attr-values.txt delete mode 100644 pills/13/package-derivation.txt delete mode 100644 pills/13/repository-derivation.txt delete mode 100644 pills/14/make-overridable-lib.txt delete mode 100644 pills/14/mygraphviz.txt delete mode 100644 pills/14/nix-repl-make-overridable-test.txt delete mode 100644 pills/14/nix-repl-make-overridable-twice.txt delete mode 100644 pills/14/rec-make-overridable.txt delete mode 100644 pills/15/mypkgs-graphviz-multiple.txt delete mode 100644 pills/15/mypkgs-install-attr-graphviz.txt delete mode 100644 pills/15/mypkgs-path.txt delete mode 100644 pills/15/mypkgs-query-all.txt delete mode 100644 pills/15/nix-instantiate-ping.txt delete mode 100644 pills/15/nixpkgs-path.txt delete mode 100644 pills/15/ping-custom-path.txt delete mode 100644 pills/16/config-foo-bar.txt delete mode 100644 pills/16/myrelease-nix.txt delete mode 100644 pills/16/pkgs-psmisc.txt delete mode 100644 pills/17/build-asciidoc-graphviz-override.txt delete mode 100644 pills/17/config-nix.txt delete mode 100644 pills/17/fix-function.txt delete mode 100644 pills/17/fix-pkgs-function.txt delete mode 100644 pills/17/graphviz-override.txt delete mode 100644 pills/17/newpkgs-override-set.txt delete mode 100644 pills/17/p-graphviz-override.txt delete mode 100644 pills/18/bar-derivation.xml delete mode 100644 pills/18/derivation-simple-content.xml delete mode 100644 pills/18/derivation-simple.txt delete mode 100644 pills/18/mycontent-string-representation.txt delete mode 100644 pills/18/mycontent.txt delete mode 100644 pills/18/myfile-final-hash.txt delete mode 100644 pills/18/myfile-hash-alternate.txt delete mode 100644 pills/18/myfile-hash.txt delete mode 100644 pills/18/myfile-string-hash.txt delete mode 100644 pills/18/myout-drv-hash.txt delete mode 100644 pills/18/output-path-replace-empty.txt delete mode 100644 pills/19/default-builder.txt delete mode 100644 pills/19/hello-derivation.xml delete mode 100644 pills/19/stdenv-derivation.txt delete mode 100644 pills/19/stdenv-hello-build.txt delete mode 100644 pills/19/stdenv-hello.txt delete mode 100644 pills/19/stdenv-mkderivation.txt delete mode 100644 pills/19/stdenv-references.txt delete mode 100644 pills/19/stdenv-setup-fake-builder.txt delete mode 100644 pills/19/stdenv-setup-head.txt delete mode 100644 pills/20/build-inputs-0.bash delete mode 100644 pills/20/build-inputs-1.bash delete mode 100644 pills/20/build-inputs-2.bash delete mode 100644 pills/20/build-inputs-3.bash delete mode 100644 pills/20/build-inputs-4.bash delete mode 100644 pills/20/env-hooks-0.bash delete mode 100644 pills/20/env-hooks-1.bash delete mode 100644 pills/20/propagated-build-inputs-0.bash delete mode 100644 pills/20/propagated-build-inputs-1.bash delete mode 100644 pills/20/propagated-build-inputs-2.bash delete mode 100644 pills/20/setup-hooks-0.bash delete mode 100644 pills/20/three-hellos.nix delete mode 100644 pills/20/two-hellos.nix diff --git a/pills/01-why-you-should-give-it-try.xml b/pills/01-why-you-should-give-it-try.xml index 84bbff8..badbbdf 100644 --- a/pills/01-why-you-should-give-it-try.xml +++ b/pills/01-why-you-should-give-it-try.xml @@ -157,7 +157,9 @@ In fact, there's no ldconfig cache either. So where does bash find libc? - + $ ldd `which bash` +libc.so.6 => /nix/store/94n64qy99ja0vgbkf675nyk39g9b978n-glibc-2.19/lib/libc.so.6 (0x00007f0248cce000) + It turns out that when bash was built, it was built against that specific version of glibc in the Nix store, and at runtime it will require exactly that diff --git a/pills/01/which-bash.txt b/pills/01/which-bash.txt deleted file mode 100644 index aee9e1f..0000000 --- a/pills/01/which-bash.txt +++ /dev/null @@ -1,2 +0,0 @@ -$ ldd `which bash` -libc.so.6 => /nix/store/94n64qy99ja0vgbkf675nyk39g9b978n-glibc-2.19/lib/libc.so.6 (0x00007f0248cce000) diff --git a/pills/02-install-on-your-running.xml b/pills/02-install-on-your-running.xml index ab4679e..3ed493c 100644 --- a/pills/02-install-on-your-running.xml +++ b/pills/02-install-on-your-running.xml @@ -124,7 +124,11 @@ xlink:href="https://nixos.org/manual/nix/stable/package-management/profiles.html">profile: - + <screen>creating /home/nix/.nix-profile +installing 'nix-2.1.3' +building path(s) `/nix/store/a7p1w3z2h8pl00ywvw6icr3g5l9vm5r7-<emphasis role="strong">user-environment</emphasis>' +created 7 symlinks in user environment</screen> + A profile in Nix is a general and convenient concept for @@ -144,7 +148,13 @@ Let's take a closer look at our profile: - + <screen>$ ls -l ~/.nix-profile/ +bin -> /nix/store/ig31y9gfpp8pf3szdd7d4sf29zr7igbr-<emphasis role="strong">nix-2.1.3</emphasis>/bin +[...] +manifest.nix -> /nix/store/q8b5238akq07lj9gfb3qb5ycq4dxxiwm-<emphasis role="strong">env-manifest.nix</emphasis> +[...] +share -> /nix/store/ig31y9gfpp8pf3szdd7d4sf29zr7igbr-<emphasis role="strong">nix-2.1.3</emphasis>/share</screen> + That nix-2.1.3 derivation in the Nix store is Nix itself, with binaries and libraries. The process of "installing" @@ -188,8 +198,11 @@ More output from the installer: - + downloading Nix expressions from `http://releases.nixos.org/nixpkgs/nixpkgs-14.10pre46060.a1a2851/nixexprs.tar.xz'... +unpacking channels... +created 2 symlinks in user environment +modifying /home/nix/.profile... + Nix expressions are written in the - + +$ ldd /nix/store/*bash*/bin/bash +[...] + Keeping the store in /nix means we can grab diff --git a/pills/02/ldd-bash.txt b/pills/02/ldd-bash.txt deleted file mode 100644 index d4553b8..0000000 --- a/pills/02/ldd-bash.txt +++ /dev/null @@ -1,3 +0,0 @@ - -$ ldd /nix/store/*bash*/bin/bash -[...] diff --git a/pills/02/nixpkgs-expressions.txt b/pills/02/nixpkgs-expressions.txt deleted file mode 100644 index 88287be..0000000 --- a/pills/02/nixpkgs-expressions.txt +++ /dev/null @@ -1,4 +0,0 @@ -downloading Nix expressions from `http://releases.nixos.org/nixpkgs/nixpkgs-14.10pre46060.a1a2851/nixexprs.tar.xz'... -unpacking channels... -created 2 symlinks in user environment -modifying /home/nix/.profile... diff --git a/pills/02/profile.xml b/pills/02/profile.xml deleted file mode 100644 index 7230087..0000000 --- a/pills/02/profile.xml +++ /dev/null @@ -1,6 +0,0 @@ -$ ls -l ~/.nix-profile/ -bin -> /nix/store/ig31y9gfpp8pf3szdd7d4sf29zr7igbr-nix-2.1.3/bin -[...] -manifest.nix -> /nix/store/q8b5238akq07lj9gfb3qb5ycq4dxxiwm-env-manifest.nix -[...] -share -> /nix/store/ig31y9gfpp8pf3szdd7d4sf29zr7igbr-nix-2.1.3/share diff --git a/pills/02/user-environment.xml b/pills/02/user-environment.xml deleted file mode 100644 index 6dcaf0c..0000000 --- a/pills/02/user-environment.xml +++ /dev/null @@ -1,4 +0,0 @@ -creating /home/nix/.nix-profile -installing 'nix-2.1.3' -building path(s) `/nix/store/a7p1w3z2h8pl00ywvw6icr3g5l9vm5r7-user-environment' -created 7 symlinks in user environment diff --git a/pills/03-enter-environment.xml b/pills/03-enter-environment.xml index ccddacb..7dbead5 100644 --- a/pills/03-enter-environment.xml +++ b/pills/03-enter-environment.xml @@ -33,7 +33,8 @@ If that's not the case: - + $ source ~/.nix-profile/etc/profile.d/nix.sh + To remind you, ~/.nix-profile/etc points to the nix-2.1.3 @@ -55,7 +56,12 @@ Back to the installation: - + $ nix-env -i hello +installing 'hello-2.10' +[...] +building '/nix/store/0vqw0ssmh6y5zj48yg34gc6macr883xk-user-environment.drv'... +created 36 symlinks in user environment + Now you can run hello. Things to notice: @@ -92,13 +98,19 @@ We can list generations without walking through the /nix hierarchy: - + $ nix-env --list-generations + 1 2014-07-24 09:23:30 + 2 2014-07-25 08:45:01 (current) + Listing installed derivations: - + $ nix-env -q +nix-2.1.3 +hello-2.10 + So, where did hello really get installed? @@ -128,7 +140,11 @@ a bit: - + $ ls -l ~/.nix-profile/ +dr-xr-xr-x 2 nix nix 4096 Jan 1 1970 bin +lrwxrwxrwx 1 nix nix 55 Jan 1 1970 etc -> /nix/store/ig31y9gfpp8pf3szdd7d4sf29zr7igbr-nix-2.1.3/etc +[...] + Now that's interesting. When only nix-2.1.3 was installed, bin was a @@ -136,7 +152,15 @@ (man, hello), it's a real directory, not a symlink. - + $ ls -l ~/.nix-profile/bin/ +[...] +man -> /nix/store/83cn9ing5sc6644h50dqzzfxcs07r2jn-man-1.6g/bin/man +[...] +nix-env -> /nix/store/ig31y9gfpp8pf3szdd7d4sf29zr7igbr-nix-2.1.3/bin/nix-env +[...] +hello -> /nix/store/58r35bqb4f3lxbnbabq718svq9i2pda3-hello-2.10/bin/hello +[...] + Okay, that's clearer now. nix-env merged the paths from the installed derivations. @@ -155,7 +179,9 @@ old generation: - + $ nix-env --rollback +switching from generation 3 to 2 + Now nix-env -q does not list man anymore. @@ -166,7 +192,9 @@ Enough with the rollback, let's go back to the most recent generation: - + $ nix-env -G 3 +switching from generation 2 to 3 + I invite you to read the manpage of nix-env. nix-env requires an operation @@ -200,7 +228,10 @@ To show the direct runtime dependencies of hello: - + $ nix-store -q --references `which hello` +/nix/store/fg4yq8i8wd08xg3fy58l6q73cjy8hjr2-glibc-2.27 +/nix/store/58r35bqb4f3lxbnbabq718svq9i2pda3-hello-2.10 + The argument to nix-store can be anything as long as it points to the @@ -212,7 +243,13 @@ dependencies of hello: - + $ nix-store -q --referrers `which hello` +/nix/store/58r35bqb4f3lxbnbabq718svq9i2pda3-hello-2.10 +/nix/store/fhvy2550cpmjgcjcx5rzz328i0kfv3z3-env-manifest.nix +/nix/store/yzdk0xvr0b8dcwhi2nns6d75k2ha5208-env-manifest.nix +/nix/store/mp987abm20c70pl8p31ljw1r5by4xwfw-user-environment +/nix/store/ppr3qbq7fk2m2pa49i2z3i32cvfhsv7p-user-environment + Was it what you expected? It turns out that our environments depend upon hello. @@ -241,7 +278,9 @@ including absolutely everything necessary to use that derivation. - + $ nix-store -qR `which man` +[...] + Copying all those derivations to the Nix store of another machine makes @@ -256,7 +295,9 @@ A nicer view of the closure: - + $ nix-store -q --tree `which man` +[...] + With the above command, you can find out exactly why a @@ -286,7 +327,11 @@
Recovering the hard way - + $ nix-env -e '*' +uninstalling 'hello-2.10' +uninstalling 'nix-2.1.3' +[...] + Oops, that uninstalled all derivations from the environment, including @@ -308,13 +353,15 @@ The first option is to rollback: - + $ /nix/store/ig31y9gfpp8pf3szdd7d4sf29zr7igbr-nix-2.1.3/bin/nix-env --rollback + The second option is to install Nix, thus creating a new generation: - + $ /nix/store/ig31y9gfpp8pf3szdd7d4sf29zr7igbr-nix-2.1.3/bin/nix-env -i /nix/store/ig31y9gfpp8pf3szdd7d4sf29zr7igbr-nix-2.1.3/bin/nix-env +
@@ -328,7 +375,9 @@ nix-channel. - + $ nix-channel --list +nixpkgs http://nixos.org/channels/nixpkgs-unstable + If you're using NixOS, you may not see any output from the above command diff --git a/pills/03/channel-list.txt b/pills/03/channel-list.txt deleted file mode 100644 index 477e56c..0000000 --- a/pills/03/channel-list.txt +++ /dev/null @@ -1,2 +0,0 @@ -$ nix-channel --list -nixpkgs http://nixos.org/channels/nixpkgs-unstable diff --git a/pills/03/generation-3.txt b/pills/03/generation-3.txt deleted file mode 100644 index c7baf1d..0000000 --- a/pills/03/generation-3.txt +++ /dev/null @@ -1,2 +0,0 @@ -$ nix-env -G 3 -switching from generation 2 to 3 diff --git a/pills/03/install-hello.txt b/pills/03/install-hello.txt deleted file mode 100644 index 7d707f2..0000000 --- a/pills/03/install-hello.txt +++ /dev/null @@ -1,5 +0,0 @@ -$ nix-env -i hello -installing 'hello-2.10' -[...] -building '/nix/store/0vqw0ssmh6y5zj48yg34gc6macr883xk-user-environment.drv'... -created 36 symlinks in user environment diff --git a/pills/03/install.txt b/pills/03/install.txt deleted file mode 100644 index 09e46d1..0000000 --- a/pills/03/install.txt +++ /dev/null @@ -1 +0,0 @@ -$ /nix/store/ig31y9gfpp8pf3szdd7d4sf29zr7igbr-nix-2.1.3/bin/nix-env -i /nix/store/ig31y9gfpp8pf3szdd7d4sf29zr7igbr-nix-2.1.3/bin/nix-env diff --git a/pills/03/list-generations.txt b/pills/03/list-generations.txt deleted file mode 100644 index d4d8f4d..0000000 --- a/pills/03/list-generations.txt +++ /dev/null @@ -1,3 +0,0 @@ -$ nix-env --list-generations - 1 2014-07-24 09:23:30 - 2 2014-07-25 08:45:01 (current) diff --git a/pills/03/list-installed-derivations.txt b/pills/03/list-installed-derivations.txt deleted file mode 100644 index b485f85..0000000 --- a/pills/03/list-installed-derivations.txt +++ /dev/null @@ -1,3 +0,0 @@ -$ nix-env -q -nix-2.1.3 -hello-2.10 diff --git a/pills/03/ls-nix-profile.txt b/pills/03/ls-nix-profile.txt deleted file mode 100644 index 57d6d96..0000000 --- a/pills/03/ls-nix-profile.txt +++ /dev/null @@ -1,4 +0,0 @@ -$ ls -l ~/.nix-profile/ -dr-xr-xr-x 2 nix nix 4096 Jan 1 1970 bin -lrwxrwxrwx 1 nix nix 55 Jan 1 1970 etc -> /nix/store/ig31y9gfpp8pf3szdd7d4sf29zr7igbr-nix-2.1.3/etc -[...] diff --git a/pills/03/ls-profile-bin.txt b/pills/03/ls-profile-bin.txt deleted file mode 100644 index 4b4fb6a..0000000 --- a/pills/03/ls-profile-bin.txt +++ /dev/null @@ -1,8 +0,0 @@ -$ ls -l ~/.nix-profile/bin/ -[...] -man -> /nix/store/83cn9ing5sc6644h50dqzzfxcs07r2jn-man-1.6g/bin/man -[...] -nix-env -> /nix/store/ig31y9gfpp8pf3szdd7d4sf29zr7igbr-nix-2.1.3/bin/nix-env -[...] -hello -> /nix/store/58r35bqb4f3lxbnbabq718svq9i2pda3-hello-2.10/bin/hello -[...] diff --git a/pills/03/nix-env-rollback.txt b/pills/03/nix-env-rollback.txt deleted file mode 100644 index dab9f06..0000000 --- a/pills/03/nix-env-rollback.txt +++ /dev/null @@ -1 +0,0 @@ -$ /nix/store/ig31y9gfpp8pf3szdd7d4sf29zr7igbr-nix-2.1.3/bin/nix-env --rollback diff --git a/pills/03/nix-store-tree.txt b/pills/03/nix-store-tree.txt deleted file mode 100644 index 31978d1..0000000 --- a/pills/03/nix-store-tree.txt +++ /dev/null @@ -1,2 +0,0 @@ -$ nix-store -q --tree `which man` -[...] diff --git a/pills/03/nix-store.txt b/pills/03/nix-store.txt deleted file mode 100644 index 9eb350f..0000000 --- a/pills/03/nix-store.txt +++ /dev/null @@ -1,2 +0,0 @@ -$ nix-store -qR `which man` -[...] diff --git a/pills/03/references.txt b/pills/03/references.txt deleted file mode 100644 index 08e391e..0000000 --- a/pills/03/references.txt +++ /dev/null @@ -1,3 +0,0 @@ -$ nix-store -q --references `which hello` -/nix/store/fg4yq8i8wd08xg3fy58l6q73cjy8hjr2-glibc-2.27 -/nix/store/58r35bqb4f3lxbnbabq718svq9i2pda3-hello-2.10 diff --git a/pills/03/referrers.txt b/pills/03/referrers.txt deleted file mode 100644 index dfef101..0000000 --- a/pills/03/referrers.txt +++ /dev/null @@ -1,6 +0,0 @@ -$ nix-store -q --referrers `which hello` -/nix/store/58r35bqb4f3lxbnbabq718svq9i2pda3-hello-2.10 -/nix/store/fhvy2550cpmjgcjcx5rzz328i0kfv3z3-env-manifest.nix -/nix/store/yzdk0xvr0b8dcwhi2nns6d75k2ha5208-env-manifest.nix -/nix/store/mp987abm20c70pl8p31ljw1r5by4xwfw-user-environment -/nix/store/ppr3qbq7fk2m2pa49i2z3i32cvfhsv7p-user-environment diff --git a/pills/03/rollback.txt b/pills/03/rollback.txt deleted file mode 100644 index 3c3de9f..0000000 --- a/pills/03/rollback.txt +++ /dev/null @@ -1,2 +0,0 @@ -$ nix-env --rollback -switching from generation 3 to 2 diff --git a/pills/03/source-nix.txt b/pills/03/source-nix.txt deleted file mode 100644 index 906e3f8..0000000 --- a/pills/03/source-nix.txt +++ /dev/null @@ -1 +0,0 @@ -$ source ~/.nix-profile/etc/profile.d/nix.sh diff --git a/pills/03/uninstall-all.txt b/pills/03/uninstall-all.txt deleted file mode 100644 index 823fd33..0000000 --- a/pills/03/uninstall-all.txt +++ /dev/null @@ -1,4 +0,0 @@ -$ nix-env -e '*' -uninstalling 'hello-2.10' -uninstalling 'nix-2.1.3' -[...] diff --git a/pills/04-basics-of-language.xml b/pills/04-basics-of-language.xml index 545b6df..b731863 100644 --- a/pills/04-basics-of-language.xml +++ b/pills/04-basics-of-language.xml @@ -65,13 +65,23 @@ Help is available through the :? command.) - + nix-repl> 1+3 +4 + +nix-repl> 7-4 +3 + +nix-repl> 3*2 +6 + Attempting to perform division in Nix can lead to some surprises. - + nix-repl> 6/3 +/home/nix/6/3 + What happened? Recall that Nix is not a general purpose language, it's a @@ -83,7 +93,12 @@ builtins.div. - + nix-repl> 6/ 3 +2 + +nix-repl> builtins.div 6 3 +2 + Other operators are ||, && and ! @@ -128,7 +143,11 @@ convenient since many packages use dash in their names. In fact: - + nix-repl> a-b +error: undefined variable `a-b' at (string):1:1 +nix-repl> a - b +error: undefined variable `a' at (string):1:1 + As you can see, a-b is parsed as identifier, not as @@ -145,7 +164,11 @@ Strings are enclosed by double quotes ("), or two single quotes (''). - + nix-repl> "foo" +"foo" +nix-repl> ''foo'' +"foo" + In other languages like Python you can also use single quotes for strings (e.g. 'foo'), @@ -159,7 +182,14 @@ not $foo or {$foo} or anything else. - + nix-repl> foo = "strval" +nix-repl> "$foo" +"$foo" +nix-repl> "${foo}" +"strval" +nix-repl> "${2+3}" +error: cannot coerce an integer to a string, at (string):1:2 + Note: ignore the foo = "strval" assignment, special syntax in nix repl. @@ -175,14 +205,22 @@ quotes inside strings without needing to escape them: - + nix-repl> ''test " test'' +"test \" test" +nix-repl> ''${foo}'' +"strval" + Escaping ${...} within double quoted strings is done with the backslash. Within two single quotes, it's done with '': - + nix-repl> "\${foo}" +"${foo}" +nix-repl> ''test ''${foo} test'' +"test ${foo} test" +
@@ -193,7 +231,9 @@ Lists are a sequence of expressions delimited by space (not comma):
- + nix-repl> [ 2 "foo" true (2+3) ] +[ 2 "foo" true 5 ] + Lists, like everything else in Nix, are immutable. Adding or removing @@ -210,7 +250,10 @@ keys. - + nix-repl> s = { foo = "bar"; a-b = "baz"; "123" = "num"; } +nix-repl> s +{ "123" = "num"; a-b = "baz"; foo = "bar"; } + For those reading Nix expressions from nixpkgs: do not confuse attribute sets with @@ -221,7 +264,11 @@ To access elements in the attribute set: - + nix-repl> s.a-b +"baz" +nix-repl> s."123" +"num" + Yes, you can use strings to address keys which aren't valid identifiers. @@ -231,14 +278,18 @@ Inside an attribute set you cannot normally refer to elements of the same attribute set: - + nix-repl> { a = 3; b = a+4; } +error: undefined variable `a' at (string):1:10 + To do so, use recursive attribute sets: - + nix-repl> rec { a = 3; b = a+4; } +{ a = 3; b = 7; } + This is very convenient when defining packages, which tend to be recursive attribute sets. @@ -252,7 +303,11 @@ These are expressions, not statements. - + nix-repl> a = 3 +nix-repl> b = 4 +nix-repl> if a > b then "yes" else "no" +"no" + You can't have only the then branch, you must specify also the else @@ -268,7 +323,9 @@ expressions. - + nix-repl> let a = "foo"; in a +"foo" + The syntax is: first assign variables, then in, then an expression which can @@ -276,33 +333,45 @@ the value of the expression after the in. - + nix-repl> let a = 3; b = 4; in a + b +7 + Let's write two let expressions, one inside the other: - + nix-repl> let a = 3; in let b = 4; in a + b +7 + With let you cannot assign twice to the same variable. However, you can shadow outer variables: - + nix-repl> let a = 3; a = 8; in a +error: attribute `a' at (string):1:12 already defined at (string):1:5 +nix-repl> let a = 3; in let a = 8; in a +8 + You cannot refer to variables in a let expression outside of it: - + nix-repl> let a = (let c = 3; in c); in c +error: undefined variable `c' at (string):1:31 + You can refer to variables in the let expression when assigning variables, like with recursive attribute sets: - + nix-repl> let a = 4; b = a + 5; in b +9 + So beware when you want to refer to a variable from the outer scope, but @@ -321,7 +390,12 @@ per-expression when to include symbols into the scope. - + nix-repl> longName = { a = 3; b = 4; } +nix-repl> longName.a + longName.b +7 +nix-repl> with longName; a + b +7 + That's it, it takes an attribute set and includes symbols from it in the scope of the inner @@ -331,7 +405,11 @@ You can however still refer to the attribute set: - + nix-repl> let a = 10; in with longName; a + b +14 +nix-repl> let a = 10; in with longName; longName.a + b +7 +
@@ -342,7 +420,9 @@ working with packages. - + nix-repl> let a = builtins.div 4 0; b = 6; in b +6 + Since a is not needed, there's no error about division by zero, because diff --git a/pills/04/basics.txt b/pills/04/basics.txt deleted file mode 100644 index 8cd4664..0000000 --- a/pills/04/basics.txt +++ /dev/null @@ -1,8 +0,0 @@ -nix-repl> 1+3 -4 - -nix-repl> 7-4 -3 - -nix-repl> 3*2 -6 diff --git a/pills/04/dash.txt b/pills/04/dash.txt deleted file mode 100644 index c018aaa..0000000 --- a/pills/04/dash.txt +++ /dev/null @@ -1,4 +0,0 @@ -nix-repl> a-b -error: undefined variable `a-b' at (string):1:1 -nix-repl> a - b -error: undefined variable `a' at (string):1:1 diff --git a/pills/04/division.txt b/pills/04/division.txt deleted file mode 100644 index 36e768b..0000000 --- a/pills/04/division.txt +++ /dev/null @@ -1,5 +0,0 @@ -nix-repl> 6/ 3 -2 - -nix-repl> builtins.div 6 3 -2 diff --git a/pills/04/double-quotes.txt b/pills/04/double-quotes.txt deleted file mode 100644 index aed382b..0000000 --- a/pills/04/double-quotes.txt +++ /dev/null @@ -1,4 +0,0 @@ -nix-repl> ''test " test'' -"test \" test" -nix-repl> ''${foo}'' -"strval" diff --git a/pills/04/escaping.txt b/pills/04/escaping.txt deleted file mode 100644 index 041a502..0000000 --- a/pills/04/escaping.txt +++ /dev/null @@ -1,4 +0,0 @@ -nix-repl> "\${foo}" -"${foo}" -nix-repl> ''test ''${foo} test'' -"test ${foo} test" diff --git a/pills/04/if.txt b/pills/04/if.txt deleted file mode 100644 index b496bc1..0000000 --- a/pills/04/if.txt +++ /dev/null @@ -1,4 +0,0 @@ -nix-repl> a = 3 -nix-repl> b = 4 -nix-repl> if a > b then "yes" else "no" -"no" diff --git a/pills/04/interpolate.txt b/pills/04/interpolate.txt deleted file mode 100644 index ef3b069..0000000 --- a/pills/04/interpolate.txt +++ /dev/null @@ -1,7 +0,0 @@ -nix-repl> foo = "strval" -nix-repl> "$foo" -"$foo" -nix-repl> "${foo}" -"strval" -nix-repl> "${2+3}" -error: cannot coerce an integer to a string, at (string):1:2 diff --git a/pills/04/lazy.txt b/pills/04/lazy.txt deleted file mode 100644 index f659943..0000000 --- a/pills/04/lazy.txt +++ /dev/null @@ -1,2 +0,0 @@ -nix-repl> let a = builtins.div 4 0; b = 6; in b -6 diff --git a/pills/04/let-basic.txt b/pills/04/let-basic.txt deleted file mode 100644 index 1b5a42e..0000000 --- a/pills/04/let-basic.txt +++ /dev/null @@ -1,2 +0,0 @@ -nix-repl> let a = "foo"; in a -"foo" diff --git a/pills/04/let-multiple-assign.txt b/pills/04/let-multiple-assign.txt deleted file mode 100644 index dc88d30..0000000 --- a/pills/04/let-multiple-assign.txt +++ /dev/null @@ -1,4 +0,0 @@ -nix-repl> let a = 3; a = 8; in a -error: attribute `a' at (string):1:12 already defined at (string):1:5 -nix-repl> let a = 3; in let a = 8; in a -8 diff --git a/pills/04/let-multiple.txt b/pills/04/let-multiple.txt deleted file mode 100644 index 5d94010..0000000 --- a/pills/04/let-multiple.txt +++ /dev/null @@ -1,2 +0,0 @@ -nix-repl> let a = 3; b = 4; in a + b -7 diff --git a/pills/04/let-nested.txt b/pills/04/let-nested.txt deleted file mode 100644 index 8acd36f..0000000 --- a/pills/04/let-nested.txt +++ /dev/null @@ -1,2 +0,0 @@ -nix-repl> let a = 3; in let b = 4; in a + b -7 diff --git a/pills/04/let-reference.txt b/pills/04/let-reference.txt deleted file mode 100644 index f947d7a..0000000 --- a/pills/04/let-reference.txt +++ /dev/null @@ -1,2 +0,0 @@ -nix-repl> let a = 4; b = a + 5; in b -9 diff --git a/pills/04/let-scope.txt b/pills/04/let-scope.txt deleted file mode 100644 index 669a98c..0000000 --- a/pills/04/let-scope.txt +++ /dev/null @@ -1,2 +0,0 @@ -nix-repl> let a = (let c = 3; in c); in c -error: undefined variable `c' at (string):1:31 diff --git a/pills/04/lists.txt b/pills/04/lists.txt deleted file mode 100644 index 70f3daf..0000000 --- a/pills/04/lists.txt +++ /dev/null @@ -1,2 +0,0 @@ -nix-repl> [ 2 "foo" true (2+3) ] -[ 2 "foo" true 5 ] diff --git a/pills/04/relative-path.txt b/pills/04/relative-path.txt deleted file mode 100644 index 7ff07b2..0000000 --- a/pills/04/relative-path.txt +++ /dev/null @@ -1,2 +0,0 @@ -nix-repl> 6/3 -/home/nix/6/3 diff --git a/pills/04/set-access.txt b/pills/04/set-access.txt deleted file mode 100644 index 50dcb65..0000000 --- a/pills/04/set-access.txt +++ /dev/null @@ -1,4 +0,0 @@ -nix-repl> s.a-b -"baz" -nix-repl> s."123" -"num" diff --git a/pills/04/set-basics.txt b/pills/04/set-basics.txt deleted file mode 100644 index ce7fe28..0000000 --- a/pills/04/set-basics.txt +++ /dev/null @@ -1,3 +0,0 @@ -nix-repl> s = { foo = "bar"; a-b = "baz"; "123" = "num"; } -nix-repl> s -{ "123" = "num"; a-b = "baz"; foo = "bar"; } diff --git a/pills/04/set-failed.txt b/pills/04/set-failed.txt deleted file mode 100644 index 4503bf4..0000000 --- a/pills/04/set-failed.txt +++ /dev/null @@ -1,2 +0,0 @@ -nix-repl> { a = 3; b = a+4; } -error: undefined variable `a' at (string):1:10 diff --git a/pills/04/set-recursive.txt b/pills/04/set-recursive.txt deleted file mode 100644 index 5dbc393..0000000 --- a/pills/04/set-recursive.txt +++ /dev/null @@ -1,2 +0,0 @@ -nix-repl> rec { a = 3; b = a+4; } -{ a = 3; b = 7; } diff --git a/pills/04/strings-basic.txt b/pills/04/strings-basic.txt deleted file mode 100644 index 05e8b75..0000000 --- a/pills/04/strings-basic.txt +++ /dev/null @@ -1,4 +0,0 @@ -nix-repl> "foo" -"foo" -nix-repl> ''foo'' -"foo" diff --git a/pills/04/with-basic.txt b/pills/04/with-basic.txt deleted file mode 100644 index 3d0ac96..0000000 --- a/pills/04/with-basic.txt +++ /dev/null @@ -1,5 +0,0 @@ -nix-repl> longName = { a = 3; b = 4; } -nix-repl> longName.a + longName.b -7 -nix-repl> with longName; a + b -7 diff --git a/pills/04/with-scope.txt b/pills/04/with-scope.txt deleted file mode 100644 index d90f07e..0000000 --- a/pills/04/with-scope.txt +++ /dev/null @@ -1,4 +0,0 @@ -nix-repl> let a = 10; in with longName; a + b -14 -nix-repl> let a = 10; in with longName; longName.a + b -7 diff --git a/pills/05-functions-and-imports.xml b/pills/05-functions-and-imports.xml index 3a5639c..04a29e1 100644 --- a/pills/05-functions-and-imports.xml +++ b/pills/05-functions-and-imports.xml @@ -37,7 +37,8 @@ xml:id="functions-and-imports"> then the body of the function. - + nix-repl> x: x*2 +«lambda» So here we defined a function that takes a parameter @@ -49,7 +50,11 @@ xml:id="functions-and-imports"> We can store functions in variables. - + nix-repl> double = x: x*2 +nix-repl> double +«lambda» +nix-repl> double 3 +6 As usual, please ignore the special syntax for assignments inside nix repl. @@ -81,8 +86,13 @@ xml:id="functions-and-imports"> it step by step. - + nix-repl> mul = a: (b: a*b) +nix-repl> mul +«lambda» +nix-repl> mul 3 +«lambda» +nix-repl> (mul 3) 4 +12 We defined a function that takes the parameter a, the body @@ -97,7 +107,15 @@ xml:id="functions-and-imports"> parsing the code: - + nix-repl> mul = a: b: a*b +nix-repl> mul +«lambda» +nix-repl> mul 3 +«lambda» +nix-repl> mul 3 4 +12 +nix-repl> mul (6+7) (8+9) +221 Much more readable, you don't even notice that functions only receive one @@ -111,8 +129,11 @@ xml:id="functions-and-imports"> partial application: - + nix-repl> foo = mul 3 +nix-repl> foo 4 +12 +nix-repl> foo 5 +15 We stored the function returned by mul 3 into a variable foo, @@ -129,7 +150,12 @@ xml:id="functions-and-imports"> a*b first by using a set as argument, then using pattern matching. - + nix-repl> mul = s: s.a*s.b +nix-repl> mul { a = 3; b = 4; } +12 +nix-repl> mul = { a, b }: a*b +nix-repl> mul { a = 3; b = 4; } +12 In the first case we defined a function that accepts a single parameter. We @@ -146,8 +172,11 @@ xml:id="functions-and-imports"> b in the function body directly. - + nix-repl> mul = { a, b }: a*b +nix-repl> mul { a = 3; b = 4; c = 6; } +error: anonymous function at (string):1:2 called with unexpected argument `c', at (string):1:1 +nix-repl> mul { a = 3; } +error: anonymous function at (string):1:2 called without required argument `b', at (string):1:1 Only a set with exactly the attributes required by the function is accepted, @@ -163,15 +192,19 @@ xml:id="functions-and-imports"> of attributes in the argument set: - + nix-repl> mul = { a, b ? 2 }: a*b +nix-repl> mul { a = 3; } +6 +nix-repl> mul { a = 3; b = 4; } +12 Also you can allow passing more attributes (variadic) than the expected ones: - + nix-repl> mul = { a, b, ... }: a*b +nix-repl> mul { a = 3; b = 4; c = 2; } However, in the function body you cannot access the "c" attribute. The @@ -179,8 +212,9 @@ xml:id="functions-and-imports"> role="strong">@-pattern: - + nix-repl> mul = s@{ a, b, ... }: a*b*s.c +nix-repl> mul { a = 3; b = 4; c = 2; } +24 That's it, you give a name to the whole parameter with name@ before the set @@ -244,24 +278,25 @@ xml:id="functions-and-imports"> a.nix: - + 3 b.nix: - + 4 mul.nix: - + a: b: a*b - + nix-repl> a = import ./a.nix +nix-repl> b = import ./b.nix +nix-repl> mul = import ./mul.nix +nix-repl> mul a b +12 Yes it's really that simple. You import a file, and it gets parsed as an @@ -273,10 +308,10 @@ xml:id="functions-and-imports"> test.nix: - + x - + nix-repl> let x = 5; in import ./test.nix +error: undefined variable `x' at /home/lethal/test.nix:1:1 So how do we pass information to the module? Use functions, like we did with @@ -287,10 +322,16 @@ xml:id="functions-and-imports"> test.nix: - + { a, b ? 3, trueMsg ? "yes", falseMsg ? "no" }: +if a > b + then builtins.trace trueMsg true + else builtins.trace falseMsg false + - + nix-repl> import ./test.nix { a = 5; trueMsg = "ok"; } +trace: ok +true + diff --git a/pills/05/a-nix.txt b/pills/05/a-nix.txt deleted file mode 100644 index e440e5c..0000000 --- a/pills/05/a-nix.txt +++ /dev/null @@ -1 +0,0 @@ -3 \ No newline at end of file diff --git a/pills/05/anon-function.txt b/pills/05/anon-function.txt deleted file mode 100644 index 21cadb5..0000000 --- a/pills/05/anon-function.txt +++ /dev/null @@ -1,2 +0,0 @@ -nix-repl> x: x*2 -«lambda» \ No newline at end of file diff --git a/pills/05/argument-set-error.txt b/pills/05/argument-set-error.txt deleted file mode 100644 index a5a4802..0000000 --- a/pills/05/argument-set-error.txt +++ /dev/null @@ -1,5 +0,0 @@ -nix-repl> mul = { a, b }: a*b -nix-repl> mul { a = 3; b = 4; c = 6; } -error: anonymous function at (string):1:2 called with unexpected argument `c', at (string):1:1 -nix-repl> mul { a = 3; } -error: anonymous function at (string):1:2 called without required argument `b', at (string):1:1 \ No newline at end of file diff --git a/pills/05/b-nix.txt b/pills/05/b-nix.txt deleted file mode 100644 index bf0d87a..0000000 --- a/pills/05/b-nix.txt +++ /dev/null @@ -1 +0,0 @@ -4 \ No newline at end of file diff --git a/pills/05/default-values.txt b/pills/05/default-values.txt deleted file mode 100644 index 6613ddd..0000000 --- a/pills/05/default-values.txt +++ /dev/null @@ -1,5 +0,0 @@ -nix-repl> mul = { a, b ? 2 }: a*b -nix-repl> mul { a = 3; } -6 -nix-repl> mul { a = 3; b = 4; } -12 \ No newline at end of file diff --git a/pills/05/import.txt b/pills/05/import.txt deleted file mode 100644 index 1defa99..0000000 --- a/pills/05/import.txt +++ /dev/null @@ -1,5 +0,0 @@ -nix-repl> a = import ./a.nix -nix-repl> b = import ./b.nix -nix-repl> mul = import ./mul.nix -nix-repl> mul a b -12 \ No newline at end of file diff --git a/pills/05/mul-nix.txt b/pills/05/mul-nix.txt deleted file mode 100644 index 5c42a1b..0000000 --- a/pills/05/mul-nix.txt +++ /dev/null @@ -1 +0,0 @@ -a: b: a*b \ No newline at end of file diff --git a/pills/05/multi-argument-function.txt b/pills/05/multi-argument-function.txt deleted file mode 100644 index 8bd584d..0000000 --- a/pills/05/multi-argument-function.txt +++ /dev/null @@ -1,7 +0,0 @@ -nix-repl> mul = a: (b: a*b) -nix-repl> mul -«lambda» -nix-repl> mul 3 -«lambda» -nix-repl> (mul 3) 4 -12 \ No newline at end of file diff --git a/pills/05/named-function.txt b/pills/05/named-function.txt deleted file mode 100644 index 3d61774..0000000 --- a/pills/05/named-function.txt +++ /dev/null @@ -1,5 +0,0 @@ -nix-repl> double = x: x*2 -nix-repl> double -«lambda» -nix-repl> double 3 -6 \ No newline at end of file diff --git a/pills/05/named-set-argument.txt b/pills/05/named-set-argument.txt deleted file mode 100644 index 1ed840b..0000000 --- a/pills/05/named-set-argument.txt +++ /dev/null @@ -1,3 +0,0 @@ -nix-repl> mul = s@{ a, b, ... }: a*b*s.c -nix-repl> mul { a = 3; b = 4; c = 2; } -24 \ No newline at end of file diff --git a/pills/05/no-parenthesis.txt b/pills/05/no-parenthesis.txt deleted file mode 100644 index 36d12ff..0000000 --- a/pills/05/no-parenthesis.txt +++ /dev/null @@ -1,9 +0,0 @@ -nix-repl> mul = a: b: a*b -nix-repl> mul -«lambda» -nix-repl> mul 3 -«lambda» -nix-repl> mul 3 4 -12 -nix-repl> mul (6+7) (8+9) -221 \ No newline at end of file diff --git a/pills/05/partial-application.txt b/pills/05/partial-application.txt deleted file mode 100644 index eb925b0..0000000 --- a/pills/05/partial-application.txt +++ /dev/null @@ -1,5 +0,0 @@ -nix-repl> foo = mul 3 -nix-repl> foo 4 -12 -nix-repl> foo 5 -15 \ No newline at end of file diff --git a/pills/05/set-argument.txt b/pills/05/set-argument.txt deleted file mode 100644 index 792b6f5..0000000 --- a/pills/05/set-argument.txt +++ /dev/null @@ -1,6 +0,0 @@ -nix-repl> mul = s: s.a*s.b -nix-repl> mul { a = 3; b = 4; } -12 -nix-repl> mul = { a, b }: a*b -nix-repl> mul { a = 3; b = 4; } -12 \ No newline at end of file diff --git a/pills/05/test-import-2.txt b/pills/05/test-import-2.txt deleted file mode 100644 index b5fadc4..0000000 --- a/pills/05/test-import-2.txt +++ /dev/null @@ -1,3 +0,0 @@ -nix-repl> import ./test.nix { a = 5; trueMsg = "ok"; } -trace: ok -true diff --git a/pills/05/test-import.txt b/pills/05/test-import.txt deleted file mode 100644 index 02b4bd7..0000000 --- a/pills/05/test-import.txt +++ /dev/null @@ -1,2 +0,0 @@ -nix-repl> let x = 5; in import ./test.nix -error: undefined variable `x' at /home/lethal/test.nix:1:1 \ No newline at end of file diff --git a/pills/05/test-nix-2.txt b/pills/05/test-nix-2.txt deleted file mode 100644 index 4738f7a..0000000 --- a/pills/05/test-nix-2.txt +++ /dev/null @@ -1,4 +0,0 @@ -{ a, b ? 3, trueMsg ? "yes", falseMsg ? "no" }: -if a > b - then builtins.trace trueMsg true - else builtins.trace falseMsg false diff --git a/pills/05/test-nix.txt b/pills/05/test-nix.txt deleted file mode 100644 index c1b0730..0000000 --- a/pills/05/test-nix.txt +++ /dev/null @@ -1 +0,0 @@ -x \ No newline at end of file diff --git a/pills/05/variadic-arguments.txt b/pills/05/variadic-arguments.txt deleted file mode 100644 index 05fdfec..0000000 --- a/pills/05/variadic-arguments.txt +++ /dev/null @@ -1,2 +0,0 @@ -nix-repl> mul = { a, b, ... }: a*b -nix-repl> mul { a = 3; b = 4; c = 2; } \ No newline at end of file diff --git a/pills/06-our-first-derivation.xml b/pills/06-our-first-derivation.xml index cad3f14..ccae65d 100644 --- a/pills/06-our-first-derivation.xml +++ b/pills/06-our-first-derivation.xml @@ -70,13 +70,16 @@ First of all, what's the name of our system as seen by nix? - + nix-repl> builtins.currentSystem +"x86_64-linux" Let's try to fake the name of the system: - + nix-repl> d = derivation { name = "myname"; builder = "mybuilder"; system = "mysystem"; } +nix-repl> d +«derivation /nix/store/z3hhlxbckx4g3n9sw91nnvlkjvyw754p-myname.drv» Oh oh, what's that? Did it build the derivation? No it didn't, but it @@ -131,7 +134,28 @@ If your version of nix doesn't have nix derivation show, use nix show-derivation instead. - + <screen xmlns="http://docbook.org/ns/docbook"><prompt>$ </prompt><userinput>nix derivation show /nix/store/z3hhlxbckx4g3n9sw91nnvlkjvyw754p-<emphasis>myname.drv</emphasis></userinput> +<computeroutput>{ + "/nix/store/z3hhlxbckx4g3n9sw91nnvlkjvyw754p-myname.drv": { + "outputs": { + "out": { + "path": "/nix/store/40s0qmrfb45vlh6610rk29ym318dswdr-myname" + } + }, + "inputSrcs": [], + "inputDrvs": {}, + "platform": "mysystem", + "builder": "mybuilder", + "args": [], + "env": { + "builder": "mybuilder", + "name": "myname", + "out": "/nix/store/40s0qmrfb45vlh6610rk29ym318dswdr-myname", + "system": "mysystem" + } + } +}</computeroutput></screen> + Ok, we can see there's an out path, but it does not exist yet. We never told @@ -207,7 +231,13 @@ Let's build our really fake derivation: - + nix-repl> d = derivation { name = "myname"; builder = "mybuilder"; system = "mysystem"; } +nix-repl> :b d +[...] +these derivations will be built: + /nix/store/z3hhlxbckx4g3n9sw91nnvlkjvyw754p-myname.drv +building path(s) `/nix/store/40s0qmrfb45vlh6610rk29ym318dswdr-myname' +error: a `mysystem' is required to build `/nix/store/z3hhlxbckx4g3n9sw91nnvlkjvyw754p-myname.drv', but I am a `x86_64-linux' The :b is a nix repl specific command to build a derivation. @@ -224,8 +254,7 @@ .drv with: - + $ nix-store -r /nix/store/z3hhlxbckx4g3n9sw91nnvlkjvyw754p-myname.drv You will get the same output as before. @@ -235,7 +264,10 @@ Let's fix the system attribute: - + nix-repl> d = derivation { name = "myname"; builder = "mybuilder"; system = builtins.currentSystem; } +nix-repl> :b d +[...] +build error: invalid file name `mybuilder' A step forward: of course, that mybuilder executable does not @@ -252,7 +284,11 @@ In this case, the returned value is a plain set: - + nix-repl> d = derivation { name = "myname"; builder = "mybuilder"; system = "mysystem"; } +nix-repl> builtins.isAttrs d +true +nix-repl> builtins.attrNames d +[ "all" "builder" "drvAttrs" "drvPath" "name" "out" "outPath" "outputName" "system" "type" ] You can guess what builtins.isAttrs does; it returns true if @@ -264,7 +300,8 @@ Start from drvAttrs: - + nix-repl> d.drvAttrs +{ builder = "mybuilder"; name = "myname"; system = "mysystem"; } That's basically the input we gave to the derivation function. Also the @@ -272,7 +309,8 @@ attributes are exactly the ones we gave as input. - + nix-repl> (d == d.out) +true So out is just the derivation itself, it seems weird but the reason is that @@ -293,7 +331,8 @@ yourself a set with that type, it's a simple set: - + nix-repl> { type = "derivation"; } +«derivation ???» Of course it has no other information, so Nix doesn't know what to say :-) @@ -325,7 +364,10 @@ Nix is able to do a conversion from a derivation set to a string. - + nix-repl> d.outPath +"/nix/store/40s0qmrfb45vlh6610rk29ym318dswdr-myname" +nix-repl> builtins.toString d +"/nix/store/40s0qmrfb45vlh6610rk29ym318dswdr-myname" Nix does the "set to string conversion" as long as there is the @@ -333,13 +375,21 @@ languages): - + nix-repl> builtins.toString { outPath = "foo"; } +"foo" +nix-repl> builtins.toString { a = "b"; } +error: cannot coerce a set to a string, at (string):1:1 Say we want to use binaries from coreutils (ignore the nixpkgs etc.): - + nix-repl> :l <nixpkgs> +Added 3950 variables. +nix-repl> coreutils +«derivation /nix/store/1zcs1y4n27lqs0gw4v038i303pb89rw6-coreutils-8.21.drv» +nix-repl> builtins.toString coreutils +"/nix/store/8w4cbiy7wqvaqsnsnb3zvabq1cp2zhyz-coreutils-8.21" Apart from the nixpkgs stuff, just think we added to the scope a series of @@ -350,7 +400,8 @@ - + $ ls /nix/store/*coreutils*/bin +[...] I remind you, inside strings it's possible to interpolate Nix expressions @@ -358,7 +409,10 @@ - + nix-repl> "${d}" +"/nix/store/40s0qmrfb45vlh6610rk29ym318dswdr-myname" +nix-repl> "${coreutils}" +"/nix/store/8w4cbiy7wqvaqsnsnb3zvabq1cp2zhyz-coreutils-8.21" That's very convenient, because then we could refer to e.g. the bin/true @@ -366,7 +420,8 @@ - + nix-repl> "${coreutils}/bin/true" +"/nix/store/8w4cbiy7wqvaqsnsnb3zvabq1cp2zhyz-coreutils-8.21/bin/true"
@@ -378,7 +433,12 @@ exits with 0 (success). - + nix-repl> :l <nixpkgs> +nix-repl> d = derivation { name = "myname"; builder = "${coreutils}/bin/true"; system = builtins.currentSystem; } +nix-repl> :b d +[...] +builder for `/nix/store/qyfrcd53wmc0v22ymhhd5r6sz5xmdc8a-myname.drv' failed to produce output path `/nix/store/ly2k1vswbfmswr33hw0kf0ccilrpisnk-myname' + Another step forward, it executed the builder (bin/true), but the builder @@ -396,7 +456,32 @@ another derivation: - + <screen xmlns="http://docbook.org/ns/docbook"><prompt>$ </prompt><userinput>nix derivation show /nix/store/qyfrcd53wmc0v22ymhhd5r6sz5xmdc8a-<emphasis>myname.drv</emphasis></userinput> +<computeroutput>{ + "/nix/store/qyfrcd53wmc0v22ymhhd5r6sz5xmdc8a-myname.drv": { + "outputs": { + "out": { + "path": "/nix/store/ly2k1vswbfmswr33hw0kf0ccilrpisnk-myname" + } + }, + "inputSrcs": [], + "inputDrvs": { + "/nix/store/hixdnzz2wp75x1jy65cysq06yl74vx7q-coreutils-8.29.drv": [ + "out" + ] + }, + "platform": "x86_64-linux", + "builder": "/nix/store/qrxs7sabhqcr3j9ai0j0cp58zfnny0jz-coreutils-8.29/bin/true", + "args": [], + "env": { + "builder": "/nix/store/qrxs7sabhqcr3j9ai0j0cp58zfnny0jz-coreutils-8.29/bin/true", + "name": "myname", + "out": "/nix/store/ly2k1vswbfmswr33hw0kf0ccilrpisnk-myname", + "system": "x86_64-linux" + } + } +}</computeroutput></screen> + Aha! Nix added a dependency to our myname.drv, it's the coreutils.drv. diff --git a/pills/06/build-derivation.txt b/pills/06/build-derivation.txt deleted file mode 100644 index f6c9c9c..0000000 --- a/pills/06/build-derivation.txt +++ /dev/null @@ -1,7 +0,0 @@ -nix-repl> d = derivation { name = "myname"; builder = "mybuilder"; system = "mysystem"; } -nix-repl> :b d -[...] -these derivations will be built: - /nix/store/z3hhlxbckx4g3n9sw91nnvlkjvyw754p-myname.drv -building path(s) `/nix/store/40s0qmrfb45vlh6610rk29ym318dswdr-myname' -error: a `mysystem' is required to build `/nix/store/z3hhlxbckx4g3n9sw91nnvlkjvyw754p-myname.drv', but I am a `x86_64-linux' \ No newline at end of file diff --git a/pills/06/check-drvattrs.txt b/pills/06/check-drvattrs.txt deleted file mode 100644 index cbc746d..0000000 --- a/pills/06/check-drvattrs.txt +++ /dev/null @@ -1,2 +0,0 @@ -nix-repl> (d == d.out) -true \ No newline at end of file diff --git a/pills/06/coreutils.txt b/pills/06/coreutils.txt deleted file mode 100644 index 50990a5..0000000 --- a/pills/06/coreutils.txt +++ /dev/null @@ -1,6 +0,0 @@ -nix-repl> :l -Added 3950 variables. -nix-repl> coreutils -«derivation /nix/store/1zcs1y4n27lqs0gw4v038i303pb89rw6-coreutils-8.21.drv» -nix-repl> builtins.toString coreutils -"/nix/store/8w4cbiy7wqvaqsnsnb3zvabq1cp2zhyz-coreutils-8.21" \ No newline at end of file diff --git a/pills/06/current-system.txt b/pills/06/current-system.txt deleted file mode 100644 index 18ebe18..0000000 --- a/pills/06/current-system.txt +++ /dev/null @@ -1,2 +0,0 @@ -nix-repl> builtins.currentSystem -"x86_64-linux" \ No newline at end of file diff --git a/pills/06/drvattrs.txt b/pills/06/drvattrs.txt deleted file mode 100644 index b9fb564..0000000 --- a/pills/06/drvattrs.txt +++ /dev/null @@ -1,2 +0,0 @@ -nix-repl> d.drvAttrs -{ builder = "mybuilder"; name = "myname"; system = "mysystem"; } \ No newline at end of file diff --git a/pills/06/examine-build.xml b/pills/06/examine-build.xml deleted file mode 100644 index 3d88ea0..0000000 --- a/pills/06/examine-build.xml +++ /dev/null @@ -1,25 +0,0 @@ -$ nix derivation show /nix/store/qyfrcd53wmc0v22ymhhd5r6sz5xmdc8a-myname.drv -{ - "/nix/store/qyfrcd53wmc0v22ymhhd5r6sz5xmdc8a-myname.drv": { - "outputs": { - "out": { - "path": "/nix/store/ly2k1vswbfmswr33hw0kf0ccilrpisnk-myname" - } - }, - "inputSrcs": [], - "inputDrvs": { - "/nix/store/hixdnzz2wp75x1jy65cysq06yl74vx7q-coreutils-8.29.drv": [ - "out" - ] - }, - "platform": "x86_64-linux", - "builder": "/nix/store/qrxs7sabhqcr3j9ai0j0cp58zfnny0jz-coreutils-8.29/bin/true", - "args": [], - "env": { - "builder": "/nix/store/qrxs7sabhqcr3j9ai0j0cp58zfnny0jz-coreutils-8.29/bin/true", - "name": "myname", - "out": "/nix/store/ly2k1vswbfmswr33hw0kf0ccilrpisnk-myname", - "system": "x86_64-linux" - } - } -} diff --git a/pills/06/fake-system.txt b/pills/06/fake-system.txt deleted file mode 100644 index 66b8c07..0000000 --- a/pills/06/fake-system.txt +++ /dev/null @@ -1,3 +0,0 @@ -nix-repl> d = derivation { name = "myname"; builder = "mybuilder"; system = "mysystem"; } -nix-repl> d -«derivation /nix/store/z3hhlxbckx4g3n9sw91nnvlkjvyw754p-myname.drv» \ No newline at end of file diff --git a/pills/06/fix-attribute.txt b/pills/06/fix-attribute.txt deleted file mode 100644 index 2e37796..0000000 --- a/pills/06/fix-attribute.txt +++ /dev/null @@ -1,4 +0,0 @@ -nix-repl> d = derivation { name = "myname"; builder = "mybuilder"; system = builtins.currentSystem; } -nix-repl> :b d -[...] -build error: invalid file name `mybuilder' \ No newline at end of file diff --git a/pills/06/inspect-values.txt b/pills/06/inspect-values.txt deleted file mode 100644 index 167c6ab..0000000 --- a/pills/06/inspect-values.txt +++ /dev/null @@ -1,5 +0,0 @@ -nix-repl> d = derivation { name = "myname"; builder = "mybuilder"; system = "mysystem"; } -nix-repl> builtins.isAttrs d -true -nix-repl> builtins.attrNames d -[ "all" "builder" "drvAttrs" "drvPath" "name" "out" "outPath" "outputName" "system" "type" ] \ No newline at end of file diff --git a/pills/06/interpolate.txt b/pills/06/interpolate.txt deleted file mode 100644 index 1925d00..0000000 --- a/pills/06/interpolate.txt +++ /dev/null @@ -1,4 +0,0 @@ -nix-repl> "${d}" -"/nix/store/40s0qmrfb45vlh6610rk29ym318dswdr-myname" -nix-repl> "${coreutils}" -"/nix/store/8w4cbiy7wqvaqsnsnb3zvabq1cp2zhyz-coreutils-8.21" \ No newline at end of file diff --git a/pills/06/list-coreutils.txt b/pills/06/list-coreutils.txt deleted file mode 100644 index b8123c9..0000000 --- a/pills/06/list-coreutils.txt +++ /dev/null @@ -1,2 +0,0 @@ -$ ls /nix/store/*coreutils*/bin -[...] \ No newline at end of file diff --git a/pills/06/outpath.txt b/pills/06/outpath.txt deleted file mode 100644 index f5d8b86..0000000 --- a/pills/06/outpath.txt +++ /dev/null @@ -1,4 +0,0 @@ -nix-repl> d.outPath -"/nix/store/40s0qmrfb45vlh6610rk29ym318dswdr-myname" -nix-repl> builtins.toString d -"/nix/store/40s0qmrfb45vlh6610rk29ym318dswdr-myname" \ No newline at end of file diff --git a/pills/06/realise-derivation.txt b/pills/06/realise-derivation.txt deleted file mode 100644 index 55582e8..0000000 --- a/pills/06/realise-derivation.txt +++ /dev/null @@ -1 +0,0 @@ -$ nix-store -r /nix/store/z3hhlxbckx4g3n9sw91nnvlkjvyw754p-myname.drv \ No newline at end of file diff --git a/pills/06/reference.txt b/pills/06/reference.txt deleted file mode 100644 index 20df787..0000000 --- a/pills/06/reference.txt +++ /dev/null @@ -1,2 +0,0 @@ -nix-repl> "${coreutils}/bin/true" -"/nix/store/8w4cbiy7wqvaqsnsnb3zvabq1cp2zhyz-coreutils-8.21/bin/true" \ No newline at end of file diff --git a/pills/06/show-derivation.xml b/pills/06/show-derivation.xml deleted file mode 100644 index 31245a8..0000000 --- a/pills/06/show-derivation.xml +++ /dev/null @@ -1,21 +0,0 @@ -$ nix derivation show /nix/store/z3hhlxbckx4g3n9sw91nnvlkjvyw754p-myname.drv -{ - "/nix/store/z3hhlxbckx4g3n9sw91nnvlkjvyw754p-myname.drv": { - "outputs": { - "out": { - "path": "/nix/store/40s0qmrfb45vlh6610rk29ym318dswdr-myname" - } - }, - "inputSrcs": [], - "inputDrvs": {}, - "platform": "mysystem", - "builder": "mybuilder", - "args": [], - "env": { - "builder": "mybuilder", - "name": "myname", - "out": "/nix/store/40s0qmrfb45vlh6610rk29ym318dswdr-myname", - "system": "mysystem" - } - } -} diff --git a/pills/06/test-build.txt b/pills/06/test-build.txt deleted file mode 100644 index 0468ce0..0000000 --- a/pills/06/test-build.txt +++ /dev/null @@ -1,5 +0,0 @@ -nix-repl> :l -nix-repl> d = derivation { name = "myname"; builder = "${coreutils}/bin/true"; system = builtins.currentSystem; } -nix-repl> :b d -[...] -builder for `/nix/store/qyfrcd53wmc0v22ymhhd5r6sz5xmdc8a-myname.drv' failed to produce output path `/nix/store/ly2k1vswbfmswr33hw0kf0ccilrpisnk-myname' diff --git a/pills/06/tostring.txt b/pills/06/tostring.txt deleted file mode 100644 index 6f45ee8..0000000 --- a/pills/06/tostring.txt +++ /dev/null @@ -1,4 +0,0 @@ -nix-repl> builtins.toString { outPath = "foo"; } -"foo" -nix-repl> builtins.toString { a = "b"; } -error: cannot coerce a set to a string, at (string):1:1 \ No newline at end of file diff --git a/pills/06/type-derivation.txt b/pills/06/type-derivation.txt deleted file mode 100644 index 17b05a4..0000000 --- a/pills/06/type-derivation.txt +++ /dev/null @@ -1,2 +0,0 @@ -nix-repl> { type = "derivation"; } -«derivation ???» \ No newline at end of file diff --git a/pills/07-working-derivation.xml b/pills/07-working-derivation.xml index 17d0dac..b3efde8 100644 --- a/pills/07-working-derivation.xml +++ b/pills/07-working-derivation.xml @@ -63,7 +63,9 @@ First of all, let's write our builder.sh in the current directory: - + declare -xp +echo foo > $out + The command declare -xp lists exported variables @@ -91,12 +93,23 @@ Like for coreutils in the previous pill, we get a blessed bash for free from our magic nixpkgs stuff: - + <screen xmlns="http://docbook.org/ns/docbook"><prompt>nix-repl> </prompt><userinput>:l &lt;nixpkgs></userinput> +<computeroutput>Added 3950 variables.</computeroutput> +<prompt>nix-repl> </prompt><userinput>"${bash}"</userinput> +<computeroutput>"/nix/store/ihmkc7z2wqk3bbipfnlh0yjrlfkkgnv6-<emphasis>bash-4.2-p45</emphasis>"</computeroutput> +</screen> + So with the usual trick, we can refer to bin/bash and create our derivation: - + <screen xmlns="http://docbook.org/ns/docbook"><prompt>nix-repl> </prompt><userinput>d = derivation { name = "foo"; builder = "${bash}/bin/bash"; args = [ ./builder.sh ]; system = builtins.currentSystem; }</userinput> +<prompt>nix-repl> </prompt><userinput>:b d</userinput> +<computeroutput>[1 built, 0.0 MiB DL] + +this derivation produced the following outputs: + out -> /nix/store/gczb4qrag22harvv693wwnflqy7lx5pb-<emphasis>foo</emphasis></computeroutput></screen> + We did it! The contents of /nix/store/w024zci0x1hh1wj6gjq0jagkc1sgrf5r-foo @@ -117,7 +130,25 @@ We can use nix-store --read-log to see the logs our builder produced: - + <screen xmlns="http://docbook.org/ns/docbook"><prompt>$ </prompt><userinput>nix-store --read-log /nix/store/gczb4qrag22harvv693wwnflqy7lx5pb-<emphasis>foo</emphasis></userinput> +<computeroutput>declare -x HOME="/homeless-shelter" +declare -x NIX_BUILD_CORES="4" +declare -x NIX_BUILD_TOP="/tmp/nix-build-foo.drv-0" +declare -x NIX_LOG_FD="2" +declare -x NIX_STORE="/nix/store" +declare -x OLDPWD +declare -x PATH="/path-not-set" +declare -x PWD="/tmp/nix-build-foo.drv-0" +declare -x SHLVL="1" +declare -x TEMP="/tmp/nix-build-foo.drv-0" +declare -x TEMPDIR="/tmp/nix-build-foo.drv-0" +declare -x TMP="/tmp/nix-build-foo.drv-0" +declare -x TMPDIR="/tmp/nix-build-foo.drv-0" +declare -x builder="/nix/store/q1g0rl8zfmz7r371fp5p42p4acmv297d-bash-4.4-p19/bin/bash" +declare -x name="foo" +declare -x out="/nix/store/gczb4qrag22harvv693wwnflqy7lx5pb-foo" +declare -x system="x86_64-linux"</computeroutput></screen> + Let's inspect those environment variables printed during the build process. @@ -169,7 +200,36 @@ We added something else to the derivation this time: the args attribute. Let's see how this changed the .drv compared to the previous pill: - + <screen xmlns="http://docbook.org/ns/docbook"><prompt>$ </prompt><userinput>nix derivation show /nix/store/i76pr1cz0za3i9r6xq518bqqvd2raspw-<emphasis>foo.drv</emphasis></userinput> +<computeroutput>{ + "/nix/store/i76pr1cz0za3i9r6xq518bqqvd2raspw-foo.drv": { + "outputs": { + "out": { + "path": "/nix/store/gczb4qrag22harvv693wwnflqy7lx5pb-foo" + } + }, + "inputSrcs": [ + "/nix/store/lb0n38r2b20r8rl1k45a7s4pj6ny22f7-builder.sh" + ], + "inputDrvs": { + "/nix/store/hcgwbx42mcxr7ksnv0i1fg7kw6jvxshb-bash-4.4-p19.drv": [ + "out" + ] + }, + "platform": "x86_64-linux", + "builder": "/nix/store/q1g0rl8zfmz7r371fp5p42p4acmv297d-bash-4.4-p19/bin/bash", + "args": [ + "/nix/store/lb0n38r2b20r8rl1k45a7s4pj6ny22f7-builder.sh" + ], + "env": { + "builder": "/nix/store/q1g0rl8zfmz7r371fp5p42p4acmv297d-bash-4.4-p19/bin/bash", + "name": "foo", + "out": "/nix/store/gczb4qrag22harvv693wwnflqy7lx5pb-foo", + "system": "x86_64-linux" + } + } +}</computeroutput></screen> + Much like the usual .drv, except that there's a list of arguments in there passed to the builder (bash) with @@ -192,16 +252,29 @@ Start off by writing a simple C program called simple.c: - + void main() { + puts("Simple!"); +} + And its simple_builder.sh: - + export PATH="$coreutils/bin:$gcc/bin" +mkdir $out +gcc -o $out/simple $src + Don't worry too much about where those variables come from yet; let's write the derivation and build it: - + <screen xmlns="http://docbook.org/ns/docbook"><prompt>nix-repl> </prompt><userinput>:l &lt;nixpkgs></userinput> +<prompt>nix-repl> </prompt><userinput>simple = derivation { name = "simple"; builder = "${bash}/bin/bash"; args = [ ./simple_builder.sh ]; gcc = gcc; coreutils = coreutils; src = ./simple.c; system = builtins.currentSystem; }</userinput> +<prompt>nix-repl> </prompt><userinput>:b simple</userinput> +<computeroutput>this derivation produced the following outputs: + + out -> /nix/store/ni66p4jfqksbmsl616llx3fbs1d232d4-simple +</computeroutput></screen> + Now you can run /nix/store/ni66p4jfqksbmsl616llx3fbs1d232d4-simple/simple @@ -261,7 +334,19 @@ Drop out of nix repl and write a file simple.nix: - + let + pkgs = import <nixpkgs> { }; +in +pkgs.stdenv.mkDerivation { + name = "simple"; + builder = "${pkgs.bash}/bin/bash"; + args = [ ./simple_builder.sh ]; + gcc = pkgs.gcc; + coreutils = pkgs.coreutils; + src = ./simple.c; + system = builtins.currentSystem; +} + Now you can build it with nix-build simple.nix. This will create a symlink result in the current @@ -311,7 +396,18 @@ Below is a revised version of the simple.nix file, using the inherit keyword: - + let + pkgs = import <nixpkgs> { }; +in +pkgs.stdenv.mkDerivation { + name = "simple"; + builder = "${pkgs.bash}/bin/bash"; + args = [ ./simple_builder.sh ]; + inherit (pkgs) gcc coreutils; + src = ./simple.c; + system = builtins.currentSystem; +} + Here we also take the opportunity to introduce the inherit keyword. diff --git a/pills/07/bash.xml b/pills/07/bash.xml deleted file mode 100644 index e288773..0000000 --- a/pills/07/bash.xml +++ /dev/null @@ -1,5 +0,0 @@ -nix-repl> :l <nixpkgs> -Added 3950 variables. -nix-repl> "${bash}" -"/nix/store/ihmkc7z2wqk3bbipfnlh0yjrlfkkgnv6-bash-4.2-p45" - diff --git a/pills/07/builder.sh.txt b/pills/07/builder.sh.txt deleted file mode 100644 index 19459b7..0000000 --- a/pills/07/builder.sh.txt +++ /dev/null @@ -1,2 +0,0 @@ -declare -xp -echo foo > $out diff --git a/pills/07/c-program-derivation.xml b/pills/07/c-program-derivation.xml deleted file mode 100644 index 2f57f2c..0000000 --- a/pills/07/c-program-derivation.xml +++ /dev/null @@ -1,7 +0,0 @@ -nix-repl> :l <nixpkgs> -nix-repl> simple = derivation { name = "simple"; builder = "${bash}/bin/bash"; args = [ ./simple_builder.sh ]; gcc = gcc; coreutils = coreutils; src = ./simple.c; system = builtins.currentSystem; } -nix-repl> :b simple -this derivation produced the following outputs: - - out -> /nix/store/ni66p4jfqksbmsl616llx3fbs1d232d4-simple - diff --git a/pills/07/foo.drv.xml b/pills/07/foo.drv.xml deleted file mode 100644 index f9a5a92..0000000 --- a/pills/07/foo.drv.xml +++ /dev/null @@ -1,29 +0,0 @@ -$ nix derivation show /nix/store/i76pr1cz0za3i9r6xq518bqqvd2raspw-foo.drv -{ - "/nix/store/i76pr1cz0za3i9r6xq518bqqvd2raspw-foo.drv": { - "outputs": { - "out": { - "path": "/nix/store/gczb4qrag22harvv693wwnflqy7lx5pb-foo" - } - }, - "inputSrcs": [ - "/nix/store/lb0n38r2b20r8rl1k45a7s4pj6ny22f7-builder.sh" - ], - "inputDrvs": { - "/nix/store/hcgwbx42mcxr7ksnv0i1fg7kw6jvxshb-bash-4.4-p19.drv": [ - "out" - ] - }, - "platform": "x86_64-linux", - "builder": "/nix/store/q1g0rl8zfmz7r371fp5p42p4acmv297d-bash-4.4-p19/bin/bash", - "args": [ - "/nix/store/lb0n38r2b20r8rl1k45a7s4pj6ny22f7-builder.sh" - ], - "env": { - "builder": "/nix/store/q1g0rl8zfmz7r371fp5p42p4acmv297d-bash-4.4-p19/bin/bash", - "name": "foo", - "out": "/nix/store/gczb4qrag22harvv693wwnflqy7lx5pb-foo", - "system": "x86_64-linux" - } - } -} diff --git a/pills/07/read-log.xml b/pills/07/read-log.xml deleted file mode 100644 index 2124383..0000000 --- a/pills/07/read-log.xml +++ /dev/null @@ -1,18 +0,0 @@ -$ nix-store --read-log /nix/store/gczb4qrag22harvv693wwnflqy7lx5pb-foo -declare -x HOME="/homeless-shelter" -declare -x NIX_BUILD_CORES="4" -declare -x NIX_BUILD_TOP="/tmp/nix-build-foo.drv-0" -declare -x NIX_LOG_FD="2" -declare -x NIX_STORE="/nix/store" -declare -x OLDPWD -declare -x PATH="/path-not-set" -declare -x PWD="/tmp/nix-build-foo.drv-0" -declare -x SHLVL="1" -declare -x TEMP="/tmp/nix-build-foo.drv-0" -declare -x TEMPDIR="/tmp/nix-build-foo.drv-0" -declare -x TMP="/tmp/nix-build-foo.drv-0" -declare -x TMPDIR="/tmp/nix-build-foo.drv-0" -declare -x builder="/nix/store/q1g0rl8zfmz7r371fp5p42p4acmv297d-bash-4.4-p19/bin/bash" -declare -x name="foo" -declare -x out="/nix/store/gczb4qrag22harvv693wwnflqy7lx5pb-foo" -declare -x system="x86_64-linux" diff --git a/pills/07/simple-derivation.xml b/pills/07/simple-derivation.xml deleted file mode 100644 index 49f3599..0000000 --- a/pills/07/simple-derivation.xml +++ /dev/null @@ -1,6 +0,0 @@ -nix-repl> d = derivation { name = "foo"; builder = "${bash}/bin/bash"; args = [ ./builder.sh ]; system = builtins.currentSystem; } -nix-repl> :b d -[1 built, 0.0 MiB DL] - -this derivation produced the following outputs: - out -> /nix/store/gczb4qrag22harvv693wwnflqy7lx5pb-foo diff --git a/pills/07/simple.c.txt b/pills/07/simple.c.txt deleted file mode 100644 index c0a3532..0000000 --- a/pills/07/simple.c.txt +++ /dev/null @@ -1,3 +0,0 @@ -void main() { - puts("Simple!"); -} diff --git a/pills/07/simple.txt b/pills/07/simple.txt deleted file mode 100644 index eef5693..0000000 --- a/pills/07/simple.txt +++ /dev/null @@ -1,12 +0,0 @@ -let - pkgs = import { }; -in -pkgs.stdenv.mkDerivation { - name = "simple"; - builder = "${pkgs.bash}/bin/bash"; - args = [ ./simple_builder.sh ]; - gcc = pkgs.gcc; - coreutils = pkgs.coreutils; - src = ./simple.c; - system = builtins.currentSystem; -} diff --git a/pills/07/simple_builder.sh.txt b/pills/07/simple_builder.sh.txt deleted file mode 100644 index 8a38571..0000000 --- a/pills/07/simple_builder.sh.txt +++ /dev/null @@ -1,3 +0,0 @@ -export PATH="$coreutils/bin:$gcc/bin" -mkdir $out -gcc -o $out/simple $src diff --git a/pills/07/simple_inherit.txt b/pills/07/simple_inherit.txt deleted file mode 100644 index 3f8c4ae..0000000 --- a/pills/07/simple_inherit.txt +++ /dev/null @@ -1,11 +0,0 @@ -let - pkgs = import { }; -in -pkgs.stdenv.mkDerivation { - name = "simple"; - builder = "${pkgs.bash}/bin/bash"; - args = [ ./simple_builder.sh ]; - inherit (pkgs) gcc coreutils; - src = ./simple.c; - system = builtins.currentSystem; -} diff --git a/pills/08-generic-builders.xml b/pills/08-generic-builders.xml index aa5062d..52e19ed 100644 --- a/pills/08-generic-builders.xml +++ b/pills/08-generic-builders.xml @@ -41,17 +41,65 @@ Let's create a builder script for GNU hello world, hello_builder.sh: - + export PATH="$gnutar/bin:$gcc/bin:$gnumake/bin:$coreutils/bin:$gawk/bin:$gzip/bin:$gnugrep/bin:$gnused/bin:$bintools/bin" +tar -xzf $src +cd hello-2.12.1 +./configure --prefix=$out +make +make install + And the derivation hello.nix: - + let + pkgs = import <nixpkgs> { }; +in +derivation { + name = "hello"; + builder = "${pkgs.bash}/bin/bash"; + args = [ ./hello_builder.sh ]; + inherit (pkgs) + gnutar + gzip + gnumake + gcc + coreutils + gawk + gnused + gnugrep + ; + bintools = pkgs.binutils.bintools; + src = ./hello-2.12.1.tar.gz; + system = builtins.currentSystem; +} + Nix on darwin Darwin (i.e. macOS) builds typically use clang rather than gcc for a C compiler. We can adapt this early example for darwin by using this modified version of hello.nix: - + let + pkgs = import <nixpkgs> { }; +in +derivation { + name = "hello"; + builder = "${pkgs.bash}/bin/bash"; + args = [ ./hello_builder.sh ]; + inherit (pkgs) + gnutar + gzip + gnumake + coreutils + gawk + gnused + gnugrep + ; + gcc = pkgs.clang; + bintools = pkgs.clang.bintools.bintools_bin; + src = ./hello-2.12.1.tar.gz; + system = builtins.currentSystem; +} + Later, we will show how Nix can automatically handle these differences. For now, please be just aware that changes similar to the above may be needed in what follows. @@ -78,7 +126,25 @@ projects: - + set -e +unset PATH +for p in $buildInputs; do + export PATH=$p/bin${PATH:+:}$PATH +done + +tar -xf $src + +for d in *; do + if [ -d "$d" ]; then + cd "$d" + break + fi +done + +./configure --prefix=$out +make +make install + What do we do here? @@ -130,7 +196,28 @@ Now let's rewrite hello.nix: - + let + pkgs = import <nixpkgs> { }; +in +derivation { + name = "hello"; + builder = "${pkgs.bash}/bin/bash"; + args = [ ./builder.sh ]; + buildInputs = with pkgs; [ + gnutar + gzip + gnumake + gcc + coreutils + gawk + gnused + gnugrep + binutils.bintools + ]; + src = ./hello-2.12.1.tar.gz; + system = builtins.currentSystem; +} + All clear, except that buildInputs. However it's easier than any black @@ -142,13 +229,23 @@ to strings, and then concatenates them separated by a space: - + nix-repl> builtins.toString 123 +"123" +nix-repl> builtins.toString [ 123 456 ] +"123 456" + Recall that derivations can be converted to a string, hence: - + nix-repl> :l <nixpkgs> +Added 3950 variables. +nix-repl> builtins.toString gnugrep +"/nix/store/g5gdylclfh6d224kqh9sja290pk186xd-gnugrep-2.14" +nix-repl> builtins.toString [ gnugrep gnused ] +"/nix/store/g5gdylclfh6d224kqh9sja290pk186xd-gnugrep-2.14 /nix/store/krgdc4sknzpw8iyk9p20lhqfd52kjmg0-gnused-4.2.2" + Simple! The buildInputs variable is a string with out paths separated by @@ -176,7 +273,28 @@ Create autotools.nix: - + pkgs: attrs: +let + defaultAttrs = { + builder = "${pkgs.bash}/bin/bash"; + args = [ ./builder.sh ]; + baseInputs = with pkgs; [ + gnutar + gzip + gnumake + gcc + coreutils + gawk + gnused + gnugrep + binutils.bintools + ]; + buildInputs = [ ]; + system = builtins.currentSystem; + }; +in +derivation (defaultAttrs // attrs) + Ok now we have to remember a little about @@ -230,7 +348,11 @@ operator: - + nix-repl> { a = "b"; } // { c = "d"; } +{ a = "b"; c = "d"; } +nix-repl> { a = "b"; } // { a = "c"; } +{ a = "c"; } + Exercise: @@ -246,7 +368,15 @@ Then we rewrite hello.nix as follows: - + let + pkgs = import <nixpkgs> { }; + mkDerivation = import ./autotools.nix pkgs; +in +mkDerivation { + name = "hello"; + src = ./hello-2.12.1.tar.gz; +} + Finally! We got a very simple description of a package! Below are a diff --git a/pills/08/autotools-nix.txt b/pills/08/autotools-nix.txt deleted file mode 100644 index 65d3bc5..0000000 --- a/pills/08/autotools-nix.txt +++ /dev/null @@ -1,21 +0,0 @@ -pkgs: attrs: -let - defaultAttrs = { - builder = "${pkgs.bash}/bin/bash"; - args = [ ./builder.sh ]; - baseInputs = with pkgs; [ - gnutar - gzip - gnumake - gcc - coreutils - gawk - gnused - gnugrep - binutils.bintools - ]; - buildInputs = [ ]; - system = builtins.currentSystem; - }; -in -derivation (defaultAttrs // attrs) diff --git a/pills/08/generic-builder.txt b/pills/08/generic-builder.txt deleted file mode 100644 index 0d1c764..0000000 --- a/pills/08/generic-builder.txt +++ /dev/null @@ -1,18 +0,0 @@ -set -e -unset PATH -for p in $buildInputs; do - export PATH=$p/bin${PATH:+:}$PATH -done - -tar -xf $src - -for d in *; do - if [ -d "$d" ]; then - cd "$d" - break - fi -done - -./configure --prefix=$out -make -make install diff --git a/pills/08/hello-builder.txt b/pills/08/hello-builder.txt deleted file mode 100644 index 3ad635e..0000000 --- a/pills/08/hello-builder.txt +++ /dev/null @@ -1,6 +0,0 @@ -export PATH="$gnutar/bin:$gcc/bin:$gnumake/bin:$coreutils/bin:$gawk/bin:$gzip/bin:$gnugrep/bin:$gnused/bin:$bintools/bin" -tar -xzf $src -cd hello-2.12.1 -./configure --prefix=$out -make -make install diff --git a/pills/08/hello-nix-darwin.txt b/pills/08/hello-nix-darwin.txt deleted file mode 100644 index 68a11bb..0000000 --- a/pills/08/hello-nix-darwin.txt +++ /dev/null @@ -1,21 +0,0 @@ -let - pkgs = import { }; -in -derivation { - name = "hello"; - builder = "${pkgs.bash}/bin/bash"; - args = [ ./hello_builder.sh ]; - inherit (pkgs) - gnutar - gzip - gnumake - coreutils - gawk - gnused - gnugrep - ; - gcc = pkgs.clang; - bintools = pkgs.clang.bintools.bintools_bin; - src = ./hello-2.12.1.tar.gz; - system = builtins.currentSystem; -} diff --git a/pills/08/hello-nix-rev-1.txt b/pills/08/hello-nix-rev-1.txt deleted file mode 100644 index 748ff5d..0000000 --- a/pills/08/hello-nix-rev-1.txt +++ /dev/null @@ -1,21 +0,0 @@ -let - pkgs = import { }; -in -derivation { - name = "hello"; - builder = "${pkgs.bash}/bin/bash"; - args = [ ./builder.sh ]; - buildInputs = with pkgs; [ - gnutar - gzip - gnumake - gcc - coreutils - gawk - gnused - gnugrep - binutils.bintools - ]; - src = ./hello-2.12.1.tar.gz; - system = builtins.currentSystem; -} diff --git a/pills/08/hello-nix-rev-2.txt b/pills/08/hello-nix-rev-2.txt deleted file mode 100644 index 8c660f7..0000000 --- a/pills/08/hello-nix-rev-2.txt +++ /dev/null @@ -1,8 +0,0 @@ -let - pkgs = import { }; - mkDerivation = import ./autotools.nix pkgs; -in -mkDerivation { - name = "hello"; - src = ./hello-2.12.1.tar.gz; -} diff --git a/pills/08/hello-nix.txt b/pills/08/hello-nix.txt deleted file mode 100644 index 948bcfd..0000000 --- a/pills/08/hello-nix.txt +++ /dev/null @@ -1,21 +0,0 @@ -let - pkgs = import { }; -in -derivation { - name = "hello"; - builder = "${pkgs.bash}/bin/bash"; - args = [ ./hello_builder.sh ]; - inherit (pkgs) - gnutar - gzip - gnumake - gcc - coreutils - gawk - gnused - gnugrep - ; - bintools = pkgs.binutils.bintools; - src = ./hello-2.12.1.tar.gz; - system = builtins.currentSystem; -} diff --git a/pills/08/set-union.txt b/pills/08/set-union.txt deleted file mode 100644 index 392e6a7..0000000 --- a/pills/08/set-union.txt +++ /dev/null @@ -1,4 +0,0 @@ -nix-repl> { a = "b"; } // { c = "d"; } -{ a = "b"; c = "d"; } -nix-repl> { a = "b"; } // { a = "c"; } -{ a = "c"; } diff --git a/pills/08/to-string-nixpkgs.txt b/pills/08/to-string-nixpkgs.txt deleted file mode 100644 index f09ee03..0000000 --- a/pills/08/to-string-nixpkgs.txt +++ /dev/null @@ -1,6 +0,0 @@ -nix-repl> :l -Added 3950 variables. -nix-repl> builtins.toString gnugrep -"/nix/store/g5gdylclfh6d224kqh9sja290pk186xd-gnugrep-2.14" -nix-repl> builtins.toString [ gnugrep gnused ] -"/nix/store/g5gdylclfh6d224kqh9sja290pk186xd-gnugrep-2.14 /nix/store/krgdc4sknzpw8iyk9p20lhqfd52kjmg0-gnused-4.2.2" diff --git a/pills/08/to-string.txt b/pills/08/to-string.txt deleted file mode 100644 index 0e126a3..0000000 --- a/pills/08/to-string.txt +++ /dev/null @@ -1,4 +0,0 @@ -nix-repl> builtins.toString 123 -"123" -nix-repl> builtins.toString [ 123 456 ] -"123 456" diff --git a/pills/09-automatic-runtime.xml b/pills/09-automatic-runtime.xml index 7af0c1b..666ae99 100644 --- a/pills/09-automatic-runtime.xml +++ b/pills/09-automatic-runtime.xml @@ -26,7 +26,22 @@ Let's start analyzing build dependencies for our GNU hello package: - + $ nix-instantiate hello.nix +/nix/store/z77vn965a59irqnrrjvbspiyl2rph0jp-hello.drv +$ nix-store -q --references /nix/store/z77vn965a59irqnrrjvbspiyl2rph0jp-hello.drv +/nix/store/0q6pfasdma4as22kyaknk4kwx4h58480-hello-2.10.tar.gz +/nix/store/1zcs1y4n27lqs0gw4v038i303pb89rw6-coreutils-8.21.drv +/nix/store/2h4b30hlfw4fhqx10wwi71mpim4wr877-gnused-4.2.2.drv +/nix/store/39bgdjissw9gyi4y5j9wanf4dbjpbl07-gnutar-1.27.1.drv +/nix/store/7qa70nay0if4x291rsjr7h9lfl6pl7b1-builder.sh +/nix/store/g6a0shr58qvx2vi6815acgp9lnfh9yy8-gnugrep-2.14.drv +/nix/store/jdggv3q1sb15140qdx0apvyrps41m4lr-bash-4.2-p45.drv +/nix/store/pglhiyp1zdbmax4cglkpz98nspfgbnwr-gnumake-3.82.drv +/nix/store/q9l257jn9lndbi3r9ksnvf4dr8cwxzk7-gawk-4.1.0.drv +/nix/store/rgyrqxz1ilv90r01zxl0sq5nq0cq7v3v-binutils-2.23.1.drv +/nix/store/qzxhby795niy6wlagfpbja27dgsz43xk-gcc-wrapper-4.8.3.drv +/nix/store/sk590g7fv53m3zp0ycnxsc41snc2kdhp-gzip-1.6.drv + It has precisely the derivations referenced in the derivation function; @@ -117,7 +132,15 @@ The snippet below shows the dependencies for hello. - + $ nix-instantiate hello.nix +/nix/store/z77vn965a59irqnrrjvbspiyl2rph0jp-hello.drv +$ nix-store -r /nix/store/z77vn965a59irqnrrjvbspiyl2rph0jp-hello.drv +/nix/store/a42k52zwv6idmf50r9lps1nzwq9khvpf-hello +$ nix-store -q --references /nix/store/a42k52zwv6idmf50r9lps1nzwq9khvpf-hello +/nix/store/94n64qy99ja0vgbkf675nyk39g9b978n-glibc-2.19 +/nix/store/8jm0wksask7cpf85miyakihyfch1y21q-gcc-4.8.3 +/nix/store/a42k52zwv6idmf50r9lps1nzwq9khvpf-hello + We see that glibc and gcc are runtime dependencies. @@ -126,7 +149,9 @@ of gcc does indeed appear: - + $ strings result/bin/hello|grep gcc +/nix/store/94n64qy99ja0vgbkf675nyk39g9b978n-glibc-2.19/lib:/nix/store/8jm0wksask7cpf85miyakihyfch1y21q-gcc-4.8.3/lib64 + This is why Nix added gcc. But why is that path present in the @@ -202,7 +227,8 @@ builder.sh, we append: - + find $out -type f -exec patchelf --shrink-rpath '{}' \; -exec strip '{}' \; 2>/dev/null + That is, for each file we run patchelf --shrink-rpath @@ -221,7 +247,12 @@ Now, we rebuild hello.nix... - + $ nix-build hello.nix +[...] +$ nix-store -q --references result +/nix/store/94n64qy99ja0vgbkf675nyk39g9b978n-glibc-2.19 +/nix/store/md4a3zv0ipqzsybhjb8ndjhhga1dj88x-hello + and we see that glibc is a runtime dependency. This is @@ -237,7 +268,11 @@ library and interpreter referred to in the binary, rather than the system one: - + $ ldd result/bin/hello + linux-vdso.so.1 (0x00007fff11294000) + libc.so.6 => /nix/store/94n64qy99ja0vgbkf675nyk39g9b978n-glibc-2.19/lib/libc.so.6 (0x00007f7ab7362000) + /nix/store/94n64qy99ja0vgbkf675nyk39g9b978n-glibc-2.19/lib/ld-linux-x86-64.so.2 (0x00007f7ab770f000) + Of course, the executable will run fine as long as everything is under the diff --git a/pills/09/build-hello-nix.txt b/pills/09/build-hello-nix.txt deleted file mode 100644 index b364bd8..0000000 --- a/pills/09/build-hello-nix.txt +++ /dev/null @@ -1,5 +0,0 @@ -$ nix-build hello.nix -[...] -$ nix-store -q --references result -/nix/store/94n64qy99ja0vgbkf675nyk39g9b978n-glibc-2.19 -/nix/store/md4a3zv0ipqzsybhjb8ndjhhga1dj88x-hello diff --git a/pills/09/find.txt b/pills/09/find.txt deleted file mode 100644 index 361ce92..0000000 --- a/pills/09/find.txt +++ /dev/null @@ -1 +0,0 @@ -find $out -type f -exec patchelf --shrink-rpath '{}' \; -exec strip '{}' \; 2>/dev/null diff --git a/pills/09/instantiate-hello.txt b/pills/09/instantiate-hello.txt deleted file mode 100644 index 53d15b7..0000000 --- a/pills/09/instantiate-hello.txt +++ /dev/null @@ -1,8 +0,0 @@ -$ nix-instantiate hello.nix -/nix/store/z77vn965a59irqnrrjvbspiyl2rph0jp-hello.drv -$ nix-store -r /nix/store/z77vn965a59irqnrrjvbspiyl2rph0jp-hello.drv -/nix/store/a42k52zwv6idmf50r9lps1nzwq9khvpf-hello -$ nix-store -q --references /nix/store/a42k52zwv6idmf50r9lps1nzwq9khvpf-hello -/nix/store/94n64qy99ja0vgbkf675nyk39g9b978n-glibc-2.19 -/nix/store/8jm0wksask7cpf85miyakihyfch1y21q-gcc-4.8.3 -/nix/store/a42k52zwv6idmf50r9lps1nzwq9khvpf-hello diff --git a/pills/09/instantiate.txt b/pills/09/instantiate.txt deleted file mode 100644 index 9be974e..0000000 --- a/pills/09/instantiate.txt +++ /dev/null @@ -1,15 +0,0 @@ -$ nix-instantiate hello.nix -/nix/store/z77vn965a59irqnrrjvbspiyl2rph0jp-hello.drv -$ nix-store -q --references /nix/store/z77vn965a59irqnrrjvbspiyl2rph0jp-hello.drv -/nix/store/0q6pfasdma4as22kyaknk4kwx4h58480-hello-2.10.tar.gz -/nix/store/1zcs1y4n27lqs0gw4v038i303pb89rw6-coreutils-8.21.drv -/nix/store/2h4b30hlfw4fhqx10wwi71mpim4wr877-gnused-4.2.2.drv -/nix/store/39bgdjissw9gyi4y5j9wanf4dbjpbl07-gnutar-1.27.1.drv -/nix/store/7qa70nay0if4x291rsjr7h9lfl6pl7b1-builder.sh -/nix/store/g6a0shr58qvx2vi6815acgp9lnfh9yy8-gnugrep-2.14.drv -/nix/store/jdggv3q1sb15140qdx0apvyrps41m4lr-bash-4.2-p45.drv -/nix/store/pglhiyp1zdbmax4cglkpz98nspfgbnwr-gnumake-3.82.drv -/nix/store/q9l257jn9lndbi3r9ksnvf4dr8cwxzk7-gawk-4.1.0.drv -/nix/store/rgyrqxz1ilv90r01zxl0sq5nq0cq7v3v-binutils-2.23.1.drv -/nix/store/qzxhby795niy6wlagfpbja27dgsz43xk-gcc-wrapper-4.8.3.drv -/nix/store/sk590g7fv53m3zp0ycnxsc41snc2kdhp-gzip-1.6.drv diff --git a/pills/09/ldd-hello.txt b/pills/09/ldd-hello.txt deleted file mode 100644 index 20c61e8..0000000 --- a/pills/09/ldd-hello.txt +++ /dev/null @@ -1,4 +0,0 @@ -$ ldd result/bin/hello - linux-vdso.so.1 (0x00007fff11294000) - libc.so.6 => /nix/store/94n64qy99ja0vgbkf675nyk39g9b978n-glibc-2.19/lib/libc.so.6 (0x00007f7ab7362000) - /nix/store/94n64qy99ja0vgbkf675nyk39g9b978n-glibc-2.19/lib/ld-linux-x86-64.so.2 (0x00007f7ab770f000) diff --git a/pills/09/strings.txt b/pills/09/strings.txt deleted file mode 100644 index f766ab9..0000000 --- a/pills/09/strings.txt +++ /dev/null @@ -1,2 +0,0 @@ -$ strings result/bin/hello|grep gcc -/nix/store/94n64qy99ja0vgbkf675nyk39g9b978n-glibc-2.19/lib:/nix/store/8jm0wksask7cpf85miyakihyfch1y21q-gcc-4.8.3/lib64 diff --git a/pills/10-developing-with-nix-shell.xml b/pills/10-developing-with-nix-shell.xml index e5dd5c1..df18401 100644 --- a/pills/10-developing-with-nix-shell.xml +++ b/pills/10-developing-with-nix-shell.xml @@ -52,7 +52,12 @@ PATH does not have the utilities we want: - + $ nix-shell hello.nix +[nix-shell]$ make +bash: make: command not found +[nix-shell]$ echo $baseInputs +/nix/store/jff4a6zqi0yrladx3kwy4v6844s3swpc-gnutar-1.27.1 [...] + This shell is rather useless. It would be reasonable to expect that the GNU @@ -73,7 +78,9 @@ not have the permission to write to /nix/store: - + [nix-shell]$ source builder.sh +... + The derivation didn't install, but it did build. Note the following: @@ -157,7 +164,31 @@ correspondingly adds a $setup environment variable in the builder. - + pkgs: attrs: +let + defaultAttrs = { + builder = "${pkgs.bash}/bin/bash"; + args = [ ./builder.sh ]; + setup = ./setup.sh; + baseInputs = with pkgs; [ + gnutar + gzip + gnumake + gcc + coreutils + gawk + gnused + gnugrep + binutils.bintools + patchelf + findutils + ]; + buildInputs = [ ]; + system = builtins.currentSystem; + }; +in +derivation (defaultAttrs // attrs) + Thanks to that, we can split builder.sh into @@ -171,25 +202,78 @@ Here is the modified builder.sh: - + set -e +source $setup +genericBuild + Here is the newly added setup.sh: - + 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 +} + Finally, here is hello.nix: - + let + pkgs = import <nixpkgs> { }; + mkDerivation = import ./autotools.nix pkgs; +in +mkDerivation { + name = "hello"; + src = ./hello-2.12.1.tar.gz; +} + Now back to nix-shell: - + $ nix-shell hello.nix +[nix-shell]$ source $setup +[nix-shell]$ + Now, for example, you can run unpackPhase which unpacks diff --git a/pills/10/autotools-nix.txt b/pills/10/autotools-nix.txt deleted file mode 100644 index fc4c43e..0000000 --- a/pills/10/autotools-nix.txt +++ /dev/null @@ -1,24 +0,0 @@ -pkgs: attrs: -let - defaultAttrs = { - builder = "${pkgs.bash}/bin/bash"; - args = [ ./builder.sh ]; - setup = ./setup.sh; - baseInputs = with pkgs; [ - gnutar - gzip - gnumake - gcc - coreutils - gawk - gnused - gnugrep - binutils.bintools - patchelf - findutils - ]; - buildInputs = [ ]; - system = builtins.currentSystem; - }; -in -derivation (defaultAttrs // attrs) diff --git a/pills/10/builder-sh.txt b/pills/10/builder-sh.txt deleted file mode 100644 index 91b081f..0000000 --- a/pills/10/builder-sh.txt +++ /dev/null @@ -1,3 +0,0 @@ -set -e -source $setup -genericBuild diff --git a/pills/10/hello-nix.txt b/pills/10/hello-nix.txt deleted file mode 100644 index 8c660f7..0000000 --- a/pills/10/hello-nix.txt +++ /dev/null @@ -1,8 +0,0 @@ -let - pkgs = import { }; - mkDerivation = import ./autotools.nix pkgs; -in -mkDerivation { - name = "hello"; - src = ./hello-2.12.1.tar.gz; -} diff --git a/pills/10/nix-shell-hello.txt b/pills/10/nix-shell-hello.txt deleted file mode 100644 index a1e5713..0000000 --- a/pills/10/nix-shell-hello.txt +++ /dev/null @@ -1,5 +0,0 @@ -$ nix-shell hello.nix -[nix-shell]$ make -bash: make: command not found -[nix-shell]$ echo $baseInputs -/nix/store/jff4a6zqi0yrladx3kwy4v6844s3swpc-gnutar-1.27.1 [...] diff --git a/pills/10/nix-shell-source.txt b/pills/10/nix-shell-source.txt deleted file mode 100644 index 870f4e4..0000000 --- a/pills/10/nix-shell-source.txt +++ /dev/null @@ -1,3 +0,0 @@ -$ nix-shell hello.nix -[nix-shell]$ source $setup -[nix-shell]$ diff --git a/pills/10/setup-sh.txt b/pills/10/setup-sh.txt deleted file mode 100644 index 90e9840..0000000 --- a/pills/10/setup-sh.txt +++ /dev/null @@ -1,39 +0,0 @@ -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 -} diff --git a/pills/10/source-builder.txt b/pills/10/source-builder.txt deleted file mode 100644 index fb87b39..0000000 --- a/pills/10/source-builder.txt +++ /dev/null @@ -1,2 +0,0 @@ -[nix-shell]$ source builder.sh -... diff --git a/pills/11-garbage-collector.xml b/pills/11-garbage-collector.xml index 2132d69..d346277 100644 --- a/pills/11-garbage-collector.xml +++ b/pills/11-garbage-collector.xml @@ -80,7 +80,13 @@ so that we have a clean setup for our experiments: - + $ nix-collect-garbage +finding garbage collector roots... +[...] +deleting unused links... +note: currently hard linking saves -0.00 MiB +1169 store paths deleted, 228.43 MiB freed + If we run the garbage collector again it won't find anything new to delete, @@ -95,14 +101,28 @@ case, our current user environment refers to bsd-games: - + $ nix-env -iA nixpkgs.bsdgames +$ readlink -f `which fortune` +/nix/store/b3lxx3d3ggxcggvjw5n0m1ya1gcrmbyn-bsd-games-2.17/bin/fortune +$ nix-store -q --roots `which fortune` +/nix/var/nix/profiles/default-9-link +$ nix-env --list-generations +[...] + 9 2014-08-20 12:44:14 (current) + Now we remove it and run the garbage collector, and note that bsd-games is still in the nix store: - + $ nix-env -e bsd-games +uninstalling `bsd-games-2.17' +$ nix-collect-garbage +[...] +$ ls /nix/store/b3lxx3d3ggxcggvjw5n0m1ya1gcrmbyn-bsd-games-2.17 +bin share + The old generation is still in the nix store because it is a GC root. @@ -116,7 +136,16 @@ that bsd-games is no longer in the nix store: - + $ rm /nix/var/nix/profiles/default-9-link +$ nix-env --list-generations +[...] + 8 2014-07-28 10:23:24 + 10 2014-08-20 12:47:16 (current) +$ nix-collect-garbage +[...] +$ ls /nix/store/b3lxx3d3ggxcggvjw5n0m1ya1gcrmbyn-bsd-games-2.17 +ls: cannot access /nix/store/b3lxx3d3ggxcggvjw5n0m1ya1gcrmbyn-bsd-games-2.17: No such file or directory + Note: @@ -159,7 +188,12 @@ are added under /nix/var/nix/gcroots/auto. - + $ ls -l /nix/var/nix/gcroots/auto/ +total 8 +drwxr-xr-x 2 nix nix 4096 Aug 20 10:24 ./ +drwxr-xr-x 3 nix nix 4096 Jul 24 10:38 ../ +lrwxrwxrwx 1 nix nix 16 Jul 31 10:51 xlgz5x2ppa0m72z5qfc78b8wlciwvgiz -> /home/nix/result/ + The name of the GC root symlink is not important to us at this time. @@ -264,7 +298,11 @@ The four steps are shown below: - + $ nix-channel --update +$ nix-env -u --always +$ rm /nix/var/nix/gcroots/auto/* +$ nix-collect-garbage -d +
diff --git a/pills/11/channel-update.txt b/pills/11/channel-update.txt deleted file mode 100644 index 27b82e0..0000000 --- a/pills/11/channel-update.txt +++ /dev/null @@ -1,4 +0,0 @@ -$ nix-channel --update -$ nix-env -u --always -$ rm /nix/var/nix/gcroots/auto/* -$ nix-collect-garbage -d diff --git a/pills/11/install-bsd-games.txt b/pills/11/install-bsd-games.txt deleted file mode 100644 index 1c068b2..0000000 --- a/pills/11/install-bsd-games.txt +++ /dev/null @@ -1,8 +0,0 @@ -$ nix-env -iA nixpkgs.bsdgames -$ readlink -f `which fortune` -/nix/store/b3lxx3d3ggxcggvjw5n0m1ya1gcrmbyn-bsd-games-2.17/bin/fortune -$ nix-store -q --roots `which fortune` -/nix/var/nix/profiles/default-9-link -$ nix-env --list-generations -[...] - 9 2014-08-20 12:44:14 (current) diff --git a/pills/11/ls-gcroots-auto.txt b/pills/11/ls-gcroots-auto.txt deleted file mode 100644 index b66ef0c..0000000 --- a/pills/11/ls-gcroots-auto.txt +++ /dev/null @@ -1,5 +0,0 @@ -$ ls -l /nix/var/nix/gcroots/auto/ -total 8 -drwxr-xr-x 2 nix nix 4096 Aug 20 10:24 ./ -drwxr-xr-x 3 nix nix 4096 Jul 24 10:38 ../ -lrwxrwxrwx 1 nix nix 16 Jul 31 10:51 xlgz5x2ppa0m72z5qfc78b8wlciwvgiz -> /home/nix/result/ diff --git a/pills/11/nix-collect-garbage.txt b/pills/11/nix-collect-garbage.txt deleted file mode 100644 index 9d2b406..0000000 --- a/pills/11/nix-collect-garbage.txt +++ /dev/null @@ -1,6 +0,0 @@ -$ nix-collect-garbage -finding garbage collector roots... -[...] -deleting unused links... -note: currently hard linking saves -0.00 MiB -1169 store paths deleted, 228.43 MiB freed diff --git a/pills/11/remove-bsd-games.txt b/pills/11/remove-bsd-games.txt deleted file mode 100644 index 49f0912..0000000 --- a/pills/11/remove-bsd-games.txt +++ /dev/null @@ -1,6 +0,0 @@ -$ nix-env -e bsd-games -uninstalling `bsd-games-2.17' -$ nix-collect-garbage -[...] -$ ls /nix/store/b3lxx3d3ggxcggvjw5n0m1ya1gcrmbyn-bsd-games-2.17 -bin share diff --git a/pills/11/remove-gen-9.txt b/pills/11/remove-gen-9.txt deleted file mode 100644 index a7084b1..0000000 --- a/pills/11/remove-gen-9.txt +++ /dev/null @@ -1,9 +0,0 @@ -$ rm /nix/var/nix/profiles/default-9-link -$ nix-env --list-generations -[...] - 8 2014-07-28 10:23:24 - 10 2014-08-20 12:47:16 (current) -$ nix-collect-garbage -[...] -$ ls /nix/store/b3lxx3d3ggxcggvjw5n0m1ya1gcrmbyn-bsd-games-2.17 -ls: cannot access /nix/store/b3lxx3d3ggxcggvjw5n0m1ya1gcrmbyn-bsd-games-2.17: No such file or directory diff --git a/pills/12-inputs-design-pattern.xml b/pills/12-inputs-design-pattern.xml index d2fa330..8fb9b3c 100644 --- a/pills/12-inputs-design-pattern.xml +++ b/pills/12-inputs-design-pattern.xml @@ -71,7 +71,15 @@ First, we download graphviz from gitlab. The graphviz.nix expression is straightforward: - + let + pkgs = import <nixpkgs> { }; + mkDerivation = import ./autotools.nix pkgs; +in +mkDerivation { + name = "graphviz"; + src = ./graphviz-2.49.3.tar.gz; +} + If we build the project with nix-build graphviz.nix, we will get runnable binaries under result/bin. Notice how we reused the same autotools.nix of hello.nix. @@ -80,7 +88,9 @@ png files. Thus, the derivation above will build a binary supporting only the native output formats, as we see below:
- + $ echo 'graph test { a -- b }'|result/bin/dot -Tpng -o test.png +Format: "png" not recognized. Use one of: canon cmap [...] + If we want to produce a png file with graphviz, we must add it to our derivation. The place to do so is @@ -119,7 +129,15 @@ automatically filling the variables from buildInputs. This is the relevant snippet of setup.sh: - + for p in $baseInputs $buildInputs; do + if [ -d $p/bin ]; then + export PATH="$p/bin${PATH:+:}$PATH" + fi + if [ -d $p/lib/pkgconfig ]; then + export PKG_CONFIG_PATH="$p/lib/pkgconfig${PKG_CONFIG_PATH:+:}$PKG_CONFIG_PATH" + fi +done + Now if we add derivations to buildInputs, their lib/pkgconfig and bin paths @@ -133,7 +151,20 @@ Below, we finish the expression for graphviz with gd support. Note the use of the with expression in buildInputs to avoid repeating pkgs: - + let + pkgs = import <nixpkgs> { }; + mkDerivation = import ./autotools.nix pkgs; +in +mkDerivation { + name = "graphviz"; + src = ./graphviz-2.49.3.tar.gz; + buildInputs = with pkgs; [ + pkg-config + (pkgs.lib.getLib gd) + (pkgs.lib.getDev gd) + ]; +} + We add pkg-config to the derivation to make this tool available for the configure script. As gd is a package @@ -163,16 +194,31 @@ To begin, create a default.nix in the current directory: - + { + hello = import ./hello.nix; + graphviz = import ./graphviz.nix; +} + This file is ready to use with nix repl: - + $ nix repl +nix-repl> :l default.nix +Added 2 variables. +nix-repl> hello +«derivation /nix/store/dkib02g54fpdqgpskswgp6m7bd7mgx89-hello.drv» +nix-repl> graphviz +«derivation /nix/store/zqv520v9mk13is0w980c91z7q1vkhhil-graphviz.drv» + With nix-build, we can pass the -A option to access an attribute of the set from the given .nix expression: - + $ nix-build default.nix -A hello +[...] +$ result/bin/hello +Hello, world! + The default.nix file is special. When a directory contains a default.nix file, it is used as the implicit @@ -184,7 +230,10 @@ We can now use nix-env to install the package into our user environment: - + $ nix-env -f . -iA graphviz +[...] +$ dot -V + Taking a closer look at the above command, we see the following options: @@ -264,7 +313,21 @@ with graphviz.nix, we make the following changes to make the derivation independent of the repository and customizable: - + { mkDerivation, lib, gdSupport ? true, gd, pkg-config }: + +mkDerivation { + name = "graphviz"; + src = ./graphviz-2.49.3.tar.gz; + buildInputs = + if gdSupport + then [ + pkg-config + (lib.getLib gd) + (lib.getDev gd) + ] + else []; +} + Recall that "{...}: ..." is the syntax for defining functions accepting an attribute set as argument; the above snippet just defines a function. @@ -281,7 +344,32 @@ Going back to back to default.nix, we modify our expression to utilize the inputs pattern: - + let + pkgs = import <nixpkgs> { }; + mkDerivation = import ./autotools.nix pkgs; +in +with pkgs; +{ + hello = import ./hello.nix { inherit mkDerivation; }; + graphviz = import ./graphviz.nix { + inherit + mkDerivation + lib + gd + pkg-config + ; + }; + graphvizCore = import ./graphviz.nix { + inherit + mkDerivation + lib + gd + pkg-config + ; + gdSupport = false; + }; +} + We factorized the import of nixpkgs and mkDerivation, and also added a variant of graphviz diff --git a/pills/12/graphviz-derivation.txt b/pills/12/graphviz-derivation.txt deleted file mode 100644 index 93e83fc..0000000 --- a/pills/12/graphviz-derivation.txt +++ /dev/null @@ -1,8 +0,0 @@ -let - pkgs = import { }; - mkDerivation = import ./autotools.nix pkgs; -in -mkDerivation { - name = "graphviz"; - src = ./graphviz-2.49.3.tar.gz; -} diff --git a/pills/12/graphviz-gd-derivation.txt b/pills/12/graphviz-gd-derivation.txt deleted file mode 100644 index 4ef0a35..0000000 --- a/pills/12/graphviz-gd-derivation.txt +++ /dev/null @@ -1,13 +0,0 @@ -let - pkgs = import { }; - mkDerivation = import ./autotools.nix pkgs; -in -mkDerivation { - name = "graphviz"; - src = ./graphviz-2.49.3.tar.gz; - buildInputs = with pkgs; [ - pkg-config - (pkgs.lib.getLib gd) - (pkgs.lib.getDev gd) - ]; -} diff --git a/pills/12/graphviz-mkderivation.txt b/pills/12/graphviz-mkderivation.txt deleted file mode 100644 index e60c7c0..0000000 --- a/pills/12/graphviz-mkderivation.txt +++ /dev/null @@ -1,14 +0,0 @@ -{ mkDerivation, lib, gdSupport ? true, gd, pkg-config }: - -mkDerivation { - name = "graphviz"; - src = ./graphviz-2.49.3.tar.gz; - buildInputs = - if gdSupport - then [ - pkg-config - (lib.getLib gd) - (lib.getDev gd) - ] - else []; -} diff --git a/pills/12/nix-env-install-graphviz.txt b/pills/12/nix-env-install-graphviz.txt deleted file mode 100644 index 844c5f4..0000000 --- a/pills/12/nix-env-install-graphviz.txt +++ /dev/null @@ -1,3 +0,0 @@ -$ nix-env -f . -iA graphviz -[...] -$ dot -V diff --git a/pills/12/repository-mkderivation.txt b/pills/12/repository-mkderivation.txt deleted file mode 100644 index 4731bf8..0000000 --- a/pills/12/repository-mkderivation.txt +++ /dev/null @@ -1,25 +0,0 @@ -let - pkgs = import { }; - mkDerivation = import ./autotools.nix pkgs; -in -with pkgs; -{ - hello = import ./hello.nix { inherit mkDerivation; }; - graphviz = import ./graphviz.nix { - inherit - mkDerivation - lib - gd - pkg-config - ; - }; - graphvizCore = import ./graphviz.nix { - inherit - mkDerivation - lib - gd - pkg-config - ; - gdSupport = false; - }; -} diff --git a/pills/12/repository-test-nix-build.txt b/pills/12/repository-test-nix-build.txt deleted file mode 100644 index fa91e01..0000000 --- a/pills/12/repository-test-nix-build.txt +++ /dev/null @@ -1,4 +0,0 @@ -$ nix-build default.nix -A hello -[...] -$ result/bin/hello -Hello, world! diff --git a/pills/12/repository-test-nix-repl.txt b/pills/12/repository-test-nix-repl.txt deleted file mode 100644 index 7bb78e8..0000000 --- a/pills/12/repository-test-nix-repl.txt +++ /dev/null @@ -1,7 +0,0 @@ -$ nix repl -nix-repl> :l default.nix -Added 2 variables. -nix-repl> hello -«derivation /nix/store/dkib02g54fpdqgpskswgp6m7bd7mgx89-hello.drv» -nix-repl> graphviz -«derivation /nix/store/zqv520v9mk13is0w980c91z7q1vkhhil-graphviz.drv» diff --git a/pills/12/repository.txt b/pills/12/repository.txt deleted file mode 100644 index 3068e07..0000000 --- a/pills/12/repository.txt +++ /dev/null @@ -1,4 +0,0 @@ -{ - hello = import ./hello.nix; - graphviz = import ./graphviz.nix; -} diff --git a/pills/12/setup-sh.txt b/pills/12/setup-sh.txt deleted file mode 100644 index bf9b24c..0000000 --- a/pills/12/setup-sh.txt +++ /dev/null @@ -1,8 +0,0 @@ -for p in $baseInputs $buildInputs; do - if [ -d $p/bin ]; then - export PATH="$p/bin${PATH:+:}$PATH" - fi - if [ -d $p/lib/pkgconfig ]; then - export PKG_CONFIG_PATH="$p/lib/pkgconfig${PKG_CONFIG_PATH:+:}$PKG_CONFIG_PATH" - fi -done diff --git a/pills/12/simple-png.txt b/pills/12/simple-png.txt deleted file mode 100644 index 9c33101..0000000 --- a/pills/12/simple-png.txt +++ /dev/null @@ -1,2 +0,0 @@ -$ echo 'graph test { a -- b }'|result/bin/dot -Tpng -o test.png -Format: "png" not recognized. Use one of: canon cmap [...] diff --git a/pills/13-callpackage-design-pattern.xml b/pills/13-callpackage-design-pattern.xml index 95c085a..edef456 100644 --- a/pills/13-callpackage-design-pattern.xml +++ b/pills/13-callpackage-design-pattern.xml @@ -33,12 +33,18 @@ For example, if we define a package derivation using the inputs pattern such as: - + { input1, input2, ... }: +... + we would likely want to bundle that package derivation into a repository via a an attribute set defined as something like: - + rec { + lib1 = import package1.nix { inherit input1 input2; }; + program2 = import package2.nix { inherit inputX inputY lib1; }; +} + There are two things to note. First, that inputs often have the same name as attributes in the repository itself. Second, that (due to the rec @@ -53,7 +59,11 @@ To achieve this, we will define a callPackage function with the following calling convention: - + { + lib1 = callPackage package1.nix { }; + program2 = callPackage package2.nix { someoverride = overriddenDerivation; }; +} + We want callPackage to be a function of two arguments, with the following behavior: @@ -86,7 +96,10 @@ Nix provides a builtin function to do this: - + nix-repl> add = { a ? 3, b }: a+b +nix-repl> builtins.functionArgs add +{ a = true; b = false; } + In addition to returning the argument names, the attribute set returned by functionArgs indicates whether or not the argument has a default value. @@ -117,7 +130,12 @@ to be package names in a repository, such as nixpkgs. For the latter, Nix provides another builtin function: - + nix-repl> values = { a = 3; b = 5; c = 10; } +nix-repl> builtins.intersectAttrs values (builtins.functionArgs add) +{ a = true; b = false; } +nix-repl> builtins.intersectAttrs (builtins.functionArgs add) values +{ a = 3; b = 5; } + The intersectAttrs returns an attribute set whose names are the intersection of both arguments' attribute names, with the attribute @@ -128,7 +146,12 @@ and populated these with an existing set of attributes. This is our simple implementation of callPackage: - + nix-repl> callPackage = set: f: f (builtins.intersectAttrs (builtins.functionArgs f) set) +nix-repl> callPackage values add +8 +nix-repl> with values; add { inherit a b; } +8 + Let's dissect the above snippet: @@ -170,7 +193,12 @@ We may not want to always call functions with values taken from the big set. Thus, we add a third parameter which takes a set of overrides: - + nix-repl> callPackage = set: f: overrides: f ((builtins.intersectAttrs (builtins.functionArgs f) set) // overrides) +nix-repl> callPackage values add { } +8 +nix-repl> callPackage values add { b = 12; } +15 + Apart from the increasing number of parentheses, it should be clear that we simply take a set union between the default arguments and the overriding set. @@ -182,7 +210,24 @@ Given our callPackages, we can simplify the repository expression in default.nix: - + let + nixpkgs = import <nixpkgs> { }; + allPkgs = nixpkgs // pkgs; + callPackage = + path: overrides: + let + f = import path; + in + f ((builtins.intersectAttrs (builtins.functionArgs f) allPkgs) // overrides); + pkgs = with nixpkgs; { + mkDerivation = import ./autotools.nix nixpkgs; + hello = callPackage ./hello.nix { }; + graphviz = callPackage ./graphviz.nix { }; + graphvizCore = callPackage ./graphviz.nix { gdSupport = false; }; + }; +in +pkgs + Let's examine this in detail: diff --git a/pills/13/callpackage-function-call.txt b/pills/13/callpackage-function-call.txt deleted file mode 100644 index 89c9f75..0000000 --- a/pills/13/callpackage-function-call.txt +++ /dev/null @@ -1,4 +0,0 @@ -{ - lib1 = callPackage package1.nix { }; - program2 = callPackage package2.nix { someoverride = overriddenDerivation; }; -} diff --git a/pills/13/callpackage-function-overrides.txt b/pills/13/callpackage-function-overrides.txt deleted file mode 100644 index aa77625..0000000 --- a/pills/13/callpackage-function-overrides.txt +++ /dev/null @@ -1,5 +0,0 @@ -nix-repl> callPackage = set: f: overrides: f ((builtins.intersectAttrs (builtins.functionArgs f) set) // overrides) -nix-repl> callPackage values add { } -8 -nix-repl> callPackage values add { b = 12; } -15 diff --git a/pills/13/callpackage-function.txt b/pills/13/callpackage-function.txt deleted file mode 100644 index 8bfbe2f..0000000 --- a/pills/13/callpackage-function.txt +++ /dev/null @@ -1,5 +0,0 @@ -nix-repl> callPackage = set: f: f (builtins.intersectAttrs (builtins.functionArgs f) set) -nix-repl> callPackage values add -8 -nix-repl> with values; add { inherit a b; } -8 diff --git a/pills/13/callpackage-usage.txt b/pills/13/callpackage-usage.txt deleted file mode 100644 index 43ad9e7..0000000 --- a/pills/13/callpackage-usage.txt +++ /dev/null @@ -1,17 +0,0 @@ -let - nixpkgs = import { }; - allPkgs = nixpkgs // pkgs; - callPackage = - path: overrides: - let - f = import path; - in - f ((builtins.intersectAttrs (builtins.functionArgs f) allPkgs) // overrides); - pkgs = with nixpkgs; { - mkDerivation = import ./autotools.nix nixpkgs; - hello = callPackage ./hello.nix { }; - graphviz = callPackage ./graphviz.nix { }; - graphvizCore = callPackage ./graphviz.nix { gdSupport = false; }; - }; -in -pkgs diff --git a/pills/13/get-args-function.txt b/pills/13/get-args-function.txt deleted file mode 100644 index c4f22cd..0000000 --- a/pills/13/get-args-function.txt +++ /dev/null @@ -1,3 +0,0 @@ -nix-repl> add = { a ? 3, b }: a+b -nix-repl> builtins.functionArgs add -{ a = true; b = false; } diff --git a/pills/13/intersect-attr-values.txt b/pills/13/intersect-attr-values.txt deleted file mode 100644 index de39d7a..0000000 --- a/pills/13/intersect-attr-values.txt +++ /dev/null @@ -1,5 +0,0 @@ -nix-repl> values = { a = 3; b = 5; c = 10; } -nix-repl> builtins.intersectAttrs values (builtins.functionArgs add) -{ a = true; b = false; } -nix-repl> builtins.intersectAttrs (builtins.functionArgs add) values -{ a = 3; b = 5; } diff --git a/pills/13/package-derivation.txt b/pills/13/package-derivation.txt deleted file mode 100644 index 1d34fbb..0000000 --- a/pills/13/package-derivation.txt +++ /dev/null @@ -1,2 +0,0 @@ -{ input1, input2, ... }: -... diff --git a/pills/13/repository-derivation.txt b/pills/13/repository-derivation.txt deleted file mode 100644 index 5ce1460..0000000 --- a/pills/13/repository-derivation.txt +++ /dev/null @@ -1,4 +0,0 @@ -rec { - lib1 = import package1.nix { inherit input1 input2; }; - program2 = import package2.nix { inherit inputX inputY lib1; }; -} diff --git a/pills/14-override-design-pattern.xml b/pills/14-override-design-pattern.xml index 06db75f..9de254d 100644 --- a/pills/14-override-design-pattern.xml +++ b/pills/14-override-design-pattern.xml @@ -65,7 +65,18 @@ gd version, we would have to repeat most of the above plus specifying an alternative gd: - + { + mygraphviz = import ./graphviz.nix { + inherit + mkDerivation + fontconfig + libjpeg + bzip2 + ; + gd = customgd; + }; +} + That's hard to maintain. Using callPackage would be easier: @@ -104,7 +115,15 @@ We will put this function in a lib.nix: - + { + makeOverridable = + f: origArgs: + let + origRes = f origArgs; + in + origRes // { override = newArgs: f (origArgs // newArgs); }; +} + makeOverridable takes a function and a set of original arguments. It returns the original returned set, plus a new override attribute. @@ -118,7 +137,18 @@ Let's try it with nix repl: - + $ nix repl +nix-repl> :l lib.nix +Added 1 variables. +nix-repl> f = { a, b }: { result = a+b; } +nix-repl> f { a = 3; b = 5; } +{ result = 8; } +nix-repl> res = makeOverridable f { a = 3; b = 5; } +nix-repl> res +{ override = «lambda»; result = 8; } +nix-repl> res.override { a = 10; } +{ result = 15; } + Note that, as we specified above, the function f does not return the plain sum. Instead, it returns a set with the sum bound to the name @@ -142,7 +172,15 @@ The solution is simple: the .override function should make the result overridable again: - + rec { + makeOverridable = + f: origArgs: + let + origRes = f origArgs; + in + origRes // { override = newArgs: makeOverridable f (origArgs // newArgs); }; +} + Please note the rec keyword. It's necessary so that we can refer to makeOverridable from makeOverridable itself. @@ -150,7 +188,16 @@ Now let's try overriding twice: - + nix-repl> :l lib.nix +Added 1 variables. +nix-repl> f = { a, b }: { result = a+b; } +nix-repl> res = makeOverridable f { a = 3; b = 5; } +nix-repl> res2 = res.override { a = 10; } +nix-repl> res2 +{ override = «lambda»; result = 15; } +nix-repl> res2.override { b = 20; } +{ override = «lambda»; result = 30; } + Success! The result is 30 (as expected) because a is overridden to 10 in the first override, and b is overridden to 20 in the diff --git a/pills/14/make-overridable-lib.txt b/pills/14/make-overridable-lib.txt deleted file mode 100644 index 9b60b67..0000000 --- a/pills/14/make-overridable-lib.txt +++ /dev/null @@ -1,8 +0,0 @@ -{ - makeOverridable = - f: origArgs: - let - origRes = f origArgs; - in - origRes // { override = newArgs: f (origArgs // newArgs); }; -} diff --git a/pills/14/mygraphviz.txt b/pills/14/mygraphviz.txt deleted file mode 100644 index f0eca70..0000000 --- a/pills/14/mygraphviz.txt +++ /dev/null @@ -1,11 +0,0 @@ -{ - mygraphviz = import ./graphviz.nix { - inherit - mkDerivation - fontconfig - libjpeg - bzip2 - ; - gd = customgd; - }; -} diff --git a/pills/14/nix-repl-make-overridable-test.txt b/pills/14/nix-repl-make-overridable-test.txt deleted file mode 100644 index 230c7ea..0000000 --- a/pills/14/nix-repl-make-overridable-test.txt +++ /dev/null @@ -1,11 +0,0 @@ -$ nix repl -nix-repl> :l lib.nix -Added 1 variables. -nix-repl> f = { a, b }: { result = a+b; } -nix-repl> f { a = 3; b = 5; } -{ result = 8; } -nix-repl> res = makeOverridable f { a = 3; b = 5; } -nix-repl> res -{ override = «lambda»; result = 8; } -nix-repl> res.override { a = 10; } -{ result = 15; } diff --git a/pills/14/nix-repl-make-overridable-twice.txt b/pills/14/nix-repl-make-overridable-twice.txt deleted file mode 100644 index 0e64dab..0000000 --- a/pills/14/nix-repl-make-overridable-twice.txt +++ /dev/null @@ -1,9 +0,0 @@ -nix-repl> :l lib.nix -Added 1 variables. -nix-repl> f = { a, b }: { result = a+b; } -nix-repl> res = makeOverridable f { a = 3; b = 5; } -nix-repl> res2 = res.override { a = 10; } -nix-repl> res2 -{ override = «lambda»; result = 15; } -nix-repl> res2.override { b = 20; } -{ override = «lambda»; result = 30; } diff --git a/pills/14/rec-make-overridable.txt b/pills/14/rec-make-overridable.txt deleted file mode 100644 index f2869a8..0000000 --- a/pills/14/rec-make-overridable.txt +++ /dev/null @@ -1,8 +0,0 @@ -rec { - makeOverridable = - f: origArgs: - let - origRes = f origArgs; - in - origRes // { override = newArgs: makeOverridable f (origArgs // newArgs); }; -} diff --git a/pills/15-nix-search-paths.xml b/pills/15-nix-search-paths.xml index b5d110f..cffd205 100644 --- a/pills/15-nix-search-paths.xml +++ b/pills/15-nix-search-paths.xml @@ -37,14 +37,24 @@ It's useless from a nix view point, but I think it's useful for your own understanding. Let's use PATH itself as NIX_PATH, and try to locate ping (or another binary if you don't have it). - + $ nix-instantiate --eval -E '<ping>' +error: file `ping' was not found in the Nix search path (add it using $NIX_PATH or -I) +$ NIX_PATH=$PATH nix-instantiate --eval -E '<ping>' +/bin/ping +$ nix-instantiate -I /bin --eval -E '<ping>' +/bin/ping + Great. At first attempt nix obviously said could not be found anywhere in the search path. Note that the -I option accepts a single directory. Paths added with -I take precedence over NIX_PATH. The NIX_PATH also accepts a different yet very handy syntax: "somename=somepath". That is, instead of searching inside a directory for a name, we specify exactly the value of that name. - + $ NIX_PATH="ping=/bin/ping" nix-instantiate --eval -E '<ping>' +/bin/ping +$ NIX_PATH="ping=/bin/foo" nix-instantiate --eval -E '<ping>' +error: file `ping' was not found in the Nix search path (add it using $N + Note in the second case how Nix checks whether the path exists or not. @@ -54,7 +64,11 @@ You are out of curiosity, right? - + $ nix-instantiate --eval -E '<nixpkgs>' +/home/nix/.nix-defexpr/channels/nixpkgs +$ echo $NIX_PATH +nixpkgs=/home/nix/.nix-defexpr/channels/nixpkgs + You may have a different path, depending on how you added channels etc.. Anyway that's the whole point. The <nixpkgs> stranger that we used in our nix expressions, is referring to a path in the filesystem specified by NIX_PATH. @@ -70,7 +84,11 @@ Let's define a path for our repository, then! Let's say all the default.nix, graphviz.nix etc. are under /home/nix/mypkgs: - + $ export NIX_PATH=mypkgs=/home/nix/mypkgs:$NIX_PATH +$ nix-instantiate --eval '<mypkgs>' +{ graphviz = <code>; graphvizCore = <code>; hello = <code>; mkDerivation = <code>; } + + Yes, nix-build also accepts paths with angular brackets. We first evaluate the whole repository (default.nix) and then pick the graphviz attribute. @@ -92,15 +110,26 @@ In order to specify an alternative to ~/.nix-defexpr it's possible to use the -f option: - + $ nix-env -f '<mypkgs>' -i graphviz +warning: there are multiple derivations named `graphviz'; using the first one +replacing old `graphviz' +installing `graphviz' + Oh why did it say there's another derivation named graphviz? Because both graphviz and graphvizCore attributes in our repository have the name "graphviz" for the derivation: - + $ nix-env -f '<mypkgs>' -qaP +graphviz graphviz +graphvizCore graphviz +hello hello + By default nix-env parses all derivations and uses the derivation names to interpret the command line. So in this case "graphviz" matched two derivations. Alternatively, like for nix-build, one can use -A to specify an attribute name instead of a derivation name: - + $ nix-env -f '<mypkgs>' -i -A graphviz +replacing old `graphviz' +installing `graphviz' + This form, other than being more precise, it's also faster because nix-env does not have to parse all the derivations. diff --git a/pills/15/mypkgs-graphviz-multiple.txt b/pills/15/mypkgs-graphviz-multiple.txt deleted file mode 100644 index a541991..0000000 --- a/pills/15/mypkgs-graphviz-multiple.txt +++ /dev/null @@ -1,4 +0,0 @@ -$ nix-env -f '' -i graphviz -warning: there are multiple derivations named `graphviz'; using the first one -replacing old `graphviz' -installing `graphviz' diff --git a/pills/15/mypkgs-install-attr-graphviz.txt b/pills/15/mypkgs-install-attr-graphviz.txt deleted file mode 100644 index 55bc05e..0000000 --- a/pills/15/mypkgs-install-attr-graphviz.txt +++ /dev/null @@ -1,3 +0,0 @@ -$ nix-env -f '' -i -A graphviz -replacing old `graphviz' -installing `graphviz' diff --git a/pills/15/mypkgs-path.txt b/pills/15/mypkgs-path.txt deleted file mode 100644 index 26d07be..0000000 --- a/pills/15/mypkgs-path.txt +++ /dev/null @@ -1,4 +0,0 @@ -$ export NIX_PATH=mypkgs=/home/nix/mypkgs:$NIX_PATH -$ nix-instantiate --eval '' -{ graphviz = ; graphvizCore = ; hello = ; mkDerivation = ; } - diff --git a/pills/15/mypkgs-query-all.txt b/pills/15/mypkgs-query-all.txt deleted file mode 100644 index 0a9f65c..0000000 --- a/pills/15/mypkgs-query-all.txt +++ /dev/null @@ -1,4 +0,0 @@ -$ nix-env -f '' -qaP -graphviz graphviz -graphvizCore graphviz -hello hello diff --git a/pills/15/nix-instantiate-ping.txt b/pills/15/nix-instantiate-ping.txt deleted file mode 100644 index d39b6b4..0000000 --- a/pills/15/nix-instantiate-ping.txt +++ /dev/null @@ -1,6 +0,0 @@ -$ nix-instantiate --eval -E '' -error: file `ping' was not found in the Nix search path (add it using $NIX_PATH or -I) -$ NIX_PATH=$PATH nix-instantiate --eval -E '' -/bin/ping -$ nix-instantiate -I /bin --eval -E '' -/bin/ping diff --git a/pills/15/nixpkgs-path.txt b/pills/15/nixpkgs-path.txt deleted file mode 100644 index 4e09280..0000000 --- a/pills/15/nixpkgs-path.txt +++ /dev/null @@ -1,4 +0,0 @@ -$ nix-instantiate --eval -E '' -/home/nix/.nix-defexpr/channels/nixpkgs -$ echo $NIX_PATH -nixpkgs=/home/nix/.nix-defexpr/channels/nixpkgs diff --git a/pills/15/ping-custom-path.txt b/pills/15/ping-custom-path.txt deleted file mode 100644 index 4298d0e..0000000 --- a/pills/15/ping-custom-path.txt +++ /dev/null @@ -1,4 +0,0 @@ -$ NIX_PATH="ping=/bin/ping" nix-instantiate --eval -E '' -/bin/ping -$ NIX_PATH="ping=/bin/foo" nix-instantiate --eval -E '' -error: file `ping' was not found in the Nix search path (add it using $N diff --git a/pills/16-nixpkgs-parameters.xml b/pills/16-nixpkgs-parameters.xml index 081a316..47d40aa 100644 --- a/pills/16-nixpkgs-parameters.xml +++ b/pills/16-nixpkgs-parameters.xml @@ -48,7 +48,11 @@ myrelease.nix: - + { system ? builtins.currentSystem }: + +let pkgs = import <nixpkgs> { inherit system; }; +... + Why is it useful? With this parameter it's very easy to select a set of packages for a particular system. For example: @@ -74,7 +78,14 @@ The config is available in the resulting repository: - + $ nix repl +nix-repl> pkgs = import <nixpkgs> {} +nix-repl> pkgs.config +{ } +nix-repl> pkgs = import <nixpkgs> { config = { foo = "bar"; }; } +nix-repl> pkgs.config +{ foo = "bar"; } + What attributes go in config is a matter of convenience and conventions. @@ -97,7 +108,10 @@ For example you can nix-build the .nix file below: - + { pkgs ? import <nixpkgs> {} }: + +pkgs.psmisc + Nix is able to call the function because the pkgs parameter has a default value. This allows you to pass a different value for pkgs using the --arg option. diff --git a/pills/16/config-foo-bar.txt b/pills/16/config-foo-bar.txt deleted file mode 100644 index f58c281..0000000 --- a/pills/16/config-foo-bar.txt +++ /dev/null @@ -1,7 +0,0 @@ -$ nix repl -nix-repl> pkgs = import {} -nix-repl> pkgs.config -{ } -nix-repl> pkgs = import { config = { foo = "bar"; }; } -nix-repl> pkgs.config -{ foo = "bar"; } diff --git a/pills/16/myrelease-nix.txt b/pills/16/myrelease-nix.txt deleted file mode 100644 index 993ce5f..0000000 --- a/pills/16/myrelease-nix.txt +++ /dev/null @@ -1,4 +0,0 @@ -{ system ? builtins.currentSystem }: - -let pkgs = import { inherit system; }; -... diff --git a/pills/16/pkgs-psmisc.txt b/pills/16/pkgs-psmisc.txt deleted file mode 100644 index fc7f650..0000000 --- a/pills/16/pkgs-psmisc.txt +++ /dev/null @@ -1,3 +0,0 @@ -{ pkgs ? import {} }: - -pkgs.psmisc diff --git a/pills/17-nixpkgs-overriding-packages.xml b/pills/17-nixpkgs-overriding-packages.xml index 5d8fd36..3b5ae17 100644 --- a/pills/17-nixpkgs-overriding-packages.xml +++ b/pills/17-nixpkgs-overriding-packages.xml @@ -22,7 +22,11 @@ Take for example graphviz. It has an input parameter xorg. If it's null, then graphviz will build without X support. - + $ nix repl +nix-repl> :l <nixpkgs> +Added 4360 variables. +nix-repl> :b graphviz.override { withXorg = false; } + This will build graphviz without X support, it's as simple as that. @@ -36,7 +40,10 @@ ...you could do something like this: - + pkgs = import <nixpkgs> {}; +pkgs.graphviz = pkgs.graphviz.override { withXorg = false; }; +build(pkgs.P) + Given pkgs.P depends on pkgs.graphviz, it's easy to build P with the replaced graphviz. In a pure functional language it's not that easy because you can assign to variables only once. @@ -49,14 +56,27 @@ Follows the definition of fixed point in nixpkgs: - + { + # Take a function and evaluate it with its own returned value. + fix = + f: + let + result = f result; + in + result; +} + It's a function that accepts a function f, calls f result on the result just returned by f result and returns it. In other words it's f(f(f(.... At first sight, it's an infinite loop. With lazy evaluation it isn't, because the call is done only when needed. - + nix-repl> fix = f: let result = f result; in result +nix-repl> pkgs = self: { a = 3; b = 4; c = self.a+self.b; } +nix-repl> fix pkgs +{ a = 3; b = 4; c = 7; } + Without the rec keyword, we were able to refer to a and b of the same set. @@ -77,7 +97,12 @@ Given that self.a and self.b refer to the passed set and not to the literal set in the function, we're able to override both a and b and get a new value for c: - + nix-repl> overrides = { a = 1; b = 2; } +nix-repl> let newpkgs = pkgs (newpkgs // overrides); in newpkgs +{ a = 3; b = 4; c = 3; } +nix-repl> let newpkgs = pkgs (newpkgs // overrides); in newpkgs // overrides +{ a = 1; b = 2; c = 3; } + In the first case we computed pkgs with the overrides, in the second case we also included the overridden attributes in the result. @@ -95,11 +120,21 @@ Create a config.nix file like this somewhere: - + { + packageOverrides = pkgs: { + graphviz = pkgs.graphviz.override { + # disable xorg support + withXorg = false; + }; + }; +} + Now we can build e.g. asciidoc-full and it will automatically use the overridden graphviz: - + nix-repl> pkgs = import <nixpkgs> { config = import ./config.nix; } +nix-repl> :b pkgs.asciidoc-full + Note how we pass the config with packageOverrides when importing nixpkgs. Then pkgs.asciidoc-full is a derivation that has graphviz input (pkgs.asciidoc is the lighter version and doesn't use graphviz at all). diff --git a/pills/17/build-asciidoc-graphviz-override.txt b/pills/17/build-asciidoc-graphviz-override.txt deleted file mode 100644 index c1c883d..0000000 --- a/pills/17/build-asciidoc-graphviz-override.txt +++ /dev/null @@ -1,2 +0,0 @@ -nix-repl> pkgs = import { config = import ./config.nix; } -nix-repl> :b pkgs.asciidoc-full diff --git a/pills/17/config-nix.txt b/pills/17/config-nix.txt deleted file mode 100644 index 078afad..0000000 --- a/pills/17/config-nix.txt +++ /dev/null @@ -1,8 +0,0 @@ -{ - packageOverrides = pkgs: { - graphviz = pkgs.graphviz.override { - # disable xorg support - withXorg = false; - }; - }; -} diff --git a/pills/17/fix-function.txt b/pills/17/fix-function.txt deleted file mode 100644 index d1a3b5c..0000000 --- a/pills/17/fix-function.txt +++ /dev/null @@ -1,9 +0,0 @@ -{ - # Take a function and evaluate it with its own returned value. - fix = - f: - let - result = f result; - in - result; -} diff --git a/pills/17/fix-pkgs-function.txt b/pills/17/fix-pkgs-function.txt deleted file mode 100644 index 4dc34f7..0000000 --- a/pills/17/fix-pkgs-function.txt +++ /dev/null @@ -1,4 +0,0 @@ -nix-repl> fix = f: let result = f result; in result -nix-repl> pkgs = self: { a = 3; b = 4; c = self.a+self.b; } -nix-repl> fix pkgs -{ a = 3; b = 4; c = 7; } diff --git a/pills/17/graphviz-override.txt b/pills/17/graphviz-override.txt deleted file mode 100644 index 5ecc6c2..0000000 --- a/pills/17/graphviz-override.txt +++ /dev/null @@ -1,4 +0,0 @@ -$ nix repl -nix-repl> :l -Added 4360 variables. -nix-repl> :b graphviz.override { withXorg = false; } diff --git a/pills/17/newpkgs-override-set.txt b/pills/17/newpkgs-override-set.txt deleted file mode 100644 index 50210c9..0000000 --- a/pills/17/newpkgs-override-set.txt +++ /dev/null @@ -1,5 +0,0 @@ -nix-repl> overrides = { a = 1; b = 2; } -nix-repl> let newpkgs = pkgs (newpkgs // overrides); in newpkgs -{ a = 3; b = 4; c = 3; } -nix-repl> let newpkgs = pkgs (newpkgs // overrides); in newpkgs // overrides -{ a = 1; b = 2; c = 3; } diff --git a/pills/17/p-graphviz-override.txt b/pills/17/p-graphviz-override.txt deleted file mode 100644 index 91984ac..0000000 --- a/pills/17/p-graphviz-override.txt +++ /dev/null @@ -1,3 +0,0 @@ -pkgs = import {}; -pkgs.graphviz = pkgs.graphviz.override { withXorg = false; }; -build(pkgs.P) diff --git a/pills/18-nix-store-paths.xml b/pills/18-nix-store-paths.xml index ee61666..ed50d30 100644 --- a/pills/18-nix-store-paths.xml +++ b/pills/18-nix-store-paths.xml @@ -23,11 +23,37 @@ I remind you, the simplest derivation you can write has a name, a builder and the system: - + $ nix repl +nix-repl> derivation { system = "x86_64-linux"; builder = ./myfile; name = "foo"; } +«derivation /nix/store/y4h73bmrc9ii5bxg6i7ck6hsf5gqv8ck-foo.drv» + Now inspect the .drv to see where is ./myfile being stored: - + <screen xmlns="http://docbook.org/ns/docbook"><prompt>$ </prompt><userinput>nix derivation show /nix/store/y4h73bmrc9ii5bxg6i7ck6hsf5gqv8ck-<emphasis>foo.drv</emphasis></userinput> +<computeroutput>{ + "/nix/store/y4h73bmrc9ii5bxg6i7ck6hsf5gqv8ck-foo.drv": { + "outputs": { + "out": { + "path": "/nix/store/hs0yi5n5nw6micqhy8l1igkbhqdkzqa1-foo" + } + }, + "inputSrcs": [ + "/nix/store/xv2iccirbrvklck36f1g7vldn5v58vck-myfile" + ], + "inputDrvs": {}, + "platform": "x86_64-linux", + "builder": "/nix/store/xv2iccirbrvklck36f1g7vldn5v58vck-myfile", + "args": [], + "env": { + "builder": "/nix/store/xv2iccirbrvklck36f1g7vldn5v58vck-myfile", + "name": "foo", + "out": "/nix/store/hs0yi5n5nw6micqhy8l1igkbhqdkzqa1-foo", + "system": "x86_64-linux" + } + } +}</computeroutput></screen> + Great, how did nix decide to use xv2iccirbrvklck36f1g7vldn5v58vck ? Keep looking at the nix comments. @@ -40,11 +66,15 @@ The comments tell us to first compute the sha256 of the NAR serialization of the file. Can be done in two ways: - + $ nix-hash --type sha256 myfile +2bfef67de873c54551d884fdab3055d84d573e654efa79db3c0d7b98883f9ee3 + Or: - + $ nix-store --dump myfile|sha256sum +2bfef67de873c54551d884fdab3055d84d573e654efa79db3c0d7b98883f9ee3 + In general, Nix understands two contents: flat for regular files, or recursive for NAR serializations which can be anything. @@ -61,7 +91,9 @@ Finally the comments tell us to compute the base-32 representation of the first 160 bits (truncation) of a sha256 of the above string: - + $ nix-hash --type sha256 --truncate --base32 --flat myfile.str +xv2iccirbrvklck36f1g7vldn5v58vck +
@@ -75,11 +107,18 @@ At the time nix computes the out path, the .drv contains an empty string for each out path. So what we do is getting our .drv and replacing the out path with an empty string: - + $ cp -f /nix/store/y4h73bmrc9ii5bxg6i7ck6hsf5gqv8ck-foo.drv myout.drv +$ sed -i 's,/nix/store/hs0yi5n5nw6micqhy8l1igkbhqdkzqa1-foo,,g' myout.drv + The myout.drv is the .drv state in which nix is when computing the out path for our derivation: - + $ sha256sum myout.drv +1bdc41b9649a0d59f270a92d69ce6b5af0bc82b46cb9d9441ebc6620665f40b5 myout.drv +$ echo -n "output:out:sha256:1bdc41b9649a0d59f270a92d69ce6b5af0bc82b46cb9d9441ebc6620665f40b5:/nix/store:foo" > myout.str +$ nix-hash --type sha256 --truncate --base32 --flat myout.str +hs0yi5n5nw6micqhy8l1igkbhqdkzqa1 + Then nix puts that out path in the .drv, and that's it. @@ -104,22 +143,46 @@ Let's say our builder should create a file whose contents is mycontent: - + $ echo mycontent > myfile +$ sha256sum myfile +f3f3c4763037e059b4d834eaf68595bbc02ba19f6d2a500dce06d124e2cd99bb myfile +nix-repl> derivation { name = "bar"; system = "x86_64-linux"; builder = "none"; outputHashMode = "flat"; outputHashAlgo = "sha256"; outputHash = "f3f3c4763037e059b4d834eaf68595bbc02ba19f6d2a500dce06d124e2cd99bb"; } +«derivation /nix/store/ymsf5zcqr9wlkkqdjwhqllgwa97rff5i-bar.drv» + Inspect the .drv and see that it also stored the fact that it's a fixed-output derivation with sha256 algorithm, compared to the previous examples: - + <screen xmlns="http://docbook.org/ns/docbook"><prompt>$ </prompt><userinput>nix derivation show /nix/store/ymsf5zcqr9wlkkqdjwhqllgwa97rff5i-<emphasis>bar.drv</emphasis></userinput> +<computeroutput>{ + "/nix/store/ymsf5zcqr9wlkkqdjwhqllgwa97rff5i-bar.drv": { + "outputs": { + "out": { + "path": "/nix/store/a00d5f71k0vp5a6klkls0mvr1f7sx6ch-bar", + "hashAlgo": "sha256", + "hash": "f3f3c4763037e059b4d834eaf68595bbc02ba19f6d2a500dce06d124e2cd99bb" + } + }, +<emphasis>[...]</emphasis> +}</computeroutput></screen> + It doesn't matter which input derivations are being used, the final out path must only depend on the declared hash. What nix does is to create an intermediate string representation of the fixed-output content: - + $ echo -n "fixed:out:sha256:f3f3c4763037e059b4d834eaf68595bbc02ba19f6d2a500dce06d124e2cd99bb:" > mycontent.str +$ sha256sum mycontent.str +423e6fdef56d53251c5939359c375bf21ea07aaa8d89ca5798fb374dbcfd7639 myfile.str + + Then proceed as it was a normal derivation output path: - + $ echo -n "output:out:sha256:423e6fdef56d53251c5939359c375bf21ea07aaa8d89ca5798fb374dbcfd7639:/nix/store:bar" > myfile.str +$ nix-hash --type sha256 --truncate --base32 --flat myfile.str +a00d5f71k0vp5a6klkls0mvr1f7sx6ch + Hence, the store path only depends on the declared fixed-output hash. diff --git a/pills/18/bar-derivation.xml b/pills/18/bar-derivation.xml deleted file mode 100644 index aeb5217..0000000 --- a/pills/18/bar-derivation.xml +++ /dev/null @@ -1,12 +0,0 @@ -$ nix derivation show /nix/store/ymsf5zcqr9wlkkqdjwhqllgwa97rff5i-bar.drv -{ - "/nix/store/ymsf5zcqr9wlkkqdjwhqllgwa97rff5i-bar.drv": { - "outputs": { - "out": { - "path": "/nix/store/a00d5f71k0vp5a6klkls0mvr1f7sx6ch-bar", - "hashAlgo": "sha256", - "hash": "f3f3c4763037e059b4d834eaf68595bbc02ba19f6d2a500dce06d124e2cd99bb" - } - }, -[...] -} diff --git a/pills/18/derivation-simple-content.xml b/pills/18/derivation-simple-content.xml deleted file mode 100644 index 9be1bad..0000000 --- a/pills/18/derivation-simple-content.xml +++ /dev/null @@ -1,23 +0,0 @@ -$ nix derivation show /nix/store/y4h73bmrc9ii5bxg6i7ck6hsf5gqv8ck-foo.drv -{ - "/nix/store/y4h73bmrc9ii5bxg6i7ck6hsf5gqv8ck-foo.drv": { - "outputs": { - "out": { - "path": "/nix/store/hs0yi5n5nw6micqhy8l1igkbhqdkzqa1-foo" - } - }, - "inputSrcs": [ - "/nix/store/xv2iccirbrvklck36f1g7vldn5v58vck-myfile" - ], - "inputDrvs": {}, - "platform": "x86_64-linux", - "builder": "/nix/store/xv2iccirbrvklck36f1g7vldn5v58vck-myfile", - "args": [], - "env": { - "builder": "/nix/store/xv2iccirbrvklck36f1g7vldn5v58vck-myfile", - "name": "foo", - "out": "/nix/store/hs0yi5n5nw6micqhy8l1igkbhqdkzqa1-foo", - "system": "x86_64-linux" - } - } -} diff --git a/pills/18/derivation-simple.txt b/pills/18/derivation-simple.txt deleted file mode 100644 index 716e921..0000000 --- a/pills/18/derivation-simple.txt +++ /dev/null @@ -1,3 +0,0 @@ -$ nix repl -nix-repl> derivation { system = "x86_64-linux"; builder = ./myfile; name = "foo"; } -«derivation /nix/store/y4h73bmrc9ii5bxg6i7ck6hsf5gqv8ck-foo.drv» diff --git a/pills/18/mycontent-string-representation.txt b/pills/18/mycontent-string-representation.txt deleted file mode 100644 index d30da2d..0000000 --- a/pills/18/mycontent-string-representation.txt +++ /dev/null @@ -1,4 +0,0 @@ -$ echo -n "fixed:out:sha256:f3f3c4763037e059b4d834eaf68595bbc02ba19f6d2a500dce06d124e2cd99bb:" > mycontent.str -$ sha256sum mycontent.str -423e6fdef56d53251c5939359c375bf21ea07aaa8d89ca5798fb374dbcfd7639 myfile.str - diff --git a/pills/18/mycontent.txt b/pills/18/mycontent.txt deleted file mode 100644 index 7d13e80..0000000 --- a/pills/18/mycontent.txt +++ /dev/null @@ -1,5 +0,0 @@ -$ echo mycontent > myfile -$ sha256sum myfile -f3f3c4763037e059b4d834eaf68595bbc02ba19f6d2a500dce06d124e2cd99bb myfile -nix-repl> derivation { name = "bar"; system = "x86_64-linux"; builder = "none"; outputHashMode = "flat"; outputHashAlgo = "sha256"; outputHash = "f3f3c4763037e059b4d834eaf68595bbc02ba19f6d2a500dce06d124e2cd99bb"; } -«derivation /nix/store/ymsf5zcqr9wlkkqdjwhqllgwa97rff5i-bar.drv» diff --git a/pills/18/myfile-final-hash.txt b/pills/18/myfile-final-hash.txt deleted file mode 100644 index a5b6c10..0000000 --- a/pills/18/myfile-final-hash.txt +++ /dev/null @@ -1,2 +0,0 @@ -$ nix-hash --type sha256 --truncate --base32 --flat myfile.str -xv2iccirbrvklck36f1g7vldn5v58vck diff --git a/pills/18/myfile-hash-alternate.txt b/pills/18/myfile-hash-alternate.txt deleted file mode 100644 index a5dca21..0000000 --- a/pills/18/myfile-hash-alternate.txt +++ /dev/null @@ -1,2 +0,0 @@ -$ nix-store --dump myfile|sha256sum -2bfef67de873c54551d884fdab3055d84d573e654efa79db3c0d7b98883f9ee3 diff --git a/pills/18/myfile-hash.txt b/pills/18/myfile-hash.txt deleted file mode 100644 index cbaf8f1..0000000 --- a/pills/18/myfile-hash.txt +++ /dev/null @@ -1,2 +0,0 @@ -$ nix-hash --type sha256 myfile -2bfef67de873c54551d884fdab3055d84d573e654efa79db3c0d7b98883f9ee3 diff --git a/pills/18/myfile-string-hash.txt b/pills/18/myfile-string-hash.txt deleted file mode 100644 index e7a5d09..0000000 --- a/pills/18/myfile-string-hash.txt +++ /dev/null @@ -1,3 +0,0 @@ -$ echo -n "output:out:sha256:423e6fdef56d53251c5939359c375bf21ea07aaa8d89ca5798fb374dbcfd7639:/nix/store:bar" > myfile.str -$ nix-hash --type sha256 --truncate --base32 --flat myfile.str -a00d5f71k0vp5a6klkls0mvr1f7sx6ch diff --git a/pills/18/myout-drv-hash.txt b/pills/18/myout-drv-hash.txt deleted file mode 100644 index fa9d2a4..0000000 --- a/pills/18/myout-drv-hash.txt +++ /dev/null @@ -1,5 +0,0 @@ -$ sha256sum myout.drv -1bdc41b9649a0d59f270a92d69ce6b5af0bc82b46cb9d9441ebc6620665f40b5 myout.drv -$ echo -n "output:out:sha256:1bdc41b9649a0d59f270a92d69ce6b5af0bc82b46cb9d9441ebc6620665f40b5:/nix/store:foo" > myout.str -$ nix-hash --type sha256 --truncate --base32 --flat myout.str -hs0yi5n5nw6micqhy8l1igkbhqdkzqa1 diff --git a/pills/18/output-path-replace-empty.txt b/pills/18/output-path-replace-empty.txt deleted file mode 100644 index 6a7eebf..0000000 --- a/pills/18/output-path-replace-empty.txt +++ /dev/null @@ -1,2 +0,0 @@ -$ cp -f /nix/store/y4h73bmrc9ii5bxg6i7ck6hsf5gqv8ck-foo.drv myout.drv -$ sed -i 's,/nix/store/hs0yi5n5nw6micqhy8l1igkbhqdkzqa1-foo,,g' myout.drv diff --git a/pills/19-fundamentals-of-stdenv.xml b/pills/19-fundamentals-of-stdenv.xml index cea606a..7410893 100644 --- a/pills/19-fundamentals-of-stdenv.xml +++ b/pills/19-fundamentals-of-stdenv.xml @@ -24,7 +24,15 @@ First of all, stdenv is a derivation, and it's a very simple one: - + $ nix-build '<nixpkgs>' -A stdenv +/nix/store/k4jklkcag4zq4xkqhkpy156mgfm34ipn-stdenv +$ ls -R result/ +result/: +nix-support/ setup + +result/nix-support: +propagated-user-env-packages + It has just two files: /setup and /nix-support/propagated-user-env-packages. Don't worry about the latter. It's empty, in fact. The important file is /setup. @@ -33,12 +41,23 @@ How can this simple derivation pull in all of the toolchain and basic tools needed to compile packages? Let's look at the runtime dependencies: - + $ nix-store -q --references result +/nix/store/3a45nb37s0ndljp68228snsqr3qsyp96-bzip2-1.0.6 +/nix/store/a457ywa1haa0sgr9g7a1pgldrg3s798d-coreutils-8.24 +/nix/store/zmd4jk4db5lgxb8l93mhkvr3x92g2sx2-bash-4.3-p39 +/nix/store/47sfpm2qclpqvrzijizimk4md1739b1b-gcc-wrapper-4.9.3 +... + How can it be? The package must be referring to those other packages somehow. In fact, they are hardcoded in the /setup file: - + $ head result/setup +export SHELL=/nix/store/zmd4jk4db5lgxb8l93mhkvr3x92g2sx2-bash-4.3-p39/bin/bash +initialPath="/nix/store/a457ywa1haa0sgr9g7a1pgldrg3s798d-coreutils-8.24 ..." +defaultNativeBuildInputs="/nix/store/sgwq15xg00xnm435gjicspm048rqg9y6-patchelf-0.8 ..." + +
The setup file @@ -72,7 +91,16 @@ How to use this file? Like our old builder. To test it, we enter a fake empty derivation, source the stdenv setup, unpack the hello sources and build it: - + $ nix-shell -E 'derivation { name = "fake"; builder = "fake"; system = "x86_64-linux"; }' +nix-shell$ unset PATH +nix-shell$ source /nix/store/k4jklkcag4zq4xkqhkpy156mgfm34ipn-stdenv/setup +nix-shell$ tar -xf hello-2.10.tar.gz +nix-shell$ cd hello-2.10 +nix-shell$ configurePhase +... +nix-shell$ buildPhase +... + I unset PATH to further show that the stdenv is sufficiently self-contained to build autotools packages that have no other dependencies. @@ -98,7 +126,12 @@ Let's write a hello.nix expression using this newly discovered stdenv: - + with import <nixpkgs> { }; +stdenv.mkDerivation { + name = "hello"; + src = ./hello-2.10.tar.gz; +} + Don't be scared by the with expression. It pulls the nixpkgs repository into scope, so we can directly use stdenv. It looks very similar to the hello expression in Pill 8. @@ -108,7 +141,12 @@ It builds, and runs fine: - + $ nix-build hello.nix +... +/nix/store/6y0mzdarm5qxfafvn2zm9nr01d1j0a72-hello +$ result/bin/hello +Hello, world! +
@@ -118,7 +156,18 @@ Let's take a look at the builder used by mkDerivation. You can read the code here in nixpkgs: - + { + # ... + builder = attrs.realBuilder or shell; + args = + attrs.args or [ + "-e" + (attrs.builder or ./default-builder.sh) + ]; + stdenv = result; + # ... +} + Also take a look at our old derivation wrapper in previous pills! The builder is bash (that shell variable), the argument to the builder (bash) is default-builder.sh, and then we add the environment variable $stdenv in the derivation which is the stdenv derivation. @@ -128,7 +177,9 @@ You can open default-builder.sh and see what it does: - + source $stdenv/setup +genericBuild + It's what we did in Pill 10 to make the derivations nix-shell friendly. When entering the shell, the setup file only sets up the environment without building anything. When doing nix-build, it actually runs the build process. @@ -138,7 +189,57 @@ To get a clear understanding of the environment variables, look at the .drv of the hello derivation: - + <screen xmlns="http://docbook.org/ns/docbook"><prompt>$ </prompt><userinput>nix derivation show $(nix-instantiate hello.nix)</userinput> +<computeroutput>warning: you did not specify '--add-root'; the result might be removed by the garbage collector +{ + "/nix/store/abwj50lycl0m515yblnrvwyydlhhqvj2-hello.drv": { + "outputs": { + "out": { + "path": "/nix/store/6y0mzdarm5qxfafvn2zm9nr01d1j0a72-hello" + } + }, + "inputSrcs": [ + "/nix/store/9krlzvny65gdc8s7kpb6lkx8cd02c25b-default-builder.sh", + "/nix/store/svc70mmzrlgq42m9acs0prsmci7ksh6h-hello-2.10.tar.gz" + ], + "inputDrvs": { + "/nix/store/hcgwbx42mcxr7ksnv0i1fg7kw6jvxshb-bash-4.4-p19.drv": [ + "out" + ], + "/nix/store/sfxh3ybqh97cgl4s59nrpi78kgcc8f3d-stdenv-linux.drv": [ + "out" + ] + }, + "platform": "x86_64-linux", + "builder": "/nix/store/q1g0rl8zfmz7r371fp5p42p4acmv297d-bash-4.4-p19/bin/bash", + "args": [ + "-e", + "/nix/store/9krlzvny65gdc8s7kpb6lkx8cd02c25b-default-builder.sh" + ], + "env": { + "buildInputs": "", + "builder": "/nix/store/q1g0rl8zfmz7r371fp5p42p4acmv297d-bash-4.4-p19/bin/bash", + "configureFlags": "", + "depsBuildBuild": "", + "depsBuildBuildPropagated": "", + "depsBuildTarget": "", + "depsBuildTargetPropagated": "", + "depsHostBuild": "", + "depsHostBuildPropagated": "", + "depsTargetTarget": "", + "depsTargetTargetPropagated": "", + "name": "hello", + "nativeBuildInputs": "", + "out": "/nix/store/6y0mzdarm5qxfafvn2zm9nr01d1j0a72-hello", + "propagatedBuildInputs": "", + "propagatedNativeBuildInputs": "", + "src": "/nix/store/svc70mmzrlgq42m9acs0prsmci7ksh6h-hello-2.10.tar.gz", + "stdenv": "/nix/store/6kz2vbh98s2r1pfshidkzhiy2s2qdw0a-stdenv-linux", + "system": "x86_64-linux" + } + } +}</computeroutput></screen> + It's so short I decided to paste it entirely above. The builder is bash, with -e default-builder.sh arguments. Then you can see the src and stdenv environment variables. diff --git a/pills/19/default-builder.txt b/pills/19/default-builder.txt deleted file mode 100644 index 273fc55..0000000 --- a/pills/19/default-builder.txt +++ /dev/null @@ -1,2 +0,0 @@ -source $stdenv/setup -genericBuild diff --git a/pills/19/hello-derivation.xml b/pills/19/hello-derivation.xml deleted file mode 100644 index e0eddc0..0000000 --- a/pills/19/hello-derivation.xml +++ /dev/null @@ -1,50 +0,0 @@ -$ nix derivation show $(nix-instantiate hello.nix) -warning: you did not specify '--add-root'; the result might be removed by the garbage collector -{ - "/nix/store/abwj50lycl0m515yblnrvwyydlhhqvj2-hello.drv": { - "outputs": { - "out": { - "path": "/nix/store/6y0mzdarm5qxfafvn2zm9nr01d1j0a72-hello" - } - }, - "inputSrcs": [ - "/nix/store/9krlzvny65gdc8s7kpb6lkx8cd02c25b-default-builder.sh", - "/nix/store/svc70mmzrlgq42m9acs0prsmci7ksh6h-hello-2.10.tar.gz" - ], - "inputDrvs": { - "/nix/store/hcgwbx42mcxr7ksnv0i1fg7kw6jvxshb-bash-4.4-p19.drv": [ - "out" - ], - "/nix/store/sfxh3ybqh97cgl4s59nrpi78kgcc8f3d-stdenv-linux.drv": [ - "out" - ] - }, - "platform": "x86_64-linux", - "builder": "/nix/store/q1g0rl8zfmz7r371fp5p42p4acmv297d-bash-4.4-p19/bin/bash", - "args": [ - "-e", - "/nix/store/9krlzvny65gdc8s7kpb6lkx8cd02c25b-default-builder.sh" - ], - "env": { - "buildInputs": "", - "builder": "/nix/store/q1g0rl8zfmz7r371fp5p42p4acmv297d-bash-4.4-p19/bin/bash", - "configureFlags": "", - "depsBuildBuild": "", - "depsBuildBuildPropagated": "", - "depsBuildTarget": "", - "depsBuildTargetPropagated": "", - "depsHostBuild": "", - "depsHostBuildPropagated": "", - "depsTargetTarget": "", - "depsTargetTargetPropagated": "", - "name": "hello", - "nativeBuildInputs": "", - "out": "/nix/store/6y0mzdarm5qxfafvn2zm9nr01d1j0a72-hello", - "propagatedBuildInputs": "", - "propagatedNativeBuildInputs": "", - "src": "/nix/store/svc70mmzrlgq42m9acs0prsmci7ksh6h-hello-2.10.tar.gz", - "stdenv": "/nix/store/6kz2vbh98s2r1pfshidkzhiy2s2qdw0a-stdenv-linux", - "system": "x86_64-linux" - } - } -} diff --git a/pills/19/stdenv-derivation.txt b/pills/19/stdenv-derivation.txt deleted file mode 100644 index 83fa0f4..0000000 --- a/pills/19/stdenv-derivation.txt +++ /dev/null @@ -1,8 +0,0 @@ -$ nix-build '' -A stdenv -/nix/store/k4jklkcag4zq4xkqhkpy156mgfm34ipn-stdenv -$ ls -R result/ -result/: -nix-support/ setup - -result/nix-support: -propagated-user-env-packages diff --git a/pills/19/stdenv-hello-build.txt b/pills/19/stdenv-hello-build.txt deleted file mode 100644 index 722f999..0000000 --- a/pills/19/stdenv-hello-build.txt +++ /dev/null @@ -1,5 +0,0 @@ -$ nix-build hello.nix -... -/nix/store/6y0mzdarm5qxfafvn2zm9nr01d1j0a72-hello -$ result/bin/hello -Hello, world! diff --git a/pills/19/stdenv-hello.txt b/pills/19/stdenv-hello.txt deleted file mode 100644 index 9557c00..0000000 --- a/pills/19/stdenv-hello.txt +++ /dev/null @@ -1,5 +0,0 @@ -with import { }; -stdenv.mkDerivation { - name = "hello"; - src = ./hello-2.10.tar.gz; -} diff --git a/pills/19/stdenv-mkderivation.txt b/pills/19/stdenv-mkderivation.txt deleted file mode 100644 index 5722254..0000000 --- a/pills/19/stdenv-mkderivation.txt +++ /dev/null @@ -1,11 +0,0 @@ -{ - # ... - builder = attrs.realBuilder or shell; - args = - attrs.args or [ - "-e" - (attrs.builder or ./default-builder.sh) - ]; - stdenv = result; - # ... -} diff --git a/pills/19/stdenv-references.txt b/pills/19/stdenv-references.txt deleted file mode 100644 index 3f99154..0000000 --- a/pills/19/stdenv-references.txt +++ /dev/null @@ -1,6 +0,0 @@ -$ nix-store -q --references result -/nix/store/3a45nb37s0ndljp68228snsqr3qsyp96-bzip2-1.0.6 -/nix/store/a457ywa1haa0sgr9g7a1pgldrg3s798d-coreutils-8.24 -/nix/store/zmd4jk4db5lgxb8l93mhkvr3x92g2sx2-bash-4.3-p39 -/nix/store/47sfpm2qclpqvrzijizimk4md1739b1b-gcc-wrapper-4.9.3 -... diff --git a/pills/19/stdenv-setup-fake-builder.txt b/pills/19/stdenv-setup-fake-builder.txt deleted file mode 100644 index febb32c..0000000 --- a/pills/19/stdenv-setup-fake-builder.txt +++ /dev/null @@ -1,9 +0,0 @@ -$ nix-shell -E 'derivation { name = "fake"; builder = "fake"; system = "x86_64-linux"; }' -nix-shell$ unset PATH -nix-shell$ source /nix/store/k4jklkcag4zq4xkqhkpy156mgfm34ipn-stdenv/setup -nix-shell$ tar -xf hello-2.10.tar.gz -nix-shell$ cd hello-2.10 -nix-shell$ configurePhase -... -nix-shell$ buildPhase -... diff --git a/pills/19/stdenv-setup-head.txt b/pills/19/stdenv-setup-head.txt deleted file mode 100644 index 5b294c7..0000000 --- a/pills/19/stdenv-setup-head.txt +++ /dev/null @@ -1,5 +0,0 @@ -$ head result/setup -export SHELL=/nix/store/zmd4jk4db5lgxb8l93mhkvr3x92g2sx2-bash-4.3-p39/bin/bash -initialPath="/nix/store/a457ywa1haa0sgr9g7a1pgldrg3s798d-coreutils-8.24 ..." -defaultNativeBuildInputs="/nix/store/sgwq15xg00xnm435gjicspm048rqg9y6-patchelf-0.8 ..." - diff --git a/pills/20-basic-dependencies-and-hooks.xml b/pills/20-basic-dependencies-and-hooks.xml index 0c29729..ccb787e 100644 --- a/pills/20-basic-dependencies-and-hooks.xml +++ b/pills/20-basic-dependencies-and-hooks.xml @@ -34,23 +34,89 @@ For the simplest dependencies where the current package directly needs another, we use the buildInputs attribute. This is exactly the pattern used in our builder in Pill 8. To demo this, let's build GNU Hello, and then another package which provides a shell script that execs it. - + let + + nixpkgs = import <nixpkgs> { }; + + inherit (nixpkgs) stdenv fetchurl which; + + actualHello = stdenv.mkDerivation { + name = "hello-2.3"; + + src = fetchurl { + url = "mirror://gnu/hello/hello-2.3.tar.bz2"; + sha256 = "0c7vijq8y68bpr7g6dh1gny0bff8qq81vnp4ch8pjzvg56wb3js1"; + }; + }; + + wrappedHello = stdenv.mkDerivation { + name = "hello-wrapper"; + + buildInputs = [ + actualHello + which + ]; + + unpackPhase = "true"; + + installPhase = '' + mkdir -p "$out/bin" + echo "#! ${stdenv.shell}" >> "$out/bin/hello" + echo "exec $(which hello)" >> "$out/bin/hello" + chmod 0755 "$out/bin/hello" + ''; + }; +in +wrappedHello + + Notice that the wrappedHello derivation finds the hello binary from the PATH. This works because stdenv contains something like: - + pkgs="" +for i in $buildInputs; do + findInputs $i +done + where findInputs is defined like: - + findInputs() { + local pkg=$1 + + ## Don't need to repeat already processed package + case $pkgs in + *\ $pkg\ *) + return 0 + ;; + esac + + pkgs="$pkgs $pkg " + + ## More goes here in reality that we can ignore for now. +} + then after this is run: - + for i in $pkgs; do + addToEnv $i +done + where addToEnv is defined like: - + addToEnv() { + local pkg=$1 + + if test -d $1/bin; then + addToSearchPath _PATH $1/bin + fi + + ## More goes here in reality that we can ignore for now. +} + The addToSearchPath call adds $1/bin to _PATH if the former exists (code here). Once all the packages in buildInputs have been processed, then content of _PATH is added to PATH, as follows: - + PATH="${_PATH-}${_PATH:+${PATH:+:}}$PATH" + With the real hello on the PATH, the installPhase should hopefully make sense. @@ -64,7 +130,53 @@ Nix itself handles this just fine, understanding various dependency closures as covered in previous builds. But what about the conveniences that buildInputs provides, namely accumulating in pkgs environment variable and inclusion of pkg/bin directories on the PATH? For this, stdenv provides the propagatedBuildInputs: - + let + + nixpkgs = import <nixpkgs> { }; + + inherit (nixpkgs) stdenv fetchurl which; + + actualHello = stdenv.mkDerivation { + name = "hello-2.3"; + + src = fetchurl { + url = "mirror://gnu/hello/hello-2.3.tar.bz2"; + sha256 = "0c7vijq8y68bpr7g6dh1gny0bff8qq81vnp4ch8pjzvg56wb3js1"; + }; + }; + + intermediary = stdenv.mkDerivation { + name = "middle-man"; + + propagatedBuildInputs = [ actualHello ]; + + unpackPhase = "true"; + + installPhase = '' + mkdir -p "$out" + ''; + }; + + wrappedHello = stdenv.mkDerivation { + name = "hello-wrapper"; + + buildInputs = [ + intermediary + which + ]; + + unpackPhase = "true"; + + installPhase = '' + mkdir -p "$out/bin" + echo "#! ${stdenv.shell}" >> "$out/bin/hello" + echo "exec $(which hello)" >> "$out/bin/hello" + chmod 0755 "$out/bin/hello" + ''; + }; +in +wrappedHello + See how the intermediate package has a propagatedBuildInputs dependency, but the wrapper only needs a buildInputs dependency on the intermediary. @@ -73,19 +185,46 @@ You might think we do something in Nix, but actually it's done not at eval time but at build time in bash. let's look at part of the fixupPhase of stdenv: - + fixupPhase() { + + ## Elided + + if test -n "$propagatedBuildInputs"; then + mkdir -p "$out/nix-support" + echo "$propagatedBuildInputs" > "$out/nix-support/propagated-build-inputs" + fi + + ## Elided + +} + This dumps the propagated build inputs in a so-named file in $out/nix-support/. Then, back in findInputs look at the lines at the bottom we elided before: - + findInputs() { + local pkg=$1 + + ## More goes here in reality that we can ignore for now. + + if test -f $pkg/nix-support/propagated-build-inputs; then + for i in $(cat $pkg/nix-support/propagated-build-inputs); do + findInputs $i + done + fi +} + See how findInputs is actually recursive, looking at the propagated build inputs of each dependency, and those dependencies' propagated build inputs, etc. We actually simplified the findInputs call site from before; propagatedBuildInputs is also looped over in reality: - + pkgs="" +for i in $buildInputs $propagatedBuildInputs; do + findInputs $i +done + This demonstrates an important point. For the current package alone, it doesn't matter whether a dependency is propagated or not. It will be processed the same way: called with findInputs and addToEnv. (The packages discovered by findInputs, which are also accumulated in pkgs and passed to addToEnv, are also the same in both cases.) @@ -118,7 +257,19 @@ Setup hooks are the basic building block we have for this. In nixpkgs, a "hook" is basically a bash callback, and a setup hook is no exception. Let's look at the last part of findInputs we haven't covered: - + findInputs() { + local pkg=$1 + + ## More goes here in reality that we can ignore for now. + + if test -f $pkg/nix-support/setup-hook; then + source $pkg/nix-support/setup-hook + fi + + ## More goes here in reality that we can ignore for now. + +} + If a package includes the path pkg/nix-support/setup-hook, it will be sourced by any stdenv-based build including that as a dependency. @@ -156,10 +307,27 @@ The other half of addToEnv is: - + addToEnv() { + local pkg=$1 + + ## More goes here in reality that we can ignore for now. + + # Run the package-specific hooks set by the setup-hook scripts. + for i in "${envHooks[@]}"; do + $i $pkg + done +} + Functions listed in envHooks are applied to every package passed to addToEnv. One can write a setup hook like: - + anEnvHook() { + local pkg=$1 + + echo "I'm depending on \"$pkg\"" +} + +envHooks+=(anEnvHook) + and if one dependency has that setup hook then all of them will be so echoed. Allowing dependencies to learn about their sibling dependencies is exactly what compilers need. diff --git a/pills/20/build-inputs-0.bash b/pills/20/build-inputs-0.bash deleted file mode 100644 index 1188faf..0000000 --- a/pills/20/build-inputs-0.bash +++ /dev/null @@ -1,4 +0,0 @@ -pkgs="" -for i in $buildInputs; do - findInputs $i -done diff --git a/pills/20/build-inputs-1.bash b/pills/20/build-inputs-1.bash deleted file mode 100644 index 1b09b42..0000000 --- a/pills/20/build-inputs-1.bash +++ /dev/null @@ -1,14 +0,0 @@ -findInputs() { - local pkg=$1 - - ## Don't need to repeat already processed package - case $pkgs in - *\ $pkg\ *) - return 0 - ;; - esac - - pkgs="$pkgs $pkg " - - ## More goes here in reality that we can ignore for now. -} diff --git a/pills/20/build-inputs-2.bash b/pills/20/build-inputs-2.bash deleted file mode 100644 index d3db58c..0000000 --- a/pills/20/build-inputs-2.bash +++ /dev/null @@ -1,3 +0,0 @@ -for i in $pkgs; do - addToEnv $i -done diff --git a/pills/20/build-inputs-3.bash b/pills/20/build-inputs-3.bash deleted file mode 100644 index 80e3207..0000000 --- a/pills/20/build-inputs-3.bash +++ /dev/null @@ -1,9 +0,0 @@ -addToEnv() { - local pkg=$1 - - if test -d $1/bin; then - addToSearchPath _PATH $1/bin - fi - - ## More goes here in reality that we can ignore for now. -} diff --git a/pills/20/build-inputs-4.bash b/pills/20/build-inputs-4.bash deleted file mode 100644 index ff883ce..0000000 --- a/pills/20/build-inputs-4.bash +++ /dev/null @@ -1 +0,0 @@ -PATH="${_PATH-}${_PATH:+${PATH:+:}}$PATH" diff --git a/pills/20/env-hooks-0.bash b/pills/20/env-hooks-0.bash deleted file mode 100644 index 68c0de6..0000000 --- a/pills/20/env-hooks-0.bash +++ /dev/null @@ -1,10 +0,0 @@ -addToEnv() { - local pkg=$1 - - ## More goes here in reality that we can ignore for now. - - # Run the package-specific hooks set by the setup-hook scripts. - for i in "${envHooks[@]}"; do - $i $pkg - done -} diff --git a/pills/20/env-hooks-1.bash b/pills/20/env-hooks-1.bash deleted file mode 100644 index f64daf9..0000000 --- a/pills/20/env-hooks-1.bash +++ /dev/null @@ -1,7 +0,0 @@ -anEnvHook() { - local pkg=$1 - - echo "I'm depending on \"$pkg\"" -} - -envHooks+=(anEnvHook) diff --git a/pills/20/propagated-build-inputs-0.bash b/pills/20/propagated-build-inputs-0.bash deleted file mode 100644 index b2ca8af..0000000 --- a/pills/20/propagated-build-inputs-0.bash +++ /dev/null @@ -1,12 +0,0 @@ -fixupPhase() { - - ## Elided - - if test -n "$propagatedBuildInputs"; then - mkdir -p "$out/nix-support" - echo "$propagatedBuildInputs" > "$out/nix-support/propagated-build-inputs" - fi - - ## Elided - -} diff --git a/pills/20/propagated-build-inputs-1.bash b/pills/20/propagated-build-inputs-1.bash deleted file mode 100644 index 5c4b217..0000000 --- a/pills/20/propagated-build-inputs-1.bash +++ /dev/null @@ -1,11 +0,0 @@ -findInputs() { - local pkg=$1 - - ## More goes here in reality that we can ignore for now. - - if test -f $pkg/nix-support/propagated-build-inputs; then - for i in $(cat $pkg/nix-support/propagated-build-inputs); do - findInputs $i - done - fi -} diff --git a/pills/20/propagated-build-inputs-2.bash b/pills/20/propagated-build-inputs-2.bash deleted file mode 100644 index a5e5e76..0000000 --- a/pills/20/propagated-build-inputs-2.bash +++ /dev/null @@ -1,4 +0,0 @@ -pkgs="" -for i in $buildInputs $propagatedBuildInputs; do - findInputs $i -done diff --git a/pills/20/setup-hooks-0.bash b/pills/20/setup-hooks-0.bash deleted file mode 100644 index a0b5de9..0000000 --- a/pills/20/setup-hooks-0.bash +++ /dev/null @@ -1,12 +0,0 @@ -findInputs() { - local pkg=$1 - - ## More goes here in reality that we can ignore for now. - - if test -f $pkg/nix-support/setup-hook; then - source $pkg/nix-support/setup-hook - fi - - ## More goes here in reality that we can ignore for now. - -} diff --git a/pills/20/three-hellos.nix b/pills/20/three-hellos.nix deleted file mode 100644 index fe59c44..0000000 --- a/pills/20/three-hellos.nix +++ /dev/null @@ -1,46 +0,0 @@ -let - - nixpkgs = import { }; - - inherit (nixpkgs) stdenv fetchurl which; - - actualHello = stdenv.mkDerivation { - name = "hello-2.3"; - - src = fetchurl { - url = "mirror://gnu/hello/hello-2.3.tar.bz2"; - sha256 = "0c7vijq8y68bpr7g6dh1gny0bff8qq81vnp4ch8pjzvg56wb3js1"; - }; - }; - - intermediary = stdenv.mkDerivation { - name = "middle-man"; - - propagatedBuildInputs = [ actualHello ]; - - unpackPhase = "true"; - - installPhase = '' - mkdir -p "$out" - ''; - }; - - wrappedHello = stdenv.mkDerivation { - name = "hello-wrapper"; - - buildInputs = [ - intermediary - which - ]; - - unpackPhase = "true"; - - installPhase = '' - mkdir -p "$out/bin" - echo "#! ${stdenv.shell}" >> "$out/bin/hello" - echo "exec $(which hello)" >> "$out/bin/hello" - chmod 0755 "$out/bin/hello" - ''; - }; -in -wrappedHello diff --git a/pills/20/two-hellos.nix b/pills/20/two-hellos.nix deleted file mode 100644 index 4a49f3c..0000000 --- a/pills/20/two-hellos.nix +++ /dev/null @@ -1,35 +0,0 @@ -let - - nixpkgs = import { }; - - inherit (nixpkgs) stdenv fetchurl which; - - actualHello = stdenv.mkDerivation { - name = "hello-2.3"; - - src = fetchurl { - url = "mirror://gnu/hello/hello-2.3.tar.bz2"; - sha256 = "0c7vijq8y68bpr7g6dh1gny0bff8qq81vnp4ch8pjzvg56wb3js1"; - }; - }; - - wrappedHello = stdenv.mkDerivation { - name = "hello-wrapper"; - - buildInputs = [ - actualHello - which - ]; - - unpackPhase = "true"; - - installPhase = '' - mkdir -p "$out/bin" - echo "#! ${stdenv.shell}" >> "$out/bin/hello" - echo "exec $(which hello)" >> "$out/bin/hello" - chmod 0755 "$out/bin/hello" - ''; - }; -in -wrappedHello -