Skip to content

Commit

Permalink
No more Path in libnixcmd
Browse files Browse the repository at this point in the history
  • Loading branch information
Ericson2314 committed Jan 20, 2025
1 parent 7bf4e0e commit 5888eab
Show file tree
Hide file tree
Showing 14 changed files with 66 additions and 40 deletions.
4 changes: 2 additions & 2 deletions src/libcmd/command.hh
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ struct MixFlakeOptions : virtual Args, EvalCommand

struct SourceExprCommand : virtual Args, MixFlakeOptions
{
std::optional<Path> file;
std::optional<std::filesystem::path> file;
std::optional<std::string> expr;

SourceExprCommand();
Expand Down Expand Up @@ -311,7 +311,7 @@ static RegisterCommand registerCommand2(std::vector<std::string> && name)

struct MixProfile : virtual StoreCommand
{
std::optional<Path> profile;
std::optional<std::filesystem::path> profile;

MixProfile();

Expand Down
15 changes: 11 additions & 4 deletions src/libcmd/common-eval-args.cc
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,13 @@ Bindings * MixEvalArgs::getAutoArgs(EvalState & state)
auto v = state.allocValue();
std::visit(overloaded {
[&](const AutoArgExpr & arg) {
state.mkThunk_(*v, state.parseExprFromString(arg.expr, compatibilitySettings.nixShellShebangArgumentsRelativeToScript ? state.rootPath(absPath(getCommandBaseDir())) : state.rootPath(".")));
state.mkThunk_(
*v,
state.parseExprFromString(
arg.expr,
compatibilitySettings.nixShellShebangArgumentsRelativeToScript
? state.rootPath(fs::weakly_canonical(getCommandBaseDir()).string())
: state.rootPath(".")));
},
[&](const AutoArgString & arg) {
v->mkString(arg.s);
Expand All @@ -169,7 +175,7 @@ Bindings * MixEvalArgs::getAutoArgs(EvalState & state)
return res.finish();
}

SourcePath lookupFileArg(EvalState & state, std::string_view s, const Path * baseDir)
SourcePath lookupFileArg(EvalState & state, std::string_view s, const fs::path * baseDir)
{
if (EvalSettings::isPseudoUrl(s)) {
auto accessor = fetchers::downloadTarball(
Expand All @@ -188,12 +194,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) == '>') {
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);
}

else
return state.rootPath(baseDir ? absPath(s, *baseDir) : absPath(s));
return state.rootPath(fs::weakly_canonical(baseDir ? *baseDir / s : s).string());
}

}
2 changes: 1 addition & 1 deletion src/libcmd/common-eval-args.hh
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,6 @@ private:
/**
* @param baseDir Optional [base directory](https://nixos.org/manual/nix/unstable/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);

}
2 changes: 1 addition & 1 deletion src/libcmd/installable-value.hh
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace eval_cache { class EvalCache; class AttrCursor; }
struct App
{
std::vector<DerivedPath> context;
Path program;
std::filesystem::path program;
// FIXME: add args, sandbox settings, metadata, ...
};

Expand Down
25 changes: 15 additions & 10 deletions src/libcmd/installables.cc
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,10 @@ MixFlakeOptions::MixFlakeOptions()
lockFlags.writeLockFile = false;
lockFlags.inputOverrides.insert_or_assign(
flake::parseInputPath(inputPath),
parseFlakeRef(fetchSettings, flakeRef, absPath(getCommandBaseDir()), true));
parseFlakeRef(
fetchSettings,
flakeRef,
fs::weakly_canonical(getCommandBaseDir()).string()));
}},
.completer = {[&](AddCompletions & completions, size_t n, std::string_view prefix) {
if (n == 0) {
Expand Down Expand Up @@ -176,7 +179,7 @@ MixFlakeOptions::MixFlakeOptions()
auto flake = flake::lockFlake(
flakeSettings,
*evalState,
parseFlakeRef(fetchSettings, flakeRef, absPath(getCommandBaseDir())),
parseFlakeRef(fetchSettings, flakeRef, fs::weakly_canonical(getCommandBaseDir()).string()),
{ .writeLockFile = false });
for (auto & [inputName, input] : flake.lockFile.root->inputs) {
auto input2 = flake.lockFile.findInput({inputName}); // resolve 'follows' nodes
Expand Down Expand Up @@ -268,7 +271,7 @@ void SourceExprCommand::completeInstallable(AddCompletions & completions, std::s
auto e =
state->parseExprFromFile(
resolveExprPath(
lookupFileArg(*state, *file)));
lookupFileArg(*state, file->string())));

Value root;
state->eval(e, root);
Expand Down Expand Up @@ -501,12 +504,12 @@ Installables SourceExprCommand::parseInstallables(
state->eval(e, *vFile);
}
else if (file) {
auto dir = absPath(getCommandBaseDir());
state->evalFile(lookupFileArg(*state, *file, &dir), *vFile);
auto dir = fs::weakly_canonical(getCommandBaseDir());
state->evalFile(lookupFileArg(*state, file->string(), &dir), *vFile);
}
else {
Path dir = absPath(getCommandBaseDir());
auto e = state->parseExprFromString(*expr, state->rootPath(dir));
auto dir = fs::weakly_canonical(getCommandBaseDir());
auto e = state->parseExprFromString(*expr, state->rootPath(dir.string()));
state->eval(e, *vFile);
}

Expand Down Expand Up @@ -542,7 +545,9 @@ Installables SourceExprCommand::parseInstallables(

try {
auto [flakeRef, fragment] = parseFlakeRefWithFragment(
fetchSettings, std::string { prefix }, absPath(getCommandBaseDir()));
fetchSettings,
std::string { prefix },
fs::weakly_canonical(getCommandBaseDir()).string());
result.push_back(make_ref<InstallableFlake>(
this,
getEvalState(),
Expand Down Expand Up @@ -862,7 +867,7 @@ std::vector<FlakeRef> RawInstallablesCommand::getFlakeRefsForCompletion()
res.push_back(parseFlakeRefWithFragment(
fetchSettings,
expandTilde(i),
absPath(getCommandBaseDir())).first);
fs::weakly_canonical(getCommandBaseDir()).string()).first);
return res;
}

Expand All @@ -885,7 +890,7 @@ std::vector<FlakeRef> InstallableCommand::getFlakeRefsForCompletion()
parseFlakeRefWithFragment(
fetchSettings,
expandTilde(_installable),
absPath(getCommandBaseDir())).first
fs::weakly_canonical(getCommandBaseDir()).string()).first
};
}

Expand Down
20 changes: 12 additions & 8 deletions src/libcmd/repl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@

namespace nix {

namespace fs { using namespace std::filesystem; }

/**
* Returned by `NixRepl::processLine`.
*/
Expand Down Expand Up @@ -61,7 +63,7 @@ struct NixRepl
{
size_t debugTraceIndex;

Strings loadedFiles;
std::list<fs::path> loadedFiles;
std::function<AnnotatedValues()> getValues;

const static int envSize = 32768;
Expand All @@ -72,7 +74,7 @@ struct NixRepl

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;

Expand All @@ -87,7 +89,7 @@ struct NixRepl
StorePath getDerivationPath(Value & v);
ProcessLineResult processLine(std::string line);

void loadFile(const Path & path);
void loadFile(const fs::path & path);
void loadFlake(const std::string & flakeRef);
void loadFiles();
void reloadFiles();
Expand Down Expand Up @@ -524,7 +526,9 @@ ProcessLineResult NixRepl::processLine(std::string line)
Value v;
evalString(arg, 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") {
state->store->buildPaths({
Expand Down Expand Up @@ -699,12 +703,12 @@ ProcessLineResult NixRepl::processLine(std::string line)
return ProcessLineResult::PromptAgain;
}

void NixRepl::loadFile(const Path & path)
void NixRepl::loadFile(const fs::path & path)
{
loadedFiles.remove(path);
loadedFiles.push_back(path);
Value v, v2;
state->evalFile(lookupFileArg(*state, path), v);
state->evalFile(lookupFileArg(*state, path.string()), v);
state->autoCallFunction(*autoArgs, v, v2);
addAttrsToScope(v2);
}
Expand Down Expand Up @@ -762,7 +766,7 @@ void NixRepl::reloadFiles()

void NixRepl::loadFiles()
{
Strings old = loadedFiles;
decltype(loadedFiles) old = loadedFiles;
loadedFiles.clear();

for (auto & i : old) {
Expand Down Expand Up @@ -821,7 +825,7 @@ void NixRepl::evalString(std::string s, Value & v)
}


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)
(*runNixPtr)(program, args, input);
Expand Down
10 changes: 9 additions & 1 deletion src/libcmd/repl.hh
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,15 @@ struct AbstractNixRepl

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
Expand Down
6 changes: 4 additions & 2 deletions src/libutil/args.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

namespace nix {

namespace fs { using namespace std::filesystem; }

void Args::addFlag(Flag && flag_)
{
auto flag = std::make_shared<Flag>(std::move(flag_));
Expand Down Expand Up @@ -352,13 +354,13 @@ void RootArgs::parseCmdline(const Strings & _cmdline, bool allowShebang)
d.completer(*completions, d.n, d.prefix);
}

Path Args::getCommandBaseDir() const
fs::path Args::getCommandBaseDir() const
{
assert(parent);
return parent->getCommandBaseDir();
}

Path RootArgs::getCommandBaseDir() const
fs::path RootArgs::getCommandBaseDir() const
{
return commandBaseDir;
}
Expand Down
2 changes: 1 addition & 1 deletion src/libutil/args.hh
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public:
*
* This only returns the correct value after parseCmdline() has run.
*/
virtual Path getCommandBaseDir() const;
virtual std::filesystem::path getCommandBaseDir() const;

protected:

Expand Down
4 changes: 2 additions & 2 deletions src/libutil/args/root.hh
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ protected:
*
* @see getCommandBaseDir()
*/
Path commandBaseDir = ".";
std::filesystem::path commandBaseDir = ".";

public:
/** Parse the command line, throwing a UsageError if something goes
Expand All @@ -48,7 +48,7 @@ public:

std::shared_ptr<Completions> completions;

Path getCommandBaseDir() const override;
std::filesystem::path getCommandBaseDir() const override;

protected:

Expand Down
6 changes: 3 additions & 3 deletions src/nix/app.cc
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,9 @@ App UnresolvedApp::resolve(ref<Store> evalStore, ref<Store> store)
make_ref<InstallableDerivedPath>(store, DerivedPath { ctxElt }));

auto builtContext = Installable::build(evalStore, store, Realise::Outputs, installableContext);
res.program = resolveString(*store, unresolved.program, builtContext);
if (!store->isInStore(res.program))
throw Error("app program '%s' is not in the Nix store", res.program);
res.program = resolveString(*store, unresolved.program.string(), builtContext);
if (!store->isInStore(res.program.string()))
throw Error("app program '%s' is not in the Nix store", res.program.string());

return res;
}
Expand Down
4 changes: 2 additions & 2 deletions src/nix/fmt.cc
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ struct CmdFmt : SourceExprCommand {
auto & installable = InstallableValue::require(*installable_);
auto app = installable.toApp(*evalState).resolve(evalStore, store);

Strings programArgs{app.program};
Strings programArgs{app.program.string()};

// Propagate arguments from the CLI
for (auto &i : args) {
Expand All @@ -48,7 +48,7 @@ struct CmdFmt : SourceExprCommand {
// we are about to exec out of this process without running C++ destructors.
evalState->evalCaches.clear();

execProgramInStore(store, UseLookupPath::DontUse, app.program, programArgs);
execProgramInStore(store, UseLookupPath::DontUse, app.program.string(), programArgs);
};
};

Expand Down
2 changes: 1 addition & 1 deletion src/nix/repl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

namespace nix {

void runNix(std::string program, const Strings & args,
void runNix(const std::string & program, const Strings & args,
const std::optional<std::string> & input = {})
{
auto subprocessEnv = getEnv();
Expand Down
4 changes: 2 additions & 2 deletions src/nix/run.cc
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ struct CmdRun : InstallableValueCommand, MixEnvironment
lockFlags.applyNixConfig = true;
auto app = installable->toApp(*state).resolve(getEvalStore(), store);

Strings allArgs{app.program};
Strings allArgs{app.program.string()};
for (auto & i : args) allArgs.push_back(i);

// Release our references to eval caches to ensure they are persisted to disk, because
Expand All @@ -137,7 +137,7 @@ struct CmdRun : InstallableValueCommand, MixEnvironment

setEnviron();

execProgramInStore(store, UseLookupPath::DontUse, app.program, allArgs);
execProgramInStore(store, UseLookupPath::DontUse, app.program.string(), allArgs);
}
};

Expand Down

0 comments on commit 5888eab

Please sign in to comment.