sysdelta

Configuration System

This project uses lib_layered_config to manage configuration through a layered merging system. Configuration values are loaded from multiple sources and merged in a defined order, allowing flexible overrides from system-wide defaults down to individual command-line arguments.

Key Concepts


Configuration Layers

Configuration is loaded and merged in the following order (lowest to highest precedence):

Priority Layer Description
1 defaults Bundled with the package (defaultconfig.toml)
2 app System-wide settings for all machines
3 host Machine-specific overrides
4 user User’s personal settings
5 .env First .env found upward, else per-user .env
6 env vars Environment variables
7 CLI Command-line --set flags (highest priority)

Merge behavior: Each layer only needs to specify values it wants to override. Unspecified values inherit from lower layers.


File Locations

Platform-Specific Paths

Layer Linux macOS Windows
defaults (bundled with package) (bundled with package) (bundled with package)
app /etc/xdg/{slug}/config.toml /Library/Application Support/{vendor}/{app}/config.toml C:\ProgramData\{vendor}\{app}\config.toml
host /etc/xdg/{slug}/hosts/{hostname}.toml /Library/Application Support/{vendor}/{app}/hosts/{hostname}.toml C:\ProgramData\{vendor}\{app}\hosts\{hostname}.toml
user ~/.config/{slug}/config.toml ~/Library/Application Support/{vendor}/{app}/config.toml %APPDATA%\{vendor}\{app}\config.toml
.env ~/.config/{slug}/.env (fallback) ~/Library/Application Support/{vendor}/{app}/.env (fallback) %APPDATA%\{vendor}\{app}\.env (fallback)

Every layer reads config.toml first and then the files in the config.d/ directory next to it (for the host layer: hosts/{hostname}.d/), in lexical order; a config.d/ file may be TOML, YAML or JSON. On Linux the app and host layers are read from /etc/xdg/{slug}/ and then from /etc/{slug}/, and $XDG_CONFIG_HOME replaces ~/.config when it is set. The .env row is a fallback: it is read only when no .env is found in the working directory or one of its parents.

Path Placeholders

Placeholder Linux macOS / Windows
{slug} sysdelta -
{vendor} - bitranox
{app} - sysdelta
{hostname} System hostname System hostname

Concrete Examples

Linux:

macOS:

Windows:


CLI Commands

Global Options

These options apply to all commands and must be specified before the command name:

Option Description
--version Show version and exit.
--profile NAME Load configuration from a named profile (e.g., production, test).
--set SECTION.KEY=VALUE Override a configuration setting. Can be repeated for multiple overrides.
--env-file PATH Explicit .env file path. Skips the default upward directory search.
--traceback Show full Python traceback on errors (useful for debugging).
--no-traceback Hide traceback, show only error message (default).

Example usage:

# Use a specific profile
sysdelta --profile production config

# Override settings at runtime (repeatable)
sysdelta --set lib_log_rich.console_level=DEBUG config
sysdelta --set example.owner=ops-team --set example.hosts='["a.example.com:8080"]' config --format json

# Load configuration from an explicit .env file
sysdelta --env-file /etc/myapp/.env config
sysdelta --env-file ./environments/production.env config --format json

# Show full traceback for debugging
sysdelta --traceback config-deploy --target user

View Configuration

Display the merged configuration from all sources (defaults -> app -> host -> user -> .env -> env vars).

Options Reference

Option Required Description
--format No Output format: human (default) or json.
--section NAME No Show only a specific section (e.g., lib_log_rich).
--profile NAME No Load configuration for a specific profile.

Examples

# Show merged configuration from all sources
sysdelta config

# Output as JSON (useful for scripting)
sysdelta config --format json

# Show specific section only
sysdelta config --section lib_log_rich

# Load configuration for a specific profile
sysdelta config --profile production

# Combine options
sysdelta config --profile staging --format json --section lib_log_rich

Deploy Configuration Files

Deploy bundled default configuration to platform-specific directories.

Options Reference

Option Required Description
--target Yes Target layer: app, host, or user. Can be specified multiple times.
--force No Replace existing files whose content differs; the old file is kept as <name>.bak. Without this, existing files are skipped.
--profile NAME No Deploy to a profile-specific subdirectory (e.g., profile/production/).
--permissions No Set Unix permissions even when the configured enabled is false.
--no-permissions No Disable permission setting; use system umask instead. Not combinable with a mode option.
--dir-mode MODE No Override directory permissions (octal: 750 or 0o750).
--file-mode MODE No Override file permissions (octal: 640 or 0o640).

