Skip to Content Skip to Search

Provide an interface for accessing configuration options stored in a .env file. Keys are accepted as symbols and turned into upcased strings. Nesting is provided by double underscores.

This interface mirrors what is used for ActiveSupport::EnvConfiguration and ActiveSupport::EncryptedConfiguration and thus allows all three to serve as interchangeable backends for Rails.app.creds.

The .env file format supports:

  • Lines with KEY=value pairs

  • Comments starting with #

  • Empty lines (ignored)

  • Quoted values (single or double quotes)

  • Variable interpolation with ${VAR} syntax

  • Command execution with $(command) syntax

The command execution allows for easy integration with third-party credential providers, like 1password:

DB_HOST=$(op read op://Vault/item/value --account=MyAccount)

When used inside Rails, the default path for the .env file will be ‘Rails.root.join(“.env”)`. Otherwise it must be passed in as path.

Examples:

require(:db_host)                                   # => value of DB_HOST from .env
require(:database, :host)                           # => value of DATABASE__HOST from .env
option(:debug)                                      # => value of DEBUG from .env or nil if missing
option(:debug, default: "true")                     # => value of DEBUG from .env or "true" if not found
option(:database, :host, default: -> { "missing" }) # => value of DATABASE__HOST from .env or "missing" if not found
Methods
N
R

Class Public methods

new(path)

# File activesupport/lib/active_support/dot_env_configuration.rb, line 35
def initialize(path)
  @path = path
  reload
end

Instance Public methods

reload()

Reload the cached .env values in case the file changed during runtime.

# File activesupport/lib/active_support/dot_env_configuration.rb, line 41
def reload
  @envs = parse_env_file
end