Skip to content

Commit

Permalink
Add option --default-flake-schemas to override the default schemas flake
Browse files Browse the repository at this point in the history
  • Loading branch information
edolstra committed Jun 25, 2024
1 parent 8d2c6be commit 922d4b5
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 15 deletions.
10 changes: 5 additions & 5 deletions src/nix/call-flake-schemas.nix
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
/* The flake providing default schemas. */
defaultSchemasFlake:

/* The flake whose contents we want to extract. */
flake:

let
Expand All @@ -11,11 +15,7 @@ in
rec {
outputNames = builtins.attrNames flake.outputs;

allSchemas = (flake.outputs.schemas or defaultSchemas) // schemaOverrides;

# FIXME: make this configurable
# FIXME: a pre-cached copy of the flake-schemas needs to be built in to the nix binary
defaultSchemas = (builtins.getFlake "github:DeterminateSystems/flake-schemas/3b4d5fef938f698c8737515532a1be53bf6355f2?narHash=sha256-TBmeIZHYzqKTaTe8YFgTimVBGLzkoUgZfkDMkZBZyfo%3D").schemas;
allSchemas = (flake.outputs.schemas or defaultSchemasFlake.schemas) // schemaOverrides;

schemaOverrides = {}; # FIXME

Expand Down
59 changes: 49 additions & 10 deletions src/nix/flake.cc
Original file line number Diff line number Diff line change
Expand Up @@ -296,37 +296,49 @@ using namespace eval_cache;
std::tuple<ref<EvalCache>, ref<eval_cache::AttrCursor>>
call(
EvalState & state,
std::shared_ptr<flake::LockedFlake> lockedFlake)
std::shared_ptr<flake::LockedFlake> lockedFlake,
const FlakeRef & defaultSchemasFlake)
{
auto fingerprint = lockedFlake->getFingerprint(state.store);

std::string callFlakeSchemasNix =
#include "call-flake-schemas.nix.gen.hh"
;

auto lockedDefaultSchemasFlake = flake::lockFlake(state, defaultSchemasFlake, {});
auto lockedDefaultSchemasFlakeFingerprint = lockedDefaultSchemasFlake.getFingerprint(state.store);

std::optional<Fingerprint> fingerprint2;
if (fingerprint)
if (fingerprint && lockedDefaultSchemasFlakeFingerprint)
fingerprint2 = hashString(HashAlgorithm::SHA256,
fmt("app:%s:%s",
fmt("app:%s:%s:%s",
hashString(HashAlgorithm::SHA256, callFlakeSchemasNix).to_string(HashFormat::Base16, false),
fingerprint->to_string(HashFormat::Base16, false)));
fingerprint->to_string(HashFormat::Base16, false),
lockedDefaultSchemasFlakeFingerprint->to_string(HashFormat::Base16, false)));

// FIXME: merge with openEvalCache().
auto cache = make_ref<EvalCache>(
evalSettings.useEvalCache && evalSettings.pureEval
? fingerprint2
: std::nullopt,
state,
[&state, lockedFlake, callFlakeSchemasNix]()
[&state, lockedFlake, callFlakeSchemasNix, lockedDefaultSchemasFlake]()
{
auto vCallFlakeSchemas = state.allocValue();
state.eval(state.parseExprFromString(callFlakeSchemasNix, state.rootPath(CanonPath::root)), *vCallFlakeSchemas);

auto vFlake = state.allocValue();
flake::callFlake(state, *lockedFlake, *vFlake);

auto vDefaultSchemasFlake = state.allocValue();
if (vFlake->type() == nAttrs && vFlake->attrs()->get(state.symbols.create("schemas")))
vDefaultSchemasFlake->mkNull();
else
flake::callFlake(state, lockedDefaultSchemasFlake, *vDefaultSchemasFlake);

auto vRes = state.allocValue();
state.callFunction(*vCallFlakeSchemas, *vFlake, *vRes, noPos);
Value * args[] = {vDefaultSchemasFlake, vFlake};
state.callFunction(*vCallFlakeSchemas, 2, args, *vRes, noPos);

return vRes;
});
Expand Down Expand Up @@ -454,7 +466,34 @@ std::shared_ptr<AttrCursor> derivation(ref<AttrCursor> leaf)

}

struct CmdFlakeCheck : FlakeCommand
struct MixFlakeSchemas : virtual Args, virtual StoreCommand
{
std::string defaultFlakeSchemas =
"github:DeterminateSystems/flake-schemas/3b4d5fef938f698c8737515532a1be53bf6355f2?narHash=sha256-TBmeIZHYzqKTaTe8YFgTimVBGLzkoUgZfkDMkZBZyfo%3D";

MixFlakeSchemas()
{
addFlag({
.longName = "default-flake-schemas",
.description = "The URL of the flake providing default flake schema definitions.",
.labels = {"flake-ref"},
.handler = {&defaultFlakeSchemas},
.completer = {[&](AddCompletions & completions, size_t, std::string_view prefix) {
completeFlakeRef(
completions,
getStore(),
prefix);
}}
});
}

FlakeRef getDefaultFlakeSchemas()
{
return parseFlakeRef(defaultFlakeSchemas, absPath("."));
}
};

struct CmdFlakeCheck : FlakeCommand, MixFlakeSchemas
{
bool build = true;
bool checkAllSystems = false;
Expand Down Expand Up @@ -498,7 +537,7 @@ struct CmdFlakeCheck : FlakeCommand
auto flake = std::make_shared<LockedFlake>(lockFlake());
auto localSystem = std::string(settings.thisSystem.get());

auto [cache, inventory] = flake_schemas::call(*state, flake);
auto [cache, inventory] = flake_schemas::call(*state, flake, getDefaultFlakeSchemas());

std::vector<DerivedPath> drvPaths;

Expand Down Expand Up @@ -881,7 +920,7 @@ struct CmdFlakeArchive : FlakeCommand, MixJSON, MixDryRun
}
};

struct CmdFlakeShow : FlakeCommand, MixJSON
struct CmdFlakeShow : FlakeCommand, MixJSON, MixFlakeSchemas
{
bool showLegacy = false;
bool showAllSystems = false;
Expand Down Expand Up @@ -918,7 +957,7 @@ struct CmdFlakeShow : FlakeCommand, MixJSON
auto flake = std::make_shared<LockedFlake>(lockFlake());
auto localSystem = std::string(settings.thisSystem.get());

auto [cache, inventory] = flake_schemas::call(*state, flake);
auto [cache, inventory] = flake_schemas::call(*state, flake, getDefaultFlakeSchemas());

if (json) {
std::function<void(ref<eval_cache::AttrCursor> node, nlohmann::json & obj)> visit;
Expand Down

0 comments on commit 922d4b5

Please sign in to comment.