| 1 | -*- mode: org; coding: utf-8; -*- |
| 2 | |
| 3 | #+TITLE: Hacking GNU Guix and Its Incredible Distro |
| 4 | |
| 5 | Copyright © 2012, 2013 Ludovic Courtès <[email protected]> |
| 6 | |
| 7 | Copying and distribution of this file, with or without modification, |
| 8 | are permitted in any medium without royalty provided the copyright |
| 9 | notice and this notice are preserved. |
| 10 | |
| 11 | |
| 12 | * Running Guix before it is installed |
| 13 | |
| 14 | Command-line tools can be used even if you have not run "make install". |
| 15 | To do that, prefix each command with ‘./pre-inst-env’, as in: |
| 16 | |
| 17 | ./pre-inst-env guix-build --help |
| 18 | |
| 19 | Similarly, for a Guile session using the Guix modules: |
| 20 | |
| 21 | ./pre-inst-env guile -c '(use-modules (guix utils)) (pk (%current-system))' |
| 22 | |
| 23 | The ‘pre-inst-env’ script sets up all the environment variables |
| 24 | necessary to support this. |
| 25 | |
| 26 | * Adding new packages |
| 27 | |
| 28 | Package recipes in Guix look like this: |
| 29 | |
| 30 | #+BEGIN_SRC scheme |
| 31 | (package |
| 32 | (name "nettle") |
| 33 | (version "2.5") |
| 34 | (source |
| 35 | (origin |
| 36 | (method url-fetch) |
| 37 | (uri (string-append "mirror://gnu/nettle/nettle-" |
| 38 | version ".tar.gz")) |
| 39 | (sha256 |
| 40 | (base32 |
| 41 | "0wicr7amx01l03rm0pzgr1qvw3f9blaw17vjsy1301dh13ll58aa")))) |
| 42 | (build-system gnu-build-system) |
| 43 | (inputs `(("m4" ,m4))) |
| 44 | (propagated-inputs `(("gmp" ,gmp))) |
| 45 | (home-page |
| 46 | "http://www.lysator.liu.se/~nisse/nettle/") |
| 47 | (synopsis "GNU Nettle, a cryptographic library") |
| 48 | (description |
| 49 | "Nettle is a cryptographic library...") |
| 50 | (license gpl2+)) |
| 51 | #+END_SRC |
| 52 | |
| 53 | Such a recipe can be written by hand, and then tested by running |
| 54 | ‘./pre-inst-env guix-build nettle’. |
| 55 | |
| 56 | When writing the recipe, the base32-encoded SHA256 hash of the source |
| 57 | code tarball, which can be seen in the example above, can be obtained by |
| 58 | running: |
| 59 | |
| 60 | guix-download http://ftp.gnu.org/gnu/nettle/nettle-2.5.tar.gz |
| 61 | |
| 62 | Alternatively, it is possible to semi-automatically import recipes from |
| 63 | the [[http://nixos.org/nixpkgs/][Nixpkgs]] software distribution using this command: |
| 64 | |
| 65 | guix-import /path/to/nixpkgs/checkout nettle |
| 66 | |
| 67 | The command automatically fetches and converts to Guix the “Nix |
| 68 | expression” of Nettle. |
| 69 | |
| 70 | * Porting the Guix distro on a new platform |
| 71 | |
| 72 | ** Introduction |
| 73 | |
| 74 | Unlike Make or similar build tools, Guix requires absolutely /all/ the |
| 75 | dependencies of a build process to be specified. |
| 76 | |
| 77 | For a user-land software distribution, that means that the process that |
| 78 | builds GCC (then used to build all other programs) must itself be |
| 79 | specified; and the process to build the C library to build that GCC; and |
| 80 | the process to build the GCC to build that library; and... See the |
| 81 | problem? Chicken-and-egg. |
| 82 | |
| 83 | To break that cycle, the distro starts from a set of pre-built |
| 84 | binaries–usually referred to as “bootstrap binaries.” These include |
| 85 | statically-linked versions of Guile, GCC, Coreutils, Make, Grep, sed, |
| 86 | etc., and the GNU C Library. |
| 87 | |
| 88 | This section describes how to build those bootstrap binaries when |
| 89 | porting to a new platform. |
| 90 | |
| 91 | ** When the platform is supported by Nixpkgs |
| 92 | |
| 93 | In that case, the easiest thing is to bootstrap the distro using |
| 94 | binaries from Nixpkgs. |
| 95 | |
| 96 | To do that, you need to comment out the definitions of |
| 97 | ‘%bootstrap-guile’ and ‘%bootstrap-inputs’ in distro/packages/bootstrap.scm |
| 98 | to force the use of Nixpkgs derivations. For instance, when porting to |
| 99 | ‘i686-linux’, you should redefine these variables along these lines: |
| 100 | |
| 101 | #+BEGIN_SRC scheme |
| 102 | (define %bootstrap-guile |
| 103 | (nixpkgs-derivation "guile" "i686-linux")) |
| 104 | |
| 105 | (define %bootstrap-inputs |
| 106 | (compile-time-value |
| 107 | `(("libc" ,(nixpkgs-derivation "glibc" "i686-linux")) |
| 108 | ,@(map (lambda (name) |
| 109 | (list name (nixpkgs-derivation name "i686-linux"))) |
| 110 | '("gnutar" "gzip" "bzip2" "xz" "patch" |
| 111 | "coreutils" "gnused" "gnugrep" "bash" |
| 112 | "gawk" ; used by `config.status' |
| 113 | "gcc" "binutils"))))) |
| 114 | #+END_SRC |
| 115 | |
| 116 | That should allow the distro to be bootstrapped. |
| 117 | |
| 118 | Then, the tarballs containing the initial binaries of Guile, Coreutils, |
| 119 | GCC, libc, etc. need to be built. To that end, run the following |
| 120 | commands: |
| 121 | |
| 122 | #+BEGIN_SRC sh |
| 123 | ./pre-inst-env guix-build \ |
| 124 | -e '(@ (distro packages make-bootstrap) bootstrap-tarballs)' \ |
| 125 | --system=i686-linux |
| 126 | |
| 127 | #+END_SRC |
| 128 | |
| 129 | These should build tarballs containing statically-linked tools usable on |
| 130 | that system. |
| 131 | |
| 132 | In the source tree, you need to install binaries for ‘mkdir’, ‘bash’, |
| 133 | ‘tar’, and ‘xz’ under ‘distro/packages/bootstrap/i686-linux’. These |
| 134 | binaries can be extracted from the static-binaries tarball built above. |
| 135 | |
| 136 | A rule for ‘distro/packages/bootstrap/i686-linux/guile-2.0.7.tar.xz’ |
| 137 | needs to be added in ‘Makefile.am’, with the appropriate hexadecimal |
| 138 | vrepresentation of its SHA256 hash. |
| 139 | |
| 140 | You may then revert your changes to ‘bootstrap.scm’. For the variables |
| 141 | ‘%bootstrap-coreutils&co’, ‘%bootstrap-binutils’, ‘%bootstrap-glibc’, |
| 142 | and ‘%bootstrap-gcc’, the expected SHA256 of the corresponding tarballs |
| 143 | for ‘i686-linux’ (built above) must be added. |
| 144 | |
| 145 | This should be enough to bootstrap the distro without resorting to |
| 146 | Nixpkgs. |
| 147 | |
| 148 | ** When the platform is *not* supported by Nixpkgs |
| 149 | |
| 150 | In that case, the bootstrap binaries should be built using whatever |
| 151 | tools are available on the target platform. That is, the tarballs and |
| 152 | binaries show above must first be built manually, using the available |
| 153 | tools. |
| 154 | |
| 155 | They should have the same properties as those built by the Guix recipes |
| 156 | shown above. For example, all the binaries (except for glibc) must be |
| 157 | statically-linked; the bootstrap Guile must be relocatable (see patch in |
| 158 | the Guix distro); the static-binaries tarball must contain the same |
| 159 | programs (Coreutils, Grep, sed, Awk, etc.); and so on. |
| 160 | |