Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to use environment variables in Lua code

I have some Lua code, which I use in my openresty nginx.conf file. This Lua code contains such lines:

...
local secret = os.getenv("PATH")
assert(secret ~= nil, "Environment variable PATH not set")
...

Just for testing reasons I tried to check if PATH variable is set and for some reason the assert statement does not pass. I see in the console:

Environment variable PATH not set

However, when I run this

$ echo $PATH

I see, that this variable indeed has some value. So, what is wrong with that and how can I fix it?

like image 870
Jacobian Avatar asked Jan 23 '17 05:01

Jacobian


People also ask

How do I set environment variables in Lua?

If you want to affect the lua process's environment (which would then, in turn, affect the environment's of processes run by lua) then you need a binding to the setenv system function (which lua itself doesn't provide as it doesn't pass the clean C test that lua uses for things it includes).

What is a Lua environment?

In Lua, objects of type thread, function and userata can be associated with a table called environment. The environment is also a regular table. It can operate like a normal table and store various variables related to the object. The environment on the associated thread s can only be accessed through C code.

Can we use environment variables in XML file?

Answer. Yes, you can set and use environment variables and use system environment variables, but you must enclose environment variable references in brackets "{}" in deployment. xml.


Video Answer


1 Answers

You need to tell nginx to make environment variables available. From the docs for the env directive: "By default, nginx removes all environment variables inherited from its parent process except the TZ variable. This directive allows preserving some of the inherited variables, changing their values, or creating new environment variables."

So, in your case you'd need to specify env PATH; in nginx.conf.

like image 100
Paul Kulchenko Avatar answered Sep 28 '22 03:09

Paul Kulchenko