Environment Variables in PureScript
Environment variables are a universal mechanism for conveying configuration information to Unix programs. Let’s look at how to set, get, and list environment variables in PureScript.
Running the program shows that we pick up the value for FOO
that we set in the program, but that BAR
is empty.
The list of keys in the environment will depend on your particular machine.
If we set BAR
in the environment first, the running program picks that value up.
In PureScript, we use the Node.Process
module to interact with environment variables. The lookupEnv
function returns a Maybe String
, which we handle using pattern matching or the show
function for simplicity in this example. The setEnv
function is used to set environment variables.
Instead of os.Environ()
, we use Process.getEnv
which returns a record of key-value pairs. We then use Object.keys
to get all the keys from this record.
Note that in PureScript, we’re working in a more functional style, using Effect
for side effects and Maybe
for potentially missing values.