rules_pkg

repository·main·Indexed 42 days ago

https://github.com/bazelbuild/rules_pkg

A set of Bazel rules for building distribution package formats, including tar, zip, deb, and rpm. It provides capabilities for composing rich package structures, dynamic package naming using toolchains or command-line flags, and integration with system rpmbuild via Bzlmod extensions and AlmaLinux-based containers.

Tokens
44K
Snippets
111
Records
140
Agent score
52%

What's inside rules_pkg

  1. Overview of rules_pkg capabilities

    main

    rules_pkg provides Bazel rules for building various package formats, including:

    • tar: Tar archives
    • zip: Zip archives
    • deb: Debian packages
    • rpm: Red Hat Package Manager files

    Note: As of Bazel 4.x, Bazel uses this rule set for its own distributions. While Bazel contains a limited version of pkg_tar, its features are frozen; all new packaging capabilities are implemented in rules_pkg.

  2. Use package mapping rules to define package contents

    main

    Instead of defining files directly in archive rules, you can use package mapping rules to describe package contents in a type-agnostic way. These rules produce providers that can be consumed by archive-making rules (like pkg_tar or pkg_zip).

    Core Mapping Rules:

    • pkg_files: Describes destinations for rule outputs.
    • pkg_mkdirs: Describes directory structures to be created.
    • pkg_mklink: Describes symbolic links.
    • pkg_filegroup: Groups the above rules together to be added to packages.
  3. Understand package creation helper mapping rules

    main

    The rules_pkg module provides a way to specify package contents in a package-type-agnostic way using provider interfaces. This allows you to define destinations, directory structures, and links before deciding on the final archive format (like .tar or .zip).

    Core helper rules include:

    • pkg_files: Describes destinations for rule outputs.
    • pkg_mkdirs: Describes directory structures.
    • pkg_mklink: Describes symbolic links.
    • pkg_filegroup: Groups the above rules together to be added to packages.
  4. Package creation helper rules

    main

    The rules_pkg module provides a set of helper rules to specify package contents in a way that is agnostic of the final package format (e.g., tar, zip, deb, rpm). These rules define the intent of the package structure, which is then consumed by actual packaging rules.

    Supported helper rules:

    • pkg_files: Describes destinations for rule outputs.
    • pkg_mkdirs: Describes directory structures.
    • pkg_mklink: Describes symbolic links.
    • pkg_filegroup: Creates groupings of the above to be added to packages.
  5. How package mapping rules work together

    main

    The rules_pkg module provides a way to specify package contents in a format that is agnostic to the final package type (e.g., .tar, .zip, .deb).

    Instead of defining files directly inside a pkg_tar rule, you can use mapping rules to describe the desired structure. These mapping rules produce provider interfaces that the actual packaging rules (like pkg_tar or pkg_zip) consume.

    Core Mapping Rules:

    • pkg_files: Describes destinations for rule outputs.
    • pkg_mkdirs: Describes directory structures.
    • pkg_mklink: Describes symbolic links.
    • pkg_filegroup: Groups the above rules together to be added to a package.
  6. Package content abstraction rules

    main

    The rules_pkg module provides a way to specify package contents in a format that is agnostic of the final package type (e.g., whether it becomes a .tar, .zip, .deb, or .rpm). You can use these rules to build up a logical representation of a package's structure before passing it to a final packaging rule.

    Core Abstractions:

    • pkg_files: Defines destinations for rule outputs.
    • pkg_mkdirs: Defines directory structures.
    • pkg_mklink: Defines symbolic links.
    • pkg_filegroup: Groups the above rules together to be consumed by a packaging rule.
  7. Understand packaging providers in rules_pkg

    main

    The rules_pkg project uses several providers to communicate packaging information between rules:

    • PackageArtifactInfo: Contains metadata about a package artifact, including the file object and the label that produced it.
    • PackageDirsInfo: Represents the creation of one or more directories in a package. Contains attributes and a list of dirs (installed directory names).
    • PackageFilegroupInfo: A collection of related packaging providers. It groups pkg_files, pkg_dirs, and pkg_symlinks along with their origin labels for better diagnostics.
    • PackageFilesInfo: Represents the installation of files to a destination with specific attributes and a dest_src_map (mapping file destinations to source File structures).
    • PackageSymlinkInfo: Represents a single symbolic link, containing attributes, the destination (filesystem link name), and the target (filesystem link target).
  8. How time stamping works in rules_pkg

    main

    Target declarations in rules_pkg can use the stamp attribute to control whether files within an archive (like a .tar file) are time-stamped during the build.

    This behavior follows the standard Bazel pattern used by the cc_binary rule. The stamp attribute allows you to define whether a target is always stamped, never stamped, or controlled by the global --stamp command-line flag.

  9. How package creation helper mapping rules work

    main

    The rules_pkg module provides a set of helper rules to describe the contents of a package in a way that is agnostic of the final package type (e.g., .tar, .zip, .deb). Instead of defining files directly in a packaging rule, you can use these helpers to build a logical structure:

    • pkg_files: Describes destinations for rule outputs.
    • pkg_mkdirs: Describes directory structures.
    • pkg_mklink: Describes symbolic links.
    • pkg_filegroup: Creates groupings of the above to be added to packages.

    These helpers allow you to define what goes into a package and where it goes, while the actual packaging rules (like pkg_tar or pkg_zip) handle the how of creating the archive.

  10. Use contextual requirements in pkg_rpm

    main

    The requires_contextual attribute allows you to specify that certain capabilities are only required during specific stages of the package lifecycle (e.g., during a scriptlet execution). This is useful for ensuring tools exist when %pre or %post scripts run, without making them a mandatory runtime dependency for all users.

    Valid keys for the dictionary include:

    • pre
    • post
    • preun
    • postun
    • pretrans
    • posttrans

    Example: To require GConf2 specifically during the pre, post, and postun scriptlet phases, use:

    requires_contextual = {"pre": ["GConf2"], "post": ["GConf2"], "postun": ["GConf2"]}
    requires_contextual = {
        "pre": ["GConf2"],
        "post": ["GConf2"],
        "postun": ["GConf2"]
    }
  11. Common attributes for rules_pkg rules

    main

    Several rules in rules_pkg share a common set of attributes used to define output files and package naming.

    • out: (Required) The name of the output file. This file is always created and serves as the access point for the package content. If package_file_name is provided, out will be a symlink to that file.
    • package_file_name: (Optional) The actual name of the file containing the package. It supports variable substitution using {var} or $(var). Substitution values come from package_variables or ctx.var.
    • package_variables: (Optional) A label pointing to a target that provides PackageVariablesInfo. These variables are used for substitution in package_file_name (and in package_dir for pkg_zip and pkg_tar).
    • attributes: (Optional) Properties to set on entities created within the package (e.g., file permissions). This is distinct from Bazel rule attributes.
    # Example of how out, package_file_name, and package_variables interact
    # (Conceptual representation based on documentation)
    
    # If package_file_name is 'app-{version}.deb' and package_variables provides version='1.0',
    # 'out' will be a symlink to 'app-1.0.deb'.
  12. Control file timestamping with the stamp attribute

    main

    Target declarations in rules_pkg can use the stamp attribute to control whether files in an archive (like an RPM or ZIP) are timestamped with the current build time. This follows the same behavior pattern as the cc_binary rule in Bazel. When stamp = 1 (or True), the archive will include build-time timestamps, which is useful for identifying when a package was built.

    # Example concept (not a full BUILD file)
    pkg_rpm(
        name = "test-rpm-stamped",
        stamp = 1,
        ...
    )