Product SiteDocumentation Site

Cap. 20. Customizing RPM Behavior

20.1. Customizing with RPM Macros
20.1.1. Defining macros
20.1.2. Customizing Macros
20.2. Configuring RPM Settings
20.2.1. Viewing the current settings
20.2.2. Locating the rpmrc files
20.2.3. Changing settings
20.3. Adding Popt Aliases
20.3.1. Defining aliases
20.3.2. Customizing popt aliases
20.4. Summary
This chapter covers:
RPM provides a high degree of customization in the form of basic settings, such as where your RPM database is located and the path to common commands like setup or make, to the ability to define new macros and command-line aliases. This chapter covers the three main ways to customize RPM behavior: RPM macros, RPM rc settings, and popt aliases.

Customizing with RPM Macros

Starting with RPM 3.0, RPM macros have replaced most RPM settings from the rpmrc files.
An RPM macro defines a setting to the RPM system. A macro can be defined as a static value, such as the directory where the RPM database is installed. A macro can also be defined in terms of other macros. Furthermore, a macro can be defined with parameters.
For example, the following defines two macros in a macro file:
%_usr /usr
%_usrsrc %{_usr}/src
In this case, the macro %_usr names the /usr directory. The macro %_usrsrc names the /usr/src directory, showing how to set a macro in terms of another macro.
Cross Reference
The macro syntax is used in the query formats introduced in Cap. 4, Using the RPM Database .

Defining macros

RPM provides a number of places you can define macros, including inside spec files (probably the most common usage), in special macro initialization files, and on the command line.
In each case, the syntax is slightly different.

Defining Macros in Spec Files

You can define macros in most places in a spec file. With spec files, use the %define syntax to define a macro, which uses the following syntax:
%define name(options) body
The options are optional and can include the special values defined in Table 21-1.
Table 21-1 Special options for macros
Option
Holds
%0
The name of the macro
%1
The first argument, after processing with getopt
%2-%9
Additional arguments
%*
All arguments, except for flags that have been processed
%#
Number of arguments
These options are similar to those for shell scripting.
Cross Reference
In addition to these options, you can use a syntax of %{-a} to hold –a if an option of –a was passed. The syntax of %{-a*} indicates the value passed after the –a option.
You can assign a macro to the value returned by a shell command by using syntax like the following:
%(shell_command)
Note
This is similar to the $(shell_command) syntax supported by bash.

Defining Macros in Macro Initialization Files

Inside a macro file, define macros with the following syntax:
%macro_name value
Macros defined for the RPM system start with an underscore. Some older macros are left without the leading underscore.
Note
The macros defined with an underscore are not exported into rpm headers.
The %expand built-in macro will expand the value of something else, including executing a shell command. For example, the following sets the user’s home directory to the %home macro:
%home %{expand:%%(cd; pwd)}
Note that it is probably easier to simply set the %home macro in your per-user $HOME/.rpmmacros file to the name of your home directory rather than try to figure this out programmatically.

Defining Macros on the Command Line

The rpm command also lets you define macros with the --define option. The basic syntax is:
$ rpm --define 'macro_name value'
Note
Do not place the leading percent sign, %, on the macro you define with --define.
You can evaluate a macro or a macro expression with --eval. For example:
$ rpm --eval %_usrsrc
/usr/src

Customizing Macros

You can add your own macro definitions, using the syntax shown in the “Defining Macros in Macro Initialization Files” section. These macros are read on each invocation of the rpm or rpmbuild commands.
To add your custom macros, you must edit one of the macro definition files. Table 21-2 lists the macro definition files and their usage.
Table 21-2 RPM macro files
File
Usage
/usr/lib/rpm/macros
Official RPM macros
/etc/rpm/macros
Per-system customizations
$HOME/.rpmmacros
Per-user customizations
Note
Do not edit the /usr/lib/rpm/macros file, as this file gets overwritten when you upgrade to a new version of rpm.