Fork me on GitHub

Maybe it's useful for some. I wrote a simple cider-jack-in-babashkafunction that's handy when you want to have a repl quick:

(defun cider-jack-in-babashka ()
  (let* ((default-directory (project-root (project-current t)))
         (port (+ 1024 (random 5000)))
         (params `(:host "localhost"
                   :port ,port
                   :project-dir ,default-directory)))
    (start-process-shell-command "babashka-nrepl" "*babashka-nrepl*"
                                 (concat "bb nrepl-server " (number-to-string port)))
    (sleep-for 0.5)
    (cider-connect-clj params)))


This seems useful to contribute to the nREPL section


Should I make a PR or you want to just add it? Also, if someone has a better idea to find a free port and check when the babashka process is really running please say 🙂 random port and just sleeping for 0.5 seconds doesn't sound ideal but was enough for me personally.


A PR is fine or just an issue


@UFAP0C8KU If the server writes the port you can just wait a bit a read it from the .nrepl_port file (or however it was named).


Or you can just extract it from the server output, as we do for the Clojure nREPL server.


Anyways, at some point I should add something similar to CIDER.


Thanks. I think parsing it from the output is a good idea. Then I don't need the sleep kludge as the server should be ready when I'm able to read the port.


@U04V15CAJ is there a simple way to start babashka on the command line to always start on a free port (e.g. if 1667 is already in use, just use 1668 etc)? I don't really want to use a bb.ednas described in


Very nice. I'll update my snippet tonight.


I have one similar:

(defun my-babashka-connect--process-filter (proc string)
  "Run cider-connect once babashka nrepl server is ready."
  (when (string-match "Started nREPL server at .+:\\([0-9]+\\)" string)
    (cider-connect-clj (list :host "localhost" :port (match-string 1 string))))
  ;; Default behavior: write to process buffer
  (internal-default-process-filter proc string))

(defun my-babashka-connect ()
  "Start babashka on a random port."
  (let ((port (+ 1230 (cl-random 300))))
     (start-process "babashka"
		    "bb" "--nrepl-server" (number-to-string port))

(defun my-babashka-quit ()
  "Quit cider and kill babashka process."
  (kill-process (get-process "babashka"))
  (message "babashka is kill"))


We could also post this on the babashka wiki if there not a single right approach (the wiki is open for anyone to post)


or ... someone can make a babashka elisp package ;)


Making a PR for CIDER is probably the way to go?


yeah why not


@U7S5E44DB nice, that looks like something I would have ended up with anyway. Just that port number can be 0 now instead of random. And an improvement would maybe to remap cider-quit to your babashka quit function.


For cider itself there is already an open issue But if I read it correctly the plan is to not add a special babashka jack-in function


My point was that I was hoping we could detect that something is a babashka project and use the existing jack-in function which simply runs bababashka's nREPL server then.


Seems like a better user experience than one more function that people will have to know about, but I don't have any fundamental issues with a dedicated babashka command.


yeah agree with Bozhidar, we could detect if the project contains bb.edn IIRC we do a similar thing for Shadow Cljs


Jep, I agree. One jack-in command where someone can choose the repl backend(?) is probably more user-friendly.


But lets start with putting a simple command in the babashka wiki or This is basically the version from @U7S5E44DB but with port 0, the default directory is set to the project root and it should work over tramp (untested):

(defun cider-jack-in-babashka ()
    "Start an babashka nREPL server for the current project and connect to it."
    (let* ((default-directory (project-root (project-current t)))
           (process-filter (lambda (proc string)
                             "Run cider-connect once babashka nrepl server is ready."
                             (when (string-match "Started nREPL server at .+:\\([0-9]+\\)" string)
                               (cider-connect-clj (list :host "localhost"
                                                        :port (match-string 1 string)
                                                        :project-dir default-directory)))
                             ;; Default behavior: write to process buffer
                             (internal-default-process-filter proc string))))
       (start-file-process "babashka"
		           "bb" "--nrepl-server" "0")

🙌 2

Perhaps start with wiki so we can tweak the code easier, and we can link in the book to the wiki


Ok. I think that's a good idea.


ok, go ahead


@U0C8489U6 the thing with bb.edn is that it's not always there. And the main reason I even wrote this snippet was that often I simply wanted a quick repl to experiment in my project. Now I can just create a buffer playground.clj and M-x cider-jack-in-babashka and I have a working nrepl session in my editor in milliseconds preloaded with lots of useful libs to manipulate all kinds of files etc 🙂

🎉 1

@UFAP0C8KU I see yeah that makes sense for random stuff. I am actually using both in project and random...however for the latter use case I have this in my conf and connect directly:

(cider-known-endpoints '(("localhost" "1667") ;; babashka
                         ("localhost" "5555") ;; common


Could probably use more text but here's a start:


FYI - I plan to cut CIDER 1.2 in a week or two, so now's a good time to test the current master. It seems to be in a good shape overall, but a bit of extra testing never hurts. 🙂

☝️ 2

I just saw and made so that's not necessary anymore. I would say bb is unique enough for us to always add to interpreter-mode-alist when the user has clojure-mode installed.

👍 3