Clone of https://github.com/NixOS/nixpkgs.git (to stress-test knotserver)
1{
2 lib,
3 stdenv,
4 buildPackages,
5 fetchurl,
6 updateAutotoolsGnuConfigScriptsHook,
7 bison,
8 util-linux,
9 coreutils,
10 libredirect,
11 glibcLocales,
12 gnused,
13
14 interactive ? true,
15 readline,
16 withDocs ? null,
17 forFHSEnv ? false,
18
19 pkgsStatic,
20}:
21
22let
23 upstreamPatches = import ./bash-5.3-patches.nix (
24 nr: sha256:
25 fetchurl {
26 url = "mirror://gnu/bash/bash-5.3-patches/bash53-${nr}";
27 inherit sha256;
28 }
29 );
30in
31lib.warnIf (withDocs != null)
32 ''
33 bash: `.override { withDocs = true; }` is deprecated, the docs are always included.
34 ''
35 stdenv.mkDerivation
36 (fa: {
37 pname = "bash${lib.optionalString interactive "-interactive"}";
38 version = "5.3${fa.patch_suffix}";
39 patch_suffix = "p${toString (builtins.length upstreamPatches)}";
40
41 src = fetchurl {
42 url = "mirror://gnu/bash/bash-${lib.removeSuffix fa.patch_suffix fa.version}.tar.gz";
43 hash = "sha256-DVzYaWX4aaJs9k9Lcb57lvkKO6iz104n6OnZ1VUPMbo=";
44 };
45
46 hardeningDisable = [
47 "format"
48 ]
49 # bionic libc is super weird and has issues with fortify outside of its own libc, check this comment:
50 # https://github.com/NixOS/nixpkgs/pull/192630#discussion_r978985593
51 # or you can check libc/include/sys/cdefs.h in bionic source code
52 ++ lib.optional (stdenv.hostPlatform.libc == "bionic") "fortify";
53
54 outputs = [
55 "out"
56 "dev"
57 "man"
58 "doc"
59 "info"
60 ];
61
62 separateDebugInfo = true;
63
64 env.NIX_CFLAGS_COMPILE = ''
65 -DSYS_BASHRC="/etc/bashrc"
66 -DSYS_BASH_LOGOUT="/etc/bash_logout"
67 ''
68 + lib.optionalString (!forFHSEnv) ''
69 -DDEFAULT_PATH_VALUE="/no-such-path"
70 -DSTANDARD_UTILS_PATH="/no-such-path"
71 -DDEFAULT_LOADABLE_BUILTINS_PATH="${placeholder "out"}/lib/bash:."
72 ''
73 + ''
74 -DNON_INTERACTIVE_LOGIN_SHELLS
75 -DSSH_SOURCE_BASHRC
76 '';
77
78 patchFlags = [ "-p0" ];
79
80 patches = upstreamPatches ++ [
81 # Enable PGRP_PIPE independently of the kernel of the build machine.
82 # This doesn't seem to be upstreamed despite such a mention of in https://github.com/NixOS/nixpkgs/pull/77196,
83 # which originally introduced the patch
84 # Some related discussion can be found in
85 # https://lists.gnu.org/archive/html/bug-bash/2015-05/msg00071.html
86 ./pgrp-pipe-5.patch
87 ];
88
89 configureFlags = [
90 # At least on Linux bash memory allocator has pathological performance
91 # in scenarios involving use of larger memory:
92 # https://lists.gnu.org/archive/html/bug-bash/2023-08/msg00052.html
93 # Various distributions default to system allocator. Let's nixpkgs
94 # do the same.
95 "--without-bash-malloc"
96 (if interactive then "--with-installed-readline" else "--disable-readline")
97 ]
98 ++ lib.optionals (stdenv.hostPlatform != stdenv.buildPlatform) [
99 "bash_cv_job_control_missing=nomissing"
100 "bash_cv_sys_named_pipes=nomissing"
101 "bash_cv_getcwd_malloc=yes"
102 # This check cannot be performed when cross compiling. The "yes"
103 # default is fine for static linking on Linux (weak symbols?) but
104 # not with BSDs, when it does clash with the regular `getenv`.
105 "bash_cv_getenv_redef=${
106 if !(with stdenv.hostPlatform; isStatic && (isOpenBSD || isFreeBSD)) then "yes" else "no"
107 }"
108 ]
109 ++ lib.optionals stdenv.hostPlatform.isCygwin [
110 "--without-libintl-prefix"
111 "--without-libiconv-prefix"
112 "--with-installed-readline"
113 "bash_cv_dev_stdin=present"
114 "bash_cv_dev_fd=standard"
115 "bash_cv_termcap_lib=libncurses"
116 ]
117 ++ lib.optionals (stdenv.hostPlatform.libc == "musl") [
118 "--disable-nls"
119 ]
120 ++ lib.optionals stdenv.hostPlatform.isFreeBSD [
121 # /dev/fd is optional on FreeBSD. we need it to work when built on a system
122 # with it and transferred to a system without it! This includes linux cross.
123 "bash_cv_dev_fd=absent"
124 ];
125
126 strictDeps = true;
127 # Note: Bison is needed because the patches above modify parse.y.
128 depsBuildBuild = [ buildPackages.stdenv.cc ];
129 nativeBuildInputs = [
130 updateAutotoolsGnuConfigScriptsHook
131 bison
132 ]
133 ++ lib.optional stdenv.hostPlatform.isDarwin stdenv.cc.bintools;
134
135 buildInputs = lib.optional interactive readline;
136
137 enableParallelBuilding = true;
138
139 makeFlags = lib.optionals stdenv.hostPlatform.isCygwin [
140 "LOCAL_LDFLAGS=-Wl,--export-all,--out-implib,libbash.dll.a"
141 "SHOBJ_LIBS=-lbash"
142 ];
143
144 doCheck = false; # Can't be enabled by default due to dependency cycle, use passthru.tests.withChecks instead
145
146 postInstall = ''
147 ln -s bash "$out/bin/sh"
148 rm -f $out/lib/bash/Makefile.inc
149 '';
150
151 postFixup =
152 if interactive then
153 ''
154 substituteInPlace "$out/bin/bashbug" \
155 --replace '#!/bin/sh' "#!$out/bin/bash"
156 ''
157 # most space is taken by locale data
158 else
159 ''
160 rm -rf "$out/share" "$out/bin/bashbug"
161 '';
162
163 passthru = {
164 shellPath = "/bin/bash";
165 tests.static = pkgsStatic.bash;
166 tests.withChecks = fa.finalPackage.overrideAttrs (attrs: {
167 doCheck = true;
168
169 nativeCheckInputs = attrs.nativeCheckInputs or [ ] ++ [
170 util-linux
171 libredirect.hook
172 glibcLocales
173 gnused
174 ];
175
176 meta = attrs.meta // {
177 # Ignore Darwin for now, because the tests fail in many more ways than on Linux
178 broken = attrs.meta.broken or false || stdenv.buildPlatform.isDarwin;
179 };
180
181 patches = attrs.patches or [ ] ++ [
182 # See commit comment, also submitted upstream: https://lists.gnu.org/archive/html/bug-bash/2025-10/msg00054.html
183 ./fail-tests.patch
184 # See commit comment, also submitted upstream: https://lists.gnu.org/archive/html/bug-bash/2025-10/msg00055.html
185 ./failed-tests-output.patch
186 # The run-builtins test _almost_ succeeds, only has a bit of PATH trouble
187 # and some odd terminal column mismatch
188 ./fix-builtins-tests.patch
189 # The run-invocation test _almost_ succeeds, only has a bit of PATH trouble
190 ./fix-invocation-tests.patch
191 ];
192
193 preCheck = attrs.preCheck or "" + ''
194 # Allows looking at actual outputs for failed tests
195 export BASH_TSTOUT_KEEPDIR=$(mktemp -d)
196 export HOME=$(mktemp -d)
197 export NIX_REDIRECTS=${
198 lib.concatMapAttrsStringSep ":" (name: value: "${name}=${value}") {
199 "/bin/echo" = lib.getExe' coreutils "echo";
200 "/bin/cat" = lib.getExe' coreutils "cat";
201 "/bin/rm" = lib.getExe' coreutils "rm";
202 "/usr" = "$(mktemp -d)";
203 }
204 }
205
206 disabled_checks=(
207 # Unsets PATH and breaks, not clear
208 run-execscript
209
210 # Fails on ZFS & needs a ja_JP.SJIS locale, which glibcLocales doesn't have
211 run-intl
212
213 # These error with "echo: write error: Broken pipe"
214 run-histexpand
215 run-lastpipe
216 run-comsub
217 run-comsub2
218
219 # For some reason has an extra 'declare -x version="5.2p37"'
220 run-nameref
221
222 # These print some extra 'trap -- ''' SIGPIPE'
223 run-trap
224 run-varenv
225
226 # These rely on /dev/tty
227 run-read
228 run-test
229 run-vredir
230
231 # Might also be related to not having a tty: "Inappropriate ioctl for device"
232 run-history
233
234 # Can be enabled in 5.4
235 run-printf
236
237 # This is probably fixable without too much trouble, but just not having a hardcoded PATH in type5.sub doesn't cut it
238 # 142,143c142,147
239 # < type5.sub: line 23: mkdir: command not found
240 # < type5.sub: line 24: cd: /build/type-23722: No such file or directory
241 # ---
242 # > cat is /bin/cat
243 # > cat is aliased to `echo cat'
244 # > /bin/cat
245 # > break is a shell builtin
246 # > break is a special shell builtin
247 # > ./e
248 run-type
249 )
250 for check in "''${disabled_checks[@]}"; do
251 # Exit before running the test script
252 sed -i "1iecho 'Skipping test $check' >&2 && exit 0" "tests/$check"
253 done
254 '';
255 });
256 };
257
258 meta = with lib; {
259 homepage = "https://www.gnu.org/software/bash/";
260 description =
261 "GNU Bourne-Again Shell, the de facto standard shell on Linux"
262 + lib.optionalString interactive " (for interactive use)";
263 longDescription = ''
264 Bash is the shell, or command language interpreter, that will
265 appear in the GNU operating system. Bash is an sh-compatible
266 shell that incorporates useful features from the Korn shell
267 (ksh) and C shell (csh). It is intended to conform to the IEEE
268 POSIX P1003.2/ISO 9945.2 Shell and Tools standard. It offers
269 functional improvements over sh for both programming and
270 interactive use. In addition, most sh scripts can be run by
271 Bash without modification.
272 '';
273 license = licenses.gpl3Plus;
274 platforms = platforms.all;
275 # https://github.com/NixOS/nixpkgs/issues/333338
276 badPlatforms = [ lib.systems.inspect.patterns.isMinGW ];
277 maintainers = with lib.maintainers; [ infinisil ];
278 mainProgram = "bash";
279 identifiers.cpeParts =
280 let
281 versionSplit = lib.split "p" version;
282 in
283 {
284 vendor = "gnu";
285 product = "bash";
286 version = lib.elemAt versionSplit 0;
287 update = lib.elemAt versionSplit 2;
288 };
289 };
290 })