Simple way to create a Nix shell with a Python package

I am looking to launch a temporary shell to play around with a tool, namely bandcamp-downloader. It happens to be a Python package, and it’s not in nixpkgs or the NUR as far as I can tell. Is there a simple way for me to open such a shell? Something on roughly the same level of simplicity as nix shell nixpkgs#hello. I don’t mind having to type a few longer commands, I just don’t want to have to create files in my working directory or permanently add something to my configuration.

I know there are tools to turn Python packages into Nix derivations or overlays. I am happy to use such tools, but all of them seem to be designed for serious projects with Python packages as dependencies. I just want to start a shell with access to a few commands that happen to be written in Python.

No.

Theoretically you can do something like this:

nix shell --impure --expr 'let pkgs = import <nixpkgs> { }; in pkgs.python3.buildPythonApplication { src = pkgs.fetchFromGithub { owner = ""; repo = ""; }; /* and so on... */ }' --command bandcamp-downloader

… but writing that off the cuff is heinously complicated, you’re better off just creating a shell.nix and using nix-shell (or nix shell --impure --file).

Here is a shell.nix that creates a shell with that python package:

It’s a bit long, as it does include the full package. But this is essentially what is needed in order to get something into Nix.

{ pkgs ? import <nixpkgs>{}, }:

let
  unicode-slugify = pkgs.python3Packages.buildPythonApplication {
    pname = "unicode-slugify";
    version = "0.1.0-dev";

    src = pkgs.fetchFromGitHub {
      # This appears to be a patch to fix an issue. The original repo is mozilla/unicode-slugify
      owner = "dnicolson";
      repo = "unicode-slugify";
      rev = "a18826f440d0b74e536f5e32ebdcf30e720f20d8";
      hash = "sha256-kxyJaHxd/cMHiycfQHJqSsecT+eMkkRqO+OMSzbtjmE=";
    };
    nativeBuildInputs = with pkgs.python3Packages; [
      setuptools
      wheel
    ];

    format = "setuptools";

    propagatedBuildInputs = with pkgs.python3Packages; [
      six unidecode
    ];
  };


  bandcamp-dl = pkgs.python3Packages.buildPythonApplication {
    pname = "bandcamp-dl";
    version = "0.1.0-dev";

    src = pkgs.fetchFromGitHub {
      owner = "Evolution0";
      repo = "bandcamp-dl";
      rev = "0bf086bc2431c0756630faafff10dee7b64b8b67";
      hash = "sha256-QzZl10YHPvJPH7ufSJxF2xhNya2khgbhN93OFJAEUFg=";
    };

    format = "pyproject";

    # Nix packages setuptools 83, and urllib 2.7
    postPatch = ''
      substituteInPlace pyproject.toml \
        --replace-fail 'requires = ["setuptools>=84.0.0", "wheel"]' 'requires = ["setuptools>=83.0.0", "wheel"]' \
        --replace-fail '"urllib3 >= 2.8.0"' '"urllib3 >= 2.7.0"'
    '';

    nativeBuildInputs = with pkgs.python3Packages; [
      setuptools
      wheel
    ];

    # Runtime dependencies
    propagatedBuildInputs = with pkgs.python3Packages; [
      beautifulsoup4
      demjson3
      mutagen
      requests
      urllib3
      requests-ratelimiter
      unicode-slugify

    ];

    meta = with pkgs.lib; {
      description = "Simple python script to download Bandcamp albums";
      homepage = "https://github.com/Evolution0/bandcamp-dl";
      license = licenses.unlicense;
    };
  };


in

pkgs.mkShellNoCC {
  buildInputs = [
      bandcamp-dl
  ];
}