1{ lib
2, stdenv
3, buildPackages
4, fetchurl
5, bison
6, util-linux
7
8 # patch for cygwin requires readline support
9, interactive ? stdenv.isCygwin
10, readline
11, withDocs ? false
12, texinfo
13, forFHSEnv ? false
14
15, pkgsStatic
16}:
17
18let
19 upstreamPatches = import ./bash-5.2-patches.nix (nr: sha256: fetchurl {
20 url = "mirror://gnu/bash/bash-5.2-patches/bash52-${nr}";
21 inherit sha256;
22 });
23in
24stdenv.mkDerivation rec {
25 name = "bash-${lib.optionalString interactive "interactive-"}${version}-p${toString (builtins.length upstreamPatches)}";
26 version = "5.2";
27
28 src = fetchurl {
29 url = "mirror://gnu/bash/bash-${version}.tar.gz";
30 sha256 = "sha256-oTnBZt9/9EccXgczBRZC7lVWwcyKSnjxRVg8XIGrMvs=";
31 };
32
33 hardeningDisable = [ "format" ]
34 # bionic libc is super weird and has issues with fortify outside of its own libc, check this comment:
35 # https://github.com/NixOS/nixpkgs/pull/192630#discussion_r978985593
36 # or you can check libc/include/sys/cdefs.h in bionic source code
37 ++ lib.optional (stdenv.hostPlatform.libc == "bionic") "fortify";
38
39 outputs = [ "out" "dev" "man" "doc" "info" ];
40
41 separateDebugInfo = true;
42
43 env.NIX_CFLAGS_COMPILE = ''
44 -DSYS_BASHRC="/etc/bashrc"
45 -DSYS_BASH_LOGOUT="/etc/bash_logout"
46 '' + lib.optionalString (!forFHSEnv) ''
47 -DDEFAULT_PATH_VALUE="/no-such-path"
48 -DSTANDARD_UTILS_PATH="/no-such-path"
49 '' + ''
50 -DNON_INTERACTIVE_LOGIN_SHELLS
51 -DSSH_SOURCE_BASHRC
52 '';
53
54 patchFlags = [ "-p0" ];
55
56 patches = upstreamPatches ++ [
57 ./pgrp-pipe-5.patch
58 (fetchurl {
59 name = "fix-static.patch";
60 url = "https://cgit.freebsd.org/ports/plain/shells/bash/files/patch-configure?id=3e147a1f594751a68fea00a28090d0792bee0b51";
61 sha256 = "XHFMQ6eXTReNoywdETyrfQEv1rKF8+XFbQZP4YoVKFk=";
62 })
63 # Apply parallel build fix pending upstream inclusion:
64 # https://savannah.gnu.org/patch/index.php?10373
65 # Had to fetch manually to workaround -p0 default.
66 ./parallel.patch
67 ];
68
69 configureFlags = [
70 # At least on Linux bash memory allocator has pathological performance
71 # in scenarios involving use of larger memory:
72 # https://lists.gnu.org/archive/html/bug-bash/2023-08/msg00052.html
73 # Various distributions default to system allocator. Let's nixpkgs
74 # do the same.
75 "--without-bash-malloc"
76 (if interactive then "--with-installed-readline" else "--disable-readline")
77 ] ++ lib.optionals (stdenv.hostPlatform != stdenv.buildPlatform) [
78 "bash_cv_job_control_missing=nomissing"
79 "bash_cv_sys_named_pipes=nomissing"
80 "bash_cv_getcwd_malloc=yes"
81 ] ++ lib.optionals stdenv.hostPlatform.isCygwin [
82 "--without-libintl-prefix"
83 "--without-libiconv-prefix"
84 "--with-installed-readline"
85 "bash_cv_dev_stdin=present"
86 "bash_cv_dev_fd=standard"
87 "bash_cv_termcap_lib=libncurses"
88 ] ++ lib.optionals (stdenv.hostPlatform.libc == "musl") [
89 "--disable-nls"
90 ];
91
92 strictDeps = true;
93 # Note: Bison is needed because the patches above modify parse.y.
94 depsBuildBuild = [ buildPackages.stdenv.cc ];
95 nativeBuildInputs = [ bison ]
96 ++ lib.optional withDocs texinfo
97 ++ lib.optional stdenv.hostPlatform.isDarwin stdenv.cc.bintools;
98
99 buildInputs = lib.optional interactive readline;
100
101 enableParallelBuilding = true;
102
103 makeFlags = lib.optionals stdenv.hostPlatform.isCygwin [
104 "LOCAL_LDFLAGS=-Wl,--export-all,--out-implib,libbash.dll.a"
105 "SHOBJ_LIBS=-lbash"
106 ];
107
108 nativeCheckInputs = [ util-linux ];
109 doCheck = false; # dependency cycle, needs to be interactive
110
111 postInstall = ''
112 ln -s bash "$out/bin/sh"
113 rm -f $out/lib/bash/Makefile.inc
114 '';
115
116 postFixup =
117 if interactive
118 then ''
119 substituteInPlace "$out/bin/bashbug" \
120 --replace '#!/bin/sh' "#!$out/bin/bash"
121 ''
122 # most space is taken by locale data
123 else ''
124 rm -rf "$out/share" "$out/bin/bashbug"
125 '';
126
127 passthru = {
128 shellPath = "/bin/bash";
129 tests.static = pkgsStatic.bash;
130 };
131
132 meta = with lib; {
133 homepage = "https://www.gnu.org/software/bash/";
134 description = "GNU Bourne-Again Shell, the de facto standard shell on Linux" + lib.optionalString interactive " (for interactive use)";
135 longDescription = ''
136 Bash is the shell, or command language interpreter, that will
137 appear in the GNU operating system. Bash is an sh-compatible
138 shell that incorporates useful features from the Korn shell
139 (ksh) and C shell (csh). It is intended to conform to the IEEE
140 POSIX P1003.2/ISO 9945.2 Shell and Tools standard. It offers
141 functional improvements over sh for both programming and
142 interactive use. In addition, most sh scripts can be run by
143 Bash without modification.
144 '';
145 license = licenses.gpl3Plus;
146 platforms = platforms.all;
147 maintainers = with maintainers; [ dtzWill ];
148 mainProgram = "bash";
149 };
150}