markmark206

markmark206

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 · GitHub

Where Next?

Popular in Announcing Top

danschultzer
None of the current solutions worked well for me, so I went ahead and built a user management system from scratch. This project took far...
548 29703 241
New
ostinelli
Let’s write a database! Well not really, but I think it’s a little sad that there doesn’t seem to be a simple in-memory distributed KV da...
New
josevalim
Yes, yet another parser combinator library! Most of the parser combinators in the ecosystem are either compile-time, often using AST tra...
159 19483 141
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 52774 488
New
Qqwy
Hello everyone, I wrote a small library today called MapDiff. It returns a map listing the (smallest amount of) changes to get from map...
New
hpopp
After just over two years in development, this latest version of Pigeon is what I finally consider done in regards to my original vision ...
New
OvermindDL1
Been making an MLElixir thing (not released yet…) for fun in spare time in the past day. I’m just trying to see how much I can get an ML...
132 14085 106
New
anshuman23
Hello all, I have been working on my proposed project called Tensorflex as part of Google Summer of Code 2018.. Tensorflex can be used f...
New
tmbb
I’ve decided to create this topic to discuss optimization possibilities for something like Phoenix LiveView. I’ve created this topic unde...
144 10483 141
New
wfgilman
I’ve cleaned up and open sourced three financial libraries I was using for my company. They are bindings for the APIs of these three comp...
New

Other popular topics Top

malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
New
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 39523 209
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 52774 488
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New

We're in Beta

About us Mission Statement