There are several use cases for environment variables:
Conan global configuration environment variables (e.g.
CONAN_COMPRESSION_LEVEL). They can be configured in conan.conf or as system environment variables, and control conan behavior.
Package recipes can access environment variables to determine their behavior. A typical example would be when launching CMake, it will check for CC and CXX environment variables to define the compiler to use. These variables are mostly transparent for conan, and just used by the package recipes.
- Environment variables can be set in different ways:
global, at the OS level, with export VAR=Value or in Windows SET VAR=Value.
In the conan command line: conan install -e VAR=Value.
In profile files.
In package recipes in the
self.env_infofield, so they are activated for dependent recipes.
Defining environment variables
You can use profiles to define environment variables that will apply to your recipes. You can also use -e parameter in conan install, conan info and conan create commands.
If you want to override an environment variable that a package has inherited from its requirements, you can use either profiles or -e to do it:
conan install . -e MyPackage:PATH=/other/path
If you want to define an environment variable but you want to append the variables declared in your requirements you can use the
$ conan install . -e PYTHONPATH=[/other/path]
This way the first entry in the PYTHONPATH variable will be /other/path but the PYTHONPATH values declared in the requirements of the project will be appended at the end using the system path separator.
Automatic environment variables inheritance
If your dependencies define some
env_info variables in the
package_info() method they will be automatically applied before calling
the consumer conanfile.py methods
imports(). You can read more about
For example, if you are creating a package for a tool, you can define the variable
name = "my_tool_installer"
If another conan recipe requires the my_tool_installer in the
imports() the bin folder of
my_tool_installer package will be automatically appended to the system PATH. If
my_tool_installer packages an executable called
my_tool_executable in the bin of the package folder we can directly call the tool, because it will be available in the path:
name = "my_lib_example"
You could also set
CXX variables if we are packing a compiler to define what compiler to use or any other environment variable.
Read more about tool packages here.