I have a clojure project using leiningen. I want to load environment variables from a file on nrepl start. I only want this to run on repl start because these environment variables are configured for local development.
The reason I don't want to use default params in edn config is that the environment variables can contain some sensitive info (even for local development) like API TOKENS which will differ for every developer. I do not want this to be commited. There will be a file "repl_env" which will contain all the development evn variables. This file will be added to .gitignore thus making sure that these are never commited.
I see there is :init under :repl-options but I am not sure how I can leverage it to load environment variables. Any suggestions?
Loading environment variables temporarily on nrepl start up
128 Views Asked by Ashwin AtThere are 2 best solutions below
On
The repl will load the environment variables from the current shell session, what you need to do is to load all those environment variables when the user starts a new session, I'm assuming you are using a *nix based OS, you can achieve this by exporting the variables inside the *rc file.
At this point you have at least two flavors: bash or zsh, for both you use the same syntax but in different files, for zsh you can add your variables into $HOME/.zshrc:
# my awesome zshrc file
export MY_SUPER_SECRET_KEY=THISISASECRETKEY
export MY_SUPER_SECRET_VALUE=THISISASECRETVALUE
Now, every time the user starts a shell session, those values will be available for repl, of course each developer should add those values on theirs own file.
I'm using environ library, loading the library into the repl and asking to resolve those variables I got:
I guess you should have similar results using #env from aero.

Can you use System properties instead of environment variables? If so, here is how you can do it. System properties can be set using command line arguments to the JVM, so it is possible to do it using Leiningen.
Here is how I did it. I created a new project
lein new syspropdemooch edited the following files:Added the line
:profiles {:repl {:jvm-opts ["@sysprops.txt"]}}toproject.cljand set the main namespace. The string@sysprops.txtmeans that I want to read additional command line arguments from a filesysprops.txt:Created a basic
-mainfunction insrc/syspropdemo/core.clj:and also added a file
sysprops.txtin the project root that lists options to be used by the JVM:where I use the
-Doption to set a system property with keyapi_keyto have valuexyz.In addition to the
:replprofile, there are other profiles too, such as:dev,:test,:production,:uberjar.