summary refs log tree commit diff
path: root/pkgs/stdenv/linux/default.nix
blob: a15cd30157fe44c2bbea63acd68b6793b9275b05 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
# This file constructs the standard build environment for the
# Linux/i686 platform.  It's completely pure; that is, it relies on no
# external (non-Nix) tools, such as /usr/bin/gcc, and it contains a C
# compiler and linker that do not search in default locations,
# ensuring purity of components produced by it.

{allPackages}:

rec {

  system = "i686-linux";


  # The bootstrap process proceeds in several steps.

  # 1) Create a standard environment by downloading pre-built
  # statically linked binaries of coreutils, gcc, etc.

  # To fetch the pre-built binaries, we use a statically linked `curl'
  # binary which is unpacked here.
  curl = derivation {
    name = "curl";
    builder = ./tools/bash;
    tar = ./tools/tar;
    bunzip2 = ./tools/bunzip2;
    cp = ./tools/cp;
    curl = ./tools/curl-7.15.1-static.tar.bz2;
    inherit system;
    args = [ ./scripts/unpack-curl.sh ];
  };

  # This function downloads a file.
  download = {url, md5, pkgname}: derivation {
    name = baseNameOf (toString url);
    builder = ./tools/bash;
    inherit system curl url;
    args = [ ./scripts/download.sh ];

    # Nix 0.8 fixed-output derivations.
    outputHashAlgo = "md5";
    outputHash = md5;
    
    # Compatibility with Nix <= 0.7.
    id = md5;
  };

  # This function downloads and unpacks a file.
  downloadAndUnpack =
  { url, md5, pkgname, postProcess ? [], addToPath ? []
  , extra ? null, extra2 ? null
  , extra3 ? null, extra4? null, patchelf ? null}:
  derivation {
    name = pkgname;
    builder = ./tools/bash;
    tar = ./tools/tar;
    bunzip2 = ./tools/bunzip2;
    cp = ./tools/cp;
    args = [ ./scripts/unpack.sh ];
    tarball = download {inherit url md5 pkgname;};
    inherit system postProcess addToPath extra extra2 extra3 extra4 patchelf;
  };

  # The various statically linked components that make up the standard
  # environment.
  staticTools = downloadAndUnpack {
    url = http://nix.cs.uu.nl/dist/tarballs/stdenv-linux/static-tools.tar.bz2;
    pkgname = "static-tools";
    md5 = "90578c603079313123e8c754a85e40d7";
  };

  binutils = downloadAndUnpack {
    url = http://nix.cs.uu.nl/dist/tarballs/stdenv-linux/binutils-2.15-static.tar.bz2;
    pkgname = "binutils";
    md5 = "9c134038b7f1894a4b307d600207047c";
  };

  staticGCC = (downloadAndUnpack {
    url = http://nix.cs.uu.nl/dist/tarballs/stdenv-linux/gcc-3.4.2-static.tar.bz2;
    pkgname = "gcc";
    md5 = "600452fac470a49a41ea81d39c209f35";
    postProcess = [./scripts/fix-outpath.sh];
    addToPath = [staticTools];
  }) // { langC = true; langCC = false; langF77 = false; };

  glibc = downloadAndUnpack {
    url = http://nix.cs.uu.nl/dist/tarballs/stdenv-linux/glibc-2.3.3-static.tar.bz2;
    pkgname = "glibc";
    md5 = "36ff244e666c60784edfe1cc66f68e4c";
    postProcess = [./scripts/fix-outpath.sh];
    addToPath = [staticTools];
  };


  # A helper function to call gcc-wrapper.
  wrapGCC =
    {gcc ? staticGCC, glibc, binutils, shell ? ""}:
    (import ../../build-support/gcc-wrapper) {
      nativeTools = false;
      nativeGlibc = false;
      inherit gcc binutils glibc shell;
      stdenv = stdenvInitial;
    };


  # The "fake" standard environment used to build "real" standard
  # environments.  It consists of just the basic statically linked
  # tools.
  stdenvInitial = let {
    body = derivation {
      name = "stdenv-linux-initial";
      builder = ./tools/bash;
      args = ./scripts/builder-stdenv-initial.sh;
      inherit system staticTools;
    }  // {
      mkDerivation = attrs: derivation ((removeAttrs attrs ["meta"]) // {
        builder = ./tools/bash;
        args = ["-e" attrs.builder];
        stdenv = body;
        system = body.system;
      });
      shell = ./tools/bash;
    };
  };


  # This function builds the various standard environments used during
  # the bootstrap.
  stdenvBootFun =
    {gcc, staticGlibc, extraAttrs ? {}}:
    
    import ../generic {
      name = "stdenv-linux-boot";
      param1 = if staticGlibc then "static" else "dynamic";
      preHook = ./prehook.sh;
      stdenv = stdenvInitial;
      shell = ./tools/bash;
      initialPath = [
        staticTools
      ];
      inherit gcc extraAttrs;
    };


  # Create the first "real" standard environment.  This one consists
  # of statically linked components only, and a minimal glibc to keep
  # the gcc configure script happy.
  stdenvLinuxBoot1 = stdenvBootFun {
    # Use the statically linked, downloaded glibc/gcc/binutils.
    gcc = wrapGCC {inherit glibc binutils;};
    staticGlibc = true;
    extraAttrs = {inherit curl;};
  };

  # 2) These are the packages that we can build with the first
  #    stdenv.  We only need Glibc (in step 3).
  stdenvLinuxBoot1Pkgs = allPackages {
    inherit system;
    bootStdenv = stdenvLinuxBoot1;
  };

  # 3) Build Glibc with the statically linked tools.  The result is the
  #    full, dynamically linked, final Glibc.
  stdenvLinuxGlibc = stdenvLinuxBoot1Pkgs.glibc;

  # 4) Construct a second stdenv identical to the first, except that
  #    this one uses the Glibc built in step 3.  It still uses
  #    statically linked tools.
  stdenvLinuxBoot2 = removeAttrs (stdenvBootFun {
    staticGlibc = false;
    gcc = wrapGCC {inherit binutils; glibc = stdenvLinuxGlibc;};
    extraAttrs = {inherit curl; glibc = stdenvLinuxGlibc;};
  }) ["gcc" "binutils"];

  # 5) The packages that can be built using the second stdenv.
  stdenvLinuxBoot2Pkgs = allPackages {
    inherit system;
    bootStdenv = stdenvLinuxBoot2;
  };

  # 6) Construct a third stdenv identical to the second, except that
  #    this one uses the dynamically linked GCC and Binutils from step
  #    5.  The other tools (e.g. coreutils) are still static.
  stdenvLinuxBoot3 = stdenvBootFun {
    staticGlibc = false;
    gcc = wrapGCC {
      inherit (stdenvLinuxBoot2Pkgs) binutils;
      glibc = stdenvLinuxGlibc;
      gcc = stdenvLinuxBoot2Pkgs.gcc.gcc;
    };
    extraAttrs = {inherit curl;};
  };

  # 7) The packages that can be built using the third stdenv.
  stdenvLinuxBoot3Pkgs = allPackages {
    inherit system;
    bootStdenv = stdenvLinuxBoot3;
  };

  # 8) Construct the final stdenv.  It uses the Glibc, GCC and
  #    Binutils built above, and adds in dynamically linked versions
  #    of all other tools.
  stdenvLinux = (import ../generic) {
    name = "stdenv-linux";
    preHook = ./prehook.sh;
    initialPath = [
      ((import ../common-path.nix) {pkgs = stdenvLinuxBoot3Pkgs;})
      stdenvLinuxBoot3Pkgs.patchelf
    ];

    stdenv = stdenvInitial;

    gcc = wrapGCC {
      inherit (stdenvLinuxBoot2Pkgs) binutils;
      glibc = stdenvLinuxGlibc;
      gcc = stdenvLinuxBoot2Pkgs.gcc.gcc;
      shell = stdenvLinuxBoot3Pkgs.bash + /bin/sh;
    };

    shell = stdenvLinuxBoot3Pkgs.bash + /bin/sh;
    
    extraAttrs = {
      curl = stdenvLinuxBoot3Pkgs.realCurl;
      inherit (stdenvLinuxBoot2Pkgs) binutils /* gcc */ glibc;
      inherit (stdenvLinuxBoot3Pkgs)
        gzip bzip2 bash coreutils diffutils findutils gawk
        gnumake gnused gnutar gnugrep patch patchelf;
    };
  };

}