Skip to content

Commit

Permalink
Create nix add-derivation command
Browse files Browse the repository at this point in the history
  • Loading branch information
Ericson2314 committed Mar 17, 2023
1 parent db8a0be commit cf58363
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 0 deletions.
63 changes: 63 additions & 0 deletions src/nix/add-derivation.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// 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
{
Path path;
std::optional<std::string> namePart;

CmdAddDerivation()
{
addFlag({
.longName = "name",
.shortName = 'n',
.description = "Override the name component of the derivation path. It defaults to the base name of *path*.",
.labels = {"name"},
.handler = {&namePart},
});
}

std::string description() override
{
return "Add a store derivation";
}

std::string doc() override
{
return
#include "add-derivation.md"
;
}

Category category() override { return catUtility; }

void run(ref<Store> store) override
{
if (!namePart) namePart = baseNameOf(path);
auto & name = *namePart;

auto json = nlohmann::json::parse(drainFD(STDIN_FILENO));

auto drv = Derivation::fromJSON(*store, name, json);
drv.name = name;

auto drvPath = writeDerivation(*store, drv, NoRepair, /* read only */ true);

drv.checkInvariants(*store, drvPath);

writeDerivation(*store, drv, NoRepair, dryRun);

logger->cout("%s", store->printStorePath(drvPath));
}
};

static auto rCmdAddDerivation = registerCommand<CmdAddDerivation>("add-derivation");
17 changes: 17 additions & 0 deletions src/nix/add-derivation.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
R""(

# Description

This command reads from the given path a JSON representation of a
[store derivation] to which *installables* evaluate. 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 [`show-derivation`] command.

[`show-derivation`]: ./nix3-show-derivation.md

)""
14 changes: 14 additions & 0 deletions tests/ca/derivation-json.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
source common.sh

export NIX_TESTS_CA_BY_DEFAULT=1

drvPath=$(nix-instantiate ../simple.nix)

nix show-derivation $drvPath | jq .[] > $TEST_HOME/simple.json

drvPath2=$(nix add-derivation --name simple < $TEST_HOME/simple.json)

[[ "$drvPath" = "$drvPath2" ]]

# Content-addressed derivations can be renamed.
nix add-derivation --name foo < $TEST_HOME/simple.json
12 changes: 12 additions & 0 deletions tests/derivation-json.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
source common.sh

drvPath=$(nix-instantiate simple.nix)

nix show-derivation $drvPath | jq .[] > $TEST_HOME/simple.json

drvPath2=$(nix add-derivation --name simple < $TEST_HOME/simple.json)

[[ "$drvPath" = "$drvPath2" ]]

# Input addressed derivations cannot be renamed.
expectStderr 1 nix add-derivation --name foo < $TEST_HOME/simple.json | grepQuiet "has incorrect output"
2 changes: 2 additions & 0 deletions tests/local.mk
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ nix_tests = \
eval-store.sh \
why-depends.sh \
ca/why-depends.sh \
derivation-json.sh \
ca/derivation-json.sh \
import-derivation.sh \
ca/import-derivation.sh \
nix_path.sh \
Expand Down

0 comments on commit cf58363

Please sign in to comment.