Skip to content

Commit

Permalink
Add basic impure derivations
Browse files Browse the repository at this point in the history
Impure derivations are derivations that can produce a different result
every time they're built. Example:

  stdenv.mkDerivation {
    name = "impure";
    __impure = true; # marks this derivation as impure
    buildCommand = "date > $out";
  };

Some important characteristics:

* Impure derivations are not "cached". Thus, running "nix-build" on
  the example above multiple times will cause a rebuild every time. In
  the future, we could implement some mechanism for reusing impure
  builds across invocations.

* The outputs of impure derivations are moved to a content-addressed
  location after the build (i.e., the resulting store path will
  correspond to the hash of the contents of the path). This way,
  multiple builds of the same impure derivation do not collide.

* Because of content-addressability, the output paths of an impure
  derivation recorded in its .drv file are "virtual" placeholders for
  the actual outputs which are not known in advance. This also means
  that "nix-store -q bla.drv" gives a meaningless path.

* Pure derivations are not allowed to depend on impure
  derivations. The only exception is fixed-output derivations. Because
  the latter always produce a known output, they can depend on impure
  shenanigans just fine. Also, repeatedly running "nix-build" on such
  a fixed-output derivation will *not* cause a rebuild of the impure
  dependency. After all, if the fixed output exists, its dependencies
  are no longer relevant. Thus, fixed-output derivations form an
  "impurity barrier" in the dependency graph.

* When sandboxing is enabled, impure derivations can access the
  network in the same way as fixed-output derivations. In relaxed
  sandboxing mode, they can access the local filesystem.

* Currently, the output of an impure derivation must have no
  references. This is because the content-addressing scheme must be
  extended to handle references, in particular self-references (as
  described in the ASE-2005 paper.)

* Currently, impure derivations can only have a single output. No real
  reason for this.

* "nix-build" on an impure derivation currently creates a result
  symlink to the incorrect, virtual output.

A motivating example is the problem of using "fetchurl" on a
dynamically generated tarball whose contents are deterministic, but
where the tarball does not have a canonical form. Previously, this
required "fetchurl" to do the unpacking in the same
derivation. (That's what "fetchzip" does.) But now we can say:

  tarball = stdenv.mkDerivation {
    __impure = true;
    name = "tarball";
    buildInputs = [ curl ];
    buildCommand =
      "curl --fail -Lk https://github.com/NixOS/patchelf/tarball/c1f89c077e44a495c62ed0dcfaeca21510df93ef > $out";
  };

  unpacked = stdenv.mkDerivation {
    name = "unpacked";
    outputHashAlgo = "sha256";
    outputHashMode = "recursive";
    outputHash = "1jl8n1n36w63wffkm56slcfa7vj9fxkv4ax0fr0mcfah55qj5l8s";
    buildCommand =
      "mkdir $out; tar xvf ${tarball} -C $out";
  };

I needed this because <nix/fetchurl.nix> does not support unpacking,
and adding untar/unzip functionality would be annoying (especially
since we can't just call "tar" or "unzip" in a sandbox).

#520
  • Loading branch information
edolstra committed Jan 31, 2019
1 parent 10053c5 commit 18c512d
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 21 deletions.
107 changes: 86 additions & 21 deletions src/libstore/build.cc
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,10 @@ class Worker
/* Cache for pathContentsGood(). */
std::map<Path, bool> pathContentsGoodCache;

/* A mapping from the virtual output paths in impure derivations
to the actual (content-addressed) resulting store paths. */
std::map<Path, Path> impureRemapping;

public:

const Activity act;
Expand Down Expand Up @@ -352,6 +356,17 @@ class Worker
act.setExpected(actDownload, expectedDownloadSize + doneDownloadSize);
act.setExpected(actCopyPath, expectedNarSize + doneNarSize);
}

void remapImpureOutput(const Path & virtualOutput, const Path & actualOutput)
{
assert(virtualOutput.size() == actualOutput.size());
impureRemapping[virtualOutput] = actualOutput;
}

std::string remappedPath(const Path & path)
{
return get(impureRemapping, path, path);
}
};


Expand Down Expand Up @@ -810,9 +825,17 @@ class DerivationGoal : public Goal
/* Whether this is a fixed-output derivation. */
bool fixedOutput;

/* Whether this is an impure derivation. */
bool isImpure = false;

/* Whether to run the build in a private network namespace. */
bool privateNetwork = false;

bool allowNetwork()
{
return fixedOutput || isImpure;
}

typedef void (DerivationGoal::*GoalState)();
GoalState state;

Expand Down Expand Up @@ -1136,12 +1159,21 @@ void DerivationGoal::haveDerivation()

retrySubstitution = false;

isImpure = drv->isImpure();
fixedOutput = drv->isFixedOutput();

