SimpleFeatureFlags – turn features on in some environments, via configuration

simple_feature_flags is a tiny package that lets you turn features on or off based on which environment (e.g. localhost, staging, production) your app is running in — without needing a database or UI, just using configuration.

See the documentation (Simple Feature Flags v0.1.4 — Documentation) for more details, but here is a summary / example:

1. Install

Add the package to your dependencies:

mix.exs:

defp deps do
  [
    {:simple_feature_flags, "~> 0.1"}
  ]
end

2. Is the feature enabled?

In your code, check if a feature (e.g. :new_algorithm) is enabled with:

 SimpleFeatureFlags.enabled?(:new_algorithm)

Example:

  def compute_pi() do
    if SimpleFeatureFlags.enabled?(:new_algorithm) do
      # Use the new, exciting algorithm.
      compute_pi_new_algorithm()
    else
      # Use the old, boring algorithm.
      3.14
    end
  end

The function returns true or false, based on whether :new_algorithm is enabled in the current environment.

3. Configuration

In your configuration file (e.g. config/runtime.exs), tell the package:

  • a. in which environment the code is currently running, and
  • b. in which environments each feature (e.g. :new_algorithm) is enabled.

Example:

config/runtime.exs

# Determine the environment in which the code is currently running in (e.g. `:localhost`, `:staging`, `:production`).
# This example uses a dedicated environment variable, set to an appropriate value.
# Note: in a Phoenix service, you might be able deduce the deployment environment from `PHX_HOST` or some such.
current_deployment_environment =
  System.get_env("DEPLOYMENT_ENVIRONMENT")
  |> case do
    nil -> raise "DEPLOYMENT_ENVIRONMENT is not defined"
    env -> String.to_atom(env)
  end

# Tell the package the name of the environment where the code is running,
# and the environments in which the feature is enabled.
config :simple_feature_flags, :flags, %{
  current_deployment_environment: current_deployment_environment,
  features: %{
    new_algorithm: %{enabled_in: [:localhost, :staging]},
    new_ui: %{enabled_in: [:staging]}
  }
}

4. Change where features are enabled

To change where features are enabled, just update your configs (e.g. add :production to the enabled_in: list for :new_algorithm) and redeploy. That’s it – no database, no UI, no runtime toggle logic.

5. This is it!

There are a couple of other (optional) ergonomics features (check the docs for known_deployment_environments and current_configuration_to_string()), but other than that, this is it!

The package is intentionally simple – its interface is just enabled?() and current_configuration_to_string() – but I have been using this package extensively and I found it quite useful for quickly toggling features, with minimal overhead, and so I thought I’d share it here, in case it saves someone time.

If you have questions or requests, please let me know here or via an issue in the github repo.

Thank you!

Here is the package on hexdocs: Simple Feature Flags v0.1.4 — Documentation
And here is the github repo: GitHub - shipworthy/simple_feature_flags: configuration-based feature flags for Elixir applications

3 Likes