Skip to content

Commit 3e9a6fd

Browse files
committed
No more Path in libnixcmd
1 parent 8ce71eb commit 3e9a6fd

File tree

14 files changed

+62
-41
lines changed

14 files changed

+62
-41
lines changed

src/libcmd/common-eval-args.cc

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,13 @@ Bindings * MixEvalArgs::getAutoArgs(EvalState & state)
158158
auto v = state.allocValue();
159159
std::visit(overloaded {
160160
[&](const AutoArgExpr & arg) {
161-
state.mkThunk_(*v, state.parseExprFromString(arg.expr, compatibilitySettings.nixShellShebangArgumentsRelativeToScript ? state.rootPath(absPath(getCommandBaseDir())) : state.rootPath(".")));
161+
state.mkThunk_(
162+
*v,
163+
state.parseExprFromString(
164+
arg.expr,
165+
compatibilitySettings.nixShellShebangArgumentsRelativeToScript
166+
? state.rootPath(std::filesystem::weakly_canonical(getCommandBaseDir()).string())
167+
: state.rootPath(".")));
162168
},
163169
[&](const AutoArgString & arg) {
164170
v->mkString(arg.s);
@@ -175,7 +181,7 @@ Bindings * MixEvalArgs::getAutoArgs(EvalState & state)
175181
return res.finish();
176182
}
177183

178-
SourcePath lookupFileArg(EvalState & state, std::string_view s, const Path * baseDir)
184+
SourcePath lookupFileArg(EvalState & state, std::string_view s, const std::filesystem::path * baseDir)
179185
{
180186
if (EvalSettings::isPseudoUrl(s)) {
181187
auto accessor = fetchers::downloadTarball(
@@ -205,12 +211,13 @@ SourcePath lookupFileArg(EvalState & state, std::string_view s, const Path * bas
205211
}
206212

207213
else if (s.size() > 2 && s.at(0) == '<' && s.at(s.size() - 1) == '>') {
208-
Path p(s.substr(1, s.size() - 2));
214+
// Should perhaps be a `CanonPath`?
215+
std::string p(s.substr(1, s.size() - 2));
209216
return state.findFile(p);
210217
}
211218

212219
else
213-
return state.rootPath(baseDir ? absPath(s, *baseDir) : absPath(s));
220+
return state.rootPath(std::filesystem::weakly_canonical(baseDir ? *baseDir / s : s).string());
214221
}
215222

216223
}

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();
@@ -314,7 +314,7 @@ static RegisterCommand registerCommand2(std::vector<std::string> && name)
314314

315315
struct MixProfile : virtual StoreCommand
316316
{
317-
std::optional<Path> profile;
317+
std::optional<std::filesystem::path> profile;
318318

319319
MixProfile();
320320

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,6 @@ private:
6868
/**
6969
* @param baseDir Optional [base directory](https://nixos.org/manual/nix/unstable/glossary#gloss-base-directory)
7070
*/
71-
SourcePath lookupFileArg(EvalState & state, std::string_view s, const Path * baseDir = nullptr);
71+
SourcePath lookupFileArg(EvalState & state, std::string_view s, const std::filesystem::path * baseDir = nullptr);
7272

7373
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ namespace eval_cache { class EvalCache; class AttrCursor; }
1414
struct App
1515
{
1616
std::vector<DerivedPath> context;
17-
Path program;
17+
std::filesystem::path program;
1818
// FIXME: add args, sandbox settings, metadata, ...
1919
};
2020

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,15 @@ 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 = void(const std::string & programName, const Strings & args, const std::optional<std::string> & input);
2331

2432
/**
2533
* @param runNix Function to run the nix CLI to support various

src/libcmd/installables.cc

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,10 @@ MixFlakeOptions::MixFlakeOptions()
131131
lockFlags.writeLockFile = false;
132132
lockFlags.inputOverrides.insert_or_assign(
133133
flake::parseInputAttrPath(inputAttrPath),
134-
parseFlakeRef(fetchSettings, flakeRef, absPath(getCommandBaseDir()), true));
134+
parseFlakeRef(
135+
fetchSettings,
136+
flakeRef,
137+
std::filesystem::weakly_canonical(getCommandBaseDir()).string()));
135138
}},
136139
.completer = {[&](AddCompletions & completions, size_t n, std::string_view prefix) {
137140
if (n == 0) {
@@ -174,7 +177,7 @@ MixFlakeOptions::MixFlakeOptions()
174177
auto flake = flake::lockFlake(
175178
flakeSettings,
176179
*evalState,
177-
parseFlakeRef(fetchSettings, flakeRef, absPath(getCommandBaseDir())),
180+
parseFlakeRef(fetchSettings, flakeRef, std::filesystem::weakly_canonical(getCommandBaseDir()).string()),
178181
{ .writeLockFile = false });
179182
for (auto & [inputName, input] : flake.lockFile.root->inputs) {
180183
auto input2 = flake.lockFile.findInput({inputName}); // resolve 'follows' nodes
@@ -266,7 +269,7 @@ void SourceExprCommand::completeInstallable(AddCompletions & completions, std::s
266269
auto e =
267270
state->parseExprFromFile(
268271
resolveExprPath(
269-
lookupFileArg(*state, *file)));
272+
lookupFileArg(*state, file->string())));
270273

271274
Value root;
272275
state->eval(e, root);
@@ -503,12 +506,12 @@ Installables SourceExprCommand::parseInstallables(
503506
state->eval(e, *vFile);
504507
}
505508
else if (file) {
506-
auto dir = absPath(getCommandBaseDir());
507-
state->evalFile(lookupFileArg(*state, *file, &dir), *vFile);
509+
auto dir = std::filesystem::weakly_canonical(getCommandBaseDir());
510+
state->evalFile(lookupFileArg(*state, file->string(), &dir), *vFile);
508511
}
509512
else {
510-
Path dir = absPath(getCommandBaseDir());
511-
auto e = state->parseExprFromString(*expr, state->rootPath(dir));
513+
auto dir = std::filesystem::weakly_canonical(getCommandBaseDir());
514+
auto e = state->parseExprFromString(*expr, state->rootPath(dir.string()));
512515
state->eval(e, *vFile);
513516
}
514517

@@ -544,7 +547,9 @@ Installables SourceExprCommand::parseInstallables(
544547

545548
try {
546549
auto [flakeRef, fragment] = parseFlakeRefWithFragment(
547-
fetchSettings, std::string { prefix }, absPath(getCommandBaseDir()));
550+
fetchSettings,
551+
std::string { prefix },
552+
std::filesystem::weakly_canonical(getCommandBaseDir()).string());
548553
result.push_back(make_ref<InstallableFlake>(
549554
this,
550555
getEvalState(),
@@ -864,7 +869,7 @@ std::vector<FlakeRef> RawInstallablesCommand::getFlakeRefsForCompletion()
864869
res.push_back(parseFlakeRefWithFragment(
865870
fetchSettings,
866871
expandTilde(i),
867-
absPath(getCommandBaseDir())).first);
872+
std::filesystem::weakly_canonical(getCommandBaseDir()).string()).first);
868873
return res;
869874
}
870875

@@ -887,7 +892,7 @@ std::vector<FlakeRef> InstallableCommand::getFlakeRefsForCompletion()
887892
parseFlakeRefWithFragment(
888893
fetchSettings,
889894
expandTilde(_installable),
890-
absPath(getCommandBaseDir())).first
895+
std::filesystem::weakly_canonical(getCommandBaseDir()).string()).first
891896
};
892897
}
893898

src/libcmd/repl.cc

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,7 @@ struct NixRepl
6161
{
6262
size_t debugTraceIndex;
6363

64-
// Arguments passed to :load, saved so they can be reloaded with :reload
65-
Strings loadedFiles;
64+
std::list<std::filesystem::path> loadedFiles;
6665
// Arguments passed to :load-flake, saved so they can be reloaded with :reload
6766
Strings loadedFlakes;
6867
std::function<AnnotatedValues()> getValues;
@@ -75,7 +74,7 @@ struct NixRepl
7574

7675
RunNix * runNixPtr;
7776

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

8079
std::unique_ptr<ReplInteracter> interacter;
8180

@@ -90,7 +89,7 @@ struct NixRepl
9089
StorePath getDerivationPath(Value & v);
9190
ProcessLineResult processLine(std::string line);
9291

93-
void loadFile(const Path & path);
92+
void loadFile(const std::filesystem::path & path);
9493
void loadFlake(const std::string & flakeRef);
9594
void loadFiles();
9695
void loadFlakes();
@@ -528,7 +527,9 @@ ProcessLineResult NixRepl::processLine(std::string line)
528527
Value v;
529528
evalString(arg, v);
530529
StorePath drvPath = getDerivationPath(v);
531-
Path drvPathRaw = state->store->printStorePath(drvPath);
530+
// N.B. This need not be a local / native file path. For
531+
// example, we might be using an SSH store to a different OS.
532+
std::string drvPathRaw = state->store->printStorePath(drvPath);
532533

533534
if (command == ":b" || command == ":bl") {
534535
state->store->buildPaths({
@@ -705,12 +706,12 @@ ProcessLineResult NixRepl::processLine(std::string line)
705706
return ProcessLineResult::PromptAgain;
706707
}
707708

708-
void NixRepl::loadFile(const Path & path)
709+
void NixRepl::loadFile(const std::filesystem::path & path)
709710
{
710711
loadedFiles.remove(path);
711712
loadedFiles.push_back(path);
712713
Value v, v2;
713-
state->evalFile(lookupFileArg(*state, path), v);
714+
state->evalFile(lookupFileArg(*state, path.string()), v);
714715
state->autoCallFunction(*autoArgs, v, v2);
715716
addAttrsToScope(v2);
716717
}
@@ -772,7 +773,7 @@ void NixRepl::reloadFilesAndFlakes()
772773

773774
void NixRepl::loadFiles()
774775
{
775-
Strings old = loadedFiles;
776+
decltype(loadedFiles) old = loadedFiles;
776777
loadedFiles.clear();
777778

778779
for (auto & i : old) {
@@ -843,7 +844,7 @@ void NixRepl::evalString(std::string s, Value & v)
843844
}
844845

845846

846-
void NixRepl::runNix(Path program, const Strings & args, const std::optional<std::string> & input)
847+
void NixRepl::runNix(const std::string & program, const Strings & args, const std::optional<std::string> & input)
847848
{
848849
if (runNixPtr)
849850
(*runNixPtr)(program, args, input);

src/libutil/args.cc

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

355-
Path Args::getCommandBaseDir() const
355+
std::filesystem::path Args::getCommandBaseDir() const
356356
{
357357
assert(parent);
358358
return parent->getCommandBaseDir();
359359
}
360360

361-
Path RootArgs::getCommandBaseDir() const
361+
std::filesystem::path RootArgs::getCommandBaseDir() const
362362
{
363363
return commandBaseDir;
364364
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public:
4949
*
5050
* This only returns the correct value after parseCmdline() has run.
5151
*/
52-
virtual Path getCommandBaseDir() const;
52+
virtual std::filesystem::path getCommandBaseDir() const;
5353

5454
protected:
5555

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)