Giter Club home page Giter Club logo

endboxes's Introduction

Emacs + Nix + Direnv = Sandbox

Emacs setup

use-package setup…

(require 'package)
(package-initialize 'noactivate)
(eval-when-compile
  (require 'use-package))

Now the important packages

(use-package nix-sandbox)
(use-package direnv
  :config (direnv-mode))

Emacs environment

This is how you setup emacs with nix, but you can skip it.

{ pkgs ? import <nixpkgs> {} }:

let
  myEmacsConfig = ./default.el;
  emacsWithPackages = (pkgs.emacsPackagesNgGen pkgs.emacs).emacsWithPackages;
in
  emacsWithPackages (epkgs: (with epkgs.melpaPackages; [
    (pkgs.runCommand "default.el" {} ''
    mkdir -p $out/share/emacs/site-lisp
    cp ${myEmacsConfig} $out/share/emacs/site-lisp/default.el
    '')
    
    use-package
    direnv
    nix-sandbox
  ]))

Sample sandbox

Direnv .envrc that enables per-dir automated environments

use_nix

shell.nix env you put in project to predefine environment

{ pkgs ? import <nixpkgs> {} }:

pkgs.mkShell {
  buildInputs = with pkgs; [ direnv hello ];
  shellHook = ''
  eval $(direnv hook bash)
  direnv allow .
  '';
}

Running

Let’s make sample project file:

(progn 
  (let ((tmp-buffer (generate-new-buffer "sample")))
    (switch-to-buffer tmp-buffer))
  (message (concat "Sandbox: " (nix-current-sandbox)))
  (message (concat "From sandbox: " (shell-command-to-string "hello")))
  (switch-to-buffer-other-window "*Messages*"))

Now run the delivered emacs w/ config (making sure you’re running the right config) and open the file within the project’s sandbox:

nix run -c emacs -q -l default.el lorem-project/verify.el

This gets you the pre-configured Emacs you’ve seen above. Now execute the buffer and see the sandbox info with hello package on it.

One more thing

A statically linked nix can easily run the preconfigured setup anywhere:

curl https://matthewbauer.us/nix sh -s run --store $HOME/.cache/nix/store -f https://github.com/peel/emacs-nix-direnv-sample/archive/master.tar.gz -c emacs

endboxes's People

Contributors

peel avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.