-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Add nix check command (#13805)
#14322
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
Open
roberth
wants to merge
2
commits into
NixOS:master
Choose a base branch
from
roberth:issue-13805
base: master
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.
+312
−9
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,109 @@ | ||
| #include "nix/cmd/command.hh" | ||
| #include "nix/cmd/installable-flake.hh" | ||
| #include "nix/main/common-args.hh" | ||
| #include "nix/main/shared.hh" | ||
| #include "nix/store/store-api.hh" | ||
| #include "nix/store/globals.hh" | ||
|
|
||
| using namespace nix; | ||
|
|
||
| struct CmdCheck : InstallablesCommand, MixDryRun | ||
| { | ||
| std::string description() override | ||
| { | ||
| return "check that a derivation can be built or substituted"; | ||
| } | ||
|
|
||
| std::string doc() override | ||
| { | ||
| return | ||
| #include "check.md" | ||
| ; | ||
| } | ||
|
|
||
| Strings getDefaultFlakeAttrPaths() override | ||
| { | ||
| return {}; | ||
| } | ||
|
|
||
| Strings getDefaultFlakeAttrPathPrefixes() override | ||
| { | ||
| return { | ||
| "checks." + settings.thisSystem.get() + ".", | ||
| "packages." + settings.thisSystem.get() + ".", | ||
| "legacyPackages." + settings.thisSystem.get() + "."}; | ||
| } | ||
|
|
||
| void applyDefaultInstallables(std::vector<std::string> & rawInstallables) override | ||
| { | ||
| if (rawInstallables.empty()) | ||
| throw UsageError( | ||
| "'nix check' requires at least one installable argument.\n\nDid you mean 'nix flake check'?"); | ||
| } | ||
|
|
||
| void run(ref<Store> store, Installables && installables) override | ||
| { | ||
| // Detect bare flake references without fragments | ||
| for (auto & installable : installables) { | ||
| if (auto installableFlake = installable.dynamic_pointer_cast<InstallableFlake>()) { | ||
| if (installableFlake->attrPaths.empty()) { | ||
| throw Error( | ||
| "Installable '%s' does not specify which outputs to check.\n" | ||
| "Use '%s#<output>' to check a specific output, or 'nix flake check %s' to check all outputs.", | ||
| installableFlake->flakeRef.to_string(), | ||
| installableFlake->flakeRef.to_string(), | ||
| installableFlake->flakeRef.to_string()); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| std::vector<DerivedPath> pathsToCheck; | ||
|
|
||
| for (auto & i : installables) | ||
| for (auto & b : i->toDerivedPaths()) | ||
| pathsToCheck.push_back(b.path); | ||
|
|
||
| // Query what needs to be built vs what can be substituted | ||
| auto missing = store->queryMissing(pathsToCheck); | ||
|
|
||
| if (dryRun) { | ||
| // Use lvlError to always show the output | ||
| printMissing(store, missing, lvlError, /* intendToRealise = */ false); | ||
| return; | ||
| } | ||
|
|
||
| // Only build what cannot be substituted | ||
| std::vector<DerivedPath> toBuild; | ||
| // Convert derivation store paths to DerivedPath::Built (same pattern as nix flake check) | ||
| for (auto & path : missing.willBuild) { | ||
| toBuild.emplace_back( | ||
| DerivedPath::Built{ | ||
| .drvPath = makeConstantStorePathRef(path), | ||
| .outputs = OutputsSpec::All{}, | ||
| }); | ||
| } | ||
|
|
||
| if (!toBuild.empty()) { | ||
| // TODO: Add a Realise mode that performs the build but does not copy | ||
| // the outputs to the local store (for remote builders). | ||
| store->buildPaths(toBuild); | ||
| } | ||
|
|
||
| // Report success for all checked paths | ||
| for (auto & path : pathsToCheck) { | ||
| std::visit( | ||
| overloaded{ | ||
| [&](const DerivedPath::Opaque & bo) { | ||
| logger->log(lvlNotice, fmt("%s: OK (opaque path)", store->printStorePath(bo.path))); | ||
| }, | ||
| [&](const DerivedPath::Built & bfd) { | ||
| auto drvPath = resolveDerivedPath(*store, *bfd.drvPath); | ||
| logger->log(lvlNotice, fmt("%s: OK (available)", store->printStorePath(drvPath))); | ||
| }, | ||
| }, | ||
| path.raw()); | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| static auto rCmdCheck = registerCommand<CmdCheck>("check"); | ||
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,52 @@ | ||
| R""( | ||
|
|
||
| # Examples | ||
|
|
||
| * Check a package from nixpkgs: | ||
|
|
||
| ```console | ||
| # nix check nixpkgs#hello | ||
| ``` | ||
|
|
||
| * Check a flake check attribute: | ||
|
|
||
| ```console | ||
| # nix check .#checks.x86_64-linux.integration-test | ||
| ``` | ||
|
|
||
| * Check multiple derivations: | ||
|
|
||
| ```console | ||
| # nix check .#package-a .#package-b | ||
| ``` | ||
|
|
||
| # Description | ||
|
|
||
| `nix check` verifies that the specified *installables* can be realised. | ||
| [Installables](./nix.md#installables) that resolve to derivations are | ||
| checked by building them from source, or verifying they can be fetched | ||
| from a substituter. | ||
|
|
||
| This command requires at least one installable argument with a specific | ||
| attribute path (e.g., `nixpkgs#hello` or `.#checks.x86_64-linux.foo`). Bare | ||
| flake references without an attribute path are not accepted. If you want to | ||
| check all outputs of a flake, use `nix flake check` instead. | ||
|
|
||
| Unlike `nix build`, this command: | ||
| - Does not create result symlinks | ||
| - Does not download outputs that are available in substituters | ||
| - Only builds derivations that cannot be substituted | ||
|
|
||
| **Note**: When checking substitutability, only metadata is queried - no full | ||
| substitution is performed. A broken or unreliable binary cache could still | ||
| fail to provide the contents even after `nix check` reports success. | ||
|
|
||
| When looking up flake attributes, this command first searches in | ||
| `checks.<system>`, then falls back to `packages.<system>` and | ||
| `legacyPackages.<system>` (like `nix build`). | ||
|
|
||
| This command is useful for CI systems and development workflows where you | ||
| want to verify that derivations are buildable without creating local store | ||
| contents and result symlinks. | ||
|
|
||
| )"" |
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,131 @@ | ||
| #!/usr/bin/env bash | ||
|
|
||
| source common.sh | ||
|
|
||
| TODO_NixOS | ||
|
|
||
| ################################################### | ||
| # Test that nix check requires explicit installables | ||
|
|
||
| output=$(nix check 2>&1) && fail "nix check with no arguments should fail" | ||
| echo "$output" | grepQuiet "requires at least one installable" | ||
| echo "$output" | grepQuiet "Did you mean 'nix flake check'?" | ||
|
|
||
| ################################################### | ||
| # Test that nix check rejects bare flake references | ||
|
|
||
| # Create a minimal flake for testing | ||
| emptyFlake=$TEST_ROOT/empty-flake | ||
| mkdir -p "$emptyFlake" | ||
| cat > "$emptyFlake/flake.nix" << 'EOF' | ||
| { | ||
| outputs = { self }: { }; | ||
| } | ||
| EOF | ||
| git -C "$emptyFlake" init | ||
| git -C "$emptyFlake" add flake.nix | ||
|
|
||
| output=$(nix check "$emptyFlake" 2>&1) && fail "nix check with bare flake reference should fail" | ||
| echo "$output" | grepQuiet "does not specify which outputs to check" | ||
| echo "$output" | grepQuiet "#<output>" | ||
| echo "$output" | grepQuiet "nix flake check" | ||
|
|
||
| ################################################### | ||
| # Test basic functionality: check derivations and report paths | ||
|
|
||
| clearStore | ||
| clearCache | ||
|
|
||
| input1_drvPath=$(nix eval -f dependencies.nix input1_drv.drvPath --raw) | ||
| input2_drvPath=$(nix eval -f dependencies.nix input2_drv.drvPath --raw) | ||
|
|
||
| # Single derivation | ||
| output=$(nix check -f dependencies.nix input1_drv 2>&1) | ||
| echo "$output" | grepQuiet "$input1_drvPath: OK (available)" | ||
|
|
||
| # Multiple derivations | ||
| output=$(nix check -f dependencies.nix input1_drv input2_drv 2>&1) | ||
| echo "$output" | grepQuiet "$input1_drvPath: OK (available)" | ||
| echo "$output" | grepQuiet "$input2_drvPath: OK (available)" | ||
|
|
||
| ################################################### | ||
| # Test dry-run doesn't build | ||
|
|
||
| clearStore | ||
| clearCache | ||
|
|
||
| # With no substituters, this needs to be built | ||
| output=$(nix check -f dependencies.nix input1_drv --dry-run 2>&1) | ||
| echo "$output" | grepQuiet "would be built" | ||
| # Verify nothing was actually built | ||
| ! nix path-info -f dependencies.nix input1_drv 2>/dev/null || fail "dry-run built the derivation" | ||
|
|
||
| ################################################### | ||
| # Test no result symlinks created | ||
|
|
||
| RESULT=$TEST_ROOT/result | ||
| rm -f "$RESULT"* | ||
| nix check -f dependencies.nix input1_drv input2_drv | ||
| [[ ! -h $RESULT ]] || fail "nix check created result symlink" | ||
|
|
||
| ################################################### | ||
| # Test opaque store paths | ||
|
|
||
| clearStore | ||
| clearCache | ||
|
|
||
| storePath=$(nix build -f dependencies.nix input1_drv --no-link --print-out-paths) | ||
| output=$(nix check "$storePath" 2>&1) | ||
| echo "$output" | grepQuiet "OK (opaque path)" | ||
|
|
||
| ################################################### | ||
| # Test failing derivation | ||
|
|
||
| clearStore | ||
| clearCache | ||
|
|
||
| # Create a derivation that will fail | ||
| cat > "$TEST_ROOT/failing.nix" <<EOF | ||
| with import ./config.nix; | ||
| mkDerivation { | ||
| name = "failing-drv"; | ||
| buildCommand = "exit 1"; | ||
| } | ||
| EOF | ||
|
|
||
| # Check should fail when derivation fails to build | ||
| ! nix check -f "$TEST_ROOT/failing.nix" 2>&1 || fail "nix check succeeded on failing derivation" | ||
|
|
||
| ################################################### | ||
| # Test optimization: | ||
| # don't download substitutable paths | ||
|
|
||
| if [[ -n "${NIX_REMOTE:-}" ]]; then | ||
| echo "Skipping substituter test with daemon" | ||
| else | ||
| clearStore | ||
| clearCacheCache | ||
|
|
||
| # Set up binary cache with a built derivation | ||
| outPath=$(nix-build dependencies.nix --no-out-link -A input1_drv) | ||
| input1_drvPath=$(nix eval -f dependencies.nix input1_drv.drvPath --raw) | ||
| nix copy --to "file://$cacheDir" "$outPath" | ||
|
|
||
| # Keep the .drv in the store so queryMissing can determine output paths | ||
| # but delete the actual output | ||
| nix-store --delete "$outPath" | ||
|
|
||
| # Dry-run should report it can be fetched | ||
| clearCacheCache | ||
|
|
||
| output=$(nix check -f dependencies.nix input1_drv --dry-run --substituters "file://$cacheDir" --no-require-sigs --option substitute true 2>&1) | ||
| echo "$output" | grepQuiet "can be fetched" | ||
|
|
||
| # Check should succeed without downloading | ||
| output=$(nix check -f dependencies.nix input1_drv --substituters "file://$cacheDir" --no-require-sigs --option substitute true 2>&1) | ||
| echo "$output" | grepQuiet "$input1_drvPath: OK (available)" | ||
|
|
||
| # Verify it didn't download | ||
| echo "$output" | grepQuietInverse "copying path.*$outPath" || fail "downloaded substitutable path" | ||
| ! nix path-info "$outPath" 2>/dev/null || fail "path exists in local store after check" | ||
| fi |
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.
Uh oh!
There was an error while loading. Please reload this page.
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.
Having all the packages in the completions is counterproductive, and I haven't wanted to
checkbuildability of a package yet.