Repository navigation
feat(nixpkgs-clarity): add the scan_nix_package pipeline for single nixpkg #1938
#2291
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
tdruez
wants to merge
8
commits into
main
Choose a base branch
from
2209-nix-pipeline
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
a492d17
feat: add the scan_nix_package pipeline #1938
tdruez bdca7a6
select the scan_nix_package pipeline for Nix PURL inputs
tdruez b5d7173
move the Nix test fixtures out of gitignored directories
tdruez c915b86
skip the Nix pipeline integration test on macOS
tdruez 44bdfee
Merge branch 'main' into 2209-nix-pipeline
tdruez e1c4169
skip non-deployed outputs, reject unknown output, and scan without so…
tdruez 741e46a
keep compound licenses, record provenance, and isolate the Nix cache
tdruez 4351894
handle GitHub rate limits, Nix timeouts, macOS binaries, and stream N…
tdruez File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,230 @@ | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
| # | ||
| # http://nexb.com and https://github.com/aboutcode-org/scancode.io | ||
| # The ScanCode.io software is licensed under the Apache License version 2.0. | ||
| # Data generated with ScanCode.io is provided as-is without warranties. | ||
| # ScanCode is a trademark of nexB Inc. | ||
| # | ||
| # You may not use this software except in compliance with the License. | ||
| # You may obtain a copy of the License at: http://apache.org/licenses/LICENSE-2.0 | ||
| # Unless required by applicable law or agreed to in writing, software distributed | ||
| # under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR | ||
| # CONDITIONS OF ANY KIND, either express or implied. See the License for the | ||
| # specific language governing permissions and limitations under the License. | ||
| # | ||
| # Data Generated with ScanCode.io is provided on an "AS IS" BASIS, WITHOUT WARRANTIES | ||
| # OR CONDITIONS OF ANY KIND, either express or implied. No content created from | ||
| # ScanCode.io should be considered or used as legal advice. Consult an Attorney | ||
| # for any legal advice. | ||
| # | ||
| # ScanCode.io is a free software code scanning tool from nexB Inc. and others. | ||
| # Visit https://github.com/aboutcode-org/scancode.io for support and download. | ||
|
|
||
| from scanpipe.pipelines.deploy_to_develop import DeployToDevelop | ||
| from scanpipe.pipes import d2d | ||
| from scanpipe.pipes import nix | ||
|
|
||
|
|
||
| class ScanNixPackage(DeployToDevelop): | ||
| """ | ||
| Scan a nixpkgs package and compare its binary with its source. | ||
|
|
||
| This pipeline takes a single nixpkgs PURL as input, for example: | ||
| ``pkg:nix/nixpkgs/hello@2.12.3?system=x86_64-linux``. | ||
| The ``system`` qualifier is required. The nixpkgs commit is looked up from | ||
| the version, or can be provided with the ``commit`` qualifier. | ||
|
|
||
| The source is fetched with the nixpkgs patches applied, and the binary is | ||
| downloaded from the Nix binary cache. A deployment to development analysis | ||
| runs between them, and the whole source is scanned to compare the detected | ||
| licenses with the license declared in nixpkgs. | ||
|
|
||
| The package outputs holding only headers and documentation, such as ``dev``, | ||
| ``doc``, or ``man``, are not downloaded. A single output can be selected with | ||
| the ``output`` qualifier. | ||
|
|
||
| When the source cannot be fetched, for a package without a ``src`` attribute, | ||
| the binary is analyzed alone and the license check is skipped. | ||
|
|
||
| The binaries mapping is always enabled: ELF for a Linux ``system``, and | ||
| Mach-O for a macOS ``system``. The ELF mapping relies on the DWARF debug | ||
| paths, available only for the packages that provide a ``debug`` output. | ||
|
|
||
| The source is evaluated for the Linux system running ScanCode.io: for a | ||
| macOS ``system``, the source does not include the macOS specific patches. | ||
|
|
||
| Add the ``match_to_matchcode`` addon pipeline to the project to match the | ||
| source with MatchCode.io and find the code borrowed from other packages. | ||
|
|
||
| The nixpkgs commit is checked with the GitHub API, limited to 60 requests | ||
| per hour without a token. Provide a token with the | ||
| ``SCANCODEIO_FETCH_HEADERS`` setting, for example: | ||
| ``api.github.com=Authorization=token <YOUR_TOKEN>``. | ||
|
|
||
| This pipeline requires the ``nix`` command, available in the full | ||
| ScanCode.io image. | ||
| """ | ||
|
|
||
| download_inputs = False | ||
|
|
||
| def __init__(self, run_instance): | ||
| super().__init__(run_instance) | ||
| binary_group = nix.get_binary_group(self.project) | ||
| if binary_group not in self.selected_groups: | ||
| self.selected_groups = [*self.selected_groups, binary_group] | ||
|
|
||
| @classmethod | ||
| def get_availability(cls): | ||
| if missing_commands := nix.get_missing_commands(): | ||
| return ( | ||
| f"Missing commands: {', '.join(missing_commands)}. " | ||
| "Use the full ScanCode.io image." | ||
| ) | ||
|
|
||
| @classmethod | ||
| def steps(cls): | ||
| excluded_steps = ( | ||
| cls.get_inputs, | ||
| cls.extract_inputs_to_codebase_directory, | ||
| cls.create_local_files_packages, | ||
| ) | ||
| d2d_steps = tuple( | ||
| step for step in super().steps() if step not in excluded_steps | ||
| ) | ||
| return ( | ||
| cls.get_nix_package, | ||
| cls.get_nix_package_metadata, | ||
| cls.fetch_nix_source, | ||
| cls.fetch_nix_binary, | ||
| *d2d_steps, | ||
| cls.create_nix_package, | ||
| cls.check_nix_package_license, | ||
| cls.collect_nix_garbage, | ||
| ) | ||
|
|
||
| def get_nix_package(self): | ||
| """Validate the input Nix PURL and get its nixpkgs commit.""" | ||
| self.purl = nix.get_nix_purl(self.project) | ||
| self.commit = nix.get_nixpkgs_commit(self.purl) | ||
| self.log(f"nixpkgs commit: {self.commit}") | ||
|
|
||
| def get_nix_package_metadata(self): | ||
| """Get the package metadata and binary store paths from nixpkgs.""" | ||
| self.metadata = nix.get_package_metadata( | ||
| attribute_path=self.purl.name, | ||
| commit=self.commit, | ||
| system=self.purl.qualifiers["system"], | ||
| cache_directory=self.project.tmp_path, | ||
| ) | ||
| self.selected_outputs = nix.get_selected_outputs( | ||
| outputs=self.metadata["outputs"], | ||
| selected_output=self.purl.qualifiers.get("output"), | ||
| ) | ||
|
|
||
| version = self.metadata["version"] | ||
| if self.purl.version and self.purl.version != version: | ||
| self.project.add_warning( | ||
| description=( | ||
| f"The PURL version {self.purl.version} differs from the " | ||
| f"nixpkgs version {version} at commit {self.commit}." | ||
| ), | ||
| model=self.pipeline_name, | ||
| ) | ||
|
|
||
| def fetch_nix_source(self): | ||
| """Fetch the source to ``from/``, with the nixpkgs patches applied.""" | ||
| try: | ||
| patch_error = nix.fetch_source( | ||
| attribute_path=self.purl.name, | ||
| commit=self.commit, | ||
| destination=self.project.codebase_path / d2d.FROM, | ||
| cache_directory=self.project.tmp_path, | ||
| ) | ||
| except nix.NixCommandError as error: | ||
| self.project.add_warning( | ||
| description=( | ||
| "The source cannot be fetched, the binary is analyzed alone " | ||
| "and the license check is skipped." | ||
| ), | ||
| model=self.pipeline_name, | ||
| details={"error": str(error)}, | ||
| ) | ||
| return | ||
|
|
||
| if patch_error: | ||
| self.project.add_warning( | ||
| description=( | ||
| "The source with the nixpkgs patches cannot be built, the " | ||
| "upstream source is used instead." | ||
| ), | ||
| model=self.pipeline_name, | ||
| details={"error": patch_error}, | ||
| ) | ||
|
|
||
| def fetch_nix_binary(self): | ||
| """Download the package outputs from the Nix binary cache to ``to/``.""" | ||
| missing_outputs = [] | ||
| for output, store_path in self.selected_outputs.items(): | ||
| is_fetched = nix.fetch_binary( | ||
| store_path=store_path, | ||
| destination=self.project.codebase_path / d2d.TO / output, | ||
| download_directory=self.project.tmp_path, | ||
| ) | ||
| if not is_fetched: | ||
| missing_outputs.append(output) | ||
|
|
||
| if missing_outputs: | ||
| self.project.add_warning( | ||
| description=( | ||
| f"Outputs not available in the Nix binary cache: " | ||
| f"{', '.join(missing_outputs)}" | ||
| ), | ||
| model=self.pipeline_name, | ||
| ) | ||
|
|
||
| def scan_mapped_from_for_files(self): | ||
| """ | ||
| Scan the ``from/`` files for copyrights, licenses, emails, and urls, | ||
| including the files not deployed in the binary. | ||
| """ | ||
| super().scan_mapped_from_for_files() | ||
| nix.scan_undeployed_from_files(self.project, progress_logger=self.log) | ||
|
|
||
| def create_nix_package(self): | ||
| """Create the Nix package and assign the codebase resources to it.""" | ||
| self.package = nix.create_nix_package( | ||
| self.project, self.purl, self.metadata, self.commit | ||
| ) | ||
| nix.add_resources_to_package( | ||
| self.package, self.project.codebaseresources.files() | ||
| ) | ||
|
|
||
| def check_nix_package_license(self): | ||
| """Store the licenses detected in the source but not declared in nixpkgs.""" | ||
| has_source = self.project.codebaseresources.from_codebase().files().exists() | ||
| if not has_source: | ||
| return | ||
|
|
||
| declared_license = self.package.declared_license_expression | ||
| if not declared_license: | ||
| self.project.add_warning( | ||
| description="No declared license in nixpkgs, license check skipped.", | ||
| model=self.pipeline_name, | ||
| ) | ||
| return | ||
|
|
||
| uncovered_licenses = nix.get_uncovered_license_keys( | ||
| self.project, declared_license | ||
| ) | ||
| self.package.update_extra_data({"uncovered_licenses": uncovered_licenses}) | ||
|
|
||
| def collect_nix_garbage(self): | ||
| """Delete the Nix store paths not in use, such as the ones of this scan.""" | ||
| try: | ||
| nix.collect_garbage() | ||
| except nix.NixCommandError as error: | ||
| self.project.add_warning( | ||
| description="The Nix store garbage collection failed.", | ||
| model=self.pipeline_name, | ||
| details={"error": str(error)}, | ||
| ) | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If there is no "selected_output", all outputs from
self.metadata["outputs"].items()will be fetched such as dev, doc, man, info, etc. Those contain source-like content that maps cleanly to from/ and may produces noise in the D2D report, the binary side is supposed to represent the deployed artifacts, not the whole package.Should we default to a preferred list instead, e.g. out, debug, and fall back to all outputs if none of those exist?