Python packages not working when installed from home-manager

I’m trying to use home-manager to install matplotlib:

home.nix
{ config, pkgs, ... }:

{
  home.username = "lain";
  home.homeDirectory = "/home/lain";
  home.stateVersion = "23.11";
  programs.home-manager.enable = true;
  programs.bash.enable = true;

  home.packages = with pkgs; [
    # Special
    python312Full
    python312Packages.matplotlib  # FIXME this isn't seen by the Python runtime
    python312Packages.pillow  # test package -- ALSO not seen by the Python runtime
    fahclient
    (llama-cpp.overrideAttrs (old : {
      patches = (old.patches or []) ++ [
        /home/lain/Documents/projects/llama-sandbox/fix-llamacpp-6723.patch
      ];
    }))
    ##(builtins.getFlake "gitlab:doronbehar/nix-matlab") # FIXME how to make this identify as "locked"??
    nodejs_20

    # Misc
    firefox
    ##librewolf # tfw librewolf is broken
      # FIXME:
      # https://github.com/NixOS/nixpkgs/issues/336490
      # https://www.reddit.com/r/NixOS/comments/1ezu788/using_3rdparty_repos_with_homemanager/
    libreoffice-qt # NOTE: using -qt while on the Plasma desktop
    elinks


    # System Administration & Casual Devtools
    sensible-utils
    file
    xdiskusage
    p7zip
    tmux
    htop
    nano
    gitFull git-lfs
  ];

  home.sessionVariables = {
    HISTIGNORE = " *";
    
  };

##  home.sessionPath = {
##    "${XDG_DATA_HOME-${HOME}/.local}/bin" # FIXME this throws an error because bash syntax isn't allowed
##  };

  systemd.user.services.fahclient = {
    Unit = {
        Type = "simple";
        Description = "Folding@home client";
        After = [ "network.target" ];
    };
    Install = {
        WantedBy = [ "default.target" ];
    };
    Service = {
        ExecStart = "systemd-inhibit --why=\"folding\" fah-client";
        WorkingDirectory = "%h/.local/share/fahclient";
    };
  };

  #home.file = { };

}

…but Python simply doesn’t see the module, with the classic ModuleNotFoundError: No module named 'matplotlib'.

Logging out and back in did not fix this, nor did home-manager switch report any issues with the package.

What’s weird is if I use nix-shell -p to “install” the package in an ephemeral shell instead, it works.

I didn’t see anything on the Wiki about workarounds that might be required in this case.

You need to do python.withPackages (ps: with ps; [ matplotlib pillow ]), because the Python that ends up in your $PATH will have the modules baked into its environment.

Right now you’re just installing Python separately, and some modules separately; there’s no way for them to know they somehow belong together.

PS: This should be documented in the Nixpkgs manual, since it’s an interface of the software distribution, part of which is the Python stuff. The NixOS wiki would not be the place to find that sort of information — already by its name it’s about NixOS, mostly how application-specific modules are supposed to fit together. There’s also some random stuff in there that by now is mostly obsolete or redundant with the reference manuals, which are by construction the single source of truth.

1 Like

Also hm config is not really the place to put dev packages (libraries and dev-only binaries like interpreters/compilers), you really are meant to use a dev shell (either a mkShell derivation or an ad-hoc nix-shell call as you’ve tried) for development

1 Like

Mandatory plug: Setting up a Python development environment — nix.dev documentation