1{
2 lib,
3 derivationWithMeta,
4 fetchurl,
5 kaem,
6 tinycc,
7 gnumake,
8 gnupatch,
9 coreutils,
10 mescc-tools-extra,
11 bash_2_05,
12}:
13let
14 pname = "bash";
15 version = "2.05b";
16
17 src = fetchurl {
18 url = "mirror://gnu/bash/bash-${version}.tar.gz";
19 sha256 = "1r1z2qdw3rz668nxrzwa14vk2zcn00hw7mpjn384picck49d80xs";
20 };
21
22 # Thanks to the live-bootstrap project!
23 # See https://github.com/fosslinux/live-bootstrap/blob/1bc4296091c51f53a5598050c8956d16e945b0f5/sysa/bash-2.05b/bash-2.05b.kaem
24 liveBootstrap = "https://github.com/fosslinux/live-bootstrap/raw/1bc4296091c51f53a5598050c8956d16e945b0f5/sysa/bash-2.05b";
25
26 main_mk = fetchurl {
27 url = "${liveBootstrap}/mk/main.mk";
28 sha256 = "0hj29q3pq3370p18sxkpvv9flb7yvx2fs96xxlxqlwa8lkimd0j4";
29 };
30
31 common_mk = fetchurl {
32 url = "${liveBootstrap}/mk/common.mk";
33 sha256 = "09rigxxf85p2ybnq248sai1gdx95yykc8jmwi4yjx389zh09mcr8";
34 };
35
36 builtins_mk = fetchurl {
37 url = "${liveBootstrap}/mk/builtins.mk";
38 sha256 = "0939dy5by1xhfmsjj6w63nlgk509fjrhpb2crics3dpcv7prl8lj";
39 };
40
41 patches = [
42 # mes libc does not have locale support
43 (fetchurl {
44 url = "${liveBootstrap}/patches/mes-libc.patch";
45 sha256 = "0zksdjf6zbb3p4hqg6plq631y76hhhgab7kdvf7cnpk8bcykn12z";
46 })
47 # int name, namelen; is wrong for mes libc, it is char* name, so we modify tinycc
48 # to reflect this.
49 (fetchurl {
50 url = "${liveBootstrap}/patches/tinycc.patch";
51 sha256 = "042d2kr4a8klazk1hlvphxr6frn4mr53k957aq3apf6lbvrjgcj2";
52 })
53 # add ifdef's for features we don't want
54 (fetchurl {
55 url = "${liveBootstrap}/patches/missing-defines.patch";
56 sha256 = "1q0k1kj5mrvjkqqly7ki5575a5b3hy1ywnmvhrln318yh67qnkj4";
57 })
58 # mes libc + setting locale = not worky
59 (fetchurl {
60 url = "${liveBootstrap}/patches/locale.patch";
61 sha256 = "1p1q1slhafsgj8x4k0dpn9h6ryq5fwfx7dicbbxhldbw7zvnnbx9";
62 })
63 # We do not have /dev at this stage of the bootstrap, including /dev/tty
64 (fetchurl {
65 url = "${liveBootstrap}/patches/dev-tty.patch";
66 sha256 = "1315slv5f7ziajqyxg4jlyanf1xwd06xw14y6pq7xpm3jzjk55j9";
67 })
68 ];
69in
70kaem.runCommand "${pname}-${version}"
71 {
72 inherit pname version;
73
74 nativeBuildInputs = [
75 tinycc.compiler
76 gnumake
77 gnupatch
78 coreutils
79 ];
80
81 passthru.runCommand =
82 name: env: buildCommand:
83 derivationWithMeta (
84 {
85 inherit name buildCommand;
86 builder = "${bash_2_05}/bin/bash";
87 args = [
88 "-e"
89 (builtins.toFile "bash-builder.sh" ''
90 export CONFIG_SHELL=$SHELL
91
92 # Normalize the NIX_BUILD_CORES variable. The value might be 0, which
93 # means that we're supposed to try and auto-detect the number of
94 # available CPU cores at run-time. We don't have nproc to detect the
95 # number of available CPU cores so default to 1 if not set.
96 NIX_BUILD_CORES="''${NIX_BUILD_CORES:-1}"
97 if [ $NIX_BUILD_CORES -le 0 ]; then
98 NIX_BUILD_CORES=1
99 fi
100 export NIX_BUILD_CORES
101
102 bash -eux $buildCommandPath
103 '')
104 ];
105 passAsFile = [ "buildCommand" ];
106
107 SHELL = "${bash_2_05}/bin/bash";
108 PATH = lib.makeBinPath (
109 (env.nativeBuildInputs or [ ])
110 ++ [
111 bash_2_05
112 coreutils
113 # provides untar, ungz, and unbz2
114 mescc-tools-extra
115 ]
116 );
117 }
118 // (builtins.removeAttrs env [ "nativeBuildInputs" ])
119 );
120
121 passthru.tests.get-version =
122 result:
123 kaem.runCommand "${pname}-get-version-${version}" { } ''
124 ${result}/bin/bash --version
125 mkdir ''${out}
126 '';
127
128 meta = with lib; {
129 description = "GNU Bourne-Again Shell, the de facto standard shell on Linux";
130 homepage = "https://www.gnu.org/software/bash";
131 license = licenses.gpl3Plus;
132 teams = [ teams.minimal-bootstrap ];
133 platforms = platforms.unix;
134 };
135 }
136 ''
137 # Unpack
138 ungz --file ${src} --output bash.tar
139 untar --file bash.tar
140 rm bash.tar
141 cd bash-${version}
142
143 # Patch
144 ${lib.concatMapStringsSep "\n" (f: "patch -Np0 -i ${f}") patches}
145
146 # Configure
147 cp ${main_mk} Makefile
148 cp ${builtins_mk} builtins/Makefile
149 cp ${common_mk} common.mk
150 touch config.h
151 touch include/version.h
152 touch include/pipesize.h
153
154 # Build
155 make \
156 CC="tcc -B ${tinycc.libs}/lib" \
157 mkbuiltins
158 cd builtins
159 make \
160 CC="tcc -B ${tinycc.libs}/lib" \
161 libbuiltins.a
162 cd ..
163 make CC="tcc -B ${tinycc.libs}/lib"
164
165 # Install
166 install -D bash ''${out}/bin/bash
167 ln -s bash ''${out}/bin/sh
168 ''