Hatch environment collector plugin that sets global dynamic environment variables
Project description
hatch-global-env-vars
| CI/CD | |
| Package | |
| Meta |
This provides an environment collector plugin for Hatch that sets global environment variables during Hatch initialization. This allows you to dynamically configure environment variables with conditional logic, variable copying, and recursive resolution.
Key Feature: Unlike Hatch's built-in env-vars which are scoped to individual environments, this plugin sets variables globally using os.environ, making them available to all environments and processes.
Features
- Copy environment variables with fallback defaults
- Optional variable copying (only set if source exists)
- Conditional variable setting based on environment state
- Recursive variable resolution with dict syntax
- Global scope - variables are set in
os.environ, not just in Hatch environments
Installation
-
pyproject.toml
[tool.hatch.env] requires = ["hatch-global-env-vars"]
-
hatch.toml
[env] requires = ["hatch-global-env-vars"]
Usage
-
pyproject.toml
[tool.hatch.env.collectors.global-env-vars] env-vars = [ # ... ]
-
hatch.toml
[env.collectors.global-env-vars] env-vars = [ # ... ]
Examples
[tool.hatch.env.collectors.global-env-vars]
env-vars = [
# Example 1: Copy with fallback
{ name = "LOG_LEVEL", copy = "CI_LOG_LEVEL", default = "info" },
# Example 2: Optional copy (only if source exists)
{ name = "API_KEY", copy = "SECRET_API_KEY", required = false },
# Example 3: Existence check
{ name = "HAS_TOKEN", value = "yes", condition = "AUTH_TOKEN" },
{ name = "NO_TOKEN", value = "yes", condition = "!AUTH_TOKEN" },
# Example 4: Empty/non-empty checks
{ name = "TOKEN_EMPTY", value = "true", condition = "AUTH_TOKEN==" },
{ name = "TOKEN_HAS_VALUE", value = "true", condition = "AUTH_TOKEN!=" },
# Example 5: Value equality
{ name = "VERBOSE", value = "true", condition = "LOG_LEVEL==debug" },
{ name = "PROD_MODE", value = "true", condition = "ENVIRONMENT==production" },
# Example 6: Value inequality
{ name = "NOT_PROD", value = "true", condition = "ENVIRONMENT!=production" },
# Example 7: AND logic (all conditions must be true)
{ name = "DEPLOY", value = "true", condition = ["CI", "BRANCH==main", "DEPLOY_KEY"] },
# Example 8: OR logic (any condition can be true)
{ name = "USE_CACHE", value = "redis", condition = { any = ["PROD", "STAGING"] } },
# Example 9: Recursive resolution with variable reference dict
{ name = "APP_ENV", copy = "ENVIRONMENT", default = { name = "ENV", default = "development" } },
# Example 10: Complex nested conditions
{ name = "FEATURE_X", value = "enabled", condition = { any = [
{ all = ["PROD", "FEATURE_FLAG==on"] },
"FORCE_FEATURE_X"
] } },
]
Configuration Options
Each entry in env-vars is a dictionary with these fields:
Required Fields
name(string): Name of the environment variable to set
Variable Source (choose one)
copy(string): Copy value from this environment variablevalue(string or dict): Set to this literal value or variable reference
Optional Fields
default(string or dict): Fallback value ifcopysource doesn't exist- Can be a literal string:
"development" - Can be a variable reference dict:
{ name = "ENV", default = "dev" } - Supports recursive nesting for fallback chains
- Can be a literal string:
required(boolean): Iftrue(default), raise error whencopysource is missing and nodefaultprovided. Iffalse, skip setting the variable whencopysource is missing.condition(string, list, or dict): Only set the variable if this condition is true- String conditions:
"ENV": true if ENV exists (regardless of value)"!ENV": true if ENV doesn't exist"ENV==": true if ENV exists and is empty string"ENV!=": true if ENV exists and is non-empty"ENV==value": true if ENV equals value (value must be non-empty)"ENV!=value": true if ENV doesn't equal value
- List conditions (AND logic):
["COND1", "COND2"]- all must be true - Dict conditions (OR logic):
{ any = ["COND1", "COND2"] }- at least one must be true - Dict conditions (explicit AND):
{ all = ["COND1", "COND2"] }- all must be true
- String conditions:
Examples
CI Detection
[tool.hatch.env.collectors.global-env-vars]
env-vars = [
# Detect CI environment
{ name = "IS_CI", copy = "CI", default = "false" },
# Set verbose logging in CI
{ name = "LOG_LEVEL", value = "debug", condition = "CI" },
# Use different pytest args in CI
{ name = "PYTEST_ARGS", value = "-v --cov --cov-report=xml", condition = "CI" },
{ name = "PYTEST_ARGS", value = "-v", condition = "!CI" },
# Only deploy from main branch in CI
{ name = "SHOULD_DEPLOY", value = "true", condition = ["CI", "BRANCH==main"] },
# Require non-empty deploy key
{ name = "CAN_DEPLOY", value = "true", condition = ["CI", "BRANCH==main", "DEPLOY_KEY!="] },
]
Multi-Environment Setup
[tool.hatch.env.collectors.global-env-vars]
env-vars = [
# Copy environment with fallback chain using dict syntax
{ name = "APP_ENV", copy = "DEPLOY_ENV", default = { name = "ENVIRONMENT", default = "development" } },
# Database URL based on environment
{ name = "DATABASE_URL", copy = "PROD_DATABASE_URL", condition = "ENVIRONMENT==production" },
{ name = "DATABASE_URL", copy = "STAGE_DATABASE_URL", condition = "ENVIRONMENT==staging" },
{ name = "DATABASE_URL", value = "sqlite:///dev.db", condition = { any = ["!ENVIRONMENT", "ENVIRONMENT==development"] } },
# Config file with multiple fallbacks
{ name = "CONFIG_FILE", copy = "CUSTOM_CONFIG", default = { name = "ENV_CONFIG", default = "config/default.yaml" } },
# Feature flags for prod or staging
{ name = "FEATURE_BETA", value = "enabled", condition = { any = ["ENVIRONMENT==production", "ENVIRONMENT==staging"] } },
]
Secret Management
[tool.hatch.env.collectors.global-env-vars]
env-vars = [
# Only set API key if secret exists (don't fail if missing)
{ name = "API_KEY", copy = "SECRET_API_KEY", required = false },
# Use different key in CI
{ name = "API_KEY", copy = "CI_API_KEY", condition = "CI_API_KEY" },
# Require authentication in production
{ name = "REQUIRE_AUTH", value = "true", condition = "ENVIRONMENT==production" },
{ name = "REQUIRE_AUTH", value = "false", condition = "ENVIRONMENT!=production" },
# Only enable feature if token has a value (not empty)
{ name = "PREMIUM_FEATURES", value = "enabled", condition = "API_TOKEN!=" },
]
Distinguishing Empty vs Non-existent Variables
[tool.hatch.env.collectors.global-env-vars]
env-vars = [
# Check if TOKEN exists (regardless of value)
{ name = "TOKEN_SET", value = "true", condition = "TOKEN" },
# Check if TOKEN doesn't exist
{ name = "NO_TOKEN", value = "true", condition = "!TOKEN" },
# Check if TOKEN exists AND is empty string
{ name = "TOKEN_EMPTY", value = "true", condition = "TOKEN==" },
# Check if TOKEN exists AND is non-empty
{ name = "TOKEN_HAS_VALUE", value = "true", condition = "TOKEN!=" },
]
Truth table for different TOKEN states:
| TOKEN state | TOKEN |
!TOKEN |
TOKEN== |
TOKEN!= |
|---|---|---|---|---|
| Not set | false | true | false | false |
| Set to "" | true | false | true | false |
| Set to "x" | true | false | false | true |
How It Works
This plugin implements a Hatch environment collector that runs during Hatch's initialization phase, before any environments are created.
The collector:
- Reads configuration from
[tool.hatch.env.collectors.global-env-vars] - Evaluates conditions to determine which variables to set
- Resolves variable references recursively
- Sets variables globally in
os.environ, making them available to:- All Hatch environments (default, test, docs, etc.)
- Child processes spawned by Hatch
- Scripts run through Hatch
This is different from Hatch's built-in env-vars section, which only sets variables within specific environment scopes.
Error Handling
The plugin will raise errors for:
- Missing required source variables (when
required=trueand nodefault) - Invalid configuration (missing required fields, conflicting options)
- Circular references in recursive resolution
License
hatch-global-env-vars is distributed under the terms of the MIT license.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file hatch_global_env_vars-1.0.0.tar.gz.
File metadata
- Download URL: hatch_global_env_vars-1.0.0.tar.gz
- Upload date:
- Size: 8.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
50ff4d6e173812866cc31bc67a1c4451dd00461ace5f10ccb7a6ac3bf7e5f650
|
|
| MD5 |
95239da1577834cda435da3a3d2184de
|
|
| BLAKE2b-256 |
4d81cfff7811a15aef7d1456b73ba5abd0765dd88d8636cad2614d0f0fc4ddb6
|
Provenance
The following attestation bundles were made for hatch_global_env_vars-1.0.0.tar.gz:
Publisher:
build.yml on valentinoli/hatch-global-env-vars
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
hatch_global_env_vars-1.0.0.tar.gz -
Subject digest:
50ff4d6e173812866cc31bc67a1c4451dd00461ace5f10ccb7a6ac3bf7e5f650 - Sigstore transparency entry: 848883799
- Sigstore integration time:
-
Permalink:
valentinoli/hatch-global-env-vars@425d8a6e3982684dfad05e9b74efbb6b87329d9f -
Branch / Tag:
refs/tags/v1.0.0 - Owner: https://github.com/valentinoli
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
build.yml@425d8a6e3982684dfad05e9b74efbb6b87329d9f -
Trigger Event:
push
-
Statement type:
File details
Details for the file hatch_global_env_vars-1.0.0-py3-none-any.whl.
File metadata
- Download URL: hatch_global_env_vars-1.0.0-py3-none-any.whl
- Upload date:
- Size: 9.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f3418b09fa2284cbe18904b14223dd4a3ed14e3d13fae51cc7184ae413b44f3c
|
|
| MD5 |
56eb48fc41cbce33c592ba22e821173e
|
|
| BLAKE2b-256 |
80e9751ac3b8b9f15f536e46e793c1b356cc5f47d038e3bed6495470da73f653
|
Provenance
The following attestation bundles were made for hatch_global_env_vars-1.0.0-py3-none-any.whl:
Publisher:
build.yml on valentinoli/hatch-global-env-vars
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
hatch_global_env_vars-1.0.0-py3-none-any.whl -
Subject digest:
f3418b09fa2284cbe18904b14223dd4a3ed14e3d13fae51cc7184ae413b44f3c - Sigstore transparency entry: 848883801
- Sigstore integration time:
-
Permalink:
valentinoli/hatch-global-env-vars@425d8a6e3982684dfad05e9b74efbb6b87329d9f -
Branch / Tag:
refs/tags/v1.0.0 - Owner: https://github.com/valentinoli
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
build.yml@425d8a6e3982684dfad05e9b74efbb6b87329d9f -
Trigger Event:
push
-
Statement type: