1{ fetchurl, lib, stdenv, buildPackages
2, curl, openssl, zlib, expat, perlPackages, python3, gettext, cpio
3, gnugrep, gnused, gawk, coreutils # needed at runtime by git-filter-branch etc
4, openssh, pcre2, bash
5, asciidoc, texinfo, xmlto, docbook2x, docbook_xsl, docbook_xml_dtd_45
6, libxslt, tcl, tk, makeWrapper, libiconv
7, svnSupport, subversionClient, perlLibs, smtpPerlLibs
8, perlSupport ? stdenv.buildPlatform == stdenv.hostPlatform
9, nlsSupport ? true
10, osxkeychainSupport ? stdenv.isDarwin
11, guiSupport
12, withManual ? true
13, pythonSupport ? true
14, withpcre2 ? true
15, sendEmailSupport
16, darwin
17, nixosTests
18, withLibsecret ? false
19, pkg-config, glib, libsecret
20, gzip # needed at runtime by gitweb.cgi
21}:
22
23assert osxkeychainSupport -> stdenv.isDarwin;
24assert sendEmailSupport -> perlSupport;
25assert svnSupport -> perlSupport;
26
27let
28 version = "2.33.1";
29 svn = subversionClient.override { perlBindings = perlSupport; };
30
31 gitwebPerlLibs = with perlPackages; [ CGI HTMLParser CGIFast FCGI FCGIProcManager HTMLTagCloud ];
32in
33
34stdenv.mkDerivation {
35 pname = "git";
36 inherit version;
37
38 src = fetchurl {
39 url = "https://www.kernel.org/pub/software/scm/git/git-${version}.tar.xz";
40 sha256 = "sha256-4FSm5sKwiL0b/19h7ZulqpHJo81QlTmktBxd3wIgHy8=";
41 };
42
43 outputs = [ "out" ] ++ lib.optional withManual "doc";
44
45 hardeningDisable = [ "format" ];
46
47 enableParallelBuilding = true;
48
49 ## Patch
50
51 patches = [
52 ./docbook2texi.patch
53 ./git-sh-i18n.patch
54 ./ssh-path.patch
55 ./git-send-email-honor-PATH.patch
56 ./installCheck-path.patch
57 ];
58
59 postPatch = ''
60 for x in connect.c git-gui/lib/remote_add.tcl ; do
61 substituteInPlace "$x" \
62 --subst-var-by ssh "${openssh}/bin/ssh"
63 done
64
65 # Fix references to gettext introduced by ./git-sh-i18n.patch
66 substituteInPlace git-sh-i18n.sh \
67 --subst-var-by gettext ${gettext}
68
69 # ensure we are using the correct shell when executing the test scripts
70 patchShebangs t/*.sh
71 '';
72
73 nativeBuildInputs = [ gettext perlPackages.perl makeWrapper ]
74 ++ lib.optionals withManual [ asciidoc texinfo xmlto docbook2x
75 docbook_xsl docbook_xml_dtd_45 libxslt ];
76 buildInputs = [ curl openssl zlib expat cpio libiconv bash ]
77 ++ lib.optionals perlSupport [ perlPackages.perl ]
78 ++ lib.optionals guiSupport [tcl tk]
79 ++ lib.optionals withpcre2 [ pcre2 ]
80 ++ lib.optionals stdenv.isDarwin [ darwin.Security ]
81 ++ lib.optionals withLibsecret [ pkg-config glib libsecret ];
82
83 # required to support pthread_cancel()
84 NIX_LDFLAGS = lib.optionalString (stdenv.cc.isGNU && stdenv.hostPlatform.libc == "glibc") "-lgcc_s"
85 + lib.optionalString (stdenv.isFreeBSD) "-lthr";
86
87 configureFlags = [
88 "ac_cv_prog_CURL_CONFIG=${lib.getDev curl}/bin/curl-config"
89 ] ++ lib.optionals (stdenv.buildPlatform != stdenv.hostPlatform) [
90 "ac_cv_fread_reads_directories=yes"
91 "ac_cv_snprintf_returns_bogus=no"
92 "ac_cv_iconv_omits_bom=no"
93 ];
94
95 preBuild = ''
96 makeFlagsArray+=( perllibdir=$out/$(perl -MConfig -wle 'print substr $Config{installsitelib}, 1 + length $Config{siteprefixexp}') )
97 '';
98
99 makeFlags = [
100 "prefix=\${out}"
101 ]
102 # Git does not allow setting a shell separately for building and run-time.
103 # Therefore lets leave it at the default /bin/sh when cross-compiling
104 ++ lib.optional (stdenv.buildPlatform == stdenv.hostPlatform) "SHELL_PATH=${stdenv.shell}"
105 ++ (if perlSupport then ["PERL_PATH=${perlPackages.perl}/bin/perl"] else ["NO_PERL=1"])
106 ++ (if pythonSupport then ["PYTHON_PATH=${python3}/bin/python"] else ["NO_PYTHON=1"])
107 ++ lib.optionals stdenv.isSunOS ["INSTALL=install" "NO_INET_NTOP=" "NO_INET_PTON="]
108 ++ (if stdenv.isDarwin then ["NO_APPLE_COMMON_CRYPTO=1"] else ["sysconfdir=/etc"])
109 ++ lib.optionals stdenv.hostPlatform.isMusl ["NO_SYS_POLL_H=1" "NO_GETTEXT=YesPlease"]
110 ++ lib.optional withpcre2 "USE_LIBPCRE2=1"
111 ++ lib.optional (!nlsSupport) "NO_GETTEXT=1"
112 # git-gui refuses to start with the version of tk distributed with
113 # macOS Catalina. We can prevent git from building the .app bundle
114 # by specifying an invalid tk framework. The postInstall step will
115 # then ensure that git-gui uses tcl/tk from nixpkgs, which is an
116 # acceptable version.
117 #
118 # See https://github.com/Homebrew/homebrew-core/commit/dfa3ccf1e7d3901e371b5140b935839ba9d8b706
119 ++ lib.optional stdenv.isDarwin "TKFRAMEWORK=/nonexistent";
120
121 disallowedReferences = lib.optionals (stdenv.buildPlatform != stdenv.hostPlatform) [
122 stdenv.shellPackage
123 ];
124
125
126 postBuild = ''
127 make -C contrib/subtree
128 '' + (lib.optionalString perlSupport ''
129 make -C contrib/diff-highlight
130 '') + (lib.optionalString osxkeychainSupport ''
131 make -C contrib/credential/osxkeychain
132 '') + (lib.optionalString withLibsecret ''
133 make -C contrib/credential/libsecret
134 '');
135
136
137 ## Install
138
139 # WARNING: Do not `rm` or `mv` files from the source tree; use `cp` instead.
140 # We need many of these files during the installCheckPhase.
141
142 installFlags = [ "NO_INSTALL_HARDLINKS=1" ];
143
144 preInstall = (lib.optionalString osxkeychainSupport ''
145 mkdir -p $out/bin
146 ln -s $out/share/git/contrib/credential/osxkeychain/git-credential-osxkeychain $out/bin/
147 rm -f $PWD/contrib/credential/osxkeychain/git-credential-osxkeychain.o
148 '') + (lib.optionalString withLibsecret ''
149 mkdir -p $out/bin
150 ln -s $out/share/git/contrib/credential/libsecret/git-credential-libsecret $out/bin/
151 rm -f $PWD/contrib/credential/libsecret/git-credential-libsecret.o
152 '');
153
154 postInstall =
155 ''
156 notSupported() {
157 unlink $1 || true
158 }
159
160 # Install git-subtree.
161 make -C contrib/subtree install ${lib.optionalString withManual "install-doc"}
162 rm -rf contrib/subtree
163
164 # Install contrib stuff.
165 mkdir -p $out/share/git
166 cp -a contrib $out/share/git/
167 mkdir -p $out/share/bash-completion/completions
168 ln -s $out/share/git/contrib/completion/git-completion.bash $out/share/bash-completion/completions/git
169 mkdir -p $out/share/bash-completion/completions
170 ln -s $out/share/git/contrib/completion/git-prompt.sh $out/share/bash-completion/completions/
171
172 # grep is a runtime dependency, need to patch so that it's found
173 substituteInPlace $out/libexec/git-core/git-sh-setup \
174 --replace ' grep' ' ${gnugrep}/bin/grep' \
175 --replace ' egrep' ' ${gnugrep}/bin/egrep'
176
177 # Fix references to the perl, sed, awk and various coreutil binaries used by
178 # shell scripts that git calls (e.g. filter-branch)
179 SCRIPT="$(cat <<'EOS'
180 BEGIN{
181 @a=(
182 '${gnugrep}/bin/grep', '${gnused}/bin/sed', '${gawk}/bin/awk',
183 '${coreutils}/bin/cut', '${coreutils}/bin/basename', '${coreutils}/bin/dirname',
184 '${coreutils}/bin/wc', '${coreutils}/bin/tr'
185 ${lib.optionalString perlSupport ", '${perlPackages.perl}/bin/perl'"}
186 );
187 }
188 foreach $c (@a) {
189 $n=(split("/", $c))[-1];
190 s|(?<=[^#][^/.-])\b''${n}(?=\s)|''${c}|g
191 }
192 EOS
193 )"
194 perl -0777 -i -pe "$SCRIPT" \
195 $out/libexec/git-core/git-{sh-setup,filter-branch,merge-octopus,mergetool,quiltimport,request-pull,submodule,subtree,web--browse}
196
197
198 # Also put git-http-backend into $PATH, so that we can use smart
199 # HTTP(s) transports for pushing
200 ln -s $out/libexec/git-core/git-http-backend $out/bin/git-http-backend
201 '' + lib.optionalString perlSupport ''
202 # wrap perl commands
203 makeWrapper "$out/share/git/contrib/credential/netrc/git-credential-netrc.perl" $out/bin/git-credential-netrc \
204 --set PERL5LIB "$out/${perlPackages.perl.libPrefix}:${perlPackages.makePerlPath perlLibs}"
205 wrapProgram $out/libexec/git-core/git-cvsimport \
206 --set GITPERLLIB "$out/${perlPackages.perl.libPrefix}:${perlPackages.makePerlPath perlLibs}"
207 wrapProgram $out/libexec/git-core/git-add--interactive \
208 --set GITPERLLIB "$out/${perlPackages.perl.libPrefix}:${perlPackages.makePerlPath perlLibs}"
209 wrapProgram $out/libexec/git-core/git-archimport \
210 --set GITPERLLIB "$out/${perlPackages.perl.libPrefix}:${perlPackages.makePerlPath perlLibs}"
211 wrapProgram $out/libexec/git-core/git-instaweb \
212 --set GITPERLLIB "$out/${perlPackages.perl.libPrefix}:${perlPackages.makePerlPath perlLibs}"
213 wrapProgram $out/libexec/git-core/git-cvsexportcommit \
214 --set GITPERLLIB "$out/${perlPackages.perl.libPrefix}:${perlPackages.makePerlPath perlLibs}"
215
216 # gzip (and optionally bzip2, xz, zip) are runtime dependencies for
217 # gitweb.cgi, need to patch so that it's found
218 sed -i -e "s|'compressor' => \['gzip'|'compressor' => ['${gzip}/bin/gzip'|" \
219 $out/share/gitweb/gitweb.cgi
220 # Give access to CGI.pm and friends (was removed from perl core in 5.22)
221 for p in ${lib.concatStringsSep " " gitwebPerlLibs}; do
222 sed -i -e "/use CGI /i use lib \"$p/${perlPackages.perl.libPrefix}\";" \
223 "$out/share/gitweb/gitweb.cgi"
224 done
225 ''
226
227 + (if svnSupport then ''
228 # wrap git-svn
229 wrapProgram $out/libexec/git-core/git-svn \
230 --set GITPERLLIB "$out/${perlPackages.perl.libPrefix}:${perlPackages.makePerlPath (perlLibs ++ [svn.out])}" \
231 --prefix PATH : "${svn.out}/bin" ''
232 else '' # replace git-svn by notification script
233 notSupported $out/libexec/git-core/git-svn
234 '')
235
236 + (if sendEmailSupport then ''
237 # wrap git-send-email
238 wrapProgram $out/libexec/git-core/git-send-email \
239 --set GITPERLLIB "$out/${perlPackages.perl.libPrefix}:${perlPackages.makePerlPath smtpPerlLibs}"
240 '' else ''
241 # replace git-send-email by notification script
242 notSupported $out/libexec/git-core/git-send-email
243 '')
244
245 + lib.optionalString withManual ''# Install man pages
246 make -j $NIX_BUILD_CORES -l $NIX_BUILD_CORES PERL_PATH="${buildPackages.perl}/bin/perl" cmd-list.made install install-html \
247 -C Documentation ''
248
249 + (if guiSupport then ''
250 # Wrap Tcl/Tk programs
251 for prog in bin/gitk libexec/git-core/{git-gui,git-citool,git-gui--askpass}; do
252 sed -i -e "s|exec 'wish'|exec '${tk}/bin/wish'|g" \
253 -e "s|exec wish|exec '${tk}/bin/wish'|g" \
254 "$out/$prog"
255 done
256 ln -s $out/share/git/contrib/completion/git-completion.bash $out/share/bash-completion/completions/gitk
257 '' else ''
258 # Don't wrap Tcl/Tk, replace them by notification scripts
259 for prog in bin/gitk libexec/git-core/git-gui; do
260 notSupported "$out/$prog"
261 done
262 '')
263 + lib.optionalString osxkeychainSupport ''
264 # enable git-credential-osxkeychain on darwin if desired (default)
265 mkdir -p $out/etc
266 cat > $out/etc/gitconfig << EOF
267 [credential]
268 helper = osxkeychain
269 EOF
270 '';
271
272
273 ## InstallCheck
274
275 doCheck = false;
276 doInstallCheck = true;
277
278 installCheckTarget = "test";
279
280 # see also installCheckFlagsArray
281 installCheckFlags = [
282 "DEFAULT_TEST_TARGET=prove"
283 "PERL_PATH=${buildPackages.perl}/bin/perl"
284 ];
285
286 preInstallCheck = ''
287 installCheckFlagsArray+=(
288 GIT_PROVE_OPTS="--jobs $NIX_BUILD_CORES --failures --state=failed,save"
289 GIT_TEST_INSTALLED=$out/bin
290 ${lib.optionalString (!svnSupport) "NO_SVN_TESTS=y"}
291 )
292
293 function disable_test {
294 local test=$1 pattern=$2
295 if [ $# -eq 1 ]; then
296 mv t/{,skip-}$test.sh || true
297 else
298 sed -i t/$test.sh \
299 -e "/^\s*test_expect_.*$pattern/,/^\s*' *\$/{s/^/: #/}"
300 fi
301 }
302
303 # Shared permissions are forbidden in sandbox builds:
304 substituteInPlace t/test-lib.sh \
305 --replace "test_set_prereq POSIXPERM" ""
306 # TODO: Investigate while these still fail (without POSIXPERM):
307 disable_test t0001-init 'shared overrides system'
308 disable_test t0001-init 'init honors global core.sharedRepository'
309 disable_test t1301-shared-repo
310 # git-completion.bash: line 405: compgen: command not found:
311 disable_test t9902-completion 'option aliases are shown with GIT_COMPLETION_SHOW_ALL'
312
313 # Our patched gettext never fallbacks
314 disable_test t0201-gettext-fallbacks
315
316 ${lib.optionalString (!sendEmailSupport) ''
317 # Disable sendmail tests
318 disable_test t9001-send-email
319 ''}
320
321 # XXX: I failed to understand why this one fails.
322 # Could someone try to re-enable it on the next release ?
323 # Tested to fail: 2.18.0 and 2.19.0
324 disable_test t1700-split-index "null sha1"
325
326 # Tested to fail: 2.18.0
327 disable_test t9902-completion "sourcing the completion script clears cached --options"
328
329 # Flaky tests:
330 disable_test t5319-multi-pack-index
331 disable_test t6421-merge-partial-clone
332
333 ${lib.optionalString (!perlSupport) ''
334 # request-pull is a Bash script that invokes Perl, so it is not available
335 # when NO_PERL=1, and the test should be skipped, but the test suite does
336 # not check for the Perl prerequisite.
337 disable_test t5150-request-pull
338 ''}
339 '' + lib.optionalString stdenv.isDarwin ''
340 # XXX: Some tests added in 2.24.0 fail.
341 # Please try to re-enable on the next release.
342 disable_test t7816-grep-binary-pattern
343 # fail (as of 2.33.0)
344 #===( 18623;1208 8/? 224/? 2/? )= =fatal: Not a valid object name refs/tags/signed-empty
345 disable_test t6300-for-each-ref
346 #===( 22665;1651 9/? 1/? 0/? 0/? )= =/private/tmp/nix-build-git-2.33.0.drv-2/git-2.33.0/t/../contrib/completion/git-completion.bash: line 405: compgen: command not found
347 disable_test t9902-completion
348 # not ok 1 - populate workdir (with 2.33.1 on x86_64-darwin)
349 disable_test t5003-archive-zip
350 '' + lib.optionalString stdenv.hostPlatform.isMusl ''
351 # Test fails (as of 2.17.0, musl 1.1.19)
352 disable_test t3900-i18n-commit
353 # Fails largely due to assumptions about BOM
354 # Tested to fail: 2.18.0
355 disable_test t0028-working-tree-encoding
356 '';
357
358 stripDebugList = [ "lib" "libexec" "bin" "share/git/contrib/credential/libsecret" ];
359
360 passthru = {
361 shellPath = "/bin/git-shell";
362 tests = {
363 buildbot-integration = nixosTests.buildbot;
364 };
365 };
366
367 meta = {
368 homepage = "https://git-scm.com/";
369 description = "Distributed version control system";
370 license = lib.licenses.gpl2;
371 changelog = "https://github.com/git/git/blob/v${version}/Documentation/RelNotes/${version}.txt";
372
373 longDescription = ''
374 Git, a popular distributed version control system designed to
375 handle very large projects with speed and efficiency.
376 '';
377
378 platforms = lib.platforms.all;
379 maintainers = with lib.maintainers; [ primeos wmertens globin ];
380 };
381}