The Nextjournal Clojure Environment

This notebook builds a reusable environment for Clojure runtimes, based on the GraalVM environment. Nextjournal's default Clojure environment provides Clojure version . However, it's easy to run Clojure version by including it as a dependency in the runtime's deps.edn.

Learn more about environments on Nextjournal.

You can quickly make use of the Clojure environment by remixing the Nextjournal Clojure template, or use these environments with any existing runtime by following these steps:

  • Activate the runtime settings in the sidebar.
  • Bring up the Environments dropdown.
  • Select Import environment… at the bottom of the list.
  • Search for this notebook, nextjournal/clojure-environment.
  • Select it, to list all environments within.
  • Select the desired environment.

1. Showcase

1.1. Package Management

1.1.1. At Boot via deps.edn

Working with deps.edn is as easy as creating a Code Listing and writing a typical edn configuration map with a top-level key for :deps.

 {org.clojure/clojure {:mvn/version "1.10.1"}
  compliment {:mvn/version "0.3.9"}}}
Extensible Data Notation

After mounting the Code Listing (here called deps.edn) and restarting the runtime, the dependencies will be available.


For details on how to mount deps.edn and restart a runtime, see Clojure Dependencies.

1.1.2. At Runtime via tools.deps/add-lib

add-lib, part of tools.deps.alpha, dynamically adds libraries to a running REPL session.

The following deps.edn configuration map contains two of the three possible coordinate types: Maven (clojure) and git (tools.deps.alpha); local is the unused third.

  { org.clojure/clojure {:mvn/version "1.9.0"}
    org.clojure/tools.deps.alpha {
      :git/url ""
      :sha "d0b33e0d346736aa985c150145b332f97b92135e"}}}
Extensible Data Notation

Note the use of Clojure 1.9 in this example rather than the Nextjournal default of 1.10.


Load tools.deps.alpha and then use add-lib to add core.async on the fly. Note core.async was not included in deps.edn.

