Use nix shells directly in org-mode source blocks. org-nix-shell
works by seamlessly
inheriting a nix shell environment using direnv before executing org-babel source blocks.
#+name: my-shell
#+begin_src nix
{ pkgs ? import <nixpkgs> {} }:
pkgs.mkShell {
buildInputs = [ pkgs.hello ];
}
#+end_src
#+begin_src sh :nix-shell "my-shell"
hello # use 'hello' from 'my-shell'
#+end_src
Enable org-nix-shell-mode
and evaluate or export the buffer.
See demo.org with examples for Python and C.
First create a nix shell in a named source block.
#+name: <name>
#+begin_src nix
{ pkgs ? import <nixpkgs> {} }:
pkgs.mkShell {
buildInputs = with pkgs; [ python3 ];
}
#+end_src
Source blocks with a :nix-shell <name>
header argument will use the nix shell
environment specified by <name>
; where <name>
is the corresponding named source block
in the same buffer. Other source blocks should be unaffected and you can explicitly set
:nix-shell 'nil
to not depend on any nix shell.
To use the nix shell above we can do:
#+begin_src python :nix-shell <name>
print("hello from python")
#+end_src
There are three ways to configure the :nix-shell
header property; also see Using Header
Arguments (The Org Manual). In increasing order of priority they are:
#+property: header-args: :nix-shell <name>
* sample header
:PROPERTIES:
:header-args: :nix-shell <name>
:END:
#+begin_src python :nix-shell <name>
print("hello from python")
#+end_src
Make sure nix-shell and direnv is installed on your system (nix-community/nix-direnv#Installation).
First, put org-nix-shell.el in your load path.
(require 'org-nix-shell)
(add-hook 'org-mode-hook 'org-nix-shell-mode)
(use-package org-nix-shell
:hook (org-mode . org-nix-shell-mode))
(use-package org-nix-shell
:straight '(org-nix-shell
:type git
:host github
:repo "AntonHakansson/org-nix-shell")
:hook (org-mode . org-nix-shell-mode))
M-x customize-group org-nix-shell
to see available customizable variables.