TransWikia.com

Nixos: how to install deps in jupyter notebook?

Unix & Linux Asked on November 21, 2021

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

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

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

Answered by The Unix Janitor on November 21, 2021

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

Answered by Shamrock Lee on November 21, 2021

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".

Answered by Gumoringer on November 21, 2021

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!

Answered by tobiasBora on November 21, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP