Environment variables

Environment variables: env

Environment variables are settings a program gets from outside: from the shell, a container, or the platform it runs on. env reads and sets them, and loads them from .env files. It also tells which system the program runs on.

host: str = env.get("DB_HOST")             // "" if it isn't set
port: int = env.get("PORT", 8080)           // 8080 if it isn't set
debug: bool = env.get("DEBUG", false)
print("Starting on ${host}:${port}")
PORT=9000 DEBUG=true luma run app.luma

Reading

CallGives
env.get(name)the variable’s value, a str; "" if it isn’t set
env.get(name, default)the value as the type of the default, or the default if it isn’t set
env.has(name)true if it is set, even to an empty value
env.all()every variable, a {str: str} sorted by name

The default says what you expect: env.get("PORT", 8080) gives an int, env.get("DEBUG", false) a bool, env.get("RATIO", 0.5) a float — like flag does. The value is read as convert reads text (spaces around it don’t matter), and a value that isn’t such raises error 11: with PORT=abc, cannot convert "abc" to an int.

A variable set to an empty value is set: env.get("NAME", "x") gives "" then, and env.has("NAME") is true. Only a variable that isn’t there at all gets the default.

Setting

env.set("APP_MODE", "testing")
env.unset("APP_MODE")

env.set and env.unset change what the program itself sees from then on, and what the commands it runs with shell see; they don’t change the shell that started the program, or other programs. A name can’t be empty or contain = (error 11).

.env files

env.load(path) sets variables from a file of NAME=value lines, as Docker, many hosting platforms and other languages’ dotenv libraries use them:

File .env:

# the app's settings
APP_NAME="My Cool App"
export PORT=9000
EMPTY=
NOTE='kept # as it is'
GREETING="say \"hi\"\n"
LEVEL = debug   # spaces around the value and comments after it don't count
env.load(".env")
env.load(".env.local")   // a file that isn't there is fine: nothing happens
name: str = env.get("APP_NAME")
  • What is set wins. A variable that is already set — by the system, by env.set or by an earlier file — is not changed by a file. So the real environment of a server always wins over a .env file, and with env.load(".env.local") first and env.load(".env") after, the local file’s values win.
  • A missing file does nothing, so the same code works where there is a .env file and where there isn’t.
  • In "double quotes", \", \\, \n and \t are escapes; 'single quotes' keep everything as it is. Without quotes, a # after a space starts a comment.
  • export before a name is allowed, so the file also works as a shell script.
  • A line that is none of these raises error 11 with the file and line: .env:3: expected NAME=value, found "PORT 8080". A quote has to end on its line.

The system

env.os() names the system the program runs on: "windows", "linux" or "macos". Commands for shell are written for one system, so this is how a program picks the right one:

files: str = ""
if env.os() == "windows" {
    files = shell("dir /b")
} else {
    files = shell("ls")
}
print(files)

A program made with luma build runs on the system it was built for, so it gives that one; luma run gives the system it runs on, which may also be "macos".

Details

  • The program reads the system’s variables when it first uses env. Each run sees the environment it was started with.
  • Names are case-sensitive, as on Linux (Path and PATH are two variables) — also on Windows.
  • env is a built-in name, so it can’t be a variable’s name.
  • TZ (the time zone) is read when the program starts, so setting it with env.set doesn’t change the local clock.