From 80202e3ca314c21547c48f3a23d3f629cd9ddb87 Mon Sep 17 00:00:00 2001 From: Maximilian Bosch Date: Sun, 15 Sep 2024 16:13:22 +0200 Subject: [PATCH] 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 --- src/libcmd/common-eval-args.cc | 26 ++++++++++++++++++++++++-- 1 file 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 + 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({