Working with notebooks

Notebooks can be valuable assets for experimenters, notably since they can bind results and their analysis together. This section shows how some notebook technologies can be used in conjunction with Nix, thus improving the notebook reproducibility.

Jupyter notebook

Jupyter is a popular web-based Python notebook, formerly called IPython Notebook. The example is stored in the notebook-jupyter directory of the Chord experiments git repository. The directory contains a Jupyter file and the following Nix shell.

  tinypkgs ? import (fetchTarball {}

with tinypkgs; # Put tinypkgs's attributes in the current scope.
with pkgs; # Same for pkgs.

mkShell {
  buildInputs = [

    # Defines a python + set of packages.
    (python3.withPackages (ps: with ps; with python3Packages; [

      # Uncomment the following lines to make them available in the shell.
      # pandas
      # numpy
      # matplotlib

  # Automatically run jupyter when entering the shell.
  shellHook = "jupyter notebook";

This shell points to the package repository we have done in section Experiment Packaging: Don’t Repeat Yourself and defines a single shell from which all desired packages are set. This example introduces a Python environment in Nix, which is here done thanks to python?.withPackages, which in short defines a python interpreter with a set of available packages. Please refer to NixOS’s wiki on Python and Nix’s official Python documentation for more information about using Python and Nix.

Running jupyter simply consists in running nix-shell in the notebook-jupyter directory. This should run a Jupyter server, which will also (in a non-pure shell) run your brower so you can interact with Jupyter.

R markdown

R markdown is a popular R notebook that can be used with or without RStudio. The example is stored in the notebook-rmarkdown directory of the Chord experiments git repository. This directory contains a R markdown file, a nix-shell and a runner script. More information on using R with Nix can be found on NixOS wiki’s page on R.

  pkgs ? import (fetchTarball {}

  r_pkgs = with pkgs.rPackages; [
    # rmarkdown-related packages.
    # Rstudio-related packages.
    # servr
pkgs.mkShell {
  buildInputs = [
    (pkgs.rWrapper.override {
      packages = r_pkgs;

    # Uncomment to add RStudio in your environment.
    # (pkgs.rstudioWrapper.override {
    #  packages = r_pkgs;
    # })
#!/usr/bin/env nix-shell
#!nix-shell --pure -i Rscript

As the runner uses a nix-shell shebang, generating the notebook output is done like this.


Emacs’s org-mode

org-mode is a popular GNU Emacs package that can be used as a notebook technology. The example is stored in the notebook-org-mode directory of the Chord experiments git repository. The directory contains an org-mode file, and a nix-shell. More information on using Emacs with Nix can be found on NixOS manual on Emacs.

  pkgs ? import (fetchTarball {}

  emacs_base = pkgs.emacs;
  emacsWithPackages = (pkgs.emacsPackagesNgGen emacs_base).emacsWithPackages;

  self = rec {
    my_emacs = emacsWithPackages (epkgs: with epkgs; [

    shell = pkgs.mkShell rec {
      name = "emacs-shell";
      buildInputs = [

Emacs can be executed on the org-mode file with the following command.

nix-shell --pure shell.nix --command 'emacs -q ./'

Once in Emacs, exporting the notebook can be done with the convenient C-c C-e h h Emacs shortcut.