1# pkg-config {#sec-pkg-config}
2
3*pkg-config* is a unified interface for declaring and querying built C/C++ libraries.
4
5Nixpkgs provides a couple of facilities for working with this tool.
6
7## Writing packages providing pkg-config modules {#pkg-config-writing-packages}
8
9Packages should set `meta.pkgConfigModules` with the list of package config modules they provide.
10They should also use `testers.hasPkgConfigModules` to check that the final built package matches that list,
11and optionally check that the pkgconf modules' version metadata matches the derivation's.
12Additionally, the [`validatePkgConfig` setup hook](https://nixos.org/manual/nixpkgs/stable/#validatepkgconfig) will do extra checks on to-be-installed pkg-config modules.
13
14A good example of all these things is miniz:
15
16```nix
17{ pkg-config, testers, ... }:
18
19stdenv.mkDerivation (finalAttrs: {
20 # ...
21
22 nativeBuildInputs = [
23 pkg-config
24 validatePkgConfig
25 ];
26
27 passthru.tests.pkg-config = testers.hasPkgConfigModules {
28 package = finalAttrs.finalPackage;
29 versionCheck = true;
30 };
31
32 meta = {
33 # ...
34 pkgConfigModules = [ "miniz" ];
35 };
36})
37```
38
39## Accessing packages via pkg-config module name {#sec-pkg-config-usage}
40
41### Within Nixpkgs {#sec-pkg-config-usage-internal}
42
43A [setup hook](#setup-hook-pkg-config) is bundled in the `pkg-config` package to bring a derivation's declared build inputs into the environment.
44This will populate environment variables like `PKG_CONFIG_PATH`, `PKG_CONFIG_PATH_FOR_BUILD`, and `PKG_CONFIG_PATH_HOST` based on:
45
46 - how `pkg-config` itself is depended upon
47
48 - how other dependencies are depended upon
49
50For more details see the section on [specifying dependencies in general](#ssec-stdenv-dependencies).
51
52Normal pkg-config commands to look up dependencies by name will then work with those environment variables defined by the hook.
53
54### Externally {#sec-pkg-config-usage-external}
55
56The `defaultPkgConfigPackages` package set is a set of aliases, named after the modules they provide.
57This is meant to be used by language-to-nix integrations.
58Hand-written packages should use the normal Nixpkgs attribute name instead.