if (isImpure && fixedOutput)
throw Error("derivation '%s' cannot be both impure and fixed-output", drvPath);

for (auto & i : drv->outputs)
worker.store.addTempRoot(i.second.path);

/* Check what outputs paths are not already valid. */
PathSet invalidOutputs = checkPathValidity(false, buildMode == bmRepair);

if (isImpure && invalidOutputs.size() != wantedOutputs.size())
throw Error("derivation '%s' is impure but some of its virtual outputs are valid", drvPath);

/* If they are all valid, then we're done. */
if (invalidOutputs.size() == 0 && buildMode == bmNormal) {
done(BuildResult::AlreadyValid);
Expand All @@ -1153,7 +1185,7 @@ void DerivationGoal::haveDerivation()
/* We are first going to try to create the invalid output paths
through substitutes. If that doesn't work, we'll build
them. */
if (settings.useSubstitutes && drv->substitutesAllowed())
if (settings.useSubstitutes && drv->substitutesAllowed() && !isImpure)
for (auto & i : invalidOutputs)
addWaitee(worker.makeSubstitutionGoal(i, buildMode == bmRepair ? Repair : NoRepair));

Expand Down Expand Up @@ -1324,13 +1356,20 @@ void DerivationGoal::inputsRealised()
that are specified as inputs. */
assert(worker.store.isValidPath(i.first));
Derivation inDrv = worker.store.derivationFromPath(i.first);
for (auto & j : i.second)
for (auto & j : i.second) {
auto j2 = worker.remappedPath(inDrv.outputs[j].path);
if (j2 != inDrv.outputs[j].path)
inputRewrites[inDrv.outputs[j].path] = j2;
if (inDrv.outputs.find(j) != inDrv.outputs.end())
worker.store.computeFSClosure(inDrv.outputs[j].path, inputPaths);
worker.store.computeFSClosure(j2, inputPaths);
else
throw Error(
format("derivation '%1%' requires non-existent output '%2%' from input derivation '%3%'")
% drvPath % j % i.first);
}

if (!isImpure && !fixedOutput && inDrv.isImpure())
throw Error("pure derivation '%s' depends on impure derivation '%s'", drvPath, i.first);
}

/* Second, the input sources. */
Expand All @@ -1344,8 +1383,9 @@ void DerivationGoal::inputsRealised()
fixedOutput = drv->isFixedOutput();

/* Don't repeat fixed-output derivations since they're already
verified by their output hash.*/
nrRounds = fixedOutput ? 1 : settings.buildRepeat + 1;
verified by their output hash. Similarly, don't repeat impure
derivations because by their nature they're not repeatable. */
nrRounds = fixedOutput || isImpure ? 1 : settings.buildRepeat + 1;

/* Okay, try to build. Note that here we don't wait for a build
slot to become available, since we don't need one if there is a
Expand Down Expand Up @@ -1384,6 +1424,7 @@ void DerivationGoal::tryToBuild()
build this derivation, so no further checks are necessary. */
validPaths = checkPathValidity(true, buildMode == bmRepair);
if (buildMode != bmCheck && validPaths.size() == drv->outputs.size()) {
assert(!isImpure);
debug(format("skipping build of derivation '%1%', someone beat us to it") % drvPath);
outputLocks.setDeletion(true);
done(BuildResult::AlreadyValid);
Expand All @@ -1398,7 +1439,10 @@ void DerivationGoal::tryToBuild()
them. */
for (auto & i : drv->outputs) {
Path path = i.second.path;
if (worker.store.isValidPath(path)) continue;
if (worker.store.isValidPath(path)) {
assert(!isImpure);
continue;
}
debug(format("removing invalid path '%1%'") % path);
deletePath(worker.store.toRealPath(path));
}
Expand Down Expand Up @@ -1631,7 +1675,7 @@ void DerivationGoal::buildDone()
st =
dynamic_cast<NotDeterministic*>(&e) ? BuildResult::NotDeterministic :
statusOk(status) ? BuildResult::OutputRejected :
fixedOutput || diskFull ? BuildResult::TransientFailure :
fixedOutput || isImpure || diskFull ? BuildResult::TransientFailure :
BuildResult::PermanentFailure;
}

Expand All @@ -1645,7 +1689,7 @@ void DerivationGoal::buildDone()

HookReply DerivationGoal::tryBuildHook()
{
if (!worker.tryBuildHook || !useDerivation) return rpDecline;
if (!worker.tryBuildHook || !useDerivation || isImpure) return rpDecline;

if (!worker.hook)
worker.hook = std::make_unique<HookInstance>();
Expand Down Expand Up @@ -1830,7 +1874,7 @@ void DerivationGoal::startBuilder()
else if (settings.sandboxMode == smDisabled)
useChroot = false;
else if (settings.sandboxMode == smRelaxed)
useChroot = !fixedOutput && !noChroot;
useChroot = !allowNetwork() && !noChroot;
}

