Hatchling build hook¶
Use this when you build wheels with Hatchling and want an SBOM embedded automatically -- no separate CLI step to remember.
Pitloom embeds the SBOM at
.dist-info/sboms/<name>-<version>.spdx3.json (e.g.
.dist-info/sboms/mypackage-1.0.0.spdx3.json), per PEP 770 (wheels
only), as compact canonical JSON. The name and version are escaped as for
loom project, so no project metadata can make the file name a path.
Quick guide¶
[build-system]
requires = ["hatchling>=1.29.0", "pitloom>=0.23.4"]
build-backend = "hatchling.build"
[tool.hatch.build.hooks.pitloom]
# This can be empty
That's all -- hatch build and python -m build embed the SBOM.
Installation¶
Add pitloom as a build requirement (Hatchling 1.29.0+ required) and
add the [tool.hatch.build.hooks.pitloom] table to pyproject.toml
(as shown above) -- both parts are required. Listing pitloom under
[build-system] requires alone does not activate the hook: Hatchling
only runs hooks whose name appears under [tool.hatch.build.hooks], so
the table itself is what turns it on, even left empty. A Hatchling older
than that, or one whose version cannot be read, fails the build before the
hook makes an SBOM
(library-unusable/hatchling).
No separate pip install step is needed beyond that -- the build
front-end (pip, build, hatch) installs pitloom as a build-time
dependency automatically, the same way it installs Hatchling itself.
To match LICENSE files, the build machine also needs the LicenseID
database: run licenseid update --source packaged once (needs licenseid
installed, as it is with pip install pitloom; no network). Without it the
hook logs a WARNING: ("licenseid database not found: ... licence text
detection skipped ... [license-db-missing]"), or an INFO: when a licence is
declared, and still reads the declared licence.
[!NOTE] Hatchling 1.32.3 changed the build-hook plugin interface, and 1.32.4 reverted the change. Pitloom works with both, but other build-hook plugins written for the usual interface may fail to import on 1.32.3. If you pin Hatchling, prefer any version other than 1.32.3.
Usage details¶
Every hatch build/python -m build invocation:
- Generates a Build SBOM (
software_SbomType.build) for the project being built. - Merges in any fragments registered under
[tool.pitloom.fragment](see the Python API tracking decorator, or a hand-authored fragment). - Embeds the result into the wheel's
.dist-info/sboms/directory.
[!NOTE] Lock-file resolution and lock-file SHA-256 hash preservation are source-stage only (
loom project/loom generate). Wheels built viahatch builddo not consult source-stage lock files; dependency integrity hashes in embedded build SBOMs are resolved via PyPI (when online) or omitted (when offline).
The table's enabled key defaults to true, so an empty
[tool.hatch.build.hooks.pitloom] is enough. Set enabled = false
inside it to skip generation for a particular build without removing
the table.
Besides enabled, the table takes the keys Hatchling itself reads from any
hook (enable-by-default, dependencies, require-runtime-dependencies,
require-runtime-features); any other key is an error.
Status/skip/deviation messages the hook logs during a build follow the
same grep-able INFO:/WARNING:/ERROR: stderr convention as the
loom CLI (see Command line) -- e.g. a merge failure (see
Merge fragments) fails the build outright
with an ERROR: Pitloom build hook: <reason> line (once, whatever the
cause: an invalid setting, a missing required = true fragment, a malformed
SOURCE_DATE_EPOCH), and a disabled hook logs INFO: Pitloom build hook:
disabled; skipping SBOM generation [hook-no-effect].
When the hook has staged the SBOM, it says so in one INFO: line that names
the SBOM file and the number of fragments merged into it; that line is a
summary, so it carries no [code].
The hook writes its SBOM into a temporary staging directory, removed when
the build finishes, and also when Hatchling never calls the hook's
finalize() (--hooks-only, a failed build). A staging directory that
cannot be fully removed (a file
still held open on Windows, for example) is one WARNING: Pitloom build hook:
DIR=<dir>: could not be fully removed; left in the system temp directory
[temp-dir-unwritable] naming it, and the build still succeeds.
The hook skips, with one INFO: line, a build that is not a wheel (an
sdist), and the editable variant of a wheel: an editable wheel holds a
.pth file, not the project's files, so an SBOM would list files that are
not in it. The three lines (disabled, not a wheel, editable) share the code
hook-no-effect, which means the
build is one the hook leaves alone and goes on, and are checked in that
order: a disabled hook says only that it is disabled. For an sdist the line
reads INFO: Pitloom build hook: target <target> is not 'wheel'; skipping
SBOM generation [hook-no-effect], and for an editable wheel INFO: Pitloom
build hook: build variant 'editable' skipped; its wheel does not hold the
project's files [hook-no-effect].
A [tool.hatch.build.targets.wheel] sbom-files entry with the same file
name as the hook's SBOM, ignoring case (typically loom project's default
<name>-<version>.spdx3.json), is an ERROR:, as the wheel would hold the
name twice, or two names that Windows and macOS extract onto one file: set
[tool.pitloom] sbom-basename to another name, or drop the entry. The line
reads ERROR: FILE=<file>: also the name, ignoring case, of sbom-files entry
'<path>'; set another sbom-basename in [tool.pitloom] or drop the entry
[sbom-name-conflict] (see Message codes: Embedded
SBOMs).
Reproducible builds¶
With SOURCE_DATE_EPOCH set (or creation-datetime in
[tool.pitloom.creation]), repeated builds of unchanged sources give the same
wheel bytes. Without either, the SBOM's created and its package's builtTime
come from the clock, so the wheel differs between builds even though Hatchling
itself is reproducible; the hook says so with one INFO:
(datetime-unpinned): Pitloom
build hook: created and builtTime come from the clock; set SOURCE_DATE_EPOCH or
creation-datetime to pin them. A SOURCE_DATE_EPOCH that is not an integer
number of seconds (SOURCE_DATE_EPOCH=abc: not an integer number of seconds
since the Unix epoch) or is out of range (out of range for a date) is an
ERROR: that fails the build
(source-date-epoch-invalid), as the
specification asks, even with creation-datetime set.
Configuration¶
Basename and fragments are configured under [tool.pitloom]:
[tool.pitloom]
sbom-basename = "custom-bom" # -> "custom-bom.spdx3.json" (default: "<name>-<version>")
# (a trailing ".spdx3.json" is dropped, with a WARNING)
[tool.pitloom.fragment]
files = ["fragments/model.json"] # merge externally tracked fragments
The hook does not act on describe-relationship or update-id-registry: a
build backend does not write to the source tree (an ID registry is read, never
updated, as with embed-wheel), and relationship descriptions are not written.
Each of them set to true in [tool.pitloom] gets one
INFO: Options: ... has no effect line (false agrees with the hook, so is
silent); harvest ids with loom project instead.
A URL field of the SBOM that holds a credential does not fail the build by
default: the SBOM is written as it is, with one WARNING: per value that ends
with how to refuse it:
WARNING: URL=https://example.org/?token=***: holds a credential (https://spdx.org/spdxdocs/demo-3d1e...#Package-1 /software_downloadLocation, sha256:3f62f6937a192ae3); the SBOM states it as written; remove it from the source, or refuse such values with [tool.pitloom] url-credentials = "refuse" [credentials-unsafe/written]
With url-credentials = "refuse" in [tool.pitloom] (recommended in CI), it
fails the build, unless accept-url-credentials lists its fingerprint (the hook
reads no flag). Each ERROR: line starts Pitloom build hook: and ends with
that advice:
ERROR: Pitloom build hook: URL=https://example.org/?token=***: holds a credential (https://spdx.org/spdxdocs/demo-3d1e...#Package-1 /software_downloadLocation, sha256:3f62f6937a192ae3); remove it from the source, or accept this value by listing it in [tool.pitloom] accept-url-credentials [credentials-unsafe/refused]
ERROR: Pitloom build hook: 1 URL value(s) in the SBOM hold credentials; the SBOM is refused [credentials-unsafe/refused]
See Credentials in URLs.
Creator/tool metadata uses the same [[tool.pitloom.creator]] /
[[tool.pitloom.creation-tool]] / [tool.pitloom.creation] tables the
CLI reads -- see Creation metadata. Provenance
detail is controlled the same way too -- see Metadata
provenance.
See also¶
- Command line -- generate an SBOM manually or post-process built
wheels with
loom embed-wheel. - Dependency sources and precedence -- why wheel embedding scopes dependencies to the build stage.
- GitHub Action -- embed PEP 770 SBOMs in CI for any build backend.
- Python API -- the tracking decorator that produces the fragments this hook merges.