Skip to content

Commit 708df97

Browse files
committed
No more Path in libnixcmd
1 parent 13de496 commit 708df97

File tree

14 files changed

+66
-40
lines changed

14 files changed

+66
-40
lines changed

src/libcmd/common-eval-args.cc

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,13 @@ Bindings * MixEvalArgs::getAutoArgs(EvalState & state)
154154
auto v = state.allocValue();
155155
std::visit(overloaded {
156156
[&](const AutoArgExpr & arg) {
157-
state.mkThunk_(*v, state.parseExprFromString(arg.expr, compatibilitySettings.nixShellShebangArgumentsRelativeToScript ? state.rootPath(absPath(getCommandBaseDir())) : state.rootPath(".")));
157+
state.mkThunk_(
158+
*v,
159+
state.parseExprFromString(
160+
arg.expr,
161+
compatibilitySettings.nixShellShebangArgumentsRelativeToScript
162+
? state.rootPath(fs::weakly_canonical(getCommandBaseDir()).string())
163+
: state.rootPath(".")));
158164
},
159165
[&](const AutoArgString & arg) {
160166
v->mkString(arg.s);
@@ -171,7 +177,7 @@ Bindings * MixEvalArgs::getAutoArgs(EvalState & state)
171177
return res.finish();
172178
}
173179

174-
SourcePath lookupFileArg(EvalState & state, std::string_view s, const Path * baseDir)
180+
SourcePath lookupFileArg(EvalState & state, std::string_view s, const fs::path * baseDir)
175181
{
176182
if (EvalSettings::isPseudoUrl(s)) {
177183
auto accessor = fetchers::downloadTarball(
@@ -192,12 +198,13 @@ SourcePath lookupFileArg(EvalState & state, std::string_view s, const Path * bas
192198
}
193199

194200
else if (s.size() > 2 && s.at(0) == '<' && s.at(s.size() - 1) == '>') {
195-
Path p(s.substr(1, s.size() - 2));
201+
// Should perhaps be a `CanonPath`?
202+
std::string p(s.substr(1, s.size() - 2));
196203
return state.findFile(p);
197204
}
198205

199206
else
200-
return state.rootPath(baseDir ? absPath(s, *baseDir) : absPath(s));
207+
return state.rootPath(fs::weakly_canonical(baseDir ? *baseDir / s : s).string());
201208
}
202209

203210
}

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

312312
struct MixProfile : virtual StoreCommand
313313
{
314-
std::optional<Path> profile;
314+
std::optional<std::filesystem::path> profile;
315315

316316
MixProfile();
317317

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
@@ -133,7 +133,10 @@ MixFlakeOptions::MixFlakeOptions()
133133
lockFlags.writeLockFile = false;
134134
lockFlags.inputOverrides.insert_or_assign(
135135
flake::parseInputAttrPath(inputAttrPath),
136-
parseFlakeRef(fetchSettings, flakeRef, absPath(getCommandBaseDir()), true));
136+
parseFlakeRef(
137+
fetchSettings,
138+
flakeRef,
139+
fs::weakly_canonical(getCommandBaseDir()).string()));
137140
}},
138141
.completer = {[&](AddCompletions & completions, size_t n, std::string_view prefix) {
139142
if (n == 0) {
@@ -176,7 +179,7 @@ MixFlakeOptions::MixFlakeOptions()
176179
auto flake = flake::lockFlake(
177180
flakeSettings,
178181
*evalState,
179-
parseFlakeRef(fetchSettings, flakeRef, absPath(getCommandBaseDir())),
182+
parseFlakeRef(fetchSettings, flakeRef, fs::weakly_canonical(getCommandBaseDir()).string()),
180183
{ .writeLockFile = false });
181184
for (auto & [inputName, input] : flake.lockFile.root->inputs) {
182185
auto input2 = flake.lockFile.findInput({inputName}); // resolve 'follows' nodes
@@ -268,7 +271,7 @@ void SourceExprCommand::completeInstallable(AddCompletions & completions, std::s
268271
auto e =
269272
state->parseExprFromFile(
270273
resolveExprPath(
271-
lookupFileArg(*state, *file)));
274+
lookupFileArg(*state, file->string())));
272275

273276
Value root;
274277
state->eval(e, root);
@@ -501,12 +504,12 @@ Installables SourceExprCommand::parseInstallables(
501504
state->eval(e, *vFile);
502505
}
503506
else if (file) {
504-
auto dir = absPath(getCommandBaseDir());
505-
state->evalFile(lookupFileArg(*state, *file, &dir), *vFile);
507+
auto dir = fs::weakly_canonical(getCommandBaseDir());
508+
state->evalFile(lookupFileArg(*state, file->string(), &dir), *vFile);
506509
}
507510
else {
508-
Path dir = absPath(getCommandBaseDir());
509-
auto e = state->parseExprFromString(*expr, state->rootPath(dir));
511+
auto dir = fs::weakly_canonical(getCommandBaseDir());
512+
auto e = state->parseExprFromString(*expr, state->rootPath(dir.string()));
510513
state->eval(e, *vFile);
511514
}
512515

@@ -542,7 +545,9 @@ Installables SourceExprCommand::parseInstallables(
542545

543546
try {
544547
auto [flakeRef, fragment] = parseFlakeRefWithFragment(
545-
fetchSettings, std::string { prefix }, absPath(getCommandBaseDir()));
548+
fetchSettings,
549+
std::string { prefix },
550+
fs::weakly_canonical(getCommandBaseDir()).string());
546551
result.push_back(make_ref<InstallableFlake>(
547552
this,
548553
getEvalState(),
@@ -862,7 +867,7 @@ std::vector<FlakeRef> RawInstallablesCommand::getFlakeRefsForCompletion()
862867
res.push_back(parseFlakeRefWithFragment(
863868
fetchSettings,
864869
expandTilde(i),
865-
absPath(getCommandBaseDir())).first);
870+
fs::weakly_canonical(getCommandBaseDir()).string()).first);
866871
return res;
867872
}
868873

@@ -885,7 +890,7 @@ std::vector<FlakeRef> InstallableCommand::getFlakeRefsForCompletion()
885890
parseFlakeRefWithFragment(
886891
fetchSettings,
887892
expandTilde(_installable),
888-
absPath(getCommandBaseDir())).first
893+
fs::weakly_canonical(getCommandBaseDir()).string()).first
889894
};
890895
}
891896

src/libcmd/repl.cc

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333

3434
namespace nix {
3535

36+
namespace fs { using namespace std::filesystem; }
37+
3638
/**
3739
* Returned by `NixRepl::processLine`.
3840
*/
@@ -61,7 +63,7 @@ struct NixRepl
6163
{
6264
size_t debugTraceIndex;
6365

64-
Strings loadedFiles;
66+
std::list<fs::path> loadedFiles;
6567
std::function<AnnotatedValues()> getValues;
6668

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

7375
RunNix * runNixPtr;
7476

75-
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 = {});
7678

7779
std::unique_ptr<ReplInteracter> interacter;
7880

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

90-
void loadFile(const Path & path);
92+
void loadFile(const fs::path & path);
9193
void loadFlake(const std::string & flakeRef);
9294
void loadFiles();
9395
void reloadFiles();
@@ -525,7 +527,9 @@ ProcessLineResult NixRepl::processLine(std::string line)
525527
Value v;
526528
evalString(arg, v);
527529
StorePath drvPath = getDerivationPath(v);
528-
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);
529533

530534
if (command == ":b" || command == ":bl") {
531535
state->store->buildPaths({
@@ -702,12 +706,12 @@ ProcessLineResult NixRepl::processLine(std::string line)
702706
return ProcessLineResult::PromptAgain;
703707
}
704708

705-
void NixRepl::loadFile(const Path & path)
709+
void NixRepl::loadFile(const fs::path & path)
706710
{
707711
loadedFiles.remove(path);
708712
loadedFiles.push_back(path);
709713
Value v, v2;
710-
state->evalFile(lookupFileArg(*state, path), v);
714+
state->evalFile(lookupFileArg(*state, path.string()), v);
711715
state->autoCallFunction(*autoArgs, v, v2);
712716
addAttrsToScope(v2);
713717
}
@@ -765,7 +769,7 @@ void NixRepl::reloadFiles()
765769

766770
void NixRepl::loadFiles()
767771
{
768-
Strings old = loadedFiles;
772+
decltype(loadedFiles) old = loadedFiles;
769773
loadedFiles.clear();
770774

771775
for (auto & i : old) {
@@ -824,7 +828,7 @@ void NixRepl::evalString(std::string s, Value & v)
824828
}
825829

826830

827-
void NixRepl::runNix(Path program, const Strings & args, const std::optional<std::string> & input)
831+
void NixRepl::runNix(const std::string & program, const Strings & args, const std::optional<std::string> & input)
828832
{
829833
if (runNixPtr)
830834
(*runNixPtr)(program, args, input);

src/libutil/args.cc

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515

1616
namespace nix {
1717

18+
namespace fs { using namespace std::filesystem; }
19+
1820
void Args::addFlag(Flag && flag_)
1921
{
2022
auto flag = std::make_shared<Flag>(std::move(flag_));
@@ -352,13 +354,13 @@ void RootArgs::parseCmdline(const Strings & _cmdline, bool allowShebang)
352354
d.completer(*completions, d.n, d.prefix);
353355
}
354356

355-
Path Args::getCommandBaseDir() const
357+
fs::path Args::getCommandBaseDir() const
356358
{
357359
assert(parent);
358360
return parent->getCommandBaseDir();
359361
}
360362

361-
Path RootArgs::getCommandBaseDir() const
363+
fs::path RootArgs::getCommandBaseDir() const
362364
{
363365
return commandBaseDir;
364366
}

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)