about summary refs log tree commit diff
path: root/nixpkgs/pkgs/development/interpreters/clojurescript/lumo/default.nix
blob: 16f3e7f73e205f13c214bd1964b26a39c83f7b9c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
{ stdenv, lib, fetchurl, clojure,
  nodejs, jre, unzip,  nodePackages,
  python, openssl, pkgs }:

let # packageJSON=./package.json;
    version = "1.9.0";
    nodeVersion = "10.9.0";
    nodeSources = fetchurl {
      url="https://nodejs.org/dist/v${nodeVersion}/node-v${nodeVersion}.tar.gz";
      sha256="0wgawq3wzw07pir73bxz13dggcc1fj0538y7y69n3cc0a2kiplqy";
    };
    lumo-internal-classpath = "LUMO__INTERNAL__CLASSPATH";

    # as found in cljs/snapshot/lumo/repl.cljs
    requireDeps = '' \
      cljs.analyzer \
      cljs.compiler \
      cljs.env \
      cljs.js \
      cljs.reader \
      cljs.repl \
      cljs.source-map \
      cljs.source-map.base64 \
      cljs.source-map.base64-vlq \
      cljs.spec.alpha \
      cljs.spec.gen.alpha \
      cljs.tagged-literals \
      cljs.tools.reader \
      cljs.tools.reader.reader-types \
      cljs.tools.reader.impl.commons \
      cljs.tools.reader.impl.utils \
      clojure.core.rrb-vector \
      clojure.core.rrb-vector.interop \
      clojure.core.rrb-vector.nodes \
      clojure.core.rrb-vector.protocols \
      clojure.core.rrb-vector.rrbt \
      clojure.core.rrb-vector.transients \
      clojure.core.rrb-vector.trees \
      clojure.string \
      clojure.set \
      clojure.walk \
      cognitect.transit \
      fipp.visit \
      fipp.engine \
      fipp.deque \
      lazy-map.core \
      lumo.pprint.data \
      lumo.repl \
      lumo.repl-resources \
      lumo.js-deps \
      lumo.common '';

    compileClojurescript = (simple: ''
      (require '[cljs.build.api :as cljs])
      (cljs/build \"src/cljs/snapshot\"
        {:optimizations      ${if simple then ":simple" else ":none"}
         :main               'lumo.core
         :cache-analysis     true
         :source-map         false
         :dump-core          false
         :static-fns         true
         :optimize-constants false
         :npm-deps           false
         :verbose            true
         :closure-defines    {'cljs.core/*target*       \"nodejs\"
                              'lumo.core/*lumo-version* \"${version}\"}
         :compiler-stats     true
         :process-shim       false
         :fn-invoke-direct   true
         :parallel-build     false
         :browser-repl       false
         :target             :nodejs
         :hashbang           false
         ;; :libs               [ \"src/cljs/bundled\" \"src/js\" ]
         :output-dir         ${if simple
                                    then ''\"cljstmp\"''
                                  else ''\"target\"''}
         :output-to          ${if simple
                                    then ''\"cljstmp/main.js\"''
                                  else ''\"target/deleteme.js\"'' }})
    '');


    cacheToJsons = ''
      (import [java.io ByteArrayOutputStream FileInputStream])
      (require '[cognitect.transit :as transit]
               '[clojure.edn :as edn]
               '[clojure.string :as str])

      (defn write-transit-json [cache]
        (let [out (ByteArrayOutputStream. 1000000)
              writer (transit/writer out :json)]
          (transit/write writer cache)
          (.toString out)))

      (defn process-caches []
        (let [cache-aot-path      \"target/cljs/core.cljs.cache.aot.edn\"
              cache-aot-edn       (edn/read-string (slurp cache-aot-path))
              cache-macros-path   \"target/cljs/core\$macros.cljc.cache.json\"
              cache-macros-stream (FileInputStream. cache-macros-path)
              cache-macros-edn    (transit/read (transit/reader cache-macros-stream :json))
              caches              [[cache-aot-path cache-aot-edn]
                                   [cache-macros-path cache-macros-edn]]]
          (doseq [[path cache-edn] caches]
            (doseq [key (keys cache-edn)]
              (let [out-path (str/replace path #\"(\.json|\.edn)\$\"
                               (str \".\" (munge key) \".json\"))
                    tr-json  (write-transit-json (key cache-edn))]
                (spit out-path tr-json))))))

      (process-caches)
    '';

    trimMainJsEnd = ''
      (let [string (slurp \"target/main.js\")]
        (spit \"target/main.js\"
          (subs string  0 (.indexOf string \"cljs.nodejs={};\"))))
    '';


    cljdeps   = import ./deps.nix { inherit pkgs; };
    classp    = cljdeps.makeClasspaths {
                  extraClasspaths=["src/js" "src/cljs/bundled" "src/cljs/snapshot"];
                };
    

    getJarPath = jarName: (lib.findFirst (p: p.name == jarName) null cljdeps.packages).path.jar;

in stdenv.mkDerivation rec {
  inherit version;
  name = "lumo-${version}";

  src = fetchurl {
    url    = "https://github.com/anmonteiro/lumo/archive/${version}.tar.gz";
    sha256 = "1mr3zjslznhv7y3mzvg1pmmvzn10d6di26izz4x8p4nfnshacwgw";
  };


  buildInputs = [ nodejs clojure jre unzip python openssl
                  nodePackages."lumo-build-deps-../interpreters/clojurescript/lumo" ];

  buildPhase = ''
    # Copy over lumo-build-deps environment
    rm yarn.lock
    cp -rf ${nodePackages."lumo-build-deps-../interpreters/clojurescript/lumo"}/lib/node_modules/lumo-build-deps/* ./

    # configure clojure-cli
    mkdir ./.cpcache
    export CLJ_CONFIG=`pwd`
    export CLJ_CACHE=`pwd`/.cpcache 

    # require more namespaces for cljs-bundle
    sed -i "s!ns lumo.core! \
              ns lumo.core  \
               (:require ${requireDeps}) \
               (:require-macros [clojure.template :as temp] \
                                [cljs.test :as test])!g" \
              ./src/cljs/snapshot/lumo/core.cljs          

    # Step 1: compile clojurescript with :none and :simple
    ${clojure}/bin/clojure -Scp ${classp} -e "${compileClojurescript true}"
    ${clojure}/bin/clojure -Scp ${classp} -e "${compileClojurescript false}"
    cp -f cljstmp/main.js target/main.js
    ${clojure}/bin/clojure -Scp ${classp} -e "${trimMainJsEnd}"

    # Step 2: sift files
    unzip -o ${getJarPath "org.clojure/clojurescript"} -d ./target
    unzip -j ${getJarPath "org.clojure/clojure"} "clojure/template.clj" -d ./target/clojure
    unzip -o ${getJarPath "org.clojure/google-closure-library"} -d ./target
    unzip -o ${getJarPath "org.clojure/google-closure-library-third-party"} -d ./target
    unzip -o ${getJarPath "org.clojure/tools.reader"} -d ./target
    unzip -o ${getJarPath "org.clojure/test.check"} -d ./target
    cp -rf ./src/cljs/bundled/lumo/* ./target/lumo/
    cp -rf ./src/cljs/snapshot/lumo/repl.clj ./target/lumo/
    # cleanup
    mv ./target/main.js ./target/main
    rm ./target/*\.js
    mv ./target/main ./target/main.js
    rm ./target/AUTHORS
    rm ./target/LICENSE
    rm ./target/*.edn
    rm ./target/*.md
    rm -rf ./target/css
    rm -rf ./target/META-INF
    rm -rf ./target/com
    rm -rf ./target/cljs/build
    rm -rf ./target/cljs/repl
    rm  ./target/cljs/core\.cljs\.cache.aot\.json
    rm  ./target/cljs/source_map\.clj
    rm  ./target/cljs/repl\.cljc
    rm  ./target/cljs/externs\.clj
    rm  ./target/cljs/closure\.clj
    rm  ./target/cljs/util\.cljc
    rm  ./target/cljs/js_deps\.cljc
    rm  ./target/cljs/analyzer/utils\.clj
    rm  ./target/cljs/core/macros\.clj
    rm  ./target/cljs/compiler/api.clj
    rm  ./target/goog/test_module*
    rm  ./target/goog/transpile\.js
    rm  ./target/goog/base_*
    find ./target -type f -name '*.class' -delete
    find ./target -type d -empty -delete

    # Step 3: generate munged cache jsons
    ${clojure}/bin/clojure -Scp ${classp} -e "${cacheToJsons}"
    rm  ./target/cljs/core\$macros\.cljc\.cache\.json
    

    # Step 4: Bunde javascript
    NODE_ENV=production node scripts/bundle.js
    node scripts/bundleForeign.js

    # Step 5: Backup resources
    cp -R target resources_bak

    # Step 6: Package executeable 1st time
    # fetch node sources and copy to palce that nexe will find
    mkdir -p tmp/${nodeVersion}
    cp ${nodeSources} tmp/${nodeVersion}/node-${nodeVersion}.tar.gz
    tar -C ./tmp/${nodeVersion} -xf ${nodeSources}
    mv ./tmp/${nodeVersion}/node-v${nodeVersion}/* ./tmp/${nodeVersion}/
    rm -rf ${lumo-internal-classpath}
    mv target ${lumo-internal-classpath}
    node scripts/package.js ${nodeVersion}
    rm -rf ${lumo-internal-classpath}

    # Step 7: AOT Macros
    sh scripts/aot-bundle-macros.sh

    # Step 8: Package executeable 2nd time
    rm -rf ${lumo-internal-classpath}
    mv target ${lumo-internal-classpath}
    node scripts/package.js ${nodeVersion}
  '';

  dontStrip = true;

  installPhase = ''
    mkdir -p $out/bin
    cp build/lumo $out/bin
  '';

  meta = {
    description = "Fast, cross-platform, standalone ClojureScript environment";
    longDescription = ''
      Lumo is a fast, standalone ClojureScript REPL that runs on Node.js and V8.
      Thanks to V8's custom startup snapshots, Lumo starts up instantaneously,
      making it the fastest Clojure REPL in existence.
    '';
    homepage = https://github.com/anmonteiro/lumo;
    license = stdenv.lib.licenses.epl10;
    maintainers = [ stdenv.lib.maintainers.hlolli ];
    platforms = stdenv.lib.platforms.linux;
  };
}