2

I'm trying to install Jupyter notebook on NixOs, but I can't find how to install numpy, and other libraries into jupyter. For now I added in my configuration.nix something like:

{ config, pkgs, ... }:
let
  python3-with-my-packages =
    pkgs.python3.withPackages (python-packages: with python-packages; [
      numpy
  ]);
in
{
  environment.systemPackages = with pkgs; [
    python3-with-my-packages
    jupyter
  ];
  [...]
}

4 Answers 4

3

A small shell.nix file for a Jupyter Notebook could look like the following.

with import <nixpkgs> {};

(pkgs.python3.withPackages (ps: with ps; [
    ipykernel jupyterlab
    matplotlib numpy pandas seaborn
    networkx
  ])).env

To start, copy a modified version of the shell.nix file to your project directory and run nix-shell --run "jupyter lab".

2
  • Thanks. This is close to the solution with notebook, except that notebook is replaced with jupyterlab. Any idea if I could make this work directly with the raw jupyter meta-package?
    – tobiasBora
    Jun 17, 2019 at 16:02
  • I typed nix-shell shell.nix --run "jupyter lab" and I did not try without shell.nix. The file of this folder are accessible, in the jupyter interface, this is nice ! Oct 19, 2021 at 9:56
0

I'm not claiming this is the best solution, but it seems that the jupyter package is not the one we need, but instead we just want to add the python package notebook to the list of deps:

{ config, pkgs, ... }:
let
  python3-with-my-packages =
    pkgs.python3.withPackages (python-packages: with python-packages; [
      numpy
      notebook
  ]);
in
{
  environment.systemPackages = with pkgs; [
    python3-with-my-packages
  ];
  [...]
}

Let me know if it's not the way to go and if there is a better solution!

0

You may also want to take a look at the topic Jupyter notebook with batteries in NixOS Discourse for some discussion on the Nixy dependency management for Jupyter Notebook.

If you want something compatible with non-Nix environment, I just managed to find a way to use Poetry inside NixOS with the help of mkPoetryEnv and buildFHSUserEnv:

Just place two files in the top-level directory of the Poetry project:

poetry-env.nix

{pkgs ? import <nixpkgs> {} }:
let
  lib = pkgs.lib;
  poetry2nix = pkgs.poetry2nix;
  python37 = pkgs.python37;
in
  poetry2nix.mkPoetryEnv {
    python = python37;
    pyproject = ./pyproject.toml;
    poetrylock = ./poetry.lock;
  }

poetry-env-fhs.nix

{
  pkgs ? import <nixpkgs> {},
  # This allows us to provide a command to run via `--argstr run COMMAND`.
  run ? "bash"
}:
let
  poetry-env = import ./poetry-env.nix { };
in
  with pkgs; (buildFHSUserEnv {
    name = "poetry-env-fhs";
    targetPkgs = pkgs: with pkgs; [
      # curl
      # git
      gcc
      gnumake
      python37Packages.poetry
      pandoc # for pdf conversion
      texlive.combined.scheme-full # for pdf conversion
      which # a convenient tool in vertualized environments
    ] ++ [
      poetry-env
    ];
    runScript = "${run}";
    profile = ''
      # export SSL_CERT_FILE=${cacert}/etc/ssl/certs/ca-bundle.crt
      # export GIT_SSL_CAINFO="$SSL_CERT_FILE"
      # export LANG=C.UTF-8
    '';
  }).env

Since it takes quite a while for mkPoetryEnv to build a package, you may want to nix-build ./poetry-env.nix to keep the build result from being GC-ed.

Jupyter notebook dependency management with Poetry

2
  • 1
    Hello and welcome to the site. When posting an answer, try posting the entire solution in to your post. Don't redirect to other websites if not necessary.
    – Rayleigh
    Jul 14, 2020 at 20:21
  • @Rayleigh Thanks for telling me that. Jul 16, 2020 at 19:06
0

There a great write up here of using Juypter with Nixos.

https://www.tweag.io/blog/2019-02-28-jupyter-with/

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .