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