-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Introduce nix derivation instantiate
#11506
Open
layus
wants to merge
3
commits into
NixOS:master
Choose a base branch
from
layus:derivation-instantiate
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.
Open
Changes from all commits
Commits
Show all changes
3 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 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 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 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,94 @@ | ||
#include "command.hh" | ||
#include "common-args.hh" | ||
#include "store-api.hh" | ||
#include "derivations.hh" | ||
#include "local-fs-store.hh" | ||
#include "progress-bar.hh" | ||
|
||
#include <nlohmann/json.hpp> | ||
|
||
using namespace nix; | ||
|
||
static nlohmann::json storePathSetToJSON(const StorePaths & paths, Store & store) | ||
{ | ||
nlohmann::json res; | ||
for (auto & path : paths) { | ||
nlohmann::json entry; | ||
entry["drvPath"] = store.printStorePath(path); | ||
res.push_back(entry); | ||
} | ||
return res; | ||
} | ||
|
||
// TODO deduplicate with other code also setting such out links. | ||
static void createOutLinks(const std::filesystem::path & outLink, const StorePaths & derivations, LocalFSStore & store) | ||
{ | ||
for (const auto & [_i, drv] : enumerate(derivations)) { | ||
auto i = _i; | ||
auto symlink = outLink; | ||
|
||
if (i) | ||
symlink += fmt("-%d", i); | ||
store.addPermRoot(drv, absPath(symlink.string())); | ||
} | ||
} | ||
|
||
struct CmdDerivationInstantiate : InstallablesCommand, MixJSON | ||
{ | ||
Path outLink = "drv"; | ||
bool printOutputPaths = false; | ||
|
||
CmdDerivationInstantiate() | ||
{ | ||
addFlag( | ||
{.longName = "out-link", | ||
.shortName = 'o', | ||
.description = "Use *path* as prefix for the symlinks to the evaluation results. It defaults to `drv`.", | ||
.labels = {"path"}, | ||
.handler = {&outLink}, | ||
.completer = completePath}); | ||
|
||
addFlag({ | ||
.longName = "no-link", | ||
.description = "Do not create symlinks to the evaluation results.", | ||
.handler = {&outLink, Path("")}, | ||
}); | ||
} | ||
|
||
std::string description() override | ||
{ | ||
return "Force the evaluation of the expression and return the corresponding .drv"; | ||
} | ||
|
||
std::string doc() override | ||
{ | ||
return | ||
#include "derivation-instantiate.md" | ||
; | ||
} | ||
|
||
Category category() override | ||
{ | ||
return catSecondary; | ||
} | ||
|
||
void run(ref<Store> store, Installables && installables) override | ||
{ | ||
auto drvPaths = Installable::toDerivations(store, installables, false); | ||
|
||
if (outLink != "") | ||
if (auto store2 = store.dynamic_pointer_cast<LocalFSStore>()) | ||
createOutLinks(outLink, drvPaths, *store2); | ||
|
||
if (json) { | ||
logger->cout("%s", storePathSetToJSON(drvPaths, *store).dump()); | ||
} else { | ||
stopProgressBar(); | ||
for (auto & path : drvPaths) { | ||
logger->cout(store->printStorePath(path)); | ||
} | ||
} | ||
} | ||
}; | ||
|
||
static auto rCmdDerivationInstantiate = registerCommand2<CmdDerivationInstantiate>({"derivation", "instantiate"}); |
This file contains 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,85 @@ | ||
R""( | ||
|
||
# Name | ||
|
||
`nix derivation instantiate` - instantiate store derivations | ||
|
||
# Synopsis | ||
|
||
`nix derivation instantiate` | ||
[`--out-link` *link prefix*] | ||
[`--json`] | ||
[`--no-link`] | ||
*installables…* | ||
|
||
# Description | ||
|
||
The command `nix derivation instantiate` produces [store derivation]s from | ||
installables. Each top-level expression should evaluate to a derivation, a list | ||
of derivations, or a set of derivations. The paths of the resulting store | ||
derivations are printed on standard output. | ||
|
||
[store derivation]: @docroot@/glossary.md#gloss-store-derivation | ||
|
||
# Options | ||
|
||
- `--out-link` *link prefix* | ||
|
||
The prefix used for gc roots. | ||
|
||
- `--no-link` | ||
|
||
Do not create garbage collector roots for the generated store derivations. | ||
|
||
- `--json` | ||
|
||
Dump a JSON list of objects containing at least a `drvPath` field with the | ||
path to the produced store derivation. | ||
|
||
# Examples | ||
|
||
* Get the store derivation for a single installable, with a gc root | ||
|
||
```console | ||
$ nix derivation instantiate github:NixOS/nixpkgs#hello | ||
/nix/store/af3rc6phyv80h7aq4y3d08awnq2ja8fp-hello-2.12.1.drv | ||
$ ls -ld drv | ||
lrwxrwxrwx [...] drv -> /nix/store/af3rc6phyv80h7aq4y3d08awnq2ja8fp-hello-2.12.1.drv | ||
``` | ||
|
||
* Get the store derivations for multiple installables, in the same order as the | ||
provided arguments. | ||
|
||
```console | ||
$ nix derivation instantiate github:NixOS/nixpkgs#{hello,xorg.xclock} | ||
/nix/store/af3rc6phyv80h7aq4y3d08awnq2ja8fp-hello-2.12.1.drv | ||
/nix/store/82w6jak6c7zldgvxyq5nwhclz3yp85zp-xclock-1.1.1.drv | ||
``` | ||
|
||
* The same, with JSON output. The values also appear in the same order as CLI parameters. | ||
|
||
```console | ||
$ nix derivation instantiate github:NixOS/nixpkgs#{xorg.xclock,hello} --json | jq | ||
[ | ||
{ | ||
"drvPath": "/nix/store/82w6jak6c7zldgvxyq5nwhclz3yp85zp-xclock-1.1.1.drv" | ||
}, | ||
{ | ||
"drvPath": "/nix/store/af3rc6phyv80h7aq4y3d08awnq2ja8fp-hello-2.12.1.drv" | ||
} | ||
] | ||
``` | ||
|
||
# Notes | ||
|
||
* JSON output format may be extended in the future with other fields. | ||
|
||
* Order guarantees will always ensure that the following bash commands output | ||
the same text. | ||
|
||
```console | ||
$ nix derivation instantiate [installables] | ||
$ nix derivation instantiate [installables] --json | jq ".[] | .drvPath" -r | ||
``` | ||
|
||
)"" |
This file contains 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 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 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,60 @@ | ||
#!/usr/bin/env bash | ||
|
||
source common.sh | ||
|
||
TODO_NixOS | ||
|
||
clearStore | ||
|
||
drvPath=$(nix derivation instantiate --no-link --file simple.nix) | ||
test -f "$drvPath" | ||
nix-store --delete "$drvPath" | ||
if test -f "$drvPath"; then false; fi | ||
|
||
rm -f drv | ||
drvPath=$(nix derivation instantiate --file simple.nix) | ||
test -f "$drvPath" | ||
test -e drv | ||
nix-store --gc --print-roots | grep "$drvPath" | ||
nix-store --gc --print-live | grep "$drvPath" | ||
if nix-store --delete "$drvPath"; then false; fi | ||
test -f "$drvPath" | ||
[ "$(nix-store -q --roots "$drvPath")" = "$(realpath --no-symlinks drv) -> $drvPath" ] | ||
rm drv | ||
nix-store --delete "$drvPath" | ||
if test -f "$drvPath"; then false; fi | ||
|
||
rm -f foobar | ||
drvPath=$(nix derivation instantiate --out-link foobar --file simple.nix) | ||
test -e foobar | ||
[ "$(nix-store -q --roots "$drvPath")" = "$(realpath --no-symlinks foobar) -> $drvPath" ] | ||
rm foobar | ||
nix-store --delete "$drvPath" | ||
|
||
drvPathJson=$(nix derivation instantiate --json --no-link --file simple.nix) | ||
[ "$drvPathJson" = "[{\"drvPath\":\"$drvPath\"}]" ] | ||
nix-store --delete "$drvPath" | ||
|
||
rm -f multidrv* | ||
mapfile -t drvPaths < <(nix derivation instantiate --json --out-link multidrv --file check.nix | jq '.[]|.drvPath' -r) | ||
roots=(./multidrv*) | ||
[ "${#roots[@]}" -gt 1 ] | ||
[ "${#roots[@]}" -eq "${#drvPaths[@]}" ] | ||
mapfile -t rootedPaths < <(readlink "${roots[@]}") | ||
[ "${rootedPaths[*]}" = "${drvPaths[*]}" ] | ||
rm -f multidrv* | ||
|
||
# The order should always be the same in text and json outputs | ||
jsonOutput=$(nix derivation instantiate --no-link --file check.nix --json | jq '.[]|.drvPath' -r) | ||
textOutput=$(nix derivation instantiate --no-link --file check.nix) | ||
[ "$jsonOutput" = "$textOutput" ] | ||
|
||
# Test that the order is the same as on the command line, and that repeated | ||
# inputs are present several times in the output, in the correct order | ||
nix derivation instantiate --no-link --file multiple-outputs.nix a b a --json | jq --exit-status ' | ||
(.[0].drvPath | match(".*multiple-outputs-a.drv")) | ||
and (.[1].drvPath | match(".*multiple-outputs-b.drv")) | ||
and (.[2].drvPath | match(".*multiple-outputs-a.drv")) | ||
' | ||
|
||
nix-collect-garbage |
This file contains 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 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
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.
Would be nice to deduplicate this, but that can also be done in a future PR.
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.
See 7f6d006 which factors out
createOutLinks()
.