Skip to main content

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

msgspec_settings-0.1.0.tar.gz (6.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

msgspec_settings-0.1.0-py3-none-any.whl (9.4 kB view details)

Uploaded Python 3

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

Hashes for msgspec_settings-0.1.0.tar.gz
Algorithm Hash digest
SHA256 be11c3bc59b919c13ef357c98ea731f1d22482e10010bf1df7d8f0097c468ddd
MD5 79b009893940e7558f29d7d5f2b439dd
BLAKE2b-256 5d18e18672f1a019891b35c4ebb53b052be6c55b5222018869343b754a0c1566

See more details on using hashes here.

File details

Details for the file msgspec_settings-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for msgspec_settings-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 9430b324b04c0c0491d954bb30b73a72b95dd09b1b396605e33e1e49c8e70548
MD5 db5fee97ce0591e988e57ccd7c8987f1
BLAKE2b-256 a9427b61c3e82ece8e8ca44b24e2de37ab7cd7c057a0bb5d674af2b3ba5411d2

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page