if (worker.store.storeDir != worker.store.realStoreDir) {
Expand Down Expand Up @@ -2014,7 +2058,7 @@ void DerivationGoal::startBuilder()
"nogroup:x:65534:\n") % sandboxGid).str());

/* Create /etc/hosts with localhost entry. */
if (!fixedOutput)
if (!allowNetwork())
writeFile(chrootRootDir + "/etc/hosts", "127.0.0.1 localhost\n::1 localhost\n");

/* Make the closure of the inputs available in the chroot,
Expand Down Expand Up @@ -2187,7 +2231,7 @@ void DerivationGoal::startBuilder()
us.
*/

if (!fixedOutput)
if (!allowNetwork())
privateNetwork = true;

userNamespaceSync.create();
Expand Down Expand Up @@ -2362,7 +2406,7 @@ void DerivationGoal::initEnv()
to the builder is generally impure, but the output of
fixed-output derivations is by definition pure (since we
already know the cryptographic hash of the output). */
if (fixedOutput) {
if (allowNetwork()) {
for (auto & i : parsedDrv->getStringsAttr("impureEnvVars").value_or(Strings()))
env[i] = getEnv(i);
}
Expand Down Expand Up @@ -2646,7 +2690,7 @@ void DerivationGoal::runChild()
/* Fixed-output derivations typically need to access the
network, so give them access to /etc/resolv.conf and so
on. */
if (fixedOutput) {
if (allowNetwork()) {
ss.push_back("/etc/resolv.conf");
ss.push_back("/etc/nsswitch.conf");
ss.push_back("/etc/services");
Expand Down Expand Up @@ -3008,10 +3052,13 @@ PathSet parseReferenceSpecifiers(Store & store, const BasicDerivation & drv, con

void DerivationGoal::registerOutputs()
{
// FIXME: This function is way to complicated.

/* When using a build hook, the build hook can register the output
as valid (by doing `nix-store --import'). If so we don't have
to do anything here. */
if (hook) {
assert(!isImpure);
bool allValid = true;
for (auto & i : drv->outputs)
if (!worker.store.isValidPath(i.second.path)) allValid = false;
Expand Down Expand Up @@ -3106,6 +3153,7 @@ void DerivationGoal::registerOutputs()
outputs (i.e., the content hash should match the specified
hash). */
if (fixedOutput) {
assert(i.second.hash != "");

bool recursive; Hash h;
i.second.parseHashInfo(recursive, h);
Expand Down Expand Up @@ -3193,14 +3241,31 @@ void DerivationGoal::registerOutputs()
continue;
}

/* For debugging, print out the referenced and unreferenced
paths. */
for (auto & i : inputPaths) {
PathSet::iterator j = references.find(i);
if (j == references.end())
debug(format("unreferenced input: '%1%'") % i);
else
debug(format("referenced input: '%1%'") % i);
if (isImpure) {

/* Currently impure derivations cannot have any references. */
if (!references.empty())
throw BuildError("impure derivation output '%s' has a reference to '%s'",
path, *references.begin());

/* Move the output to its content-addressed location. */
auto caPath = worker.store.makeFixedOutputPath(true, hash.first, storePathToName(path));
debug("moving impure output '%s' to content-addressed '%s'", path, caPath);

worker.remapImpureOutput(path, caPath);

if (worker.store.isValidPath(caPath))
continue;

actualPath = worker.store.toRealPath(caPath);
deletePath(actualPath);

if (rename(path.c_str(), actualPath.c_str()))
throw SysError("moving '%s' to '%s'", path, caPath);

path = caPath;

info.ca = makeFixedOutputCA(true, hash.first);
}

if (curRound == nrRounds) {
Expand Down
9 changes: 9 additions & 0 deletions src/libstore/derivations.cc
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,15 @@ bool BasicDerivation::isFixedOutput() const
}


bool BasicDerivation::isImpure() const
{
// FIXME: drop single output restriction
return outputs.size() == 1 &&
outputs.begin()->first == "out" &&
get(env, "__impure", "") == "1";
}


DrvHashes drvHashes;


Expand Down
3 changes: 3 additions & 0 deletions src/libstore/derivations.hh
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ struct BasicDerivation
/* Return true iff this is a fixed-output derivation. */
bool isFixedOutput() const;

/* Return true iff this is an impure derivation. */
bool isImpure() const;

/* Return the output paths of a derivation. */
PathSet outputPaths() const;

Expand Down

0 comments on commit 18c512d

Please sign in to comment.