Development with Nix: Python

I wrote a tutorial on using Nix to provision Python development environments:

I appreciate feedback and suggestions on how to improve it.


Cool blog post. Very nice and soft introduction of python for nix beginners which doesn’t scare them away immediately. The nix world needs more tutorials of this kind!

Did you consider using python.withPackages which is a short form of python38.buildEnv.override {....

Also for python itself you don’t need pkgs.mkShell at all to build a shell environment.
The derivation returned by python.withPackages or python.buildEnv contains an attribute env which gives you a shell derivation exactly like pkgs.mkShell. This is applicable to all your examples including the mach-nix one.

Applying both improvements would result in the following expression for your example:

{ pkgs ? import (fetchTarball {} }:

  grpclib = pkgs.python38Packages.buildPythonPackage rec {
    pname = "grpclib";
    version = "0.3.1";

    src = pkgs.python38Packages.fetchPypi {
      inherit pname version;
      sha256 = "0dw7jzw3pf2ckzrl808ayqvk9yqjhc45rj8qhmdxifv4ynwnyjam";

    propagatedBuildInputs = with pkgs.python38Packages; [ h2 multidict ];

    doCheck = false;

  customPython = pkgs.python38.withPackages (ps: [ grpclib ]);


But of course withPackages might introduce additional complexity for beginners since you now need to also explain that ps: [ grpclib ] is a function definition.

1 Like

@DavHau, thanks for the work on mach-nix and the other Nix+Python tools!

I considered withPackages but avoided it for the reason you mentioned.

I also reflected on using the env attribute and decided to leave the mkShell call because it serves as a hint of where to add non-Python packages (which I do often on Python projects). But I should add a note that this option exists.