Skip to content

Commit d1470f7

Browse files
authored
Merge pull request #14640 from vinayakankugoyal/path
Use std::filesystem::path instead of Path in libcmd
2 parents 88c9c6d + 84079e1 commit d1470f7

File tree

14 files changed

+52
-39
lines changed

14 files changed

+52
-39
lines changed

src/libcmd/common-eval-args.cc

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ Bindings * MixEvalArgs::getAutoArgs(EvalState & state)
165165
state.parseExprFromString(
166166
arg.expr,
167167
compatibilitySettings.nixShellShebangArgumentsRelativeToScript
168-
? state.rootPath(absPath(getCommandBaseDir()))
168+
? state.rootPath(absPath(getCommandBaseDir()).string())
169169
: state.rootPath(".")));
170170
},
171171
[&](const AutoArgString & arg) { v->mkString(arg.s, state.mem); },
@@ -177,7 +177,7 @@ Bindings * MixEvalArgs::getAutoArgs(EvalState & state)
177177
return res.finish();
178178
}
179179

180-
SourcePath lookupFileArg(EvalState & state, std::string_view s, const Path * baseDir)
180+
SourcePath lookupFileArg(EvalState & state, std::string_view s, const std::filesystem::path * baseDir)
181181
{
182182
if (EvalSettings::isPseudoUrl(s)) {
183183
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
197197
}
198198

199199
else if (s.size() > 2 && s.at(0) == '<' && s.at(s.size() - 1) == '>') {
200-
Path p(s.substr(1, s.size() - 2));
200+
// Should perhaps be a `CanonPath`?
201+
std::string p(s.substr(1, s.size() - 2));
201202
return state.findFile(p);
202203
}
203204

204205
else
205-
return state.rootPath(baseDir ? absPath(s, *baseDir) : absPath(s));
206+
return state.rootPath(absPath(std::filesystem::path{s}, baseDir).string());
206207
}
207208

208209
} // namespace nix

src/libcmd/include/nix/cmd/command.hh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ struct MixFlakeOptions : virtual Args, EvalCommand
134134

