- Zig 86%
- Nix 14%
zig-build-gnutls #
Zig 0.17 build package for GnuTLS. It builds libgnutls directly from the official release tarball as a static library and exposes both the library artifact and raw translate-C bindings. Autotools and a system C compiler are not required.
nettle and gmp come from zig-build-nettle, which uses zig-build-gmp. libtasn1 and libunistring are the copies bundled with GnuTLS.
Use as a dependency #
Add this package to your project's build.zig.zon, then pass the target and
optimization mode when requesting the dependency:
const gnutls = b.dependency("gnutls", .{
.target = target,
.optimize = optimize,
});
Import the translated GnuTLS API as a Zig module:
exe.root_module.addImport("gnutls", gnutls.module("gnutls"));
The module covers every installed header (gnutls/gnutls.h,
gnutls/x509.h, gnutls/abstract.h, gnutls/compat.h and the others) and
links libgnutls, libhogweed, libnettle, gmp and libc transitively. It can be
used with const gnutls = @import("gnutls");.
Alternatively, link the C library artifact directly:
exe.root_module.linkLibrary(gnutls.artifact("gnutls"));
The artifact links nettle, hogweed and gmp transitively and propagates the
installed gnutls/*.h include path.
As upstream's configure points out for static builds, applications should
call gnutls_global_init() themselves rather than rely on the library
constructor.
Options #
-Dtrust-store-file=<path>: the CA bundle thatgnutls_certificate_set_x509_system_trustandgnutls_x509_trust_list_add_system_trustload. The default on Linux is/etc/ssl/certs/ca-certificates.crt(Debian, Ubuntu, Arch, Alpine, NixOS); Fedora and RHEL use/etc/pki/tls/certs/ca-bundle.crt. On macOS there is no default, see below. An empty value means no trust store: the system trust functions then returnGNUTLS_E_UNIMPLEMENTED_FEATURE. A configured file that is missing at run time yields no certificates.
The system priority file is /etc/gnutls/config, as in an upstream build
with --sysconfdir=/etc. GnuTLS reads it at run time, so system-wide crypto
policies such as Fedora's apply. A missing file is ignored.
Supported targets #
x86_64 and aarch64, each with linux-gnu (glibc 2.25 or later, for
getrandom), linux-musl or macos. The build stops with an error for any
other target. Windows is not supported.
The build corresponds to configuring upstream with
--disable-shared --enable-static --with-included-libtasn1
--with-included-unistring --without-p11-kit --without-idn --without-tpm
--without-tpm2 --without-zlib --without-brotli --without-zstd
--disable-libdane --disable-cxx --disable-tools --disable-doc --disable-nls
--disable-hardware-acceleration --disable-padlock --enable-srp-authentication
--with-default-trust-store-file=/etc/ssl/certs/ca-certificates.crt
--with-system-priority-file=/etc/gnutls/config
- Only portable C is compiled: no x86 or aarch64 assembly, no padlock, no AF_ALG and no cryptodev. nettle is also built without assembly. This is noticeably slower than a distribution build.
- Enabled are TLS 1.0 to 1.3, DTLS, X.509, OCSP, PKCS #7, #8 and #12, PSK, SRP, anonymous authentication, DHE, ECDHE, GOST, DSA, ALPN, DTLS-SRTP, the non-Suite-B curves and the SSL 2.0 client hello. These are upstream's defaults, plus SRP.
- Disabled are SSL 3.0, heartbeat, HPKE, KTLS, FIPS 140 mode, SHA-1 signatures and crypto auditing (upstream's defaults), and PKCS #11 (p11-kit), IDNA (libidn2), TPM, certificate compression, the DANE and OpenSSL compatibility libraries, the C++ library, the tools, the documentation and translations.
- On macOS the system trust does not come from the keychain, because that
needs Apple's Security framework, which Zig cannot link without an Apple
SDK. Pass
-Dtrust-store-file=/etc/ssl/cert.pemto use the CA bundle that macOS ships for OpenSSL and LibreSSL.
GnuTLS calls functions through pointers of other types. The library sources
are therefore compiled with -fno-sanitize=function, because Zig's C
sanitizer in safe modes would otherwise trap on these calls.
Build and test #
Zig 0.17.0 is required.
zig build
zig build test
The default build installs libgnutls.a, libnettle.a, libhogweed.a,
libgmp.a and their headers under zig-out/. The test step runs a Zig smoke
test through the gnutls module, covering what Gwenhywfar uses: it
creates an ECDSA CA and an RSA server certificate, imports them from PEM and
checks the DN, hostname, fingerprint, times, RSA parameters and chain
verification, round-trips PKCS #3 DH parameters, and runs TLS 1.3 and
TLS 1.2 (DHE-RSA) handshakes between a client and a server over in-memory
push and pull functions, with certificate verification and records in both
directions.
The Nix flake provides the pinned Zig toolchain and two checks:
nix develop
nix flake check
checks.defaultruns the build and the test step. Inside the Nix build sandbox Zig skips natively linked test executables, so on Linux it also runs the test step for the static<arch>-linux-musltarget.checks.symbolsbuilds the same tarball with upstream's own configure and Makefiles, with the options above, against nixpkgs' nettle and gmp. It requires that itslibgnutls.aexports exactly the same symbols as the Zig build, apart from gnulib's own (see below), and the samegnutls/gnutls.h, apart from the line Zig's ConfigHeader adds at the top.
How the build works #
Upstream's configure and Makefiles generate several files at build time:
config.his produced fromconfig.h.in. About 170 of its roughly 800 entries are mentioned by the sources this build compiles or by their headers.build.ziggives these the values configure finds on each supported target, and leaves every entry it does not list undefined. The result was compared with configure's output for all six targets.gnutls/gnutls.his produced fromgnutls.h.inwith the version and the POSIXgiovec_tdefinition.- The bundled libunistring headers are produced from their
.in.htemplates, whose only variables are Windows DLL markers. - translate-C reads a generated umbrella header that includes every installed header.
Upstream also builds about a hundred gnulib modules and generates replacement
system headers (stdio.h, string.h and so on) that redirect libc functions
gnulib distrusts to its own versions. This build compiles only the gnulib
modules that libgnutls calls (lists, read_file, c_strcasecmp, once
and locks, memset_explicit; on macOS also explicit_bzero,
secure_getenv, strverscmp and byteswap.h) against the system headers,
and declares the few functions that libc lacks in its config.h.
Upstream source #
- Version:
3.8.13 - Archive: https://www.gnupg.org/ftp/gcrypt/gnutls/v3.8/gnutls-3.8.13.tar.xz
- SHA-256:
ffed8ec1bf09c2426d4f14aae377de4753b53e537d685e604e99a8b16ca9c97e
The archive is pinned by its Zig package hash in build.zig.zon.
License #
Like the GnuTLS library itself, this package is licensed under the GNU Lesser
General Public License, version 2.1 or later. See LICENSE.