Operating System


Quick Start

(module demo
  (import std.os std.io)
  (begin
    ;; Inspect the script's invocation
    (println (os:command-line-arguments))      ;; e.g. ("--verbose" "in.csv")

    ;; Environment variables
    (println (or (os:getenv "ETA_HOME") "(unset)"))
    (os:setenv! "ETA_DEMO_RUN" "1")

    ;; Working directory
    (println (os:current-directory))

    ;; Clean exit with a status code
    (when (equal? (os:getenv "DEMO_FAIL") "1")
      (os:exit 2))))

std.os is a thin Eta layer over the native process / environment builtins (getenv, setenv!, current-directory, …). It is auto-imported by std.prelude; you can also import it directly:

(import std.os)

API

Environment Variables

FunctionSignatureNotes
os:getenv(name) -> string | #fReturns #f for unset variables.
os:setenv!(name value) -> '()Mutates the live process environment.
os:unsetenv!(name) -> '()Idempotent — succeeds even if the variable is already unset.
os:environment-variables() -> alistSorted alist of ("KEY" . "value") pairs.

Process

FunctionSignatureNotes
os:command-line-arguments() -> listList of strings passed to etai / etac after the script path.
os:exit([code]) -> neverTerminates the host process. code defaults to 0; non-integer codes are coerced where unambiguous.

Working Directory

FunctionSignatureNotes
os:current-directory() -> stringAbsolute path, with the platform-preferred separator.
os:change-directory!(path) -> '()Equivalent of chdir. Errors if path is not a directory.

Patterns

Config from environment with a fallback

(import std.os)
(defun config-or (name default)
  (or (os:getenv name) default))

(define log-level (config-or "ETA_LOG_LEVEL" "info"))

CLI flag parsing

(import std.os std.collections)
(defun has-flag? (flag)
  (any? (lambda (a) (equal? a flag))
        (os:command-line-arguments)))

(when (has-flag? "--verbose")
  (println "verbose mode on"))

Scoped working directory

(import std.os)
(defun with-cwd (dir thunk)
  (let ((prev (os:current-directory)))
    (dynamic-wind
      (lambda () (os:change-directory! dir))
      thunk
      (lambda () (os:change-directory! prev)))))

Exit codes from a script

(import std.os)
(catch (lambda () (run-pipeline!))
       (lambda (tag payload)
         (eprintln "fatal: " tag " " payload)
         (os:exit 1)))

Notes