1
0
Fork 0
mirror of https://github.com/NixOS/nix synced 2025-06-27 12:41:15 +02:00

Merge pull request #12759 from roberth/c-api-libflake-settings

C API / settings: remove nix-flake-c global init
This commit is contained in:
John Ericson 2025-03-27 12:38:25 -04:00 committed by GitHub
commit a26a15d05c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
20 changed files with 277 additions and 189 deletions

View file

@ -1,96 +0,0 @@
# This is a helper to callFlake() to lazily fetch flake inputs.
# The contents of the lock file, in JSON format.
lockFileStr:
# A mapping of lock file node IDs to { sourceInfo, subdir } attrsets,
# with sourceInfo.outPath providing an SourceAccessor to a previously
# fetched tree. This is necessary for possibly unlocked inputs, in
# particular the root input, but also --override-inputs pointing to
# unlocked trees.
overrides:
# This is `prim_fetchFinalTree`.
fetchTreeFinal:
let
lockFile = builtins.fromJSON lockFileStr;
# Resolve a input spec into a node name. An input spec is
# either a node name, or a 'follows' path from the root
# node.
resolveInput =
inputSpec: if builtins.isList inputSpec then getInputByPath lockFile.root inputSpec else inputSpec;
# Follow an input attrpath (e.g. ["dwarffs" "nixpkgs"]) from the
# root node, returning the final node.
getInputByPath =
nodeName: path:
if path == [ ] then
nodeName
else
getInputByPath
# Since this could be a 'follows' input, call resolveInput.
(resolveInput lockFile.nodes.${nodeName}.inputs.${builtins.head path})
(builtins.tail path);
allNodes = builtins.mapAttrs (
key: node:
let
parentNode = allNodes.${getInputByPath lockFile.root node.parent};
sourceInfo =
if overrides ? ${key} then
overrides.${key}.sourceInfo
else if node.locked.type == "path" && builtins.substring 0 1 node.locked.path != "/" then
parentNode.sourceInfo
// {
outPath = parentNode.outPath + ("/" + node.locked.path);
}
else
# FIXME: remove obsolete node.info.
# Note: lock file entries are always final.
fetchTreeFinal (node.info or { } // removeAttrs node.locked [ "dir" ]);
subdir = overrides.${key}.dir or node.locked.dir or "";
outPath = sourceInfo + ((if subdir == "" then "" else "/") + subdir);
flake = import (outPath + "/flake.nix");
inputs = builtins.mapAttrs (inputName: inputSpec: allNodes.${resolveInput inputSpec}) (
node.inputs or { }
);
outputs = flake.outputs (inputs // { self = result; });
result =
outputs
# We add the sourceInfo attribute for its metadata, as they are
# relevant metadata for the flake. However, the outPath of the
# sourceInfo does not necessarily match the outPath of the flake,
# as the flake may be in a subdirectory of a source.
# This is shadowed in the next //
// sourceInfo
// {
# This shadows the sourceInfo.outPath
inherit outPath;
inherit inputs;
inherit outputs;
inherit sourceInfo;
_type = "flake";
};
in
if node.flake or true then
assert builtins.isFunction flake.outputs;
result
else
sourceInfo
) lockFile.nodes;
in
allNodes.${lockFile.root}

View file

@ -103,4 +103,4 @@ Path getNixDefExpr()
: getHome() + "/.nix-defexpr";
}
}
} // namespace nix

View file

@ -7,6 +7,7 @@
namespace nix {
class EvalState;
struct PrimOp;
struct EvalSettings : Config
{
@ -50,6 +51,8 @@ struct EvalSettings : Config
LookupPathHooks lookupPathHooks;
std::vector<PrimOp> extraPrimOps;
Setting<bool> enableNativeCode{this, false, "allow-unsafe-native-code-during-evaluation", R"(
Enable built-in functions that allow executing native code.

View file

@ -288,10 +288,6 @@ EvalState::EvalState(
CanonPath("derivation-internal.nix"),
#include "primops/derivation.nix.gen.hh"
)}
, callFlakeInternal{internalFS->addFile(
CanonPath("call-flake.nix"),
#include "call-flake.nix.gen.hh"
)}
, store(store)
, buildStore(buildStore ? buildStore : store)
, debugRepl(nullptr)
@ -353,7 +349,7 @@ EvalState::EvalState(
#include "fetchurl.nix.gen.hh"
);
createBaseEnv();
createBaseEnv(settings);
}

View file

@ -274,14 +274,12 @@ public:
/**
* In-memory filesystem for internal, non-user-callable Nix
* expressions like call-flake.nix.
* expressions like `derivation.nix`.
*/
const ref<MemorySourceAccessor> internalFS;
const SourcePath derivationInternal;
const SourcePath callFlakeInternal;
/**
* Store used to materialise .drv files.
*/
@ -633,7 +631,7 @@ private:
unsigned int baseEnvDispl = 0;
void createBaseEnv();
void createBaseEnv(const EvalSettings & settings);
Value * addConstant(const std::string & name, Value & v, Constant info);

View file

@ -126,7 +126,6 @@ generated_headers = []
foreach header : [
'imported-drv-to-derivation.nix',
'fetchurl.nix',
'call-flake.nix',
]
generated_headers += gen_header.process(header)
endforeach

View file

@ -4675,7 +4675,7 @@ RegisterPrimOp::RegisterPrimOp(PrimOp && primOp)
}
void EvalState::createBaseEnv()
void EvalState::createBaseEnv(const EvalSettings & evalSettings)
{
baseEnv.up = 0;
@ -4934,6 +4934,12 @@ void EvalState::createBaseEnv()
addPrimOp(std::move(primOpAdjusted));
}
for (auto & primOp : evalSettings.extraPrimOps) {
auto primOpAdjusted = primOp;
primOpAdjusted.arity = std::max(primOp.args.size(), primOp.arity);
addPrimOp(std::move(primOpAdjusted));
}
/* Add a wrapper around the derivation primop that computes the
`drvPath' and `outPath' attributes lazily.