Top Related Projects
A Ruby gem to load environment variables from `.env`.
Easiest way to add multi-environment yaml settings to Rails, Sinatra, Padrino and other Ruby projects.
Manage settings with Ruby on Rails
Global settings for your Rails application.
Forms made easy for Rails! It's tied to a simple DSL, with no opinion on markup.
Quick Overview
Figaro is a simple and flexible configuration management system for Ruby applications. It allows you to securely configure your app, keeping sensitive information out of version control while providing a simple interface for accessing configuration variables across different environments.
Pros
- Easy to set up and use with minimal configuration
- Supports multiple environments (development, test, production)
- Integrates well with various Ruby frameworks, including Rails
- Provides a secure way to manage sensitive configuration data
Cons
- Limited built-in encryption options
- May require additional setup for more complex configuration scenarios
- Documentation could be more comprehensive for advanced use cases
- Lacks some features found in more robust configuration management systems
Code Examples
Setting configuration values:
Figaro.env.database_url = "postgres://localhost/myapp_development"
Figaro.env.api_key = "abc123"
Accessing configuration values:
database_url = Figaro.env.database_url
api_key = Figaro.env.api_key
Using Figaro in a Rails initializer:
# config/initializers/figaro.rb
Figaro.require_keys("database_url", "api_key")
Getting Started
- Add Figaro to your Gemfile:
gem "figaro"
- Install the gem:
bundle install
- Generate the configuration file:
bundle exec figaro install
- Add your configuration to
config/application.yml
:
development:
database_url: "postgres://localhost/myapp_development"
api_key: "abc123"
production:
database_url: "postgres://user:password@host/database"
api_key: "xyz789"
- Access your configuration in your Ruby code:
database_url = Figaro.env.database_url
api_key = Figaro.env.api_key
Competitor Comparisons
A Ruby gem to load environment variables from `.env`.
Pros of dotenv
- Simpler setup and usage, requiring minimal configuration
- Supports multiple environments out of the box
- Widely adopted across various programming languages and frameworks
Cons of dotenv
- Less secure by default, as it doesn't encrypt sensitive information
- Lacks built-in features for managing complex configuration scenarios
- May require additional tools or practices for production deployments
Code Comparison
dotenv:
require 'dotenv'
Dotenv.load
puts ENV['SECRET_KEY']
Figaro:
require 'figaro'
Figaro.application = Figaro::Application.new(environment: 'development', path: 'config/application.yml')
Figaro.load
puts Figaro.env.secret_key
Both Figaro and dotenv are popular Ruby gems for managing environment variables and configuration. Figaro offers more robust security features and integrates well with Rails applications, while dotenv provides a simpler, more universal approach that can be easily adopted across different projects and languages.
Figaro encrypts sensitive information by default and offers a more structured way of managing configurations, especially in Rails applications. On the other hand, dotenv's simplicity and widespread adoption make it an attractive choice for developers working on various types of projects.
When choosing between the two, consider your project's specific requirements, security needs, and the level of complexity you're comfortable managing in your configuration setup.
Easiest way to add multi-environment yaml settings to Rails, Sinatra, Padrino and other Ruby projects.
Pros of Config
- More flexible configuration options, supporting YAML, JSON, and Ruby files
- Allows for nested configuration structures
- Provides a more robust API for accessing and manipulating configuration values
Cons of Config
- Slightly more complex setup and usage compared to Figaro's simplicity
- Lacks built-in Heroku integration, which Figaro provides out-of-the-box
- May require more configuration and customization for specific use cases
Code Comparison
Config:
Config.setup do |config|
config.const_name = 'Settings'
config.use_env = true
end
Settings.database.host
Figaro:
Figaro.require_keys("DATABASE_HOST")
ENV["DATABASE_HOST"]
Summary
Config offers more flexibility and advanced features for managing configuration in Ruby applications, while Figaro focuses on simplicity and easy integration with Heroku. Config is better suited for complex projects with nested configurations, while Figaro excels in straightforward environment variable management. The choice between the two depends on the specific needs of your project and your preferred level of configuration complexity.
Manage settings with Ruby on Rails
Pros of Rails-settings
- Supports multiple types of settings (string, integer, float, boolean, array, hash)
- Allows for default values and scoped settings (e.g., per-user settings)
- Provides a web interface for managing settings
Cons of Rails-settings
- More complex setup and configuration compared to Figaro
- Requires database migrations and model creation
- May have a steeper learning curve for beginners
Code Comparison
Rails-settings:
class Setting < RailsSettings::Base
cache_prefix { "v1" }
field :host, default: "localhost"
field :enabled, type: :boolean, default: true
end
Setting.host # => "localhost"
Setting.enabled? # => true
Figaro:
# config/application.yml
HOST: "localhost"
ENABLED: "true"
# In your application
ENV["HOST"] # => "localhost"
ENV["ENABLED"] == "true" # => true
Summary
Rails-settings offers more advanced features and flexibility for managing application settings, while Figaro provides a simpler approach focused on environment variables. Rails-settings is better suited for complex applications with dynamic settings, whereas Figaro excels in simplicity and ease of use for managing configuration variables across different environments.
Global settings for your Rails application.
Pros of rails-settings-cached
- Provides a caching mechanism for improved performance
- Supports different types of settings (global, object-specific)
- Offers a simple API for managing settings
Cons of rails-settings-cached
- Limited to Rails applications
- Requires database migrations for setup
- Less focus on environment variable management
Code Comparison
rails-settings-cached:
Setting.admin_email = 'admin@example.com'
Setting.admin_email # => 'admin@example.com'
Setting.all # => {"admin_email"=>"admin@example.com"}
Figaro:
Figaro.env.secret_key_base
Figaro.env.aws_access_key_id
Figaro.require_keys("secret_key_base", "aws_access_key_id")
Key Differences
- Figaro focuses on managing environment variables, while rails-settings-cached is designed for application settings storage
- rails-settings-cached uses the database for persistence, whereas Figaro typically uses a YAML file
- Figaro is more suitable for managing sensitive configuration data, while rails-settings-cached is better for runtime application settings
Use Cases
- Choose Figaro for managing environment-specific configuration and sensitive data
- Opt for rails-settings-cached when you need to store and retrieve application settings with caching support
Both libraries serve different purposes and can be used together in a Rails application to manage different aspects of configuration and settings.
Forms made easy for Rails! It's tied to a simple DSL, with no opinion on markup.
Pros of Simple Form
- More focused on form building and customization, offering a wide range of form helpers and input types
- Actively maintained with frequent updates and a large community
- Seamless integration with Bootstrap and other CSS frameworks
Cons of Simple Form
- Larger footprint and potentially more complex setup compared to Figaro
- May introduce unnecessary complexity for simple forms or projects not heavily focused on form building
Code Comparison
Simple Form:
<%= simple_form_for @user do |f| %>
<%= f.input :username %>
<%= f.input :email %>
<%= f.button :submit %>
<% end %>
Figaro:
# config/application.yml
SECRET_KEY: "abcdefghijklmnop"
# In your Ruby code
ENV["SECRET_KEY"]
Key Differences
Simple Form is primarily focused on simplifying and enhancing form creation in Rails applications, while Figaro is designed for managing and securing application configuration and environment variables. Simple Form offers more features related to form building and customization, whereas Figaro provides a straightforward solution for handling sensitive configuration data.
Simple Form is better suited for projects with complex form requirements, while Figaro is ideal for managing application secrets and configuration across different environments. The choice between the two depends on the specific needs of your project and whether form building or configuration management is a higher priority.
Convert
designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
ð¢ UPDATE
Figaro v2 development is underway but stalled. I need your help! Please consider sponsoring Figaro's development.
With the constraints of a family and a full-time job, even the emotional support that sponsorship provides is extremely valuable in re-energizing Figaro development. Thank you! ð
Simple, Heroku-friendly Rails app configuration using ENV
and a single YAML file
NOTE: If you're using Figaro 0.7 or prior, please refer to the appropriate documentation or upgrade to Figaro 1.0.
Why does Figaro exist?
Figaro was written to make it easy to securely configure Rails applications.
Configuration values often include sensitive information. Figaro strives to be secure by default by encouraging a convention that keeps configuration out of Git.
How does Figaro work?
Figaro is inspired by the Twelve-Factor App methodology, which states:
The twelve-factor app stores config in environment variables (often shortened to env vars or env). Env vars are easy to change between deploys without changing any code; unlike config files, there is little chance of them being checked into the code repo accidentally; and unlike custom config files, or other config mechanisms such as Java System Properties, they are a language- and OS-agnostic standard.
This is straightforward in production environments but local development environments are often shared between multiple applications, requiring multiple configurations.
Figaro parses a Git-ignored YAML file in your application and loads its values
into ENV
.
Getting Started
Add Figaro to your Gemfile and bundle install
:
gem "figaro"
Figaro installation is easy:
$ bundle exec figaro install
This creates a commented config/application.yml
file and adds it to your
.gitignore
. Add your own configuration to this file and you're done!
Example
Given the following configuration file:
# config/application.yml
pusher_app_id: "2954"
pusher_key: "7381a978f7dd7f9a1117"
pusher_secret: "abdc3b896a0ffb85d373"
You can configure Pusher in an initializer:
# config/initializers/pusher.rb
Pusher.app_id = ENV["pusher_app_id"]
Pusher.key = ENV["pusher_key"]
Pusher.secret = ENV["pusher_secret"]
Please note: ENV
is a simple key/value store. All values will be converted
to strings. Deeply nested configuration structures are not possible.
Environment-Specific Configuration
Oftentimes, local configuration values change depending on Rails environment. In such cases, you can add environment-specific values to your configuration file:
# config/application.yml
pusher_app_id: "2954"
pusher_key: "7381a978f7dd7f9a1117"
pusher_secret: "abdc3b896a0ffb85d373"
test:
pusher_app_id: "5112"
pusher_key: "ad69caf9a44dcac1fb28"
pusher_secret: "83ca7aa160fedaf3b350"
You can also nullify configuration values for a specific environment:
# config/application.yml
google_analytics_key: "UA-35722661-5"
test:
google_analytics_key: ~
Using Figaro.env
Figaro.env
is a convenience that acts as a proxy to ENV
.
In testing, it is sometimes more convenient to stub and unstub Figaro.env
than
to set and reset ENV
. Whether your application uses ENV
or Figaro.env
is
entirely a matter of personal preference.
# config/application.yml
stripe_api_key: "sk_live_dSqzdUq80sw9GWmuoI0qJ9rL"
ENV["stripe_api_key"] # => "sk_live_dSqzdUq80sw9GWmuoI0qJ9rL"
ENV.key?("stripe_api_key") # => true
ENV["google_analytics_key"] # => nil
ENV.key?("google_analytics_key") # => false
Figaro.env.stripe_api_key # => "sk_live_dSqzdUq80sw9GWmuoI0qJ9rL"
Figaro.env.stripe_api_key? # => true
Figaro.env.google_analytics_key # => nil
Figaro.env.google_analytics_key? # => false
Required Keys
If a particular configuration value is required but not set, it's appropriate to raise an error. With Figaro, you can either raise these errors proactively or lazily.
To proactively require configuration keys:
# config/initializers/figaro.rb
Figaro.require_keys("pusher_app_id", "pusher_key", "pusher_secret")
If any of the configuration keys above are not set, your application will raise an error during initialization. This method is preferred because it prevents runtime errors in a production application due to improper configuration.
To require configuration keys lazily, reference the variables via "bang" methods
on Figaro.env
:
# config/initializers/pusher.rb
Pusher.app_id = Figaro.env.pusher_app_id!
Pusher.key = Figaro.env.pusher_key!
Pusher.secret = Figaro.env.pusher_secret!
Deployment
Figaro is written with deployment in mind. In fact, Heroku's
use of ENV
for application configuration was the original inspiration for
Figaro.
Heroku
Heroku already makes setting application configuration easy:
$ heroku config:set google_analytics_key=UA-35722661-5
Using the figaro
command, you can set values from your configuration file all
at once:
$ figaro heroku:set -e production
For more information:
$ figaro help heroku:set
Other Hosts
If you're not deploying to Heroku, you have two options:
- Generate a remote configuration file
- Set
ENV
variables directly
Generating a remote configuration file is preferred because of:
- familiarity â Management of
config/application.yml
is like that ofconfig/database.yml
. - isolation â Multiple applications on the same server will not produce configuration key collisions.
Is Figaro like dotenv?
Yes. Kind of.
Figaro and dotenv were written around the same time to solve similar problems.
Similarities
- Both libraries are useful for Ruby application configuration.
- Both are popular and well maintained.
- Both are inspired by Twelve-Factor App's concept of proper configuration.
- Both store configuration values in
ENV
.
Differences
- Configuration File
- Figaro expects a single file.
- Dotenv supports separate files for each environment.
- Configuration File Format
- Figaro expects YAML containing key/value pairs.
- Dotenv convention is a collection of
KEY=VALUE
pairs.
- Security vs. Convenience
- Figaro convention is to never commit configuration files.
- Dotenv encourages committing configuration files containing development values.
- Framework Focus
- Figaro was written with a focus on Rails development and conventions.
- Dotenv was written to accommodate any type of Ruby application.
Either library may suit your configuration needs. It often boils down to personal preference.
Is application.yml like secrets.yml?
Yes. Kind of.
Rails 4.1 introduced the secrets.yml
convention for Rails application
configuration. Figaro predated the Rails 4.1 release by two years.
Similarities
- Both are useful for Rails application configuration.
- Both are popular and well maintained.
- Both expect a single YAML file.
Differences
- Configuration Access
- Figaro stores configuration values in
ENV
. - Rails stores configuration values in
Rails.application.secrets
.
- Figaro stores configuration values in
- Configuration File Structure
- Figaro expects YAML containing key/value string pairs.
- Secrets may contain nested structures with rich objects.
- Security vs. Convenience
- Figaro convention is to never commit configuration files.
- Secrets are committed by default.
- Consistency
- Figaro uses
ENV
for configuration in every environment. - Secrets encourage using
ENV
for production only.
- Figaro uses
- Approach
- Figaro is inspired by Twelve-Factor App's concept of proper configuration.
- Secrets are⦠not.
The emergence of a configuration convention for Rails is an important step, but
as long as the last three differences above exist, Figaro will continue to be
developed as a more secure, more consistent, and more standards-compliant
alternative to secrets.yml
.
For more information, read the original The Marriage of Figaro⦠and Rails blog post.
How do I upgrade to Figaro 1.0?
In most cases, upgrading from Figaro 0.7 to 1.0 is painless. The format
expectations for application.yml
are the same in 1.0 and values from
application.yml
are loaded into ENV
as they were in 0.7.
However, there are breaking changes:
Figaro.env
In Figaro 0.7, calling a method on the Figaro.env
proxy would raise an error
if a corresponding key were not set in ENV
.
In Figaro 1.0, calling a method on Figaro.env
corresponding to an unset key
will return nil
. To emulate the behavior of Figaro 0.7, use "bang" methods as
described in the Required Keys section.
NOTE: In Figaro 0.7, Figaro.env
inherited from Hash
but in Figaro 1.0,
hash access has been removed.
Heroku Configuration
In Figaro 0.7, a Rake task existed to set remote Heroku configuration according
to values in application.yml
.
In Figaro 1.0, the Rake task was replaced by a command for the figaro
executable:
$ figaro heroku:set -e production
For more information:
$ figaro help heroku:set
NOTE: The environment option is required for the heroku:set
command. The
Rake task in Figaro 0.7 used the default of "development" if unspecified.
Spring Configuration
If you're using Spring, either stop
or add config/application.yml
to the watch list:
# config/spring.rb
%w(
...
config/application.yml
).each { |path| Spring.watch(path) }
Who wrote Figaro?
My name is Steve Richert and I wrote Figaro in March, 2012 with overwhelming encouragement from my employer, Collective Idea. Figaro has improved very much since then, thanks entirely to inspiration and contribution from developers everywhere.
Thank you!
How can I help?
Figaro is open source and contributions from the community are encouraged! No contribution is too small.
See Figaro's contribution guidelines for more information.
Top Related Projects
A Ruby gem to load environment variables from `.env`.
Easiest way to add multi-environment yaml settings to Rails, Sinatra, Padrino and other Ruby projects.
Manage settings with Ruby on Rails
Global settings for your Rails application.
Forms made easy for Rails! It's tied to a simple DSL, with no opinion on markup.
Convert
designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot