How to reference environmental variable or home dir in project.clj

1.4k views Asked by At

Is there a way to programmatically insert the name of my home directory into file paths in Leiningen's project.clj?

I run a Leiningen project on different machines, where I have different home directories. The project uses jar files that are not managed by Maven; I download 'em and put them in a directory that's relative to my home directory, or copy them into the Leiningen project. The second option works but is undesirable.

An easy way to use the first option--keeping the jar files somewhere else--is to add a soft link to the "somewhere else" directory in my Leiningen directory. That works, but the link has to be different on each machine, so I can't include the link file in the git repository, and I'd rather include everything in the git repo.

Isn't there a way to use environmental variables, or otherwise refer to my home directory, in my project.clj file? I've gone through the sample project file and have not found a solution, so far.

I thought I could just construct path strings at run time--what's in project.clj is just Clojure code, after all. Since hard-coding my home directory into project.clj works without any trouble:

  :resource-paths [/Users/myhomedir/dist/mason/jar/mason.19.jar")]

I figured I could do this:

:resource-paths [(str (System/getenv "HOME") "/dist/mason/jar/mason.19.jar")]

However, Leiningen doesn't like this at all:

java.lang.IllegalArgumentException: No implementation of method: :as-file of protocol: #'clojure.java.io/Coercions found for class: clojure.lang.PersistentList

Changing [...] to (vector ...) gives the same error but with 'clojure.lang.Symbol` at the end.

1

There are 1 answers

1
nberger On BEST ANSWER

In the name of repeatability you'd better have the jar installed in the local maven repository and have it added to the project.clj :dependencies so it's fetched from there. But you said those jars won't be managed by maven, so here we go:

defproject is a macro and it allows to use unquoting to do arbitrary evaluation. It does it by calling the internal fn unquote-project. So you can do the following:

:resource-paths [~(str (System/getenv "HOME") "/dist/mason/jar/mason.19.jar")]