Setting Environment Variables for Child Process
Setting environment variables for the current process is less common in pure Lean code. Usually, you set environment variables when spawning a new child process to configure its environment.
When using IO.Process.spawn, you can pass an env array to specify variables for the new process:
def runWithCustomEnv : IO Unit := do
let child ← IO.Process.spawn {
cmd := "printenv",
args := #["MY_VAR"],
env := #[("MY_VAR", "1234")]
}
let exitCode ← child.wait
IO.println s!"Process exited with code: {exitCode}"
This ensures that MY_VAR is available to the child process without affecting the parent process's environment.