aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMaximilian Bosch <maximilian@mbosch.me>2024-09-15 16:13:22 +0200
committerMaximilian Bosch <maximilian@mbosch.me>2024-09-15 16:52:30 +0200
commit80202e3ca314c21547c48f3a23d3f629cd9ddb87 (patch)
tree62d383f680d625443eb36123b783775e4bcf0c15 /src
parent727258241fc0b3c02691b72302d2c3092baca275 (diff)
common-eval-args: raise warning if `--arg` isn't a valid Nix identifier
See https://git.lix.systems/lix-project/lix/issues/496. The core idea is to be able to do e.g. nix-instantiate -A some-nonfree-thing --arg config.allowUnfree true which is currently not possible since `config.allowUnfree` is interpreted as attribute name with a dot in it. In order to change that (probably), Jade suggested to find out if there are any folks out there relying on this behavior. For such a use-case, it may still be possible to accept strings, i.e. `--arg '"config.allowUnfree"'. Change-Id: I986c73619fbd87a95b55e2f0ac03feaed3de2d2d
Diffstat (limited to 'src')
-rw-r--r--src/libcmd/common-eval-args.cc26
1 files changed, 24 insertions, 2 deletions
diff --git a/src/libcmd/common-eval-args.cc b/src/libcmd/common-eval-args.cc
index 9a7c30d57..cbb7edbdd 100644
--- a/src/libcmd/common-eval-args.cc
+++ b/src/libcmd/common-eval-args.cc
@@ -9,8 +9,24 @@
#include "store-api.hh"
#include "command.hh"
+#include <regex>
+
namespace nix {
+static std::regex const identifierRegex("^[A-Za-z_][A-Za-z0-9_'-]*$");
+static void warnInvalidNixIdentifier(const std::string & name)
+{
+ std::smatch match;
+ if (!std::regex_match(name, match, identifierRegex)) {
+ warn("This Nix invocation specifies a value for argument '%s' which isn't a valid \
+Nix identifier. The project is considering to drop support for this \
+or to require quotes around args that aren't valid Nix identifiers. \
+If you depend on this behvior, please reach out in \
+https://git.lix.systems/lix-project/lix/issues/496 so we can discuss \
+your use-case.", name);
+ }
+}
+
MixEvalArgs::MixEvalArgs()
{
addFlag({
@@ -18,7 +34,10 @@ 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) {
+ warnInvalidNixIdentifier(name);
+ autoArgs[name] = 'E' + expr;
+ }}
});
addFlag({
@@ -26,7 +45,10 @@ 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) {
+ warnInvalidNixIdentifier(name);
+ autoArgs[name] = 'S' + s;
+ }},
});
addFlag({