(use '
(add-lib 'org.clojure/core.async {:mvn/version "0.4.490"})

Require core.async and test.

(require '[clojure.core.async :as async])
(async/timeout 100)
Vector(4) [clojure.core.async.impl.channels.ManyToManyChannel, "0x58ed703c", "clojure.core.async.impl.channels.ManyToManyChannel@58ed703c", Map]

1.2. Plotting

1.2.1. Basic: Using Data From Clojure

Generate some random data for plotly and plot it by selecting the plotly viewer via the :nextjournal/viewer metadata attribute.

(defn get-coordinates [y] 
	{:x '(0 10 20 30 40) 
	 :y (take 5 (repeatedly #(rand-int y)))
   :type "scatter"
   :text ["one" "two" "three"]})

^{:nextjournal/viewer :plotly}
{:data [(conj (get-coordinates 35) {:name "The Federation"}) 
        (conj (get-coordinates 35) {:name "The Empire"})]
 :layout {:autosize false :width 600 :height 500 
          :xaxis1 {:title "year"} 
          :yaxis1 {:title "revenue"}}}

ClojureScript support is currently experimental. A list of features and caveats is available in the ClojureScript template.

1.2.2. Advanced: Using Data From Datomic

Datomic queries return data that is simple to work with in Clojure and ClojureScript. This section is a brief demonstration.

The following Clojure runtimes transclude a complete Datomic environment and dataset. For a deeper understanding of how to setup Datomic Free with Nextjournal, please read this Datomic article. Setup

Start the transactor.

/usr/bin/nohup /datomic-free/bin/transactor /datomic-free/config/samples/ &> /datomic-free.log & sleep 1
tail /datomic-free.log

Load a subset of the MusicBrainz dataset. In this case, music-related information from 1968 to 1973.

/datomic-free/bin/datomic restore-db file:/mbrainz-1968-1973 datomic:free://localhost:4334/mbrainz-1968-1973

Require Datomic and get a value of the database from the connection, defined by (d/connect uri). The d/db API returns the latest db value from the specified connection.

(require '[datomic.api :as d])

(def uri "datomic:free://localhost:4334/mbrainz-1968-1973")
(def conn (d/connect uri))

(def db (d/db conn))
user/db Query

The query, written in Datalog. It is looking for every John Lennon album with a release date from 1968 to 1973.

(def rules
  '[;; Given ?t bound to track entity-ids, binds ?r to the corresponding
    ;; set of album release entity-ids
 [(track-release ?t ?r)
  [?m :medium/tracks ?t]
  [?r :release/media ?m]]])

(def albums-year 
  (into [] (d/q '[:find ?album ?year
 :in $ % ?artist-name
 [?a :artist/name   ?artist-name]
 [?t :track/artists ?a]
 [?t :track/name    ?title]
 (track-release ?t ?r)
 [?r :release/name  ?album]
 [?r :release/year  ?year]]
     db rules "John Lennon")))

Vector(16) [Vector(2), Vector(2), Vector(2), Vector(2), Vector(2), Vector(2), Vector(2), Vector(2), Vector(2), Vector(2), Vector(2), Vector(2), Vector(2), Vector(2), Vector(2), Vector(2)] Plot

First, collect albums by year. This will make them easy to count and display later.

(defn add-album-to-year [m year title]
  (if (contains? m year) 
    (assoc m year (conj (year m) title)) 
    (assoc m year [title])))

(defn albums-by-year [albums-year m]
  (if-let [album (first albums-year)]
    (let [[title year] album
           sorted-albums (add-album-to-year m (keyword (str year)) title)]
	    (albums-by-year (rest albums-year) sorted-albums))

(albums-by-year albums-year {})
Map {:1971: Vector(4), :1969: Vector(3), :1973: Vector(3), :1972: Vector(3), :1970: Vector(3)}

Second, provide the ploty with the :data and :layout information and plot it by setting the {:nextjournal/viewer :plotly} metadata.

(let [final-sort (albums-by-year albums-year {})]
  ^{:nextjournal/viewer :plotly}
  {:data [{:x (keys final-sort)
           :y (map #(count (second %)) (albums-by-year albums-year {}))
           :type "bar"}]
   :layout {:autosize false :width 600 :height 500
          :type "bar"
          :xaxis1 {:title "Year"} 
          :yaxis1 {:title "# of Albums"}}})
Loading viewer…

2. Setup

2.1. Build Clojure

Now for the Clojure installation. Note that the stable Clojure version is set as an environment variable on the runtime.

Clojure (Bash)
exporting environment
Type: Nextjournal
Environment Variables:
Download this environment as a Docker image from:

Install the one missing dependency, rlwrap, as well as git.

apt-get -qq update
apt-get install --no-install-recommends \
apt-get clean
rm -rf /var/lib/apt/lists/*

Next, download and run the Clojure installer.

wget --progress=bar:force \${CLOJURE_VERSION}.sh 
/bin/bash linux-install-${CLOJURE_VERSION}.sh
rm ./linux-install-${CLOJURE_VERSION}.sh

Test clojure from the command line.

clojure -e "(clojure-version)"

2.2. Install Useful Dependencies

Install leiningen.

cd /usr/local/bin
wget --progress=bar:force \
chmod +x lein

Install tools.deps.alpha to get add-lib to dynamically add libraries.

clojure -Sdeps '{ :deps {
  org.clojure/tools.deps.alpha {:mvn/version "0.7.516"}
  compliment {:mvn/version "0.3.9"}
}}' -e "(clojure-version)"
clojure  -Sdeps '{ :deps {
  {:git/url ""
   :sha "d0b33e0d346736aa985c150145b332f97b92135e"}
  compliment {:mvn/version "0.3.9"}
}}' -Stree
clojure  -Sdeps '{ :deps {
  org.clojure/clojure {:mvn/version "1.9.0"}
}}' -Stree