Merge pull request #4819 from NixOS/ca/fix-nix-run

Resolve the program path in `nix run`
This commit is contained in:
Eelco Dolstra 2021-05-17 16:28:14 +02:00 committed by GitHub
commit bd6cf25952
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 116 additions and 33 deletions

1
.gitignore vendored
View file

@ -82,6 +82,7 @@ perl/Makefile.config
/tests/shell
/tests/shell.drv
/tests/config.nix
/tests/ca/config.nix
# /tests/lang/
/tests/lang/*.out

View file

@ -3,9 +3,61 @@
#include "eval-inline.hh"
#include "eval-cache.hh"
#include "names.hh"
#include "command.hh"
namespace nix {
struct InstallableDerivedPath : Installable
{
ref<Store> store;
const DerivedPath derivedPath;
InstallableDerivedPath(ref<Store> store, const DerivedPath & derivedPath)
: store(store)
, derivedPath(derivedPath)
{
}
std::string what() override { return derivedPath.to_string(*store); }
DerivedPaths toDerivedPaths() override
{
return {derivedPath};
}
std::optional<StorePath> getStorePath() override
{
return std::nullopt;
}
};
/**
* Return the rewrites that are needed to resolve a string whose context is
* included in `dependencies`
*/
StringPairs resolveRewrites(Store & store, const BuiltPaths dependencies)
{
StringPairs res;
for (auto & dep : dependencies)
if (auto drvDep = std::get_if<BuiltPathBuilt>(&dep))
for (auto & [ outputName, outputPath ] : drvDep->outputs)
res.emplace(
downstreamPlaceholder(store, drvDep->drvPath, outputName),
store.printStorePath(outputPath)
);
return res;
}
/**
* Resolve the given string assuming the given context
*/
std::string resolveString(Store & store, const std::string & toResolve, const BuiltPaths dependencies)
{
auto rewrites = resolveRewrites(store, dependencies);
return rewriteStrings(toResolve, rewrites);
}
App Installable::toApp(EvalState & state)
{
auto [cursor, attrPath] = getCursor(state);
@ -18,19 +70,21 @@ App Installable::toApp(EvalState & state)
throw Error("app program '%s' is not in the Nix store", program);
};
std::vector<std::shared_ptr<Installable>> context;
std::string unresolvedProgram;
if (type == "app") {
auto [program, context] = cursor->getAttr("program")->getStringWithContext();
auto [program, context_] = cursor->getAttr("program")->getStringWithContext();
unresolvedProgram = program;
checkProgram(program);
std::vector<StorePathWithOutputs> context2;
for (auto & [path, name] : context)
context2.push_back({state.store->parseStorePath(path), {name}});
return App {
.context = std::move(context2),
.program = program,
};
for (auto & [path, name] : context_)
context.push_back(std::make_shared<InstallableDerivedPath>(
state.store,
DerivedPathBuilt{
.drvPath = state.store->parseStorePath(path),
.outputs = {name},
}));
}
else if (type == "derivation") {
@ -44,16 +98,24 @@ App Installable::toApp(EvalState & state)
aMainProgram
? aMainProgram->getString()
: DrvName(name).name;
auto program = outPath + "/bin/" + mainProgram;
checkProgram(program);
return App {
.context = { { drvPath, {outputName} } },
.program = program,
};
unresolvedProgram = outPath + "/bin/" + mainProgram;
context = {std::make_shared<InstallableDerivedPath>(
state.store,
DerivedPathBuilt{
.drvPath = drvPath,
.outputs = {outputName},
})};
}
else
throw Error("attribute '%s' has unsupported type '%s'", attrPath, type);
auto builtContext = build(state.store, Realise::Outputs, context);
auto program = resolveString(*state.store, unresolvedProgram, builtContext);
checkProgram(program);
return App {
.program = program,
};
}
}

1
tests/ca/config.nix.in Symbolic link
View file

@ -0,0 +1 @@
../config.nix.in

View file

@ -1,4 +1,11 @@
with import ../config.nix;
with import ./config.nix;
let mkCADerivation = args: mkDerivation ({
__contentAddressed = true;
outputHashMode = "recursive";
outputHashAlgo = "sha256";
} // args);
in
{ seed ? 0 }:
# A simple content-addressed derivation.
@ -14,7 +21,7 @@ rec {
echo "Hello World" > $out/hello
'';
};
rootCA = mkDerivation {
rootCA = mkCADerivation {
name = "rootCA";
outputs = [ "out" "dev" "foo"];
buildCommand = ''
@ -27,11 +34,8 @@ rec {
ln -s $out $dev
ln -s $out $foo
'';
__contentAddressed = true;
outputHashMode = "recursive";
outputHashAlgo = "sha256";
};
dependentCA = mkDerivation {
dependentCA = mkCADerivation {
name = "dependent";
buildCommand = ''
echo "building a dependent derivation"
@ -39,20 +43,14 @@ rec {
cat ${rootCA}/self/dep
echo ${rootCA}/self/dep > $out/dep
'';
__contentAddressed = true;
outputHashMode = "recursive";
outputHashAlgo = "sha256";
};
transitivelyDependentCA = mkDerivation {
transitivelyDependentCA = mkCADerivation {
name = "transitively-dependent";
buildCommand = ''
echo "building transitively-dependent"
cat ${dependentCA}/dep
echo ${dependentCA} > $out
'';
__contentAddressed = true;
outputHashMode = "recursive";
outputHashAlgo = "sha256";
};
dependentNonCA = mkDerivation {
name = "dependent-non-ca";
@ -72,6 +70,14 @@ rec {
cat ${dependentCA}/dep
echo foo > $out
'';
};
runnable = mkCADerivation rec {
name = "runnable-thing";
buildCommand = ''
mkdir -p $out/bin
echo ${rootCA} # Just to make it depend on it
echo "" > $out/bin/${name}
chmod +x $out/bin/${name}
'';
};
}

3
tests/ca/flake.nix Normal file
View file

@ -0,0 +1,3 @@
{
outputs = { self }: import ./content-addressed.nix {};
}

9
tests/ca/nix-run.sh Executable file
View file

@ -0,0 +1,9 @@
#!/usr/bin/env bash
source common.sh
sed -i 's/experimental-features .*/& ca-derivations ca-references nix-command flakes/' "$NIX_CONF_DIR"/nix.conf
FLAKE_PATH=path:$PWD
nix run --no-write-lock-file $FLAKE_PATH#runnable

View file

@ -46,6 +46,7 @@ nix_tests = \
ca/build.sh \
ca/substitute.sh \
ca/signatures.sh \
ca/nix-run.sh \
ca/nix-copy.sh
# parallel.sh
@ -53,6 +54,6 @@ install-tests += $(foreach x, $(nix_tests), tests/$(x))
tests-environment = NIX_REMOTE= $(bash) -e
clean-files += $(d)/common.sh $(d)/config.nix
clean-files += $(d)/common.sh $(d)/config.nix $(d)/ca/config.nix
test-deps += tests/common.sh tests/config.nix tests/plugins/libplugintest.$(SO_EXT)
test-deps += tests/common.sh tests/config.nix tests/ca/config.nix tests/plugins/libplugintest.$(SO_EXT)