LoginSignup
7
7

More than 5 years have passed since last update.

Vagrant VM上で立ち上げたnREPLサーバにホストのEmacsから接続する方法

Posted at

サーバ (lein repl on VM)

まずVagrantfileで立ち上げるVMのprivate ipを固定する.

Vagrantfile
Vagrant.configure("2") do |config|
  config.vm.define :debian do |debian|
    debian.vm.box = "debian7.2"
    debian.vm.network :private_network, ip: "192.168.99.99"
  end
end
host
$ vagrant up debian
$ vagrant ssh debian

vm上でClojureプロジェクトを作成し, lein replを実行する.

vm
$ lein new hoge && cd hoge
$ edit project.clj

ちなみにここでデフォルトのままlein replを起動すると127.0.0.1のみ受け付ける設定になっているので, project.clj:repl-options {:host "0.0.0.0"}の設定を加えてどこからでも接続できるようにする.

project.clj
(defproject hoge "0.1.0-SNAPSHOT"
  :description "FIXME: write description"
  :url "http://example.com/FIXME"
  :license {:name "Eclipse Public License"
            :url "http://www.eclipse.org/legal/epl-v10.html"}
  :dependencies [[org.clojure/clojure "1.5.1"]]
  :repl-options {:host "0.0.0.0"})

起動.

vm
$ lein repl
nREPL server started on port 41080 on host 0.0.0.0
REPL-y 0.3.0
Clojure 1.5.1
    Docs: (doc function-name-here)
          (find-doc "part-of-name-here")
  Source: (source function-name-here)
 Javadoc: (javadoc java-object-or-class-here)
    Exit: Control+D or (exit) or (quit)
 Results: Stored in vars *1, *2, *3, an exception in *e

user=>

host 0.0.0.0 となっていればok.

portは毎回変わるが, ./target/repl-portにポート番号が吐き出されていて, ciderが接続時にこれを見てくれるので気にしなくていい.

クライアント (Emacs)

slime + swank-clojureとかnrepl.elとかあるけど, ナウいのは clojure-emacs/cider らしい.

emacs
M-x package-install <RET> cider <RET>

ssh経由でvm上のファイルを開く. vagrant ssh-configを使って~/.ssh/configを設定しておくと繋ぎやすい.

host
$ vagrant ssh-config debian >> ~/.ssh/config

C-x C-fでFind fileを開き, /ssh:...でリモートのファイルを開くことが出来る.

emacs
Find file: /ssh:vagrant@debian:/path/to/clojure/hoge/src/hoge/core.clj

無事開けたら, ciderで起動中のnREPLサーバに繋ぐ.

emacs
M-x cider <RET>
  Host: 192.168.99.99 <RET> (Vagrantfileで定義したip)
  Port: 41080 <RET> (最初から入ってるはず)

別ウィンドウで*cider-nrepl*バッファが開けば成功. in-nsでプロジェクトのnamespaceに移って作業を始められる.

emacs
; CIDER 0.5.0alpha (package: 20140111.332) (Clojure 1.5.1, nREPL 0.2.3)
user> (in-ns 'hoge.core)
hoge.core> 
7
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
7