Clojure Environment

This notebook describes and creates the default Clojure environment in Nextjournal. Check out the showcase if you want to see what the environment contains. To see how it’s built, see setup.

If you don't specify a version, Clojure version "1.10.3" is used. However, it's easy to run Clojure version "1.9.0" by including it as a dependency in the runtime's deps.edn.

Showcase

Package Management

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.

{:deps
 {org.clojure/clojure {:mvn/version "1.10.3"}
  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.

(clojure-version)
0.0s

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

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.

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

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

(clojure-version)
0.1s

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 'clojure.tools.deps.alpha.repl)
2.6s
(add-lib 'org.clojure/core.async {:mvn/version "1.3.610"})
1.7s

Require core.async and test.

(require '[clojure.core.async :as async])
(async/timeout 100)
8.1s

Plotting

For plotting, Nextjournal comes with built-in support for Plotly and Vega Lite.

Plotly

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"}}}
plotly
0.4s

Vega Lite

Generate some random data for Vega-Lite and plot it by selecting the vega-lite viewer via the :nextjournal/viewer metadata attribute.

^{:nextjournal/viewer "vega-lite"}
{:width 650
 :height 400
 :data
 {:url "https://vega.github.io/vega-datasets/data/us-10m.json"
  :format
  {:type "topojson" :feature "counties"}}
 :transform
 [{:lookup "id"
   :from
   {:data {:url "https://vega.github.io/vega-datasets/data/unemployment.tsv"}
    :key "id"
    :fields ["rate"]}}]
 :projection {:type "albersUsa"}
 :mark "geoshape"
 :encoding
 {:color {:field "rate" :type "quantitative"}}}
0.1s

Setup

Build the default Clojure environment

Install Clojure CLI

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

Clojure
Clojure (Bash) using
exporting environment
Type: Nextjournal
Environment:
Machine Type:
Environment Variables:
CLOJURE_VERSION1.10.3.967
Download this environment as a Docker image from:

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

apt-get -qq update
DEBIAN_FRONTEND=noninteractive apt-get install --no-install-recommends \
  rlwrap
apt-get clean
rm -rf /var/lib/apt/lists/*
9.4s

Next, download and run the Clojure CLI installer.

echo ${CLOJURE_VERSION}
0.9s
wget -q --show-progress --progress=bar:force -P . \
  https://download.clojure.org/install/linux-install-${CLOJURE_VERSION}.sh
1.0s
/bin/bash ./linux-install-${CLOJURE_VERSION}.sh
1.5s
rm ./linux-install-${CLOJURE_VERSION}.sh
0.6s

Test clojure from the command line.

clojure -e "(clojure-version)"
7.9s

Add Some Tools and Dependencies

Install leiningen.

cd /usr/local/bin
wget --progress=bar:force \
  https://raw.githubusercontent.com/technomancy/leiningen/stable/bin/lein
chmod +x lein
./lein
8.8s

Install tools.deps.alpha to get add-lib to dynamically add libraries, and compliment for autocompletion.

clojure -Sdeps '{ :deps {
  org.clojure/tools.deps.alpha {:mvn/version "0.12.1036"}
  compliment/compliment {:mvn/version "0.3.11"}
}}' -e "(clojure-version)"
12.8s

Install the Apache Arrow and Parquet format libraries.

clojure  -Sdeps '{ :deps {
  org.apache.arrow/arrow-format {:mvn/version "0.15.1"}
  org.apache.arrow/arrow-plasma {:mvn/version "0.15.1"}
  org.apache.parquet/parquet-format {:mvn/version "2.7.0"}
}}' -Stree
5.5s

Pre-install Clojure 1.9.0.

clojure  -Sdeps '{ :deps {
  org.clojure/clojure {:mvn/version "1.9.0"}
}}' -Stree
5.6s

Check size.

du -hsx /
6.2s
Runtimes (3)