1{ lib
2, stdenv
3, fetchurl
4, fetchpatch
5, gettext
6, libgpg-error
7, enableCapabilities ? false, libcap
8, buildPackages
9# for passthru.tests
10, gnupg
11, libotr
12, rsyslog
13}:
14
15assert enableCapabilities -> stdenv.isLinux;
16
17stdenv.mkDerivation rec {
18 pname = "libgcrypt";
19 version = "1.10.2";
20
21 src = fetchurl {
22 url = "mirror://gnupg/libgcrypt/${pname}-${version}.tar.bz2";
23 hash = "sha256-O5wCoAS2jCVq3ZlwHeALODrMzPNxd+DWxYKJZkzODAM=";
24 };
25
26 patches = lib.optionals (!stdenv.isLinux) [ # not everywhere to avoid rebuild for now
27 (fetchpatch {
28 name = "getrandom-conditionalize.patch";
29 url = "https://git.gnupg.org/cgi-bin/gitweb.cgi?p=libgcrypt.git;a=commitdiff_plain;h=d41177937cea4aa1e9042ebcd195a349c40e8071";
30 hash = "sha256-CgQjNtC1qLe5LicIc8rESc6Z1u4fk7ErMUVcG/2G9gM=";
31 })
32 ];
33
34 outputs = [ "out" "dev" "info" ];
35 outputBin = "dev";
36
37 # The CPU Jitter random number generator must not be compiled with
38 # optimizations and the optimize -O0 pragma only works for gcc.
39 # The build enables -O2 by default for everything else.
40 hardeningDisable = lib.optional stdenv.cc.isClang "fortify";
41
42 depsBuildBuild = [ buildPackages.stdenv.cc ];
43
44 buildInputs = [ libgpg-error ]
45 ++ lib.optional stdenv.isDarwin gettext
46 ++ lib.optional enableCapabilities libcap;
47
48 strictDeps = true;
49
50 configureFlags = [ "--with-libgpg-error-prefix=${libgpg-error.dev}" ]
51 ++ lib.optional (stdenv.hostPlatform.isMusl || (stdenv.hostPlatform.isDarwin && stdenv.hostPlatform.isAarch64)) "--disable-asm"; # for darwin see https://dev.gnupg.org/T5157
52
53 # Necessary to generate correct assembly when compiling for aarch32 on
54 # aarch64
55 configurePlatforms = [ "host" "build" ];
56
57 postConfigure = ''
58 sed -i configure \
59 -e 's/NOEXECSTACK_FLAGS=$/NOEXECSTACK_FLAGS="-Wa,--noexecstack"/'
60 '';
61
62 # Make sure libraries are correct for .pc and .la files
63 # Also make sure includes are fixed for callers who don't use libgpgcrypt-config
64 postFixup = ''
65 sed -i 's,#include <gpg-error.h>,#include "${libgpg-error.dev}/include/gpg-error.h",g' "$dev/include/gcrypt.h"
66 '' + lib.optionalString enableCapabilities ''
67 sed -i 's,\(-lcap\),-L${libcap.lib}/lib \1,' $out/lib/libgcrypt.la
68 '';
69
70 # TODO: figure out why this is even necessary and why the missing dylib only crashes
71 # random instead of every test
72 preCheck = lib.optionalString stdenv.isDarwin ''
73 mkdir -p $out/lib
74 cp src/.libs/libgcrypt.20.dylib $out/lib
75 '';
76
77 doCheck = true;
78
79 passthru.tests = {
80 inherit gnupg libotr rsyslog;
81 };
82
83 meta = with lib; {
84 homepage = "https://www.gnu.org/software/libgcrypt/";
85 changelog = "https://git.gnupg.org/cgi-bin/gitweb.cgi?p=${pname}.git;a=blob;f=NEWS;hb=refs/tags/${pname}-${version}";
86 description = "General-purpose cryptographic library";
87 license = licenses.lgpl2Plus;
88 platforms = platforms.all;
89 maintainers = with maintainers; [ vrthra ];
90 };
91}