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

Where Next?

Popular in Libraries Top

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 13487 106
New
cjen07
parameterized pipe in elixir: |n> edit: negative index in |n> and mixed usage with |> are supported example: use ParamP...
New
mplatts
With HEEX released we decided to start a components library using Tailwind CSS - check it out here: Petal Components. We also have a boi...
New
riverrun
I’ve just released version 3 of Comeonin, a password hashing library. The following small changes have been made: changes to the NIF c...
New
woutdp
Hi! I wanted to introduce my latest project LiveSvelte. It allows you to render Svelte inside LiveView with end-to-end reactivity. It’s ...
New
woylie
Flop is an Elixir library that applies filtering, ordering and pagination parameters to your Ecto queries. offset-based pagination with...
New
engineeringdept
I’ve just released the first version of Snap, an Elasticsearch client. It borrows ideas about application structure and process managemen...
New
mischov
import Meeseeks.CSS html = HTTPoison.get!("https://news.ycombinator.com/").body for story <- Meeseeks.all(html, css("tr.athing")) do...
New
maltoe
Hello! Came here to announce ChromicPDF, a pet project PDF generator I’ve been working on for the past few months. Why another PDF gener...
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

Other popular topics Top

shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
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
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New

Sub Categories:

We're in Beta

About us Mission Statement