add nix-store --query --valid-derivers command

notably useful when nix-store --query --deriver returns a non-existing
path.

Co-authored-by: Felix Uhl <iFreilicht@users.noreply.github.com>
This commit is contained in:
Guillaume Girol 2023-08-23 12:00:00 +00:00
parent 6459a1c7ad
commit 925a444b92
9 changed files with 68 additions and 10 deletions

View file

@ -5,8 +5,8 @@
# Synopsis # Synopsis
`nix-store` {`--query` | `-q`} `nix-store` {`--query` | `-q`}
{`--outputs` | `--requisites` | `-R` | `--references` | {`--outputs` | `--requisites` | `-R` | `--references` | `--referrers` |
`--referrers` | `--referrers-closure` | `--deriver` | `-d` | `--referrers-closure` | `--deriver` | `-d` | `--valid-derivers` |
`--graph` | `--tree` | `--binding` *name* | `-b` *name* | `--hash` | `--graph` | `--tree` | `--binding` *name* | `-b` *name* | `--hash` |
`--size` | `--roots`} `--size` | `--roots`}
[`--use-output`] [`-u`] [`--force-realise`] [`-f`] [`--use-output`] [`-u`] [`--force-realise`] [`-f`]
@ -82,13 +82,21 @@ symlink.
in the Nix store that are dependent on *paths*. in the Nix store that are dependent on *paths*.
- `--deriver`; `-d`\ - `--deriver`; `-d`\
Prints the [deriver] of the store paths *paths*. If Prints the [deriver] that was used to build the store paths *paths*. If
the path has no deriver (e.g., if it is a source file), or if the the path has no deriver (e.g., if it is a source file), or if the
deriver is not known (e.g., in the case of a binary-only deriver is not known (e.g., in the case of a binary-only
deployment), the string `unknown-deriver` is printed. deployment), the string `unknown-deriver` is printed.
The returned deriver is not guaranteed to exist in the local store, for
example when *paths* were substituted from a binary cache.
Use `--valid-derivers` instead to obtain valid paths only.
[deriver]: ../../glossary.md#gloss-deriver [deriver]: ../../glossary.md#gloss-deriver
- `--valid-derivers`\
Prints a set of derivation files (`.drv`) which are supposed produce
said paths when realized. Might print nothing, for example for source paths
or paths subsituted from a binary cache.
- `--graph`\ - `--graph`\
Prints the references graph of the store paths *paths* in the format Prints the references graph of the store paths *paths* in the format
of the `dot` tool of AT\&T's [Graphviz of the `dot` tool of AT\&T's [Graphviz

View file

@ -22,3 +22,7 @@
- Introduce a new [`outputOf`](@docroot@/language/builtins.md#builtins-outputOf) builtin. - Introduce a new [`outputOf`](@docroot@/language/builtins.md#builtins-outputOf) builtin.
It is part of the [`dynamic-derivations`](@docroot@/contributing/experimental-features.md#xp-feature-dynamic-derivations) experimental feature. It is part of the [`dynamic-derivations`](@docroot@/contributing/experimental-features.md#xp-feature-dynamic-derivations) experimental feature.
- [`nix-store --query`](@docroot@/command-ref/nix-store/query.md) gained a new type of query: `--valid-derivers`. It returns all `.drv` files in the local store that *can be* used to build the output passed in argument.
This is in contrast to `--deriver`, which returns the single `.drv` file that *was actually* used to build the output passed in argument. In case the output was substituted from a binary cache,
this `.drv` file may only exist on said binary cache and not locally.

View file

@ -283,7 +283,7 @@ static void opQuery(Strings opFlags, Strings opArgs)
{ {
enum QueryType enum QueryType
{ qOutputs, qRequisites, qReferences, qReferrers { qOutputs, qRequisites, qReferences, qReferrers
, qReferrersClosure, qDeriver, qBinding, qHash, qSize , qReferrersClosure, qDeriver, qValidDerivers, qBinding, qHash, qSize
, qTree, qGraph, qGraphML, qResolve, qRoots }; , qTree, qGraph, qGraphML, qResolve, qRoots };
std::optional<QueryType> query; std::optional<QueryType> query;
bool useOutput = false; bool useOutput = false;
@ -299,6 +299,7 @@ static void opQuery(Strings opFlags, Strings opArgs)
else if (i == "--referrers" || i == "--referers") query = qReferrers; else if (i == "--referrers" || i == "--referers") query = qReferrers;
else if (i == "--referrers-closure" || i == "--referers-closure") query = qReferrersClosure; else if (i == "--referrers-closure" || i == "--referers-closure") query = qReferrersClosure;
else if (i == "--deriver" || i == "-d") query = qDeriver; else if (i == "--deriver" || i == "-d") query = qDeriver;
else if (i == "--valid-derivers") query = qValidDerivers;
else if (i == "--binding" || i == "-b") { else if (i == "--binding" || i == "-b") {
if (opArgs.size() == 0) if (opArgs.size() == 0)
throw UsageError("expected binding name"); throw UsageError("expected binding name");
@ -372,6 +373,21 @@ static void opQuery(Strings opFlags, Strings opArgs)
} }
break; break;
case qValidDerivers: {
StorePathSet result;
for (auto & i : opArgs) {
auto derivers = store->queryValidDerivers(store->followLinksToStorePath(i));
for (const auto &i: derivers) {
result.insert(i);
}
}
auto sorted = store->topoSortPaths(result);
for (StorePaths::reverse_iterator i = sorted.rbegin();
i != sorted.rend(); ++i)
cout << fmt("%s\n", store->printStorePath(*i));
break;
}
case qBinding: case qBinding:
for (auto & i : opArgs) { for (auto & i : opArgs) {
auto path = useDeriver(store->followLinksToStorePath(i)); auto path = useDeriver(store->followLinksToStorePath(i));

View file

@ -2,7 +2,7 @@ with import ./config.nix;
rec { rec {
dep = import ./dependencies.nix; dep = import ./dependencies.nix {};
makeTest = nr: args: mkDerivation ({ makeTest = nr: args: mkDerivation ({
name = "check-refs-" + toString nr; name = "check-refs-" + toString nr;

View file

@ -1,3 +1,4 @@
{ hashInvalidator ? "" }:
with import ./config.nix; with import ./config.nix;
let { let {
@ -21,6 +22,17 @@ let {
''; '';
}; };
fod_input = mkDerivation {
name = "fod-input";
buildCommand = ''
echo ${hashInvalidator}
echo FOD > $out
'';
outputHashMode = "flat";
outputHashAlgo = "sha256";
outputHash = "1dq9p0hnm1y75q2x40fws5887bq1r840hzdxak0a9djbwvx0b16d";
};
body = mkDerivation { body = mkDerivation {
name = "dependencies-top"; name = "dependencies-top";
builder = ./dependencies.builder0.sh + "/FOOBAR/../."; builder = ./dependencies.builder0.sh + "/FOOBAR/../.";
@ -29,6 +41,7 @@ let {
input1_drv = input1; input1_drv = input1;
input2_drv = input2; input2_drv = input2;
input0_drv = input0; input0_drv = input0;
fod_input_drv = fod_input;
meta.description = "Random test package"; meta.description = "Random test package";
}; };

View file

@ -53,3 +53,20 @@ nix-store -q --referrers-closure "$input2OutPath" | grep "$outPath"
# Check that the derivers are set properly. # Check that the derivers are set properly.
test $(nix-store -q --deriver "$outPath") = "$drvPath" test $(nix-store -q --deriver "$outPath") = "$drvPath"
nix-store -q --deriver "$input2OutPath" | grepQuiet -- "-input-2.drv" nix-store -q --deriver "$input2OutPath" | grepQuiet -- "-input-2.drv"
# --valid-derivers returns the currently single valid .drv file
test "$(nix-store -q --valid-derivers "$outPath")" = "$drvPath"
# instantiate a different drv with the same output
drvPath2=$(nix-instantiate dependencies.nix --argstr hashInvalidator yay)
# now --valid-derivers returns both
test "$(nix-store -q --valid-derivers "$outPath" | sort)" = "$(sort <<< "$drvPath"$'\n'"$drvPath2")"
# check that nix-store --valid-derivers only returns existing drv
nix-store --delete "$drvPath"
test "$(nix-store -q --valid-derivers "$outPath")" = "$drvPath2"
# check that --valid-derivers returns nothing when there are no valid derivers
nix-store --delete "$drvPath2"
test -z "$(nix-store -q --valid-derivers "$outPath")"

View file

@ -13,14 +13,14 @@ nix --experimental-features 'nix-command' eval --impure --expr \
# the future so it does work, but there are some design questions to # the future so it does work, but there are some design questions to
# resolve first. Adding a test so we don't liberalise it by accident. # resolve first. Adding a test so we don't liberalise it by accident.
expectStderr 1 nix --experimental-features 'nix-command dynamic-derivations' eval --impure --expr \ expectStderr 1 nix --experimental-features 'nix-command dynamic-derivations' eval --impure --expr \
'builtins.outputOf (import ../dependencies.nix) "out"' \ 'builtins.outputOf (import ../dependencies.nix {}) "out"' \
| grepQuiet "value is a set while a string was expected" | grepQuiet "value is a set while a string was expected"
# Test that "DrvDeep" string contexts are not supported at this time # Test that "DrvDeep" string contexts are not supported at this time
# #
# Like the above, this is a restriction we could relax later. # Like the above, this is a restriction we could relax later.
expectStderr 1 nix --experimental-features 'nix-command dynamic-derivations' eval --impure --expr \ expectStderr 1 nix --experimental-features 'nix-command dynamic-derivations' eval --impure --expr \
'builtins.outputOf (import ../dependencies.nix).drvPath "out"' \ 'builtins.outputOf (import ../dependencies.nix {}).drvPath "out"' \
| grepQuiet "has a context which refers to a complete source and binary closure. This is not supported at this time" | grepQuiet "has a context which refers to a complete source and binary closure. This is not supported at this time"
# Test using `builtins.outputOf` with static derivations # Test using `builtins.outputOf` with static derivations

View file

@ -17,13 +17,13 @@ rec {
foo."bar.runtimeGraph" = mkDerivation { foo."bar.runtimeGraph" = mkDerivation {
name = "dependencies"; name = "dependencies";
builder = builtins.toFile "build-graph-builder" "${printRefs}"; builder = builtins.toFile "build-graph-builder" "${printRefs}";
exportReferencesGraph = ["refs" (import ./dependencies.nix)]; exportReferencesGraph = ["refs" (import ./dependencies.nix {})];
}; };
foo."bar.buildGraph" = mkDerivation { foo."bar.buildGraph" = mkDerivation {
name = "dependencies"; name = "dependencies";
builder = builtins.toFile "build-graph-builder" "${printRefs}"; builder = builtins.toFile "build-graph-builder" "${printRefs}";
exportReferencesGraph = ["refs" (import ./dependencies.nix).drvPath]; exportReferencesGraph = ["refs" (import ./dependencies.nix {}).drvPath];
}; };
} }

View file

@ -24,7 +24,7 @@ fi
import ( import (
mkDerivation { mkDerivation {
name = "foo"; name = "foo";
bla = import ./dependencies.nix; bla = import ./dependencies.nix {};
buildCommand = " buildCommand = "
echo \\\"hi\\\" > $out echo \\\"hi\\\" > $out
"; ";