Parameterizing project.clj

687 views
Skip to first unread message

Ulrik Sandberg

unread,
May 23, 2011, 7:31:47 AM5/23/11
to Clojure
How can I parameterize stuff in Leiningen's project.clj? For example,
I don't want to put my AWS credentials inside the project file:

...
:aws {:access-key "XXXXXXXXXXXXXXXXXX"
:secret-key "YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY"}

but instead use some kind of property names that refer to environment
variables or something:

...
:aws {:access-key "${aws.access.key}"
:secret-key "${aws.secret.key}"}

Rasmus Svensson

unread,
May 23, 2011, 9:19:25 AM5/23/11
to clo...@googlegroups.com
2011/5/23 Ulrik Sandberg <ulrik.s...@gmail.com>:

Actually, the project.clj file is evaluated by Leiningen using the
ordinary Clojure evaluator. This means that you can put arbitrary code
at the top level of the file to, for example, extract the keys from
environment variables. Any dev-dependencies will be available in this
Leiningen JVM instance too.

'defproject' is a macro, so its contents is not evaluated the usual
way. But it does have a helpful feature: Unquoted forms will be
evaluated with normal Clojure rules, so it's possible to do something
like this:

(def access-key ...)

(def secret-key ...)

(defproject foo 1.0.0
...the usual stuff...
:aws {:access-key ~access-key
:secret-key ~secret-key}

You can of course write the whole expressions directly after the
tildes, but I wanted to demonstrate the possibility of using def here.

Hope this helps!
// Rasmus Svensson (raek)

Phil Hagelberg

unread,
May 23, 2011, 6:44:01 PM5/23/11
to Clojure
On May 23, 6:19 am, Rasmus Svensson <r...@lysator.liu.se> wrote:
>     (defproject foo 1.0.0
>       ...the usual stuff...
>       :aws {:access-key ~access-key
>                :secret-key ~secret-key}

This is good advice, but you can also use leiningen.core/user-
settings, which returns a map of user-level configuration. This should
be set in ~/.lein/init.clj using (def settings {:aws {:access-key
"[...]"}})

-Phil

Ulrik Sandberg

unread,
May 24, 2011, 4:23:15 AM5/24/11
to Clojure
Brilliant. Two good solutions. Thanks.
Reply all
Reply to author
Forward
0 new messages