diff --git a/doc/manual/rl-next/arg-from-file.md b/doc/manual/rl-next/arg-from-file.md new file mode 100644 index 000000000..5849b11a3 --- /dev/null +++ b/doc/manual/rl-next/arg-from-file.md @@ -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. diff --git a/src/libcmd/common-eval-args.cc b/src/libcmd/common-eval-args.cc index 444ff81c9..b87bbbc27 100644 --- a/src/libcmd/common-eval-args.cc +++ b/src/libcmd/common-eval-args.cc @@ -20,7 +20,7 @@ MixEvalArgs::MixEvalArgs() .description = "Pass the value *expr* as the argument *name* to Nix functions.", .category = category, .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({ @@ -28,7 +28,24 @@ MixEvalArgs::MixEvalArgs() .description = "Pass the string *string* as the argument *name* to Nix functions.", .category = category, .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({ @@ -154,13 +171,23 @@ MixEvalArgs::MixEvalArgs() Bindings * MixEvalArgs::getAutoArgs(EvalState & state) { auto res = state.buildBindings(autoArgs.size()); - for (auto & i : autoArgs) { + for (auto & [name, arg] : autoArgs) { auto v = state.allocValue(); - if (i.second[0] == 'E') - state.mkThunk_(*v, state.parseExprFromString(i.second.substr(1), state.rootPath("."))); - else - v->mkString(((std::string_view) i.second).substr(1)); - res.insert(state.symbols.create(i.first), v); + std::visit(overloaded { + [&](const AutoArgExpr & arg) { + state.mkThunk_(*v, state.parseExprFromString(arg.expr, state.rootPath("."))); + }, + [&](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(); } diff --git a/src/libcmd/common-eval-args.hh b/src/libcmd/common-eval-args.hh index 2eb63e15d..25ce5b9da 100644 --- a/src/libcmd/common-eval-args.hh +++ b/src/libcmd/common-eval-args.hh @@ -6,6 +6,8 @@ #include "common-args.hh" #include "search-path.hh" +#include + namespace nix { class Store; @@ -26,7 +28,14 @@ struct MixEvalArgs : virtual Args, virtual MixRepair std::optional evalStoreUrl; private: - std::map autoArgs; + struct AutoArgExpr { std::string expr; }; + struct AutoArgString { std::string s; }; + struct AutoArgFile { std::filesystem::path path; }; + struct AutoArgStdin { }; + + using AutoArg = std::variant; + + std::map autoArgs; }; SourcePath lookupFileArg(EvalState & state, std::string_view s, const Path * baseDir = nullptr); diff --git a/tests/functional/eval.sh b/tests/functional/eval.sh index b81bb1e2c..c6a475cd0 100644 --- a/tests/functional/eval.sh +++ b/tests/functional/eval.sh @@ -41,3 +41,14 @@ mkdir -p $TEST_ROOT/xyzzy $TEST_ROOT/foo ln -sfn ../xyzzy $TEST_ROOT/foo/bar printf 123 > $TEST_ROOT/xyzzy/default.nix [[ $(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 ]]