mirror of
https://github.com/privatevoid-net/nix-super.git
synced 2024-11-22 14:06:16 +02:00
Merge pull request #10122 from edolstra/arg-from-file
Add --arg-from-file and --arg-from-stdin
This commit is contained in:
commit
438855952b
4 changed files with 65 additions and 9 deletions
9
doc/manual/rl-next/arg-from-file.md
Normal file
9
doc/manual/rl-next/arg-from-file.md
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
---
|
||||||
|
synopsis: "CLI options `--arg-from-file` and `--arg-from-stdin`"
|
||||||
|
prs: 10122
|
||||||
|
---
|
||||||
|
|
||||||
|
The new CLI option `--arg-from-file` *name* *path* passes the contents
|
||||||
|
of file *path* as a string value via the function argument *name* to a
|
||||||
|
Nix expression. Similarly, the new option `--arg-from-stdin` *name*
|
||||||
|
reads the contents of the string from standard input.
|
|
@ -20,7 +20,7 @@ MixEvalArgs::MixEvalArgs()
|
||||||
.description = "Pass the value *expr* as the argument *name* to Nix functions.",
|
.description = "Pass the value *expr* as the argument *name* to Nix functions.",
|
||||||
.category = category,
|
.category = category,
|
||||||
.labels = {"name", "expr"},
|
.labels = {"name", "expr"},
|
||||||
.handler = {[&](std::string name, std::string expr) { autoArgs[name] = 'E' + expr; }}
|
.handler = {[&](std::string name, std::string expr) { autoArgs.insert_or_assign(name, AutoArg{AutoArgExpr(expr)}); }}
|
||||||
});
|
});
|
||||||
|
|
||||||
addFlag({
|
addFlag({
|
||||||
|
@ -28,7 +28,24 @@ MixEvalArgs::MixEvalArgs()
|
||||||
.description = "Pass the string *string* as the argument *name* to Nix functions.",
|
.description = "Pass the string *string* as the argument *name* to Nix functions.",
|
||||||
.category = category,
|
.category = category,
|
||||||
.labels = {"name", "string"},
|
.labels = {"name", "string"},
|
||||||
.handler = {[&](std::string name, std::string s) { autoArgs[name] = 'S' + s; }},
|
.handler = {[&](std::string name, std::string s) { autoArgs.insert_or_assign(name, AutoArg{AutoArgString(s)}); }},
|
||||||
|
});
|
||||||
|
|
||||||
|
addFlag({
|
||||||
|
.longName = "arg-from-file",
|
||||||
|
.description = "Pass the contents of file *path* as the argument *name* to Nix functions.",
|
||||||
|
.category = category,
|
||||||
|
.labels = {"name", "path"},
|
||||||
|
.handler = {[&](std::string name, std::string path) { autoArgs.insert_or_assign(name, AutoArg{AutoArgFile(path)}); }},
|
||||||
|
.completer = completePath
|
||||||
|
});
|
||||||
|
|
||||||
|
addFlag({
|
||||||
|
.longName = "arg-from-stdin",
|
||||||
|
.description = "Pass the contents of stdin as the argument *name* to Nix functions.",
|
||||||
|
.category = category,
|
||||||
|
.labels = {"name"},
|
||||||
|
.handler = {[&](std::string name) { autoArgs.insert_or_assign(name, AutoArg{AutoArgStdin{}}); }},
|
||||||
});
|
});
|
||||||
|
|
||||||
addFlag({
|
addFlag({
|
||||||
|
@ -154,13 +171,23 @@ MixEvalArgs::MixEvalArgs()
|
||||||
Bindings * MixEvalArgs::getAutoArgs(EvalState & state)
|
Bindings * MixEvalArgs::getAutoArgs(EvalState & state)
|
||||||
{
|
{
|
||||||
auto res = state.buildBindings(autoArgs.size());
|
auto res = state.buildBindings(autoArgs.size());
|
||||||
for (auto & i : autoArgs) {
|
for (auto & [name, arg] : autoArgs) {
|
||||||
auto v = state.allocValue();
|
auto v = state.allocValue();
|
||||||
if (i.second[0] == 'E')
|
std::visit(overloaded {
|
||||||
state.mkThunk_(*v, state.parseExprFromString(i.second.substr(1), state.rootPath(".")));
|
[&](const AutoArgExpr & arg) {
|
||||||
else
|
state.mkThunk_(*v, state.parseExprFromString(arg.expr, state.rootPath(".")));
|
||||||
v->mkString(((std::string_view) i.second).substr(1));
|
},
|
||||||
res.insert(state.symbols.create(i.first), v);
|
[&](const AutoArgString & arg) {
|
||||||
|
v->mkString(arg.s);
|
||||||
|
},
|
||||||
|
[&](const AutoArgFile & arg) {
|
||||||
|
v->mkString(readFile(arg.path));
|
||||||
|
},
|
||||||
|
[&](const AutoArgStdin & arg) {
|
||||||
|
v->mkString(readFile(STDIN_FILENO));
|
||||||
|
}
|
||||||
|
}, arg);
|
||||||
|
res.insert(state.symbols.create(name), v);
|
||||||
}
|
}
|
||||||
return res.finish();
|
return res.finish();
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,6 +6,8 @@
|
||||||
#include "common-args.hh"
|
#include "common-args.hh"
|
||||||
#include "search-path.hh"
|
#include "search-path.hh"
|
||||||
|
|
||||||
|
#include <filesystem>
|
||||||
|
|
||||||
namespace nix {
|
namespace nix {
|
||||||
|
|
||||||
class Store;
|
class Store;
|
||||||
|
@ -26,7 +28,14 @@ struct MixEvalArgs : virtual Args, virtual MixRepair
|
||||||
std::optional<std::string> evalStoreUrl;
|
std::optional<std::string> evalStoreUrl;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::map<std::string, std::string> autoArgs;
|
struct AutoArgExpr { std::string expr; };
|
||||||
|
struct AutoArgString { std::string s; };
|
||||||
|
struct AutoArgFile { std::filesystem::path path; };
|
||||||
|
struct AutoArgStdin { };
|
||||||
|
|
||||||
|
using AutoArg = std::variant<AutoArgExpr, AutoArgString, AutoArgFile, AutoArgStdin>;
|
||||||
|
|
||||||
|
std::map<std::string, AutoArg> autoArgs;
|
||||||
};
|
};
|
||||||
|
|
||||||
SourcePath lookupFileArg(EvalState & state, std::string_view s, const Path * baseDir = nullptr);
|
SourcePath lookupFileArg(EvalState & state, std::string_view s, const Path * baseDir = nullptr);
|
||||||
|
|
|
@ -41,3 +41,14 @@ mkdir -p $TEST_ROOT/xyzzy $TEST_ROOT/foo
|
||||||
ln -sfn ../xyzzy $TEST_ROOT/foo/bar
|
ln -sfn ../xyzzy $TEST_ROOT/foo/bar
|
||||||
printf 123 > $TEST_ROOT/xyzzy/default.nix
|
printf 123 > $TEST_ROOT/xyzzy/default.nix
|
||||||
[[ $(nix eval --impure --expr "import $TEST_ROOT/foo/bar") = 123 ]]
|
[[ $(nix eval --impure --expr "import $TEST_ROOT/foo/bar") = 123 ]]
|
||||||
|
|
||||||
|
# Test --arg-from-file.
|
||||||
|
[[ "$(nix eval --raw --arg-from-file foo config.nix --expr '{ foo }: { inherit foo; }' foo)" = "$(cat config.nix)" ]]
|
||||||
|
|
||||||
|
# Check that special(-ish) files are drained.
|
||||||
|
if [[ -e /proc/version ]]; then
|
||||||
|
[[ "$(nix eval --raw --arg-from-file foo /proc/version --expr '{ foo }: { inherit foo; }' foo)" = "$(cat /proc/version)" ]]
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Test --arg-from-stdin.
|
||||||
|
[[ "$(echo bla | nix eval --raw --arg-from-stdin foo --expr '{ foo }: { inherit foo; }' foo)" = bla ]]
|
||||||
|
|
Loading…
Reference in a new issue