### pkg_install API
Source: https://github.com/bazelbuild/rules_pkg/blob/main/docs/1.2.0/reference.md
Provides an interface for creating a `bazel run`-able installation script. This is the main entry point for generating installation scripts.
```APIDOC
## pkg_install API
### Description
Creates a `bazel run`-able installation script.
### Method
Function Call
### Endpoint
N/A (Starlark function)
### Parameters
#### Path Parameters
None
#### Query Parameters
None
#### Request Body
None
### Request Example
```python
load("@rules_pkg//pkg:install.bzl", "pkg_install")
pkg_install(
name="install_script",
# ... other arguments for pkg_install ...
)
```
### Response
#### Success Response (200)
Generates an installation script.
#### Response Example
N/A (This function defines a build rule, it does not return a value in the typical sense for API calls.)
```
--------------------------------
### RPM Build Toolchain Setup
Source: https://github.com/bazelbuild/rules_pkg/blob/main/docs/latest.md
Instructions on how to set up the `rpmbuild` toolchain for use with `rules_pkg`.
```APIDOC
## RPM Toolchain Configuration
### Description
Provides rules for creating RPM packages via `pkg_filegroup` and friends.
`pkg_rpm()` depends on the existence of an `rpmbuild` toolchain. Many users will
find it convenient to use the one provided with their system. To enable that
toolchain add the following stanza to WORKSPACE:
### Method
load
### Endpoint
@rules_pkg//toolchains/rpm:rpmbuild_configure.bzl
### Parameters
#### Path Parameters
None
#### Query Parameters
None
#### Request Body
None
### Request Example
```WORKSPACE
# Find rpmbuild if it exists.
load("@rules_pkg//toolchains/rpm:rpmbuild_configure.bzl", "find_system_rpmbuild")
find_system_rpmbuild(name="rules_pkg_rpmbuild")
```
### Response
This configuration snippet enables the system's `rpmbuild` toolchain for use with `rules_pkg`.
```
--------------------------------
### RPM Toolchain Setup
Source: https://github.com/bazelbuild/rules_pkg/blob/main/docs/1.2.0/reference.md
Instructions on how to configure the rpmbuild toolchain for use with rules_pkg.
```APIDOC
## RPM Toolchain Setup
### Description
Provides rules for creating RPM packages via `pkg_filegroup` and friends.
`pkg_rpm()` depends on the existence of an `rpmbuild` toolchain. Many users will find it convenient to use the one provided with their system. To enable that toolchain add the following stanza to WORKSPACE:
### Method
N/A (Configuration)
### Endpoint
N/A (Configuration)
### Parameters
N/A (Configuration)
### Request Example
```bash
# Find rpmbuild if it exists.
load("@rules_pkg//toolchains/rpm:rpmbuild_configure.bzl", "find_system_rpmbuild")
find_system_rpmbuild(name="rules_pkg_rpmbuild")
```
### Response
N/A (Configuration)
```
--------------------------------
### pkg_install Rule
Source: https://github.com/bazelbuild/rules_pkg/blob/main/docs/1.2.0/reference.md
The pkg_install rule creates an installer script from pkg_filegroups. It allows for the creation of runnable installation scripts that can be invoked using `bazel run`.
```APIDOC
## pkg_install
### Description
Create an installer script from pkg_filegroups and friends. This macro allows users to create `bazel run`nable installation scripts using the pkg_filegroup framework.
### Method
`load()`
### Endpoint
`@rules_pkg//pkg:install.bzl`
### Parameters
#### Path Parameters
None
#### Query Parameters
None
#### Request Body
None
### Request Example
```python
pkg_install(
name = "install",
srcs = [
# mapping/grouping targets here
],
destdir = "out/install",
)
```
### Response
#### Success Response (200)
None (This is a macro for rule definition, not an API endpoint).
#### Response Example
None
```
--------------------------------
### RPM Contextual Requires Example
Source: https://github.com/bazelbuild/rules_pkg/blob/main/docs/1.2.0/reference.md
This example demonstrates how to specify contextual requirements for RPM packages, ensuring that specific tools or libraries are available when certain scriptlets are executed. It maps scriptlet types to capability names.
```python
{
"pre": ["GConf2"],
"post": ["GConf2"],
"postun": ["GConf2"]
}
```
--------------------------------
### Run pkg_install Script
Source: https://github.com/bazelbuild/rules_pkg/blob/main/docs/1.2.0/reference.md
Execute the generated installation script using bazel run. Use the --help flag for additional options.
```bash
bazel run -- //path/to:install
```
```bash
bazel run -- //path/to:install --help
```
--------------------------------
### pkg_install Rule
Source: https://github.com/bazelbuild/rules_pkg/blob/main/docs/latest.md
The pkg_install rule creates an executable installer script from pkg_filegroup targets. It allows for flexible installation of files and directories within a Bazel build.
```APIDOC
## pkg_install Rule
### Description
Creates an installer script from pkg_filegroup and other targets. This macro allows users to create `bazel run`-nable installation scripts using the pkg_filegroup framework.
### Method
load
### Endpoint
@rules_pkg//pkg:install.bzl
### Parameters
#### Path Parameters
None
#### Query Parameters
None
#### Request Body
None
### Request Example
```python
pkg_install(
name = "install",
srcs = [
# mapping/grouping targets here
],
destdir = "out/install",
)
```
### Response
#### Success Response (200)
None
#### Response Example
None
### Usage
Installation can be done by invoking:
```bash
bazel run -- //path/to:install
```
Additional features can be accessed by invoking the script with the `--help` option:
```bash
bazel run -- //path/to:install --help
```
**Parameters**
| Name | Description | Default Value |
| :------------- | :------------- | :------------- |
| name | rule name | none |
| srcs | pkg_filegroup framework mapping or grouping targets | none |
| destdir | The default destination directory. If it is specified, this is the default destination to install the files. It is overridable by explicitly specifying `--destdir` in the command line or specifying the `DESTDIR` environment variable. If it is not specified, `--destdir` must be set on the command line, or the `DESTDIR` environment variable must be set. If this is an absolute path, it is used as-is. If this is a relative path, it is interpreted against `BUILD_WORKSPACE_DIRECTORY`. | `None` |
| destdir_flag | A string_flag target used to obtain the value of destdir. | `None` |
| kwargs | common rule attributes | none |
```
--------------------------------
### PackageFilesInfo Provider
Source: https://github.com/bazelbuild/rules_pkg/blob/main/docs/latest.md
Represents the installation of one or more files to a destination with specified attributes. It maps file destinations to their sources.
```bzl
load("@rules_pkg//providers:providers.bzl", "PackageFilesInfo")
PackageFilesInfo(attributes, dest_src_map)
```
--------------------------------
### Define pkg_install Rule
Source: https://github.com/bazelbuild/rules_pkg/blob/main/docs/1.2.0/reference.md
Use this macro to create bazel run-nable installation scripts from pkg_filegroups. Ensure to use the --destdir argument when running from within a bazel rule.
```bazel
load("@rules_pkg//pkg:install.bzl", "pkg_install")
pkg_install(name, srcs, destdir, destdir_flag, **kwargs)
```
```python
pkg_install(
name = "install",
srcs = [
# mapping/grouping targets here
],
destdir = "out/install",
)
```
--------------------------------
### Build and Inspect RPM Package
Source: https://github.com/bazelbuild/rules_pkg/blob/main/examples/rpm/debuginfo/README.md
Use these commands to build the RPM package and then inspect its contents. Ensure you have bazel and rpm2cpio installed.
```bash
bazel build :*
```
```bash
rpm2cpio bazel-bin/test-rpm.rpm | cpio -ivt
```
```bash
cat bazel-bin/content.txt
```
--------------------------------
### Define pkg_install Rule
Source: https://github.com/bazelbuild/rules_pkg/blob/main/docs/latest.md
Use this macro to create runnable installation scripts from pkg_filegroups. Ensure 'destdir' is specified when running from within a Bazel rule.
```bazel
load("@rules_pkg//pkg:install.bzl", "pkg_install")
pkg_install(name = "install",
srcs = [
# mapping/grouping targets here
],
destdir = "out/install")
```
--------------------------------
### Run rpmbuild Docker Container
Source: https://github.com/bazelbuild/rules_pkg/blob/main/contrib/alma9_rpmbuild_docker/README.md
Execute the `run.sh` script to start the AlmaLinux Docker container for `rpmbuild`. This is the initial step to enter the environment.
```console
[rules_pkg]$ cd examples/rpm/system_rpmbuild_bzlmod/
[system_rpmbuild_bzlmod]$ ../../../contrib/alma9_rpmbuild_docker/run.sh
[+] Building 0.1s (9/9) FINISHED
```
--------------------------------
### PackageFilesInfo Provider
Source: https://github.com/bazelbuild/rules_pkg/blob/main/docs/latest.md
Represents the installation of one or more files to a destination, including associated attributes. It maps destination paths to their source files.
```APIDOC
## PackageFilesInfo
Provider representing the installation of one or more files to destination with attributes.
### Fields
* **attributes** (dict) - Attribute information, represented as a `dict`. Keys are strings representing attribute identifiers, values are arbitrary data structures that represent the associated data. For known attributes and data type expectations, see the Common Attributes documentation in the `rules_pkg` reference.
* **dest_src_map** (dict[str, File]) - Map of file destinations to sources. Sources are represented by bazel `File` structures.
```
--------------------------------
### RPM Scriptlet Configuration
Source: https://github.com/bazelbuild/rules_pkg/blob/main/docs/latest.md
Configure scriptlets for RPM package installation and uninstallation.
```APIDOC
## RPM Scriptlets
### Description
Configure scriptlets that are executed during different stages of the RPM package lifecycle.
### Parameters
#### post_scriptlet
- **post_scriptlet** (String) - Optional - RPM `%post` scriptlet. Currently only allowed to be a shell script. `post_scriptlet` and `post_scriptlet_file` are mutually exclusive.
#### post_scriptlet_file
- **post_scriptlet_file** (Label) - Optional - File containing the RPM `%post` scriptlet.
#### posttrans_scriptlet
- **posttrans_scriptlet** (String) - Optional - RPM `%posttrans` scriptlet. Currently only allowed to be a shell script. `posttrans_scriptlet` and `posttrans_scriptlet_file` are mutually exclusive.
#### posttrans_scriptlet_file
- **posttrans_scriptlet_file** (Label) - Optional - File containing the RPM `%posttrans` scriptlet.
#### postun_scriptlet
- **postun_scriptlet** (String) - Optional - RPM `%postun` scriptlet. Currently only allowed to be a shell script. `postun_scriptlet` and `postun_scriptlet_file` are mutually exclusive.
#### postun_scriptlet_file
- **postun_scriptlet_file** (Label) - Optional - File containing the RPM `%postun` scriptlet.
#### pre_scriptlet
- **pre_scriptlet** (String) - Optional - RPM `%pre` scriptlet. Currently only allowed to be a shell script. `pre_scriptlet` and `pre_scriptlet_file` are mutually exclusive.
#### pre_scriptlet_file
- **pre_scriptlet_file** (Label) - Optional - File containing the RPM `%pre` scriptlet.
#### preun_scriptlet
- **preun_scriptlet** (String) - Optional - RPM `%preun` scriptlet. Currently only allowed to be a shell script. `preun_scriptlet` and `preun_scriptlet_file` are mutually exclusive.
#### preun_scriptlet_file
- **preun_scriptlet_file** (Label) - Optional - File containing the RPM `%preun` scriptlet.
```
--------------------------------
### Configure RPM File Attributes
Source: https://github.com/bazelbuild/rules_pkg/blob/main/docs/1.2.0/reference.md
This example demonstrates how to set RPM file attributes like '%config' using the `rpm_filetag` attribute within a packaging rule. This translates to specific entries in the RPM's %files list.
```python
attrs = {"rpm_filetag": ("config(missingok, noreplace)",)},
```
--------------------------------
### RPM Scriptlet Configuration
Source: https://github.com/bazelbuild/rules_pkg/blob/main/docs/1.2.0/reference.md
Configure RPM scriptlets for various stages of package installation and uninstallation.
```APIDOC
## RPM Scriptlet Configuration
This section details the configuration options for RPM scriptlets, which are executed at different stages of the package lifecycle.
### Scriptlet Types
- **`post_scriptlet`**: RPM `%post` scriptlet. Must be a shell script. Mutually exclusive with `post_scriptlet_file`.
- **`post_scriptlet_file`**: File containing the RPM `%post` scriptlet. Mutually exclusive with `post_scriptlet`.
- **`posttrans_scriptlet`**: RPM `%posttrans` scriptlet. Must be a shell script. Mutually exclusive with `posttrans_scriptlet_file`.
- **`posttrans_scriptlet_file`**: File containing the RPM `%posttrans` scriptlet. Mutually exclusive with `posttrans_scriptlet`.
- **`postun_scriptlet`**: RPM `%postun` scriptlet. Must be a shell script. Mutually exclusive with `postun_scriptlet_file`.
- **`postun_scriptlet_file`**: File containing the RPM `%postun` scriptlet. Mutually exclusive with `postun_scriptlet`.
- **`pre_scriptlet`**: RPM `%pre` scriptlet. Must be a shell script. Mutually exclusive with `pre_scriptlet_file`.
- **`pre_scriptlet_file`**: File containing the RPM `%pre` scriptlet. Mutually exclusive with `pre_scriptlet`.
- **`preun_scriptlet`**: RPM `%preun` scriptlet. Must be a shell script. Mutually exclusive with `preun_scriptlet_file`.
- **`preun_scriptlet_file`**: File containing the RPM `%preun` scriptlet. Mutually exclusive with `preun_scriptlet`.
### Default Values
- `post_scriptlet`: `""`
- `posttrans_scriptlet`: `""`
- `postun_scriptlet`: `""`
- `pre_scriptlet`: `""`
- `preun_scriptlet`: `""`
### Parameter Types
- Scriptlet content: String
- Scriptlet file: Label (e.g., `//path/to/script:file`)
### Mutually Exclusive Options
For each scriptlet type (e.g., `post`), you can provide either the scriptlet content directly as a string (`post_scriptlet`) or specify a file containing the scriptlet (`post_scriptlet_file`). You cannot use both for the same scriptlet type.
```
--------------------------------
### PackageDirsInfo Provider
Source: https://github.com/bazelbuild/rules_pkg/blob/main/docs/latest.md
Represents the creation of one or more directories within a package. It takes attributes and a list of installed directory names.
```bzl
load("@rules_pkg//providers:providers.bzl", "PackageDirsInfo")
PackageDirsInfo(attributes, dirs)
```
--------------------------------
### Configure System rpmbuild for WORKSPACE
Source: https://github.com/bazelbuild/rules_pkg/blob/main/README.md
This snippet configures Bazel to find and use the system's rpmbuild executable when using WORKSPACE setup. Set verbose to True for debugging.
```starlark
load("@rules_pkg//toolchains/rpm:rpmbuild_configure.bzl", "find_system_rpmbuild")
find_system_rpmbuild(
name = "rules_pkg_rpmbuild",
verbose = False,
)
```
--------------------------------
### Configure System RPM Build Toolchain
Source: https://github.com/bazelbuild/rules_pkg/blob/main/docs/latest.md
This snippet shows how to load and configure the system's rpmbuild toolchain for use with rules_pkg. Ensure this is added to your WORKSPACE file.
```bzl
# Find rpmbuild if it exists.
load(@rules_pkg//toolchains/rpm:rpmbuild_configure.bzl", "find_system_rpmbuild")
find_system_rpmbuild(name="rules_pkg_rpmbuild")
```
--------------------------------
### Run All Tests
Source: https://github.com/bazelbuild/rules_pkg/blob/main/CLAUDE.md
Execute all tests within the rules_pkg project using Bazel.
```bash
bazel test //tests/...
```
--------------------------------
### strip_prefix
Source: https://github.com/bazelbuild/rules_pkg/blob/main/docs/latest.md
Specifies what prefix of a file's path to discard prior to installation. This is applied to full directory names.
```APIDOC
## strip_prefix
### Description
What prefix of a file's path to discard prior to installation.
This specifies what prefix of an incoming file's path should not be included in the output package at after being appended to the install prefix (the `prefix` attribute). Note that this is only applied to full directory names.
Use the `strip_prefix` struct to define this attribute. If this attribute is not specified, all directories will be stripped from all files prior to being included in packages (`strip_prefix.files_only()`).
If prefix stripping fails on any file provided in `srcs`, the build will fail.
Note that this only functions on paths that are known at analysis time. Specifically, this will not consider directories within TreeArtifacts (directory outputs), or the directories themselves.
### Method
This is an attribute of other rules, not a standalone rule.
### Parameters
#### Path Parameters
- **strip_prefix** (String) - Optional - The prefix to discard from file paths. Defaults to stripping all directories if not specified.
### Request Example
```json
{
"strip_prefix": "src/my_package/"
}
```
### Response
#### Success Response (200)
This attribute modifies how file paths are handled during package creation.
#### Response Example
```json
{
"message": "Prefix 'src/my_package/' will be stripped from file paths."
}
```
```
--------------------------------
### strip_prefix
Source: https://github.com/bazelbuild/rules_pkg/blob/main/docs/1.2.0/reference.md
Specifies what prefix of a file's path to discard prior to installation. This is applied to full directory names.
```APIDOC
## strip_prefix
### Description
What prefix of a file's path to discard prior to installation.
This specifies what prefix of an incoming file's path should not be included in the output package at after being appended to the install prefix (the `prefix` attribute). Note that this is only applied to full directory names, see `strip_prefix` for more details.
Use the `strip_prefix` struct to define this attribute. If this attribute is not specified, all directories will be stripped from all files prior to being included in packages (`strip_prefix.files_only()`).
If prefix stripping fails on any file provided in `srcs`, the build will fail.
Note that this only functions on paths that are known at analysis time. Specifically, this will not consider directories within TreeArtifacts (directory outputs), or the directories themselves. See also #269.
### Method
Not applicable (attribute of another rule).
### Endpoint
Not applicable.
### Parameters
#### Path Parameters
None
#### Query Parameters
None
#### Request Body
* **strip_prefix** (String) - Optional - The prefix to discard. Defaults to `"`.`"` (meaning no prefix is stripped by default if not specified, but the documentation implies `strip_prefix.files_only()` is the default behavior if not specified, which effectively strips all directories).
### Request Example
```json
{
"strip_prefix": "some/path/"
}
```
### Response
#### Success Response (200)
Not applicable (attribute of another rule).
#### Response Example
None
```
--------------------------------
### Build and Inspect RPM Package
Source: https://github.com/bazelbuild/rules_pkg/blob/main/examples/rpm/prebuilt_rpmbuild/README.md
Build an RPM package using Bazel and then inspect its contents using rpm2cpio and cat. This verifies the RPM creation process.
```bash
bazel build :*
rpm2cpio bazel-bin/test-rpm.rpm | cpio -ivt
cat bazel-bin/content.txt
```
--------------------------------
### PackageDirsInfo Provider
Source: https://github.com/bazelbuild/rules_pkg/blob/main/docs/latest.md
Represents the creation of one or more directories within a package. It holds information about attributes and the list of installed directory names.
```APIDOC
## PackageDirsInfo
Provider representing the creation of one or more directories in a package.
### Fields
* **attributes** (dict) - See `attributes` in PackageFilesInfo.
* **dirs** (list[str]) - Installed directory names.
```
--------------------------------
### Build and Inspect Package with Flags
Source: https://github.com/bazelbuild/rules_pkg/blob/main/examples/naming_package_files/readme.md
Build the target with and without the special flag to observe the effect on the generated package names. The `ls` command shows the output files in `bazel-bin`.
```shell
bazel build :example1
ls -l bazel-bin/example1.tar bazel-bin/RulesPkgExamples-k8-fastbuild.tar
```
```shell
bazel build :example1 --define=SPECIAL=1
ls -l bazel-bin/example1*.tar
```
--------------------------------
### pkg_files
Source: https://github.com/bazelbuild/rules_pkg/blob/main/docs/latest.md
Rule for packaging files.
```APIDOC
## pkg_files
Rule for packaging files.
```
--------------------------------
### pkg_mkdirs
Source: https://github.com/bazelbuild/rules_pkg/blob/main/docs/latest.md
Rule for creating directories.
```APIDOC
## pkg_mkdirs
Rule for creating directories.
```
--------------------------------
### Inspect Debian Package Output
Source: https://github.com/bazelbuild/rules_pkg/blob/main/examples/naming_package_files/readme.md
Build the Debian package target and examine the output files in `bazel-bin`. This verifies that the `package_file_name` format was correctly applied to the generated `.deb` file.
```console
$ ls -l bazel-bin/a_deb_package.deb bazel-bin/foo-tools_1-2_k8.deb
lrwxrwxrwx 1 user primarygroup 163 Jul 26 12:56 bazel-bin/a_deb_package.deb -> /home/user/.cache/bazel/_bazel_user/.../execroot/rules_pkg_examples/bazel-out/k8-fastbuild/bin/foo-tools_1-2_k8.deb
-r-xr-xr-x 1 user primarygroup 10662 Jul 26 12:56 bazel-bin/foo-tools_1-2_k8.deb
```
--------------------------------
### Build RPM Package with Bazel
Source: https://github.com/bazelbuild/rules_pkg/blob/main/contrib/alma9_rpmbuild_docker/README.md
After entering the Docker container, use `bazel build` to create RPM packages. This command compiles and packages the specified target.
```console
[devuser@fd4cc85e6e5a system_rpmbuild_bzlmod]$ bazel build test-rpm
Starting local Bazel server (8.3.1) and connecting to it...
INFO: Analyzed target //:test-rpm (80 packages loaded, 3318 targets configured).
INFO: Found 1 target...
Target //:test-rpm up-to-date:
bazel-bin/test-rpm.rpm
bazel-bin/test-rpm-all.rpm
bazel-bin/test-rpm-1-0.all.rpm
INFO: Elapsed time: 5.007s, Critical Path: 0.06s
INFO: 1 process: 9 action cache hit, 1 internal.
INFO: Build completed successfully, 1 total action
```
--------------------------------
### Define pkg_files Target
Source: https://github.com/bazelbuild/rules_pkg/blob/main/docs/1.2.0/reference.md
Use this rule to specify files and their attributes for packaging. It takes a name, source files, and optional attributes like excludes, prefix, and renames.
```bazel
load("@rules_pkg//pkg:mappings.bzl", "pkg_files")
pkg_files(name, srcs, attributes, excludes, include_runfiles, prefix, renames, strip_prefix)
```
--------------------------------
### Configure rpmbuild toolchain
Source: https://github.com/bazelbuild/rules_pkg/blob/main/docs/1.2.0/reference.md
Loads the find_system_rpmbuild function to configure the rpmbuild toolchain. This is necessary for using pkg_rpm and related rules.
```bzl
# Find rpmbuild if it exists.
load("@rules_pkg//toolchains/rpm:rpmbuild_configure.bzl", "find_system_rpmbuild")
find_system_rpmbuild(name="rules_pkg_rpmbuild")
```
--------------------------------
### Build and Inspect Package Named with Toolchain Data
Source: https://github.com/bazelbuild/rules_pkg/blob/main/examples/naming_package_files/readme.md
Build a target that uses toolchain information for naming and inspect the resulting package file in `bazel-bin`. This confirms that the toolchain data was correctly incorporated.
```shell
bazel build :example2
ls -l bazel-bin/example2*.tar
```
--------------------------------
### Run Mappings Analysis Tests
Source: https://github.com/bazelbuild/rules_pkg/blob/main/CLAUDE.md
Execute tests for mappings (pkg_files / pkg_filegroup) using Bazel.
```bash
bazel test //tests/mappings/...
```
--------------------------------
### Configure System RPM Build Toolchain
Source: https://github.com/bazelbuild/rules_pkg/blob/main/docs/1.2.0/reference.md
Load and configure the system's rpmbuild toolchain for use with rules_pkg. This is typically added to the WORKSPACE file.
```bzl
load("@rules_pkg//toolchains/rpm:rpmbuild_configure.bzl", "find_system_rpmbuild")
find_system_rpmbuild(name="rules_pkg_rpmbuild")
```
--------------------------------
### Build and Inspect Archives With Stamping Enabled
Source: https://github.com/bazelbuild/rules_pkg/blob/main/examples/time_stamping/readme.md
Builds archives with the `--stamp=1` option enabled and lists their contents. Observe the differences in timestamps compared to builds without stamping.
```bash
bazel build :* --stamp=1
for tarball in bazel-bin/*.tar ; do
echo ==== $tarball
tar tvf $tarball
done
```
--------------------------------
### Define pkg_files Target
Source: https://github.com/bazelbuild/rules_pkg/blob/main/docs/latest.md
Use this rule to define a set of files and their packaging attributes. It requires a name and a list of source files or labels.
```bazel
load("@rules_pkg//pkg:mappings.bzl", "pkg_files")
pkg_files(name, srcs, attributes, excludes, include_runfiles, package_variables, prefix, renames,
strip_prefix)
```
--------------------------------
### List Contents of Built RPM Package
Source: https://github.com/bazelbuild/rules_pkg/blob/main/contrib/alma9_rpmbuild_docker/README.md
Inspect the files included within a generated RPM package using the `rpm -qlp` command. This helps verify the package contents.
```console
[devuser@fd4cc85e6e5a system_rpmbuild_bzlmod]$ rpm -qlp bazel-bin/test-rpm.rpm
/BUILD
/MODULE.bazel
/README.md
/test_rpm.spec
```
--------------------------------
### Define a pkg_zip target
Source: https://github.com/bazelbuild/rules_pkg/blob/main/docs/1.2.0/reference.md
Use this rule to define a target that creates a zip archive. Specify the name, source files, and output file name. Optional attributes control compression, file modes, and more.
```bazel
load("@rules_pkg//pkg/private/zip:zip.bzl", "pkg_zip")
pkg_zip(
name = "my_archive",
srcs = ["//path/to/files:src1", "//path/to/files:src2"],
out = "my_archive.zip",
# Optional attributes can be added here
# compression_level = 9,
# mode = "0644",
# strip_prefix = "my_dir/",
)
```
--------------------------------
### RPM Build Configuration
Source: https://github.com/bazelbuild/rules_pkg/blob/main/docs/1.2.0/reference.md
Configure build-time settings for RPM packages.
```APIDOC
## RPM Build Configuration
This section covers configuration options related to the RPM build process itself.
### Source Date Epoch
- **`source_date_epoch`**: Value to export as `SOURCE_DATE_EPOCH` to facilitate reproducible builds. This implicitly sets `%clamp_mtime_to_source_date_epoch` in the subordinate `rpmbuild` call for consistent in-RPM file timestamps.
- Type: Integer
- Optional
- Default: `-1` (disables the feature)
- Negative values disable this feature.
### RPM Build Path (Deprecated)
- **`rpmbuild_path`**: Path to a `rpmbuild` binary. This is deprecated in favor of the rpmbuild toolchain.
- Type: String
- Optional
- Default: `""`
```
--------------------------------
### Query Target Outputs with cquery
Source: https://github.com/bazelbuild/rules_pkg/blob/main/examples/where_is_my_output/README.md
Use `cquery` with `--output=starlark` and a Starlark file to inspect target outputs. Redirect stderr to `/dev/null` to clean up output.
```shell
bazel build :deb
bazel cquery :deb --output=starlark --starlark:file=show_all_outputs.bzl 2>/dev/null
```
--------------------------------
### Add rules_pkg using MODULE.bazel
Source: https://github.com/bazelbuild/rules_pkg/blob/main/README.md
Declare rules_pkg as a dependency in your MODULE.bazel file. This is the recommended approach for Bazel 6 and later.
```starlark
bazel_dep(name = "rules_pkg", version = "0.0.10")
```
--------------------------------
### Regenerate Reference Documentation
Source: https://github.com/bazelbuild/rules_pkg/blob/main/CLAUDE.md
Rebuild the reference documentation using Bazel and copy the output to the docs directory. This should be done before committing feature changes.
```bash
bazel build //doc_build:reference
cp bazel-bin/doc_build/reference.md docs/latest.md
```
--------------------------------
### Configure Package Name with Command Line Flags
Source: https://github.com/bazelbuild/rules_pkg/blob/main/examples/naming_package_files/readme.md
Use `config_setting` to capture command-line flags and `select` to conditionally modify package names. This allows for dynamic naming based on build configurations.
```python
config_setting(
name = "special_build",
values = {"define": "SPECIAL=1"},
)
my_package_naming(
name = "my_naming_vars",
special_build = select({
":special_build": "-IsSpecial",
"//conditions:default": "",
}),
)
```
--------------------------------
### Add rules_pkg to WORKSPACE
Source: https://github.com/bazelbuild/rules_pkg/blob/main/README.md
Use this snippet to add the rules_pkg repository to your WORKSPACE file. Ensure you use the correct version and SHA256 checksum.
```starlark
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
http_archive(
name = "rules_pkg",
urls = [
"https://mirror.bazel.build/github.com/bazelbuild/rules_pkg/releases/download/0.9.1/rules_pkg-0.9.1.tar.gz",
"https://github.com/bazelbuild/rules_pkg/releases/download/0.9.1/rules_pkg-0.9.1.tar.gz",
],
sha256 = "8f9ee2dc10c1ae514ee599a8b42ed99fa262b757058f65ad3c384289ff70c4b8",
)
load(@rules_pkg//:deps.bzl", "rules_pkg_dependencies")
rules_pkg_dependencies()
```
--------------------------------
### Copy rpmbuild Binary
Source: https://github.com/bazelbuild/rules_pkg/blob/main/examples/rpm/prebuilt_rpmbuild/README.md
Copy the system's rpmbuild binary to a local path for use by rules_pkg. This ensures a consistent rpmbuild environment.
```bash
cp /usr/bin/rpmbuild local/rpmbuild_binary
```
--------------------------------
### Call pkg_filegroup Function
Source: https://github.com/bazelbuild/rules_pkg/blob/main/docs/latest.md
Demonstrates the signature for calling the pkg_filegroup function. It groups packaging specifications and supports optional prefixes and package variables.
```bzl
pkg_filegroup(name, srcs, package_variables, prefix)
```
--------------------------------
### pkg_mkdirs
Source: https://github.com/bazelbuild/rules_pkg/blob/main/docs/1.2.0/reference.md
Defines the creation and ownership of directories within packages. Use this to create empty directories, explicitly own directories, or set nonstandard permissions.
```APIDOC
## pkg_mkdirs
### Description
Defines creation and ownership of directories in packages.
Use this if:
1) You need to create an empty directory in your package.
2) Your package needs to explicitly own a directory, even if it already owns files in those directories.
3) You need nonstandard permissions (typically, not "0755") on a directory in your package.
### Method
load("@rules_pkg//pkg:mappings.bzl", "pkg_mkdirs")
pkg_mkdirs(name, attributes, dirs)
### Parameters
#### Path Parameters
None
#### Query Parameters
None
#### Request Body
* **name** (Name) - Required - A unique name for this target.
* **attributes** (String) - Optional - Attributes to set on packaged directories. Always use `pkg_attributes()` to set this rule attribute. Defaults to "{}".
* **dirs** (List of strings) - Required - Directory names to make within the package. If any part of the requested directory structure does not already exist within a package, the package builder will create it for you with a reasonable set of default permissions (typically `0755 root.root`).
### Request Example
```json
{
"name": "my_directory_rule",
"dirs": ["path/to/my/dir", "another/dir"]
}
```
### Response
#### Success Response (200)
This rule does not return a value, it defines package content.
#### Response Example
None
```
--------------------------------
### Run Format-Specific Package Tests
Source: https://github.com/bazelbuild/rules_pkg/blob/main/CLAUDE.md
Run tests for specific package formats using Bazel. Note that rpm tests may require an rpm toolchain.
```bash
bazel test //tests/tar/...
```
```bash
bazel test //tests/deb/...
```
```bash
bazel test //tests/zip/...
```
```bash
bazel test //tests/rpm/... # may require rpm toolchain
```
--------------------------------
### Build and Query Stamped RPM Package
Source: https://github.com/bazelbuild/rules_pkg/blob/main/examples/rpm/system_rpmbuild_stamp_bzlmod/README.md
Builds an RPM package with stamped release information and then queries the package details to verify the timestamp. This is useful for tracking build versions.
```bash
bazel build test-rpm-stamped
# look for the build timestamp in the Release field:
rpm -qip bazel-bin/test-rpm-stamped-1.noarch.rpm
```