Python library using nix-shell

I’m running nix shell to setup a ptyhon development environment in a directory. Installing Librosa and Streamlit libraries works fine. But when I add Music21 I get this error:

       error: attribute 'music21' missing
       at /my/path/to/shell.nix:10:7:
            9|       ps.streamlit
           10|       ps.music21
             |       ^
           11|     ]))

This is the shell.nix file:

{ pkgs ? import <nixpkgs> {} }:

with pkgs;

mkShell {
  buildInputs = [
    (python3.withPackages (ps: [
      ps.librosa
      ps.streamlit
      ps.music21
    ]))
  ];
}

I don’t quite get why Librosa and Streamlit get installed fine but Music21 doesn’t. The installation instructions for all of them use pip. I assumed nix-shell is a replacement and would work for all cases, but it doesn’t.

Is there something I should be considering?

Those packages come from Nixpkgs. To use that library you’ll have to package it or use an option like uv2nix or poetry2nix if you want it automated.

Edit: You can also try GitHub - nix-community/pip2nix: Freeze pip-installable packages into Nix expressions [maintainer=@datakurre] to create the package

thanks, I’ll look into that

This worked great. Thanks!

  1. Created a music21.nix file in the same directory as shell.nix with all the needed dependencies:
# music21.nix
{
  lib,
  buildPythonPackage,
  fetchPypi,
  setuptools,
  wheel,
  # ... adding the following to the wiki example
  hatchling,
  chardet,
  joblib,
  jsonpickle,
  matplotlib,
  more-itertools,
  numpy,
  requests,
  webcolors,
}:

buildPythonPackage rec {
  pname = "music21";
  version = "9.5.0";

  src = fetchPypi {
    inherit pname version;
    hash = "sha256-+IioT3q/pucntwKM2/e3vZ+nRgCbdmymBPt0mmcHjRo=";
  };

  # do not run tests
  doCheck = false;

  # specific to buildPythonPackage, see its reference
  pyproject = true;
  build-system = [
    setuptools
    wheel
    # ... adding the following to the wiki example
    hatchling
    chardet
    joblib
    jsonpickle
    matplotlib
    more-itertools
    numpy
    requests
    webcolors
  ];
}
  1. Then used it in the shell.nixfile:
# shell.nix
let
  pkgs = import <nixpkgs> {};

  python = pkgs.python3.override {
    self = python;
    packageOverrides = pyfinal: pyprev: {
      music21 = pyfinal.callPackage ./music21.nix { };
    };
  };

in pkgs.mkShell {
  packages = [
    (python.withPackages (python-pkgs: [
      # select Python packages here
      # ... more packages
      python-pkgs.music21
    ]))
  ];
}
  1. running nix-shell should install everything.
1 Like