[Nix-dev] Announcing nix-buffer, nix-shell for emacs
Shea Levy
shea at shealevy.com
Tue Sep 6 11:39:00 CEST 2016
Hmm, I think we need more than just environment variables. See for
example my recent commit[1] to nixpkgs, where in addition to setting up
process-environment and exec-path I also set, when appropriate, several
proofgeneral variables like coq-prog-name and even call some
proofgeneral functions.
~Shea
[1]: https://github.com/NixOS/nixpkgs/commit/b04cdae902398aa76fb81a0470e18fd527c6ba3d
zimbatm <zimbatm at zimbatm.com> writes:
> Hi Shea,
>
> did you look at direnv[1] yet? I have a feeling that it would mix with
> emacs pretty well and implement all that you need in a more generic way
> (which is a pro and con).
>
> Essentially what I do in my projects is use the following .envrc:
> ```bash
> use nix
> ```
> The `use nix` directive calls nix-shell under the hood so then the usual
> default.nix and shell.nix are loaded.
>
> In emacs the plugin would need to call `direnv export json` on enter/leave
> of a context. This gives you a diff of environment variables to apply.
> There is a bit more complexity when implementing the security framework but
> that's about it.
>
> I'm just curious if you thought of that approach, keep up the good work!
>
> Cheers,
> z
>
> [1]: http://direnv.net/
>
> On Mon, 5 Sep 2016 at 17:11 Shea Levy <shea at shealevy.com> wrote:
>
>> Sure! In my .emacs, I have:
>>
>> > (defun nix-buffer-find-file-hook ()
>> > (when (not (file-remote-p (buffer-file-name)))
>> > (nix-buffer)))
>> > (add-hook 'find-file-hook 'nix-buffer-find-file-hook)
>>
>> I have coq 8.5pl1 installed in my configuration.nix, but I have a coq
>> project that requires 8.4pl6, so I have in that project's root directory
>> dir-locals.nix:
>>
>> > let pkgs = import <nixpkgs> {}; in
>> > pkgs.nixBufferBuilders.withPackages [ pkgs.coq ]
>>
>> (note that this relies on a recent nixpkgs commit[1]). Then whenever I
>> open a .v file in the project, ProofGeneral uses coq 8.4 rather than
>> 8.5.
>>
>> If I wanted, my dir-locals.nix could take a 'root' argument, which would
>> point to the file I opened.
>>
>> ~Shea
>>
>> [1]:
>> https://github.com/NixOS/nixpkgs/commit/05c132486d8cfae600bbfe8c9ac5d799b298afed
>>
>> Bas van Dijk <v.dijk.bas at gmail.com> writes:
>>
>> > Hi Shea,
>> >
>> > As a Nix and Emacs user this looks very interesting. I didn't know about
>> > Buffer-Local Variables before.
>> >
>> > Can you share some applications of nix-buffer / how you are using it?
>> >
>> > Thanks!
>> >
>> > Bas
>> >
>> > On 5 September 2016 at 08:06, Shea Levy <shea at shealevy.com> wrote:
>> >
>> >> Hi all,
>> >>
>> >> I've just pushed the initial version of nix-buffer[1], and opened a PR
>> >> for adding it to melpa[2]. The description of the 'nix-buffer-enter'
>> >> command this library defines:
>> >>
>> >> > Set up the buffer according to the directory-local nix expression.
>> >> >
>> >> > Looks for dir-locals.nix upward from the current directory. If found,
>> >> > builds the derivation defined there with the 'root' arg set to the
>> >> > current buffer file name or directory and evaluates the resulting
>> >> > elisp if safe to do so.
>> >> >
>> >> > Because in practice dir-locals.nix will always want to do things that
>> >> > are unsafe in dir-locals.el (e.g. append to 'exec-path'), we don't
>> >> > reuse that mechanism and instead just load the file as elisp. Because
>> >> > this allows arbitrary code execution, the first time we're asked to
>> >> > load a particular store path we query the user to verify if it's safe
>> >> > to load beforehand.
>> >> >
>> >> > The Lisp code generated by dir-locals.nix should limit itself to
>> >> > modifying buffer-local variables, but there is no actual enforcement
>> >> > of this. 'setq-local' is your friend.
>> >>
>> >> It may be useful to advise the 'normal-mode' function to call
>> >> 'nix-buffer-enter' beforehand for local files, so that all visited files
>> >> have their environments set up before modes are loaded.
>> >>
>> >> Note that this is my first elisp package, so feedback definitely
>> >> welcome!
>> >>
>> >> In addition to the elisp work mentioned in the TODO[3], it would be
>> >> useful to add some functions to nixpkgs to build elisp files,
>> >> especially:
>> >>
>> >> * A function taking a derivation and building an elisp file that sets up
>> >> the buffer environment analogous to the build environment specified by
>> >> that derivation
>> >> * A function taking a list of packages and building an elisp file that
>> >> sets up the buffer environment with all of those packages available
>> >> (in PATH, in the emacs load path, etc.).
>> >>
>> >> Happy hacking!
>> >>
>> >> ~Shea
>> >>
>> >> [1]: https://github.com/shlevy/nix-buffer/tree/v1.0
>> >> [2]: https://github.com/melpa/melpa/pull/4204
>> >> [3]: https://github.com/shlevy/nix-buffer/blob/v1.0/TODO.md
>> >>
>> >> _______________________________________________
>> >> nix-dev mailing list
>> >> nix-dev at lists.science.uu.nl
>> >> http://lists.science.uu.nl/mailman/listinfo/nix-dev
>> >>
>> >>
>> _______________________________________________
>> nix-dev mailing list
>> nix-dev at lists.science.uu.nl
>> http://lists.science.uu.nl/mailman/listinfo/nix-dev
>>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 818 bytes
Desc: not available
URL: <http://lists.science.uu.nl/pipermail/nix-dev/attachments/20160906/f79ec026/attachment-0001.sig>
More information about the nix-dev
mailing list