2023-01-25 12:59:38 +01:00
# pkg-config {#sec-pkg-config}
*pkg-config* is a unified interface for declaring and querying built C/C++ libraries.
Nixpkgs provides a couple of facilities for working with this tool.
2023-03-27 22:39:11 +02:00
## Writing packages providing pkg-config modules {#pkg-config-writing-packages}
2023-02-03 05:49:15 +01:00
2023-02-15 13:51:06 +01:00
Packages should set `meta.pkgConfigModules` with the list of package config modules they provide.
2024-04-29 16:48:20 +02:00
They should also use `testers.hasPkgConfigModules` to check that the final built package matches that list,
and optionally check that the pkgconf modules' version metadata matches the derivation's.
2023-02-03 05:49:15 +01:00
Additionally, the [`validatePkgConfig` setup hook ](https://nixos.org/manual/nixpkgs/stable/#validatepkgconfig ), will do extra checks on to-be-installed pkg-config modules.
2024-04-29 16:48:20 +02:00
A good example of all these things is miniz:
2023-02-03 05:49:15 +01:00
2024-03-27 17:33:27 +01:00
```nix
2023-02-03 05:49:15 +01:00
{ pkg-config, testers, ... }:
stdenv.mkDerivation (finalAttrs: {
2024-03-27 19:10:27 +01:00
/* ... */
2023-02-03 05:49:15 +01:00
nativeBuildInputs = [ pkg-config validatePkgConfig ];
2024-04-29 16:48:20 +02:00
passthru.tests.pkg-config = testers.hasPkgConfigModules {
package = finalAttrs.finalPackage;
versionCheck = true;
};
2023-02-03 05:49:15 +01:00
meta = {
2024-03-27 19:10:27 +01:00
/* ... */
2024-04-29 16:48:20 +02:00
pkgConfigModules = [ "miniz" ];
2023-02-03 05:49:15 +01:00
};
})
```
2023-03-27 22:39:11 +02:00
## Accessing packages via pkg-config module name {#sec-pkg-config-usage}
2023-02-03 05:49:15 +01:00
2023-03-27 22:39:11 +02:00
### Within Nixpkgs {#sec-pkg-config-usage-internal}
2023-02-03 05:49:15 +01:00
A [setup hook ](#setup-hook-pkg-config ) is bundled in the `pkg-config` package to bring a derivation's declared build inputs into the environment.
This will populate environment variables like `PKG_CONFIG_PATH` , `PKG_CONFIG_PATH_FOR_BUILD` , and `PKG_CONFIG_PATH_HOST` based on:
- how `pkg-config` itself is depended upon
- how other dependencies are depended upon
For more details see the section on [specifying dependencies in general ](#ssec-stdenv-dependencies ).
Normal pkg-config commands to look up dependencies by name will then work with those environment variables defined by the hook.
2023-03-27 22:39:11 +02:00
### Externally {#sec-pkg-config-usage-external}
2023-02-03 05:49:15 +01:00
The `defaultPkgConfigPackages` package set is a set of aliases, named after the modules they provide.
This is meant to be used by language-to-nix integrations.
Hand-written packages should use the normal Nixpkgs attribute name instead.