Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Check the flake epoch
Closes #2883.
  • Loading branch information
edolstra committed May 22, 2019
1 parent 3cecf3f commit e414bde
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 4 deletions.
11 changes: 9 additions & 2 deletions src/libexpr/primops/flake.cc
Expand Up @@ -309,10 +309,17 @@ Flake getFlake(EvalState & state, const FlakeRef & flakeRef, bool impureIsAllowe

state.forceAttrs(vInfo);

if (auto epoch = vInfo.attrs->get(state.symbols.create("epoch"))) {
flake.epoch = state.forceInt(*(**epoch).value, *(**epoch).pos);
if (flake.epoch > 2019)
throw Error("flake '%s' requires unsupported epoch %d; please upgrade Nix", flakeRef, flake.epoch);
} else
throw Error("flake '%s' lacks attribute 'epoch'", flakeRef);

if (auto name = vInfo.attrs->get(state.sName))
flake.id = state.forceStringNoCtx(*(**name).value, *(**name).pos);
else
throw Error("flake lacks attribute 'name'");
throw Error("flake '%s' lacks attribute 'name'", flakeRef);

if (auto description = vInfo.attrs->get(state.sDescription))
flake.description = state.forceStringNoCtx(*(**description).value, *(**description).pos);
Expand All @@ -337,7 +344,7 @@ Flake getFlake(EvalState & state, const FlakeRef & flakeRef, bool impureIsAllowe
state.forceFunction(*(**provides).value, *(**provides).pos);
flake.vProvides = (**provides).value;
} else
throw Error("flake lacks attribute 'provides'");
throw Error("flake '%s' lacks attribute 'provides'", flakeRef);

return flake;
}
Expand Down
3 changes: 2 additions & 1 deletion src/libexpr/primops/flake.hh
Expand Up @@ -101,7 +101,8 @@ struct Flake
std::map<FlakeAlias, FlakeRef> nonFlakeRequires;
Value * vProvides; // FIXME: gc
// date
// content hash
unsigned int epoch;

Flake(const FlakeRef & origRef, const SourceInfo & sourceInfo) : originalRef(origRef),
resolvedRef(sourceInfo.resolvedRef), revCount(sourceInfo.revCount), storePath(sourceInfo.storePath) {};
};
Expand Down
2 changes: 2 additions & 0 deletions src/nix/flake.cc
Expand Up @@ -83,6 +83,7 @@ void printFlakeInfo(const Flake & flake, bool json) {
if (flake.revCount)
j["revCount"] = *flake.revCount;
j["path"] = flake.storePath;
j["epoch"] = flake.epoch;
std::cout << j.dump(4) << std::endl;
} else {
std::cout << "ID: " << flake.id << "\n";
Expand All @@ -95,6 +96,7 @@ void printFlakeInfo(const Flake & flake, bool json) {
if (flake.revCount)
std::cout << "Revcount: " << *flake.revCount << "\n";
std::cout << "Path: " << flake.storePath << "\n";
std::cout << "Epoch: " << flake.epoch << "\n";
}
}

Expand Down
6 changes: 5 additions & 1 deletion tests/flakes.sh
Expand Up @@ -146,7 +146,7 @@ nix build -o $TEST_ROOT/result file://$flake2Dir:bar
# Test whether indirect dependencies work.
nix build -o $TEST_ROOT/result --flake-registry $registry $flake3Dir:xyzzy

# Add dependency to flake3
# Add dependency to flake3.
rm $flake3Dir/flake.nix

cat > $flake3Dir/flake.nix <<EOF
Expand Down Expand Up @@ -174,3 +174,7 @@ nix build -o $TEST_ROOT/result --flake-registry $registry $flake3Dir:sth

# Check whether it saved the lockfile
[[ ! (-z $(git -C $flake3Dir diff master)) ]]

# Unsupported epochs should be an error.
sed -i $flake3Dir/flake.nix -e s/2019/2030/
nix build -o $TEST_ROOT/result --flake-registry $registry $flake3Dir:sth 2>&1 | grep 'unsupported epoch'

0 comments on commit e414bde

Please sign in to comment.