135135
struct SourceExprCommand : virtual Args, MixFlakeOptions
136136
{
137-
std::optional<Path> file;
137+
std::optional<std::filesystem::path> file;
138138
std::optional<std::string> expr;
139139

140140
SourceExprCommand();
@@ -310,7 +310,7 @@ static RegisterCommand registerCommand2(std::vector<std::string> && name)
310310

311311
struct MixProfile : virtual StoreCommand
312312
{
313-
std::optional<Path> profile;
313+
std::optional<std::filesystem::path> profile;
314314

315315
MixProfile();
316316

src/libcmd/include/nix/cmd/common-eval-args.hh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,6 @@ private:
8484
/**
8585
* @param baseDir Optional [base directory](https://nix.dev/manual/nix/development/glossary#gloss-base-directory)
8686
*/
87-
SourcePath lookupFileArg(EvalState & state, std::string_view s, const Path * baseDir = nullptr);
87+
SourcePath lookupFileArg(EvalState & state, std::string_view s, const std::filesystem::path * baseDir = nullptr);
8888

8989
} // namespace nix

src/libcmd/include/nix/cmd/installable-value.hh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class AttrCursor;
1717
struct App
1818
{
1919
std::vector<DerivedPath> context;
20-
Path program;
20+
std::filesystem::path program;
2121
// FIXME: add args, sandbox settings, metadata, ...
2222
};
2323

src/libcmd/include/nix/cmd/repl.hh

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,16 @@ struct AbstractNixRepl
1919

2020
typedef std::vector<std::pair<Value *, std::string>> AnnotatedValues;
2121

22-
using RunNix = void(Path program, const Strings & args, const std::optional<std::string> & input);
22+
/**
23+
* Run a nix executable
24+
*
25+
* @todo this is a layer violation
26+
*
27+
* @param programName Name of the command, e.g. `nix` or `nix-env`.
28+
* @param args aguments to the command.
29+
*/
30+
using RunNix =
31+
void(const std::string & programName, const Strings & args, const std::optional<std::string> & input);
2332

2433
/**
2534
* @param runNix Function to run the nix CLI to support various

src/libcmd/installables.cc

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ MixFlakeOptions::MixFlakeOptions()
132132
lockFlags.writeLockFile = false;
133133
lockFlags.inputOverrides.insert_or_assign(
134134
flake::parseInputAttrPath(inputAttrPath),
135-
parseFlakeRef(fetchSettings, flakeRef, absPath(getCommandBaseDir()), true));
135+
parseFlakeRef(fetchSettings, flakeRef, absPath(getCommandBaseDir()).string(), true));
136136
}},
137137
.completer = {[&](AddCompletions & completions, size_t n, std::string_view prefix) {
138138
if (n == 0) {
@@ -173,7 +173,7 @@ MixFlakeOptions::MixFlakeOptions()
173173
auto flake = flake::lockFlake(
174174
flakeSettings,
175175
*evalState,
176-
parseFlakeRef(fetchSettings, flakeRef, absPath(getCommandBaseDir())),
176+
parseFlakeRef(fetchSettings, flakeRef, absPath(getCommandBaseDir()).string()),
177177
{.writeLockFile = false});
178178
for (auto & [inputName, input] : flake.lockFile.root->inputs) {
179179
auto input2 = flake.lockFile.findInput({inputName}); // resolve 'follows' nodes
@@ -263,7 +263,7 @@ void SourceExprCommand::completeInstallable(AddCompletions & completions, std::s
263263

264264
evalSettings.pureEval = false;
265265
auto state = getEvalState();
266-
auto e = state->parseExprFromFile(resolveExprPath(lookupFileArg(*state, *file)));
266+
auto e = state->parseExprFromFile(resolveExprPath(lookupFileArg(*state, file->string())));
267267

268268
Value root;
269269
state->eval(e, root);
@@ -465,10 +465,10 @@ Installables SourceExprCommand::parseInstallables(ref<Store> store, std::vector<
465465
state->eval(e, *vFile);
466466
} else if (file) {
467467
auto dir = absPath(getCommandBaseDir());
468-
state->evalFile(lookupFileArg(*state, *file, &dir), *vFile);
468+
state->evalFile(lookupFileArg(*state, file->string(), &dir), *vFile);
469469
} else {
470-
Path dir = absPath(getCommandBaseDir());
471-
auto e = state->parseExprFromString(*expr, state->rootPath(dir));
470+
auto dir = absPath(getCommandBaseDir());
471+
auto e = state->parseExprFromString(*expr, state->rootPath(dir.string()));
472472
state->eval(e, *vFile);
473473
}
474474

@@ -801,7 +801,8 @@ std::vector<FlakeRef> RawInstallablesCommand::getFlakeRefsForCompletion()
801801
std::vector<FlakeRef> res;
802802
res.reserve(rawInstallables.size());
803803
for (const auto & i : rawInstallables)
804-
res.push_back(parseFlakeRefWithFragment(fetchSettings, expandTilde(i), absPath(getCommandBaseDir())).first);
804+
res.push_back(
805+
parseFlakeRefWithFragment(fetchSettings, expandTilde(i), absPath(getCommandBaseDir()).string()).first);
805806
return res;
806807
}
807808

@@ -820,7 +821,8 @@ void RawInstallablesCommand::run(ref<Store> store)
820821

821822
std::vector<FlakeRef> InstallableCommand::getFlakeRefsForCompletion()
822823
{
823-
return {parseFlakeRefWithFragment(fetchSettings, expandTilde(_installable), absPath(getCommandBaseDir())).first};
824+
return {parseFlakeRefWithFragment(fetchSettings, expandTilde(_installable), absPath(getCommandBaseDir()).string())
825+
.first};
824826
}
825827

826828
void InstallablesCommand::run(ref<Store> store, std::vector<std::string> && rawInstallables)

src/libcmd/repl.cc

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,7 @@ struct NixRepl : AbstractNixRepl, detail::ReplCompleterMixin, gc
5858
{
5959
size_t debugTraceIndex;
6060

61-
// Arguments passed to :load, saved so they can be reloaded with :reload
62-
Strings loadedFiles;
61+
std::list<std::filesystem::path> loadedFiles;
6362
// Arguments passed to :load-flake, saved so they can be reloaded with :reload
6463
Strings loadedFlakes;
6564
std::function<AnnotatedValues()> getValues;
@@ -73,7 +72,7 @@ struct NixRepl : AbstractNixRepl, detail::ReplCompleterMixin, gc
7372

7473
RunNix * runNixPtr;
7574

76-
void runNix(Path program, const Strings & args, const std::optional<std::string> & input = {});
75+
void runNix(const std::string & program, const Strings & args, const std::optional<std::string> & input = {});
7776

7877
std::unique_ptr<ReplInteracter> interacter;
7978

@@ -92,7 +91,7 @@ struct NixRepl : AbstractNixRepl, detail::ReplCompleterMixin, gc
9291
StorePath getDerivationPath(Value & v);
9392
ProcessLineResult processLine(std::string line);
9493

95-
void loadFile(const Path & path);
94+
void loadFile(const std::filesystem::path & path);
9695
void loadFlake(const std::string & flakeRef);
9796
void loadFiles();
9897
void loadFlakes();
@@ -539,7 +538,9 @@ ProcessLineResult NixRepl::processLine(std::string line)
539538
Value v;
540539
evalString(arg, v);
541540
StorePath drvPath = getDerivationPath(v);
542-
Path drvPathRaw = state->store->printStorePath(drvPath);
541+
// N.B. This need not be a local / native file path. For
542+
// example, we might be using an SSH store to a different OS.
543+
std::string drvPathRaw = state->store->printStorePath(drvPath);
543544

544545
if (command == ":b" || command == ":bl") {
545546
state->store->buildPaths({
@@ -712,12 +713,12 @@ ProcessLineResult NixRepl::processLine(std::string line)
712713
return ProcessLineResult::PromptAgain;
713714
}
714715

715-
void NixRepl::loadFile(const Path & path)
716+
void NixRepl::loadFile(const std::filesystem::path & path)
716717
{
717718
loadedFiles.remove(path);
718719
loadedFiles.push_back(path);
719720
Value v, v2;
720-
state->evalFile(lookupFileArg(*state, path), v);
721+
state->evalFile(lookupFileArg(*state, path.string()), v);
721722
state->autoCallFunction(*autoArgs, v, v2);
722723
addAttrsToScope(v2);
723724
}
@@ -790,7 +791,7 @@ void NixRepl::reloadFilesAndFlakes()
790791

791792
void NixRepl::loadFiles()
792793
{
793-
Strings old = loadedFiles;
794+
decltype(loadedFiles) old = loadedFiles;
794795
loadedFiles.clear();
795796

796797
for (auto & i : old) {
@@ -888,7 +889,7 @@ void NixRepl::evalString(std::string s, Value & v)
888889
state->forceValue(v, v.determinePos(noPos));
889890
}
890891

891-
void NixRepl::runNix(Path program, const Strings & args, const std::optional<std::string> & input)
892+
void NixRepl::runNix(const std::string & program, const Strings & args, const std::optional<std::string> & input)
892893
{
893894
if (runNixPtr)
894895
(*runNixPtr)(program, args, input);

src/libutil/args.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -371,13 +371,13 @@ void RootArgs::parseCmdline(const Strings & _cmdline, bool allowShebang)
371371
d.completer(*completions, d.n, d.prefix);
372372
}
373373

374-
Path Args::getCommandBaseDir() const
374+
std::filesystem::path Args::getCommandBaseDir() const
375375
{
376376
assert(parent);
377377
return parent->getCommandBaseDir();
378378
}
379379

380-
Path RootArgs::getCommandBaseDir() const
380+
std::filesystem::path RootArgs::getCommandBaseDir() const
381381
{
382382
return commandBaseDir;
383383
}

src/libutil/include/nix/util/args.hh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public:
5959
*
6060
* This only returns the correct value after parseCmdline() has run.
6161
*/
62-
virtual Path getCommandBaseDir() const;
62+
virtual std::filesystem::path getCommandBaseDir() const;
6363

6464
protected:
6565

src/libutil/include/nix/util/args/root.hh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ protected:
3838
*
3939
* @see getCommandBaseDir()
4040
*/
41-
Path commandBaseDir = ".";
41+
std::filesystem::path commandBaseDir = ".";
4242

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

4949
std::shared_ptr<Completions> completions;
5050

51-
Path getCommandBaseDir() const override;
51+
std::filesystem::path getCommandBaseDir() const override;
5252

5353
protected:
5454

0 commit comments

Comments
 (0)