13

I do my development in a Nix shell (create a default.nix file in my project root and then run nix-shell . to give me a shell with access to the project dependencies).

Spacemacs is my main editor, but when I try to run the GUI version via emacs & I don't have access to the programs in my nix-shell (if I were in a Ruby on Rails project for example, and Ruby was declared as a dependency in my default.nix, I would have no syntax highlighting in Spacemacs because the GUI version of Emacs doesn't see my Nix-shell dependencies). If I run :!which ruby, it can't even find the which command.

Right now, I'm running spacemacs via emacs -nw and just using it from the console, but I would really like to be able to use the GUI editor and get the full colorschemes available rather than being limited to the ones that look nice in 256 color mode. It's also quicker for me to switch between the terminal and the editor than between tmux panes or terminal splits to get to my CLI editor.

with import <nixpkgs> {}; {
  cannyFreeRadicalEnv = stdenv.mkDerivation rec {
    name = "rails-project-env";
    version = "0.1";
    src = ./.;
    buildInputs = [
        stdenv
        ruby_2_2_2
        bundler
        zlib
        postgresql94
        sqlite
        zsh
        git
        nodejs-0_12
    ];
  };
}
josiah
  • 1,314
  • 1
  • 13
  • 33

3 Answers3

5

I was able to fix this issue by running space emacs in daemon mode. https://www.emacswiki.org/emacs/EmacsAsDaemon

in the directory with default.nix: nix-shell . emacs --daemon emacsclient -c -a emacs

user514156
  • 630
  • 1
  • 7
  • 14
  • 1
    Does this open a while new window or buffer? What if I have multiple nix-shell projects? Will each one run in its own window? Can it be made to run in their own buffers instead? – CMCDragonkai Apr 22 '18 at 03:59
  • @CMCDragonkai This is no longer how I use space emacs. I do what Netsu does now. The only trick is that you need to also add emacs to your shell.nix if you want if you want it to be able to find all the libraries that your shell.nix includes. – user514156 Mar 24 '19 at 21:39
4

You can run your GUI Emacs as

setsid nix-shell . --command "emacs" &> /dev/null

Also see discussion about nix-shell integration to flycheck and ghc-mode.


Tip: you can use alias for this in your .zshrc or .bashrc

run-nix-emacs () {
  setsid nix-shell . --command "emacs" &> /dev/null
}

alias ne='run-nix-emacs'
Netsu
  • 355
  • 4
  • 13
  • I tried this out. As the answer is, running `:!which ruby` still says it cannot find the `which` command, and my Ruby configuration layer is still unavailable because the irb and ruby commands can't be found. I'll read through the link you provided to the flycheck and ghc-mod discussion, but my sense is that this is probably an Emacs with Nix issue rather than a general Nix issue. – josiah Oct 24 '15 at 21:49
  • @josiah, did you install `which`? – Netsu Oct 25 '15 at 21:56
  • not system-wide, but within the Nix-shell development environment, which is available because it's part of nixpkgs.stdenv. – josiah Oct 25 '15 at 22:29
  • 1
    Seems like it's really Spacemacs issue. My Emacs even with GUI just work in this case. – Netsu Oct 26 '15 at 05:38
  • I'll dig in and try to get some more info on the issue – josiah Oct 26 '15 at 19:21
  • 1
    Is this not affected by: https://github.com/syl20bnr/spacemacs/issues/2294 – CMCDragonkai Jul 09 '17 at 08:08
1

I recommend this nix-shell.el. My fork of it provides some configuration examples. While direnv integration to Emacs works, it is very slow. Not that this is faster, but it happens when you execute M-x nix-shell-activate and so you control when the latency happens. This becomes import if for instance you have a shell.nix in your org-directory for org source blocks in a multi-language environment and do not always want the direnv delay when you are opening an org file. Mario's nix-shell.el should be more widely known. Here is where I discovered it.

Joe
  • 965
  • 11
  • 16