[Help] User service with Python packages for Jupyter

Hi All,
I’m still very new to Nix but trying to daily-drive NixOS.

What I’m currently stuck on is injecting Python packages into a Jupyterlab service. What I have at the moment in the home-manager.home portion of my system flake is the following:


<span style="color:#323232;">  systemd.user.services.jupyter = let
</span><span style="color:#323232;">    jupyter = pkgs.jupyter-all.override {
</span><span style="color:#323232;">      python3 = pkgs.python311.withPackages (python-pkgs: with python-pkgs; [
</span><span style="color:#323232;">        numpy
</span><span style="color:#323232;">        matplotlib
</span><span style="color:#323232;">      ]);
</span><span style="color:#323232;">    };
</span><span style="color:#323232;">  in {
</span><span style="color:#323232;">    Service = {
</span><span style="color:#323232;">      Type = "simple";
</span><span style="color:#323232;">      WorkingDirectory = "${home.homeDirectory}/notebooks";
</span><span style="color:#323232;">      ExecStart = "${jupyter}/bin/jupyter-lab --no-browser";
</span><span style="color:#323232;">    };
</span><span style="color:#323232;">  };
</span>

This fires up a JupyterLab process that I can connect to and which runs fine, but numpy etc. can’t be imported. From a devshell, I see that the python.withPackages mechanism seems to rely on $PYTHONPATH to pass in a python3-3.11.8-env package that contains a lib/python3.11/site-packages. I’m guessing that the systemd service just needs to have an Environment key, but where do I get the python3-3.11.8-env path from?

The Jupiter executable is in a /nix/store/#-python3-3.11.8-env/bin location, but the site-packages only include the modules for Jupyter so I assume there’s another python3-3.11.8-env in the nix-store that does have the python packages I’m trying to get. Trying things like jupyter.env gives errors like *** Python ‘env’ attributes are intended for interactive nix-shell sessions, not for building! *** but I’m just taking a stab in the dark here.

I’d appreciate any pointers on this. I see there are helpers like JupyEnv, but these seem focused on setting up ephemeral devshells, not running a service for long-term notes and seem like overkill for what I want.

flashgnash,

I’ve found the best way to use python under nix is not to use nix for its packages because not all of its libraries are in nixpkgs

Requirements.txt and a venv still work on nixos the same way they would on a normal system

  • All
  • Subscribed
  • Moderated
  • Favorites
  • nix@programming.dev
  • DreamBathrooms
  • everett
  • InstantRegret
  • magazineikmin
  • thenastyranch
  • rosin
  • GTA5RPClips
  • Durango
  • Youngstown
  • slotface
  • khanakhh
  • kavyap
  • ngwrru68w68
  • tacticalgear
  • JUstTest
  • osvaldo12
  • tester
  • cubers
  • cisconetworking
  • mdbf
  • ethstaker
  • modclub
  • Leos
  • anitta
  • normalnudes
  • megavids
  • provamag3
  • lostlight
  • All magazines