Basic Examples

# Create user configuration file
sysdelta config-deploy --target user

# Deploy to system-wide location (requires privileges)
sudo sysdelta config-deploy --target app

# Deploy host-specific configuration
sudo sysdelta config-deploy --target host

# Deploy to multiple locations at once
sysdelta config-deploy --target user --target host

# Overwrite existing configuration
sysdelta config-deploy --target user --force

# Deploy to a specific profile directory
sysdelta config-deploy --target user --profile production

# Deploy production profile and overwrite if exists
sysdelta config-deploy --target user --profile production --force

Every target receives config.toml plus config.d/40-layered-config.toml and config.d/90-logging.toml (the host target writes hosts/{hostname}.toml and hosts/{hostname}.d/). With --force, a file whose content differs from the bundled one is replaced and the old file is kept as <name>.bak (numbered, <name>.bak.1 and so on, when a backup already exists); a file whose content is already identical is left as it is, mode included. When nothing differs, config-deploy --force writes nothing and says so.

Deploying for Other Users

To deploy user-level configuration for a different user account, use sudo -u:

# Deploy user config for 'serviceaccount' user
sudo -u serviceaccount sysdelta config-deploy --target user

# Deploy with a specific profile
sudo -u serviceaccount sysdelta config-deploy --target user --profile production

# The config will be created at that user's home directory:
# /home/serviceaccount/.config/sysdelta/config.toml

Important notes:

Common deployment scenarios:

# System admin deploying app-wide config (all users)
sudo sysdelta config-deploy --target app

# System admin deploying for a service account
sudo -u myservice sysdelta config-deploy --target user

# System admin deploying host-specific config
sudo sysdelta config-deploy --target host

# Regular user deploying their own config (no sudo needed)
sysdelta config-deploy --target user

File Permissions (POSIX Only)

On Linux and macOS, config-deploy sets Unix file permissions based on the target layer. Windows uses ACLs and ignores these settings.

Target Directory Mode File Mode Description
app 0o755 (rwxr-xr-x) 0o644 (rw-r–r–) World-readable for system-wide config
host 0o755 (rwxr-xr-x) 0o644 (rw-r–r–) World-readable for host-specific config
user 0o700 (rwx——) 0o600 (rw——-) Private to user only

Permission options:

# Skip permission setting entirely (use system umask)
sysdelta config-deploy --target user --no-permissions

# Override directory mode (octal)
sysdelta config-deploy --target user --dir-mode 750

# Override file mode (octal)
sysdelta config-deploy --target user --file-mode 640

# Both overrides together
sysdelta config-deploy --target user --dir-mode 750 --file-mode 640

# Octal formats: both "750" and "0o750" are accepted
sysdelta config-deploy --target user --dir-mode 0o750

A mode must be a plain octal literal (750 or 0o750; no sign, whitespace, _ or other base) within 0..0o7777. Because the deployed files can hold credentials, a mode is also refused when it sets the setuid, setgid or sticky bit, grants group or world write, puts an execute bit on a file, or takes the owner’s access away (a directory needs owner rwx, a file owner rw). Group write is refused like world write because a group can hold every local account (on macOS all of them share staff), and a configuration file never needs x. A refused mode is a usage error (exit 2) that names the offending bits, and nothing is written.

Configurable defaults:

Permission defaults can be customized in [lib_layered_config.default_permissions]:

[lib_layered_config.default_permissions]
# Values: octal strings ("0o755", "755"); a bare integer is refused (see below)
app_directory = "0o755"
app_file = "0o644"
host_directory = "0o755"
host_file = "0o644"
user_directory = "0o700"
user_file = "0o600"

# Set to false to disable permission setting by default
enabled = true

config-deploy decides none of this itself: it passes its options and any --set of this section to lib_layered_config, which applies each target’s own layer modes. The library reads the section from the bundled defaults, the configuration files this deploy does not overwrite and the environment, with the --set values laid over them. It never reads .env for it (neither one found from the working directory nor an explicit --env-file), so a .env can neither change a deployed mode nor block a deploy. --dir-mode/--file-mode override the configured modes for every target, and a key left out falls back to the layer default in the table above. Without --permissions/--no-permissions, enabled decides; enabled = false behaves like --no-permissions, and --permissions sets the modes anyway.

