Search code examples
clojurebuildleiningenringenviron

Correct usage of Environ library in profiles


I have a question regarding of the usage of profiles and environment variables. Is it possible to export variables with environ depending on the which profile is set? This is what I have tried (partial project.clj)

  :env {:time-to-wait-for-response "72" ; in hours
        :crm-address "https://app.onepagecrm.com/api/v3/"}
:profiles
  {:uberjar {:omit-source true
             :aot :all}
   :uberwar {:omit-source true
             :aot :all}
   :prod {:ring {:open-browser? false 
                 :stacktraces? false 
                 :auto-reload? false}
         {:env {:prod? true
                :db-user "mailer"
                :db-password "" }}
   :dev  {:env {:db-user "mailer" 
                :db-password ""}

          :dependencies [[ring-mock "0.1.5"]
                        [ring/ring-devel "1.3.1"]
                        [midje "1.6.3"]]
          :ring {:open-browser? false}
          :plugins [[lein-midje "3.2.1"]]}})

However if I build the WAR file with lein ring uberwar(even with with-profile dev) and deploy it to Tomcat I will get db-user as nil (called with (env :db-user). However lein ring server correctly uses dev profile so it works. Is there something wrong I am doing? Or environ is not supposed to be used this way?


Solution

  • This reason it is not working as you expect is because you're only setting those variables when the profile is active. Once you have built a WAR file and deployed it, lein is no longer in the picture. You'll need to load these variables in a different way. If you want to stick with environ, the two options seem to be as Java system properties or environment variables.