Type-safe settings from environment variables and Docker secrets using msgspec
Project description
msgspec-settings
Type-safe settings from environment variables and Docker secrets using msgspec.
Description
msgspec-settings is a lightweight library for loading application settings from environment variables and Docker secrets with automatic type validation using msgspec.Struct. It provides a schema-first approach where you define your configuration structure using msgspec's typed structs, and the library handles parsing, type conversion, and validation.
The library supports nested structures of arbitrary depth, automatic type casting for common Python types (str, int, float, bool), and follows a predictable environment variable naming convention with customizable prefixes. Values are loaded with priority: environment variables override Docker secrets.
Installation
uv add msgspec-settings
or with pip:
pip install msgspec-settings
Quick Start
import msgspec
from msgspec_settings import BaseSettings
class PostgresConfig(msgspec.Struct, frozen=True):
host: str
port: int = 5432
password: str
class DatabaseConfig(msgspec.Struct, frozen=True):
postgres: PostgresConfig
pool_size: int = 10
class AppSettings(BaseSettings):
database: DatabaseConfig
debug: bool = False
# Environment variables:
# APP__DATABASE__POSTGRES__HOST=localhost
# APP__DATABASE__POSTGRES__PORT=3306
# APP__DATABASE__POOL_SIZE=20
# Docker secret file:
# /run/secrets/app__database__postgres__password
# Contents: "super_secret_password"
settings = AppSettings(env_prefix="app", secrets_dir="/run/secrets")
print(settings.database.postgres.host) # localhost
print(settings.database.postgres.port) # 3306
print(settings.database.postgres.password) # super_secret_password (from secret file)
print(settings.database.pool_size) # 20
License
MIT License - see LICENSE file for details.
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 msgspec_settings-0.1.0.tar.gz.
File metadata
- Download URL: msgspec_settings-0.1.0.tar.gz
- Upload date:
- Size: 6.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
be11c3bc59b919c13ef357c98ea731f1d22482e10010bf1df7d8f0097c468ddd
|
|
| MD5 |
79b009893940e7558f29d7d5f2b439dd
|
|
| BLAKE2b-256 |
5d18e18672f1a019891b35c4ebb53b052be6c55b5222018869343b754a0c1566
|
File details
Details for the file msgspec_settings-0.1.0-py3-none-any.whl.
File metadata
- Download URL: msgspec_settings-0.1.0-py3-none-any.whl
- Upload date:
- Size: 9.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9430b324b04c0c0491d954bb30b73a72b95dd09b1b396605e33e1e49c8e70548
|
|
| MD5 |
db5fee97ce0591e988e57ccd7c8987f1
|
|
| BLAKE2b-256 |
a9427b61c3e82ece8e8ca44b24e2de37ab7cd7c057a0bb5d674af2b3ba5411d2
|