A configured mode follows the same rules as --dir-mode/--file-mode: a plain octal STRING ("0o750", "750"), never setuid/setgid/sticky, group or world write, an execute bit on a file, or a directory without owner rwx / a file without owner rw. A bare integer is refused rather than reinterpreted: TOML user_file = 400, --set ...user_file=400 and an environment value 400 all arrive as the DECIMAL integer 400, which is 0o620, not the owner-read-only mode the digits suggest. Quote it in TOML (user_file = "640"); in an environment variable or --set, use the 0o prefix (0o640), which is never read as a number. enabled must be a real boolean (true/false in TOML, an environment variable or --set); "no", "off", 0 or 1 are refused rather than read as one. An unknown key in the section is refused rather than ignored. Any violation stops config-deploy before it writes anything, with exit 78, one line per problem naming the key and where it was set, and a hint, for example:

Error: lib_layered_config.default_permissions.user_file: a bare integer is read as decimal (400 = 0o620); write the mode as an octal string: "0o640" (quoted) in a file, 0o640 in the environment or a runtime override (such as an application's --set) (source: env)
Hint: to deploy anyway, pass both --dir-mode and --file-mode (the built-in modes are 700 and 600 for user, 755 and 644 for app and host); --no-permissions also deploys, but leaves every mode to the umask, which can make a user file that holds secrets readable by other accounts.

The same refusal applies when a file the library reads cannot be loaded: config-deploy never falls back to the layer defaults, which may be wider than what was configured. A refused --set of the section names (source: override) and has no hint, since no option gets past it: fix or drop the --set. The files a deploy writes are never read for it, so config-deploy --force replaces a destination that carries a bad value, or does not parse at all, without further options.

Generate Example Configuration Files

Create example TOML files showing all available options with default values and documentation comments. Useful for learning the configuration structure or creating initial configuration files.

Options Reference

Option Required Description
--destination DIR Yes Directory to write example files.
--force No Overwrite existing files. Without this, existing files are skipped.

Examples

# Generate examples in a specific directory
sysdelta config-generate-examples --destination ./examples

# Overwrite existing example files
sysdelta config-generate-examples --destination ./examples --force

# Generate examples in current directory
sysdelta config-generate-examples --destination .

Generated Files

The command writes lib_layered_config’s example layout: one placeholder file per layer location (config.toml, config.d/10-override.toml, hosts/your-hostname.toml, under xdg/ and home/ or the platform’s equivalents) plus a .env.example. They show WHERE each layer reads from and hold a generic [service] placeholder, not this application’s settings. For a file holding this application’s defaults with every option documented, use config-deploy.

Runtime Overrides

Use --set to override configuration values without modifying files. This option:

Syntax

--set SECTION.KEY=VALUE
--set SECTION.SUBSECTION.KEY=VALUE

Examples

# Override a single value
sysdelta --set lib_log_rich.console_level=DEBUG config

# Override multiple values
sysdelta --set lib_log_rich.console_level=DEBUG --set lib_log_rich.console_format_preset=short config

# Override nested values
sysdelta --set lib_log_rich.payload_limits.context_max_keys=30 config

# Override with JSON arrays/objects (use single quotes around the value)
sysdelta --set example.hosts='["a.example.com:8080"]' config
sysdelta --set example.owners='["admin", "ops"]' config

# Combine with profile
sysdelta --profile production --set lib_log_rich.console_level=DEBUG config

Supported Value Types

Type Example
String --set section.key=value
Integer --set section.timeout=30
Float --set section.ratio=0.5
Boolean --set section.enabled=true or --set section.enabled=false
JSON Array --set section.hosts='["a.com", "b.com"]'
JSON Object --set section.metadata='{"key": "value"}'

Profiles

Profiles provide isolated configuration namespaces for different environments (e.g., production, staging, test).

Profile Name Requirements

Profile names are validated for security and cross-platform compatibility:

Rule Description
Maximum length 64 characters
Allowed characters ASCII letters (a-z, A-Z), digits (0-9), hyphens (-), underscores (_)
Start character Must start with a letter or digit (not - or _)
Reserved names Windows reserved names rejected: CON, PRN, AUX, NUL, COM1-COM9, LPT1-LPT9
Path safety No path separators (/, \) or traversal sequences (..)

Valid examples: production, staging-v2, test_env, dev01

Invalid examples: ../etc (path traversal), -invalid (starts with hyphen), CON (Windows reserved)

Which Layers Are Affected?

Layer Affected by Profile? Notes
defaults No Always loaded from package
app Yes Uses profile/<name>/ subdirectory
host Yes Uses profile/<name>/ subdirectory
user Yes Uses profile/<name>/ subdirectory
.env Fallback only The per-user fallback .env uses profile/<name>/; one found upward is not
env vars No Environment
CLI No Command line

Profile Path Examples

Without profile:

With profile production:

Reading Behavior

Profile directories are separate namespaces. Configuration deployed with a profile is only visible when reading with that same profile.

Command Sees app layer? Sees user layer?
config (no profile) Only if deployed without profile Only if deployed without profile
config --profile production Only if deployed with production Only if deployed with production

Example: If you deploy app with --profile production but user without a profile:

Command app layer user layer
config No Yes
config --profile production Yes No

Environment Variables

Configuration can be overridden via environment variables using two methods:

Method 1: Native lib_log_rich Variables

For logging configuration, use the native LOG_* variables (highest precedence):

LOG_CONSOLE_LEVEL=DEBUG sysdelta hello
LOG_ENABLE_GRAYLOG=true LOG_GRAYLOG_ENDPOINT="logs.example.com:12201" sysdelta hello

Method 2: Application-Prefixed Variables

For any configuration section, use the format: <PREFIX>___<SECTION>__<KEY>=value

SYSDELTA___LIB_LOG_RICH__CONSOLE_LEVEL=DEBUG sysdelta hello
SYSDELTA___EXAMPLE__HOSTS='["a.example.com:8080"]' sysdelta config --section example

Separator reference:


.env File Support

Create a .env file in your project directory for local development overrides:

# .env
LOG_CONSOLE_LEVEL=DEBUG
LOG_CONSOLE_FORMAT_PRESET=short
LOG_ENABLE_GRAYLOG=false

By default, the configuration loader reads ONE .env as its .env layer: the first one from the current directory up to the filesystem root (past any pyproject.toml or .git, so a checkout nested in another reads the outer one’s .env), or, when there is none, the per-user fallback .env (see the .env row of the layer table above).

Logging reads the LOG_* lines of that same file (the --env-file when one is given; when the configuration did not load, the first .env on the same upward walk) and copies only those into the environment, never over a variable that is already set. No other .env line reaches the environment, so a .env cannot act as the environment layer for a later configuration load.

To load a specific .env file instead, use --env-file:

# Load from an explicit path (skips upward directory search)
sysdelta --env-file /opt/myapp/config/.env config
sysdelta --env-file ./environments/staging.env config --format json

The file must exist and be readable; Click validates this before the command runs.


Default Configuration

The defaultconfig.toml and files in defaultconfig.d/ (bundled with the package) provide baseline values. These serve as the fallback when no external configuration files are deployed.


Customization Best Practices

Do NOT modify deployed configuration files directly. config-deploy --force replaces them (keeping the old file as <name>.bak).

Instead, create your own override file in the layer’s config.d/ directory using a high-numbered prefix:

# User-level customization (Linux)
~/.config/sysdelta/config.d/999-myconfig.toml

# User-level customization (macOS)
~/Library/Application Support/bitranox/sysdelta/config.d/999-myconfig.toml

# User-level customization (Windows)
%APPDATA%\bitranox\sysdelta\config.d\999-myconfig.toml

# System-wide customization (Linux)
/etc/xdg/sysdelta/config.d/999-myconfig.toml

# Host-specific customization (Linux)
/etc/xdg/sysdelta/hosts/{hostname}.d/999-myconfig.toml

A file placed directly next to config.toml (outside config.d/) is never read: each layer reads only config.toml plus its config.d/ files.

Why this works:

Example 999-myconfig.toml:

# My custom overrides - survives a redeploy

[lib_log_rich]
console_level = "DEBUG"

[example]
hosts = ["a.mycompany.com:8080"]

This approach keeps your customizations separate and safe from updates while still benefiting from new default values added in future versions.


Library Use

You can import the configuration system directly in Python:

import sysdelta as btcli

# Get the merged configuration object
config = btcli.get_config()
print(config.as_dict())

# Access specific sections
log_config = config.get("lib_log_rich", default={})
layered_config = config.get("lib_layered_config", default={})