dotenv
A lightweight .env file loader for Free Pascal
- Active · last change 9-15-2025
- ★ 2 on GitHub
README
Dotenv Free Pascal .env Loader
A lightweight .env file loader for Free Pascal. This library lets you load key/value pairs from a .env file into your Free Pascal program, with support for:
KEY=VALUEparsing- Single-quoted and double-quoted values
- Escaped characters inside double quotes (
\n,\r,\t,\\,\") - Variable expansion using
${VAR} - Required variable checks
⚠️ Note: This implementation does not modify the system environment.
All variables are stored internally in memory and accessed viaDotEnvGet(Key).
Why?
Managing configuration through environment variables is a best practice, especially for web or desktop applications.
Instead of hardcoding secrets or config values, you can keep them in a .env file and load them safely into your Pascal application. .env files can be excluded from your repository to keep sensitive data secure.
Installation
If you use Nova Packager, add this package to your project:
nova require daar/dotenvThen import it in your Pascal code:
uses Dotenv;Usage
1. Create a .env file
# Database settings
DB_HOST=localhost
DB_USER=root
DB_PASS="secret"
# Paths
BASE_DIR=/home/user
LOG_DIR=${BASE_DIR}/logs2. Load the .env file in your Pascal code
program DemoEnv;
uses
SysUtils, Dotenv;
begin
// Load .env file)
LoadDotEnv('.env');
// Require some variables
RequireEnvVars(['DB_HOST', 'DB_USER', 'DB_PASS']);
// Print them
WriteLn('DB_HOST = ', DotEnvGet('DB_HOST'));
WriteLn('DB_USER = ', DotEnvGet('DB_USER'));
WriteLn('DB_PASS = ', DotEnvGet('DB_PASS'));
WriteLn('LOG_DIR = ', DotEnvGet('LOG_DIR'));
end.Output:
DB_HOST = localhost
DB_USER = root
DB_PASS = secret
LOG_DIR = /home/user/logs3. Require specific variables
Ensure that critical variables exist before running:
RequireEnvVars(['DB_HOST', 'DB_USER', 'DB_PASS']);If any variable is missing, the program raises an exception:
Required environment variable "DB_PASS" is not set!Features
- ✅ Reads
.envfiles line by line - ✅ Supports comments (
# ...) - ✅ Handles quotes and escape sequences
- ✅ Expands variables (
${VAR}) using internal values - ✅ Provides
RequireEnvVars()for safety checks - ✅ Fully self-contained — no system environment modification
License
MIT — free to use and modify.

