1
1
Fork 0
mirror of https://github.com/NixOS/nix.git synced 2025-11-27 12:41:00 +01:00

No more Path in libnixcmd

Co-authored-by: Vinayak Goyal <vinayakankugoyal@gmail.com>
This commit is contained in:
John Ericson 2024-09-18 12:07:15 -04:00 committed by Ubuntu
parent 88c9c6d89d
commit 84079e10cf
14 changed files with 52 additions and 39 deletions

View file

@ -165,7 +165,7 @@ Bindings * MixEvalArgs::getAutoArgs(EvalState & state)
state.parseExprFromString( state.parseExprFromString(
arg.expr, arg.expr,
compatibilitySettings.nixShellShebangArgumentsRelativeToScript compatibilitySettings.nixShellShebangArgumentsRelativeToScript
? state.rootPath(absPath(getCommandBaseDir())) ? state.rootPath(absPath(getCommandBaseDir()).string())
: state.rootPath("."))); : state.rootPath(".")));
}, },
[&](const AutoArgString & arg) { v->mkString(arg.s, state.mem); }, [&](const AutoArgString & arg) { v->mkString(arg.s, state.mem); },
@ -177,7 +177,7 @@ Bindings * MixEvalArgs::getAutoArgs(EvalState & state)
return res.finish(); return res.finish();
} }
SourcePath lookupFileArg(EvalState & state, std::string_view s, const Path * baseDir) SourcePath lookupFileArg(EvalState & state, std::string_view s, const std::filesystem::path * baseDir)
{ {
if (EvalSettings::isPseudoUrl(s)) { if (EvalSettings::isPseudoUrl(s)) {
auto accessor = fetchers::downloadTarball(*state.store, state.fetchSettings, EvalSettings::resolvePseudoUrl(s)); auto accessor = fetchers::downloadTarball(*state.store, state.fetchSettings, EvalSettings::resolvePseudoUrl(s));
@ -197,12 +197,13 @@ SourcePath lookupFileArg(EvalState & state, std::string_view s, const Path * bas
} }
else if (s.size() > 2 && s.at(0) == '<' && s.at(s.size() - 1) == '>') { else if (s.size() > 2 && s.at(0) == '<' && s.at(s.size() - 1) == '>') {
Path p(s.substr(1, s.size() - 2)); // Should perhaps be a `CanonPath`?
std::string p(s.substr(1, s.size() - 2));
return state.findFile(p); return state.findFile(p);
} }
else else
return state.rootPath(baseDir ? absPath(s, *baseDir) : absPath(s)); return state.rootPath(absPath(std::filesystem::path{s}, baseDir).string());
} }
} // namespace nix } // namespace nix

View file

