Skip to content

config: add environment variable macros#466

Merged
mostlygeek merged 2 commits intomainfrom
env-macros-issue462
Jan 17, 2026
Merged

config: add environment variable macros#466
mostlygeek merged 2 commits intomainfrom
env-macros-issue462

Conversation

@mostlygeek
Copy link
Owner

@mostlygeek mostlygeek commented Jan 17, 2026

Summary

  • Add ${env.VAR_NAME} syntax to pull values from environment variables
  • Env macros processed before regular macros (allows macros to reference env vars)
  • Returns error if referenced env var is not set

fixes #462

Summary by CodeRabbit

  • New Features

    • Added environment variable substitution using ${env.VAR} across configuration, including model fields and metadata.
    • Substitutions run before other macro processing and now surface explicit, field- and model-specific errors for unset env vars.
    • Updated example config documentation showing ${env.VAR} usage.
  • Tests

    • Added comprehensive tests covering env macro expansion, interactions with other macros, and error scenarios.

✏️ Tip: You can customize this high-level summary in your review settings.

Add support for ${env.VAR_NAME} syntax to pull values from system
environment variables during config loading.

- env macros processed before regular macros (allows macros to reference env vars)
- works in cmd, cmdStop, proxy, checkEndpoint, filters.stripParams, metadata
- returns error if env var is not set
- add comprehensive tests

fixes #462
@coderabbitai
Copy link

coderabbitai bot commented Jan 17, 2026

Walkthrough

Adds ${env.VAR} environment-variable macro substitution to the config loader. Env macros are expanded (and validated) in global macros, model-level macros, model fields (Cmd, CmdStop, Proxy, CheckEndpoint, Filters.StripParams), and metadata before existing macro substitution passes.

Changes

Cohort / File(s) Summary
Core implementation
proxy/config/config.go
Introduced envMacroRegex, substituteEnvMacros() and substituteEnvMacrosInValue(); added pre-substitution of ${env.VAR} for global and model macros, model fields, and metadata; extended validation to error on unset env vars and report locations; integrated env substitution before LIFO macro pass.
Tests
proxy/config/config_test.go
Added TestConfig_EnvMacros with subtests covering env substitution across fields, global/model macros, metadata, filters, cmdStop, multi-var strings, interactions with regular macros, and error cases for missing env vars.
Documentation/example
config.example.yaml
Documented ${env.VAR_NAME} usage in global macros and added example models_dir: "${env.HOME}/models".

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Possibly related PRs

Suggested labels

enhancement, configuration

🚥 Pre-merge checks | ✅ 4 | ❌ 1
❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 57.14% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately summarizes the main change: adding environment variable macro support to the config system.
Linked Issues check ✅ Passed Implementation fully addresses issue #462 requirements by adding ${env.VAR} syntax, processing env macros first, and validating required variables.
Out of Scope Changes check ✅ Passed All changes directly support the stated objective of adding environment variable macro expansion; no unrelated or out-of-scope modifications detected.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Fix all issues with AI agents
In `@proxy/config/config.go`:
- Around line 284-287: The Filters.SetParams map is not being run through
env-macro substitution like Metadata is; update the model config loading to call
substituteEnvMacrosInValue (or substituteEnvMacros) on
modelConfig.Filters.SetParams and handle/return any error the same way as for
Metadata (wrap with fmt.Errorf including modelId), so SetParams supports
recursive env variable macros just like Metadata (or, if intentional, add a
comment documenting the exclusion).

Comment on lines +284 to +287
modelConfig.Filters.StripParams, err = substituteEnvMacros(modelConfig.Filters.StripParams)
if err != nil {
return Config{}, fmt.Errorf("model %s filters.stripParams: %w", modelId, err)
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
# Search for setParams usage across the codebase
rg -i "setparams" --type yaml --type go -C 2

Repository: mostlygeek/llama-swap

Length of output: 9852


🏁 Script executed:

#!/bin/bash
# Also check the Filters struct definition to understand both fields
rg -n "type Filters struct" -A 10 --type go

Repository: mostlygeek/llama-swap

Length of output: 792


🏁 Script executed:

#!/bin/bash
# Look for any references to SetParams in tests or config examples
rg -n "SetParams" --type go --type yaml --type json -C 3

Repository: mostlygeek/llama-swap

Length of output: 5780


🏁 Script executed:

#!/bin/bash
# Search for the env macro substitution implementation pattern around lines 267-307
sed -n '260,310p' proxy/config/config.go

Repository: mostlygeek/llama-swap

Length of output: 1939


🏁 Script executed:

#!/bin/bash
# Look for any comments or TODOs explaining SetParams handling
rg -n "SetParams" proxy/config/config.go -B 2 -A 2

Repository: mostlygeek/llama-swap

Length of output: 47


SetParams should support environment variable macros for consistency with Metadata.

Both Filters.SetParams and model-level Metadata are map[string]any structures that can contain sensitive string values. The config loading code applies recursive env macro substitution to Metadata (lines 293-299) but skips SetParams entirely, creating an inconsistency. Since the infrastructure (substituteEnvMacrosInValue) already handles recursive substitution of map[string]any, consider extending it to SetParams to allow users to inject environment variables into parameter values (e.g., API keys, provider-specific settings). If intentionally excluded, document this limitation.

🤖 Prompt for AI Agents
In `@proxy/config/config.go` around lines 284 - 287, The Filters.SetParams map is
not being run through env-macro substitution like Metadata is; update the model
config loading to call substituteEnvMacrosInValue (or substituteEnvMacros) on
modelConfig.Filters.SetParams and handle/return any error the same way as for
Metadata (wrap with fmt.Errorf including modelId), so SetParams supports
recursive env variable macros just like Metadata (or, if intentional, add a
comment documenting the exclusion).

@mostlygeek mostlygeek merged commit 124007c into main Jan 17, 2026
3 checks passed
@mostlygeek mostlygeek deleted the env-macros-issue462 branch January 17, 2026 06:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Feature request: Support environment variable expansion for apiKeys

1 participant