3

How do I get the environment name when using environ in Clojure? I mean, :dev, :test, etc. The reason for wanting this is to pass it to Yeller so when it displays errors it can tell me which environment they happened into. Errors in staging are treated differently than errors in production.

1
  • 1
    What is the use case here? If it is to take different actions depending on the environment, perhaps adding specific items into the environment (e.g. :database-url, :nrepl-port, etc) may address the issue better. You can also add a key with the environment name into the environment itself. Commented Oct 22, 2015 at 19:31

2 Answers 2

2

Environ only provides access to environment variables, you need to set them yourself. You can use lein-environ to set environment variables in your project.clj in different profiles. These profiles will be picked by leiningen and merged together, which you can then access from your code. For example, we have:

 :profiles {:dev {:resource-paths ["test-resources"] :env {:environment "development" :db-host "localhost" :port 5000}}} 

In production we provide actual environment variables instead.

Sign up to request clarification or add additional context in comments.

1 Comment

I ended up doing something like this.
1

Just the same as you want to "Tell, don't ask" in your code, you need to just use the config options and let environ figure out what the right one is based on the environment. In the rare event that you actually need the environment name itself just put it in the :env map for each environment.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.