-
Notifications
You must be signed in to change notification settings - Fork 92
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Accessing environment variables #2043
Comments
A few thoughts of my own:
should evaluate to
|
Maybe there should be a stdlib function like |
I really want to avoid the situation when your config might depend on statically-unknown environment variables |
Right. Another solution is to not bless any name, but always require to pass one through the command line, which is also reasonable (
This is a very good point 👍 |
I'm including @vi's suggestion that the customize mode of the CLI has a special syntax (as long as it doesn't clash with actual Nickel syntax, but it's easy to do) to embed environment variables and/or files directly, which I think is appealing as well: #2048 (reply in thread) |
Is your feature request related to a problem? Please describe.
A feature request that came up several times on various channels is to be able to access environment variables. This is possible today by performing some pre-processing step (for example, a script dumping the environment to a file or converting it to a
.ncl
file that can be imported), or by using the customize command line, for example:The pre-processing approach has the obvious drawback of requiring everyone to come up with their own scheme and scripts for a task that seems to be common to many use-cases. The customize-mode approach can quickly lead to very long and unreadable command line (needing some form of scripting anyway, probably). In both cases, it's also not trivial to handles cases where the environment variable has special characters that might be interpreted in the Nickel string, such as
"
, escape sequences or%{
. It can be solved practically by using a multi-line strings with a complicated enough delimiters, but then new lines and indentation can become an issue as well...Having a standard built-in solution to this problem is reasonable.
Describe the solution you'd like
Using variables from the environment have the potential of breaking Nickel's purity if done wrong. If the result of the program can suddenly depend on unspecified environment variables in an uncontrolled way, it can become very challenging to ensure reproducibility of evaluation across different environments. Thus, whatever we end up with, we must ensure that which variables are taken from the environment (and from which environment variable) can be easily and immediately decided, without having to evaluate the program.
This precludes a simple approach such as a free-form dynamic
std.env.get
that can retrieve any value dynamically from the environment. An explicit mapping of some top-level variables to environment variables might be a better idea.One simple solution would be to propose a shortcut for the customize-mode approach, with proper escaping, such as:
nickel eval config.ncl --field config --take-from-env ENV_PATH PATH
. We could also decide that the name of the Nickel variable and the environment variable are deducible one from each other, to end up with something likenickel eval config.ncl --take-from-env PATH CLASSPATH
, which would be equivalent tonickel eval config.ncl -- ENV_PATH="escape("$PATH")" ENV_CLASSPATH="escape("$path")"
(where escape is a bash functions performing the required escaping)Another approach is to use a special file instead. Either specified on the command line, or fixed by Nickel, we would have say an
env_var.ncl
file with a mapping of the form:Either the mere presence of this file, or a command-line argument would cause Nickel to evaluate it, build the corresponding record
{my_path = <value of PATH>, my_classpath = <value of CLASSPATH>}
, and merge it with the original config.There might be other solutions in between.
The text was updated successfully, but these errors were encountered: