.env support for open.mp and legacy SA-MP: load
configuration and secrets from a .env file (and the OS environment) and read
them from Pawn.
Keep secrets like database keys out of your committed .pwn and config.json.
The same natives ship as an open.mp component and a SA-MP plugin, both built from this repo.
#include <omp-dotenv>
native ENV_Load(const path[] = ".env");
native ENV_Get(const key[], dest[], size = sizeof(dest), const fallback[] = "");
native ENV_GetInt(const key[], fallback = 0);
native Float:ENV_GetFloat(const key[], Float:fallback = 0.0);
native bool:ENV_GetBool(const key[], bool:fallback = false);
native bool:ENV_Has(const key[]);Lookup order for every key: the .env file first, then an OS environment
variable, then the fallback.
.env in the server root:
# comments and an optional "export" prefix are allowed
SUPABASE_URL=https://yourproject.supabase.co
SUPABASE_KEY="eyJ..."
MAX_PLAYERS=100
DEBUG=truepublic OnGameModeInit()
{
new url[160], key[400];
ENV_Get("SUPABASE_URL", url);
ENV_Get("SUPABASE_KEY", key);
new maxPlayers = ENV_GetInt("MAX_PLAYERS", 50);
new bool:debug = ENV_GetBool("DEBUG", false);
return 1;
}Both builds use the same include/omp-dotenv.inc and the same .env file.
- Build it (see below) or download a release
omp-dotenv.so/omp-dotenv.dll. - Put the binary in your server's
components/folder (it auto-loads). - Put
include/omp-dotenv.incon your include path and#include <omp-dotenv>. - Create a
.envfile in the server root.
- Build it (see below) or download a release
dotenv.so/dotenv.dll. - Put the binary in your server's
plugins/folder. - Add
dotenvto thepluginsline inserver.cfg(plugins dotenvon Linux,plugins dotenv.dllon Windows). - Put
include/omp-dotenv.incon your include path and#include <omp-dotenv>. - Create a
.envfile in the server root.
Either way the natives auto-load .env from the server root on startup. Unlike
Pawn's own file functions (limited to scriptfiles/), this C++ code reads the
root and also falls back to real OS environment variables.
Requires a 32-bit toolchain (open.mp servers are i386), CMake 3.19+, and the submodules.
git clone --recursive https://github.com/ricardoofnl/omp-dotenv
cd omp-dotenv
CMAKE_POLICY_VERSION_MINIMUM=3.5 cmake -B build -DCMAKE_BUILD_TYPE=Release
cmake --build buildThis produces both binaries: build/omp-dotenv.so (open.mp component) and
build/dotenv.so (SA-MP plugin).
The CMAKE_POLICY_VERSION_MINIMUM env var lets very new CMake configure the
older bundled submodules. On Windows PowerShell, set it first:
$env:CMAKE_POLICY_VERSION_MINIMUM=3.5, then cmake -B build -A Win32.
On Arch Linux you need multilib (lib32-gcc-libs, lib32-glibc). On Debian or
Ubuntu use gcc-multilib g++-multilib.
A sample Pawn gamemode is in test/.
- Copy
test/sample.envto your server root as.env. - Put
omp-dotenv.soincomponents/andomp-dotenv.incon the include path. - Compile
test/test.pwnas the gamemode and run the server.
The results are printed to the server console, ending with X/X passed.
GNU General Public License v3.0. See LICENSE.