Skip to content

Instantly share code, notes, and snippets.

@adrianparvino
Last active September 19, 2020 07:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adrianparvino/c0267a3efa6b87430dbc14d3977c358a to your computer and use it in GitHub Desktop.
Save adrianparvino/c0267a3efa6b87430dbc14d3977c358a to your computer and use it in GitHub Desktop.

Converting ClojureScript projects into Nix packages

JavaScript/NPM

Run ~node2nix~[fn:node2nix] on the repo to convert JavaScript dependencies into Nix-addressable packages

Clojure

Run ~clj2nix~[fn:clj2nix] on the repo to convert Clojure/Java/Maven dependencies into Nix-addressible packages

Generating a bundle

Nix needs a way to integrate both of these together. NPM has a mostly freeform build system, composing only of build artifacts and you can do fuck all. I overrode the Node component instead, because of this. I used the following code:

{ pkgs ? import <nixpkgs> { inherit system; }
, system ? builtins.currentSystem
, clojure ? pkgs.clojure
, stdenv ? pkgs.stdenv
}@args:

with import ./default.nix args;

let cljdeps = import ./clj-deps.nix { inherit pkgs; };
    classp  = cljdeps.makeClasspaths {};
    clojure_wrapped = pkgs.runCommand "clojure-wrapped" {
      nativeBuildInputs = [ pkgs.makeWrapper ];
    } ''
      mkdir -p $out/bin
      makeWrapper ${clojure}/bin/clojure $out/bin/clojure \
        --add-flags "-Scp ${classp}:src"
    '';

in package.override {
  nativeBuildInputs = [clojure_wrapped];
  outputs = [ "out" "bundle" ];
  postInstall = ''
    mkdir -p $bundle
    sed -i "s|:output-dir \"js\"|:output-dir \"$bundle\"|" shadow-cljs.edn
    npx shadow-cljs release app
  '';
}

[fn:node2nix] https://github.com/svanderburg/node2nix or nixpkgs.node2nix [fn:clj2nix] https://github.com/hlolli/clj2nix

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment