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