mirror of
https://github.com/privatevoid-net/nix-super.git
synced 2024-11-10 08:16:15 +02:00
Create nix derivation add
command
Also refine `nix derivation show`'s docs very slightly.
This commit is contained in:
parent
27597f8131
commit
59e0728714
6 changed files with 108 additions and 4 deletions
45
src/nix/derivation-add.cc
Normal file
45
src/nix/derivation-add.cc
Normal file
|
@ -0,0 +1,45 @@
|
||||||
|
// FIXME: rename to 'nix plan add' or 'nix derivation add'?
|
||||||
|
|
||||||
|
#include "command.hh"
|
||||||
|
#include "common-args.hh"
|
||||||
|
#include "store-api.hh"
|
||||||
|
#include "archive.hh"
|
||||||
|
#include "derivations.hh"
|
||||||
|
#include <nlohmann/json.hpp>
|
||||||
|
|
||||||
|
using namespace nix;
|
||||||
|
using json = nlohmann::json;
|
||||||
|
|
||||||
|
struct CmdAddDerivation : MixDryRun, StoreCommand
|
||||||
|
{
|
||||||
|
std::string description() override
|
||||||
|
{
|
||||||
|
return "Add a store derivation";
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string doc() override
|
||||||
|
{
|
||||||
|
return
|
||||||
|
#include "derivation-add.md"
|
||||||
|
;
|
||||||
|
}
|
||||||
|
|
||||||
|
Category category() override { return catUtility; }
|
||||||
|
|
||||||
|
void run(ref<Store> store) override
|
||||||
|
{
|
||||||
|
auto json = nlohmann::json::parse(drainFD(STDIN_FILENO));
|
||||||
|
|
||||||
|
auto drv = Derivation::fromJSON(*store, json);
|
||||||
|
|
||||||
|
auto drvPath = writeDerivation(*store, drv, NoRepair, /* read only */ dryRun);
|
||||||
|
|
||||||
|
drv.checkInvariants(*store, drvPath);
|
||||||
|
|
||||||
|
writeDerivation(*store, drv, NoRepair, dryRun);
|
||||||
|
|
||||||
|
logger->cout("%s", store->printStorePath(drvPath));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
static auto rCmdAddDerivation = registerCommand2<CmdAddDerivation>({"derivation", "add"});
|
18
src/nix/derivation-add.md
Normal file
18
src/nix/derivation-add.md
Normal file
|
@ -0,0 +1,18 @@
|
||||||
|
R""(
|
||||||
|
|
||||||
|
# Description
|
||||||
|
|
||||||
|
This command reads from standard input a JSON representation of a
|
||||||
|
[store derivation] to which an [*installable*](./nix.md#installables) evaluates.
|
||||||
|
|
||||||
|
Store derivations are used internally by Nix. They are store paths with
|
||||||
|
extension `.drv` that represent the build-time dependency graph to which
|
||||||
|
a Nix expression evaluates.
|
||||||
|
|
||||||
|
[store derivation]: ../../glossary.md#gloss-store-derivation
|
||||||
|
|
||||||
|
The JSON format is documented under the [`derivation show`] command.
|
||||||
|
|
||||||
|
[`derivation show`]: ./nix3-derivation-show.md
|
||||||
|
|
||||||
|
)""
|
|
@ -39,10 +39,11 @@ R""(
|
||||||
# Description
|
# Description
|
||||||
|
|
||||||
This command prints on standard output a JSON representation of the
|
This command prints on standard output a JSON representation of the
|
||||||
[store derivation]s to which [*installables*](./nix.md#installables) evaluate. Store derivations
|
[store derivation]s to which [*installables*](./nix.md#installables) evaluate.
|
||||||
are used internally by Nix. They are store paths with extension `.drv`
|
|
||||||
that represent the build-time dependency graph to which a Nix
|
Store derivations are used internally by Nix. They are store paths with
|
||||||
expression evaluates.
|
extension `.drv` that represent the build-time dependency graph to which
|
||||||
|
a Nix expression evaluates.
|
||||||
|
|
||||||
By default, this command only shows top-level derivations, but with
|
By default, this command only shows top-level derivations, but with
|
||||||
`--recursive`, it also shows their dependencies.
|
`--recursive`, it also shows their dependencies.
|
||||||
|
|
26
tests/ca/derivation-json.sh
Normal file
26
tests/ca/derivation-json.sh
Normal file
|
@ -0,0 +1,26 @@
|
||||||
|
source common.sh
|
||||||
|
|
||||||
|
export NIX_TESTS_CA_BY_DEFAULT=1
|
||||||
|
|
||||||
|
drvPath=$(nix-instantiate ../simple.nix)
|
||||||
|
|
||||||
|
nix derivation show $drvPath | jq .[] > $TEST_HOME/simple.json
|
||||||
|
|
||||||
|
drvPath2=$(nix derivation add < $TEST_HOME/simple.json)
|
||||||
|
|
||||||
|
[[ "$drvPath" = "$drvPath2" ]]
|
||||||
|
|
||||||
|
# Content-addressed derivations can be renamed.
|
||||||
|
jq '.name = "foo"' < $TEST_HOME/simple.json > $TEST_HOME/foo.json
|
||||||
|
drvPath3=$(nix derivation add --dry-run < $TEST_HOME/foo.json)
|
||||||
|
# With --dry-run nothing is actually written
|
||||||
|
[[ ! -e "$drvPath3" ]]
|
||||||
|
|
||||||
|
# Without --dry-run it is actually written
|
||||||
|
drvPath4=$(nix derivation add < $TEST_HOME/foo.json)
|
||||||
|
[[ "$drvPath4" = "$drvPath3" ]]
|
||||||
|
[[ -e "$drvPath3" ]]
|
||||||
|
|
||||||
|
# The modified derivation read back as JSON matches
|
||||||
|
nix derivation show $drvPath3 | jq .[] > $TEST_HOME/foo-read.json
|
||||||
|
diff $TEST_HOME/foo.json $TEST_HOME/foo-read.json
|
12
tests/derivation-json.sh
Normal file
12
tests/derivation-json.sh
Normal file
|
@ -0,0 +1,12 @@
|
||||||
|
source common.sh
|
||||||
|
|
||||||
|
drvPath=$(nix-instantiate simple.nix)
|
||||||
|
|
||||||
|
nix derivation show $drvPath | jq .[] > $TEST_HOME/simple.json
|
||||||
|
|
||||||
|
drvPath2=$(nix derivation add < $TEST_HOME/simple.json)
|
||||||
|
|
||||||
|
[[ "$drvPath" = "$drvPath2" ]]
|
||||||
|
|
||||||
|
# Input addressed derivations cannot be renamed.
|
||||||
|
jq '.name = "foo"' < $TEST_HOME/simple.json | expectStderr 1 nix derivation add | grepQuiet "has incorrect output"
|
|
@ -102,6 +102,8 @@ nix_tests = \
|
||||||
eval-store.sh \
|
eval-store.sh \
|
||||||
why-depends.sh \
|
why-depends.sh \
|
||||||
ca/why-depends.sh \
|
ca/why-depends.sh \
|
||||||
|
derivation-json.sh \
|
||||||
|
ca/derivation-json.sh \
|
||||||
import-derivation.sh \
|
import-derivation.sh \
|
||||||
ca/import-derivation.sh \
|
ca/import-derivation.sh \
|
||||||
nix_path.sh \
|
nix_path.sh \
|
||||||
|
|
Loading…
Reference in a new issue