@ -134,7 +134,7 @@ struct MixFlakeOptions : virtual Args, EvalCommand
struct SourceExprCommand : virtual Args, MixFlakeOptions struct SourceExprCommand : virtual Args, MixFlakeOptions
{ {
std::optional<Path> file; std::optional<std::filesystem::path> file;
std::optional<std::string> expr; std::optional<std::string> expr;
SourceExprCommand(); SourceExprCommand();
@ -310,7 +310,7 @@ static RegisterCommand registerCommand2(std::vector<std::string> && name)
struct MixProfile : virtual StoreCommand struct MixProfile : virtual StoreCommand
{ {
std::optional<Path> profile; std::optional<std::filesystem::path> profile;
MixProfile(); MixProfile();

View file

@ -84,6 +84,6 @@ private:
/** /**
* @param baseDir Optional [base directory](https://nix.dev/manual/nix/development/glossary#gloss-base-directory) * @param baseDir Optional [base directory](https://nix.dev/manual/nix/development/glossary#gloss-base-directory)
*/ */
SourcePath lookupFileArg(EvalState & state, std::string_view s, const Path * baseDir = nullptr); SourcePath lookupFileArg(EvalState & state, std::string_view s, const std::filesystem::path * baseDir = nullptr);
} // namespace nix } // namespace nix

View file

@ -17,7 +17,7 @@ class AttrCursor;
struct App struct App
{ {
std::vector<DerivedPath> context; std::vector<DerivedPath> context;
Path program; std::filesystem::path program;
// FIXME: add args, sandbox settings, metadata, ... // FIXME: add args, sandbox settings, metadata, ...
}; };

View file

@ -19,7 +19,16 @@ struct AbstractNixRepl
typedef std::vector<std::pair<Value *, std::string>> AnnotatedValues; typedef std::vector<std::pair<Value *, std::string>> AnnotatedValues;
using RunNix = void(Path program, const Strings & args, const std::optional<std::string> & input); /**
* Run a nix executable
*
* @todo this is a layer violation
*
* @param programName Name of the command, e.g. `nix` or `nix-env`.
* @param args aguments to the command.
*/
using RunNix =
void(const std::string & programName, const Strings & args, const std::optional<std::string> & input);
/** /**
* @param runNix Function to run the nix CLI to support various * @param runNix Function to run the nix CLI to support various

View file

@ -132,7 +132,7 @@ MixFlakeOptions::MixFlakeOptions()
lockFlags.writeLockFile = false; lockFlags.writeLockFile = false;
lockFlags.inputOverrides.insert_or_assign( lockFlags.inputOverrides.insert_or_assign(
flake::parseInputAttrPath(inputAttrPath), flake::parseInputAttrPath(inputAttrPath),
parseFlakeRef(fetchSettings, flakeRef, absPath(getCommandBaseDir()), true)); parseFlakeRef(fetchSettings, flakeRef, absPath(getCommandBaseDir()).string(), true));
}}, }},
.completer = {[&](AddCompletions & completions, size_t n, std::string_view prefix) { .completer = {[&](AddCompletions & completions, size_t n, std::string_view prefix) {
if (n == 0) { if (n == 0) {
@ -173,7 +173,7 @@ MixFlakeOptions::MixFlakeOptions()
auto flake = flake::lockFlake( auto flake = flake::lockFlake(
flakeSettings, flakeSettings,
*evalState, *evalState,
parseFlakeRef(fetchSettings, flakeRef, absPath(getCommandBaseDir())), parseFlakeRef(fetchSettings, flakeRef, absPath(getCommandBaseDir()).string()),
{.writeLockFile = false}); {.writeLockFile = false});
for (auto & [inputName, input] : flake.lockFile.root->inputs) { for (auto & [inputName, input] : flake.lockFile.root->inputs) {
auto input2 = flake.lockFile.findInput({inputName}); // resolve 'follows' nodes auto input2 = flake.lockFile.findInput({inputName}); // resolve 'follows' nodes
@ -263,7 +263,7 @@ void SourceExprCommand::completeInstallable(AddCompletions & completions, std::s
evalSettings.pureEval = false; evalSettings.pureEval = false;
auto state = getEvalState(); auto state = getEvalState();
auto e = state->parseExprFromFile(resolveExprPath(lookupFileArg(*state, *file))); auto e = state->parseExprFromFile(resolveExprPath(lookupFileArg(*state, file->string())));
Value root; Value root;
state->eval(e, root); state->eval(e, root);
@ -465,10 +465,10 @@ Installables SourceExprCommand::parseInstallables(ref<Store> store, std::vector<
state->eval(e, *vFile); state->eval(e, *vFile);
} else if (file) { } else if (file) {
auto dir = absPath(getCommandBaseDir()); auto dir = absPath(getCommandBaseDir());
state->evalFile(lookupFileArg(*state, *file, &dir), *vFile); state->evalFile(lookupFileArg(*state, file->string(), &dir), *vFile);
} else { } else {
Path dir = absPath(getCommandBaseDir()); auto dir = absPath(getCommandBaseDir());
auto e = state->parseExprFromString(*expr, state->rootPath(dir)); auto e = state->parseExprFromString(*expr, state->rootPath(dir.string()));
state->eval(e, *vFile); state->eval(e, *vFile);
} }
@ -801,7 +801,8 @@ std::vector<FlakeRef> RawInstallablesCommand::getFlakeRefsForCompletion()
std::vector<FlakeRef> res; std::vector<FlakeRef> res;
res.reserve(rawInstallables.size()); res.reserve(rawInstallables.size());
for (const auto & i : rawInstallables) for (const auto & i : rawInstallables)
res.push_back(parseFlakeRefWithFragment(fetchSettings, expandTilde(i), absPath(getCommandBaseDir())).first); res.push_back(
parseFlakeRefWithFragment(fetchSettings, expandTilde(i), absPath(getCommandBaseDir()).string()).first);
return res; return res;
} }
@ -820,7 +821,8 @@ void RawInstallablesCommand::run(ref<Store> store)
std::vector<FlakeRef> InstallableCommand::getFlakeRefsForCompletion() std::vector<FlakeRef> InstallableCommand::getFlakeRefsForCompletion()
{ {
return {parseFlakeRefWithFragment(fetchSettings, expandTilde(_installable), absPath(getCommandBaseDir())).first}; return {parseFlakeRefWithFragment(fetchSettings, expandTilde(_installable), absPath(getCommandBaseDir()).string())
.first};
} }
void InstallablesCommand::run(ref<Store> store, std::vector<std::string> && rawInstallables) void InstallablesCommand::run(ref<Store> store, std::vector<std::string> && rawInstallables)

View file

@ -58,8 +58,7 @@ struct NixRepl : AbstractNixRepl, detail::ReplCompleterMixin, gc
{ {
size_t debugTraceIndex; size_t debugTraceIndex;
// Arguments passed to :load, saved so they can be reloaded with :reload std::list<std::filesystem::path> loadedFiles;
Strings loadedFiles;
// Arguments passed to :load-flake, saved so they can be reloaded with :reload // Arguments passed to :load-flake, saved so they can be reloaded with :reload
Strings loadedFlakes; Strings loadedFlakes;
std::function<AnnotatedValues()> getValues; std::function<AnnotatedValues()> getValues;
@ -73,7 +72,7 @@ struct NixRepl : AbstractNixRepl, detail::ReplCompleterMixin, gc
RunNix * runNixPtr; RunNix * runNixPtr;
void runNix(Path program, const Strings & args, const std::optional<std::string> & input = {}); void runNix(const std::string & program, const Strings & args, const std::optional<std::string> & input = {});
std::unique_ptr<ReplInteracter> interacter; std::unique_ptr<ReplInteracter> interacter;
@ -92,7 +91,7 @@ struct NixRepl : AbstractNixRepl, detail::ReplCompleterMixin, gc
StorePath getDerivationPath(Value & v); StorePath getDerivationPath(Value & v);
ProcessLineResult processLine(std::string line); ProcessLineResult processLine(std::string line);
void loadFile(const Path & path); void loadFile(const std::filesystem::path & path);
void loadFlake(const std::string & flakeRef); void loadFlake(const std::string & flakeRef);
void loadFiles(); void loadFiles();
void loadFlakes(); void loadFlakes();
@ -539,7 +538,9 @@ ProcessLineResult NixRepl::processLine(std::string line)
Value v; Value v;
evalString(arg, v); evalString(arg, v);
StorePath drvPath = getDerivationPath(v); StorePath drvPath = getDerivationPath(v);
Path drvPathRaw = state->store->printStorePath(drvPath); // N.B. This need not be a local / native file path. For
// example, we might be using an SSH store to a different OS.
std::string drvPathRaw = state->store->printStorePath(drvPath);
if (command == ":b" || command == ":bl") { if (command == ":b" || command == ":bl") {
state->store->buildPaths({ state->store->buildPaths({
@ -712,12 +713,12 @@ ProcessLineResult NixRepl::processLine(std::string line)
return ProcessLineResult::PromptAgain; return ProcessLineResult::PromptAgain;
} }
void NixRepl::loadFile(const Path & path) void NixRepl::loadFile(const std::filesystem::path & path)
{ {
loadedFiles.remove(path); loadedFiles.remove(path);
loadedFiles.push_back(path); loadedFiles.push_back(path);
Value v, v2; Value v, v2;
state->evalFile(lookupFileArg(*state, path), v); state->evalFile(lookupFileArg(*state, path.string()), v);
state->autoCallFunction(*autoArgs, v, v2); state->autoCallFunction(*autoArgs, v, v2);
addAttrsToScope(v2); addAttrsToScope(v2);
} }
@ -790,7 +791,7 @@ void NixRepl::reloadFilesAndFlakes()
void NixRepl::loadFiles() void NixRepl::loadFiles()
{ {
Strings old = loadedFiles; decltype(loadedFiles) old = loadedFiles;
loadedFiles.clear(); loadedFiles.clear();
for (auto & i : old) { for (auto & i : old) {
@ -888,7 +889,7 @@ void NixRepl::evalString(std::string s, Value & v)
state->forceValue(v, v.determinePos(noPos)); state->forceValue(v, v.determinePos(noPos));
} }
void NixRepl::runNix(Path program, const Strings & args, const std::optional<std::string> & input) void NixRepl::runNix(const std::string & program, const Strings & args, const std::optional<std::string> & input)
{ {
if (runNixPtr) if (runNixPtr)
(*runNixPtr)(program, args, input); (*runNixPtr)(program, args, input);

View file

@ -371,13 +371,13 @@ void RootArgs::parseCmdline(const Strings & _cmdline, bool allowShebang)
d.completer(*completions, d.n, d.prefix); d.completer(*completions, d.n, d.prefix);
} }
Path Args::getCommandBaseDir() const std::filesystem::path Args::getCommandBaseDir() const
{ {
assert(parent); assert(parent);
return parent->getCommandBaseDir(); return parent->getCommandBaseDir();
} }
Path RootArgs::getCommandBaseDir() const std::filesystem::path RootArgs::getCommandBaseDir() const
{ {
return commandBaseDir; return commandBaseDir;
} }

View file

@ -59,7 +59,7 @@ public:
* *
* This only returns the correct value after parseCmdline() has run. * This only returns the correct value after parseCmdline() has run.
*/ */
virtual Path getCommandBaseDir() const; virtual std::filesystem::path getCommandBaseDir() const;
protected: protected:

View file

@ -38,7 +38,7 @@ protected:
* *
* @see getCommandBaseDir() * @see getCommandBaseDir()
*/ */
Path commandBaseDir = "."; std::filesystem::path commandBaseDir = ".";
public: public:
/** Parse the command line, throwing a UsageError if something goes /** Parse the command line, throwing a UsageError if something goes
@ -48,7 +48,7 @@ public:
std::shared_ptr<Completions> completions; std::shared_ptr<Completions> completions;
Path getCommandBaseDir() const override; std::filesystem::path getCommandBaseDir() const override;
protected: protected:

View file

@ -140,9 +140,9 @@ App UnresolvedApp::resolve(ref<Store> evalStore, ref<Store> store)
auto res = unresolved; auto res = unresolved;
auto builtContext = build(evalStore, store); auto builtContext = build(evalStore, store);
res.program = resolveString(*store, unresolved.program, builtContext); res.program = resolveString(*store, unresolved.program.string(), builtContext);
if (!store->isInStore(res.program)) if (!store->isInStore(res.program.string()))
throw Error("app program '%s' is not in the Nix store", res.program); throw Error("app program '%s' is not in the Nix store", res.program.string());
return res; return res;
} }

View file

@ -84,7 +84,7 @@ struct CmdFormatterRun : MixFormatter, MixJSON
assert(maybeFlakeDir.has_value()); assert(maybeFlakeDir.has_value());
auto flakeDir = maybeFlakeDir.value(); auto flakeDir = maybeFlakeDir.value();
Strings programArgs{app.program}; Strings programArgs{app.program.string()};
// Propagate arguments from the CLI // Propagate arguments from the CLI
for (auto & i : args) { for (auto & i : args) {
@ -103,7 +103,7 @@ struct CmdFormatterRun : MixFormatter, MixJSON
execProgramInStore( execProgramInStore(
store, store,
UseLookupPath::DontUse, UseLookupPath::DontUse,
app.program, app.program.string(),
programArgs, programArgs,
std::nullopt, // Use default system std::nullopt, // Use default system
env); env);

View file

@ -11,7 +11,7 @@
namespace nix { namespace nix {
void runNix(Path program, const Strings & args, const std::optional<std::string> & input = {}) void runNix(const std::string & program, const Strings & args, const std::optional<std::string> & input = {})
{ {
auto subprocessEnv = getEnv(); auto subprocessEnv = getEnv();
subprocessEnv["NIX_CONFIG"] = globalConfig.toKeyValue(); subprocessEnv["NIX_CONFIG"] = globalConfig.toKeyValue();

View file

@ -160,7 +160,7 @@ struct CmdRun : InstallableValueCommand, MixEnvironment
lockFlags.applyNixConfig = true; lockFlags.applyNixConfig = true;
auto app = installable->toApp(*state).resolve(getEvalStore(), store); auto app = installable->toApp(*state).resolve(getEvalStore(), store);
Strings allArgs{app.program}; Strings allArgs{app.program.string()};
for (auto & i : args) for (auto & i : args)
allArgs.push_back(i); allArgs.push_back(i);
@ -170,7 +170,7 @@ struct CmdRun : InstallableValueCommand, MixEnvironment
setEnviron(); setEnviron();
execProgramInStore(store, UseLookupPath::DontUse, app.program, allArgs); execProgramInStore(store, UseLookupPath::DontUse, app.program.string(), allArgs);
} }
}; };