Python and Qt Development in Nix

:bomb: I just spend a good amount of time drafting what I considered was a thoughtful post just to find myself facepalming a few minutes ago when I noticed I mv SOME_UNRELATED_STUB CURRENT_DRAFT-ed, effectively overwriting my work. So this is a pivot – a note-to-self to future me dealing with dev issues that may touch upon Python3, Poetry, Qt5, PyQt5 and nix shells.

I won’t go into why Poetry is a nice package manager to use, but here is the shell.nix that I have been using recently to play with some cute Qt code.

{ pkgs ? import <nixpkgs> { } }:
  qt = pkgs.qt514;
pkgs.mkShell {

  buildInputs = with pkgs; [

  # Keep Poetry cache within development directory
  POETRY_CACHE_DIR = "./.cache/poetry";

  # Just for some extra debug-useful visibility

  LD_LIBRARY_PATH = with pkgs; stdenv.lib.makeLibraryPath [
    glib #
    xorg.libX11 #
    xorg.libxcb #
    xorg.xcbutilwm #
    xorg.xcbutil #
    xorg.xcbutilimage #
    xorg.xcbutilkeysyms #
    xorg.xcbutilrenderutil #
    xorg.xcbutilrenderutil #
    dbus #
    libxkbcommon #

Start your nix-shell using nix-shell -I nixpkgs=/path/to/nixpkgs where /path/to/nixpkgs is replaced for the path to the nixpkgs that you really intend to use. Remember that versions of nix packages are pinned to the nixpkgs that you are using, so in order to keep your nix use reproducible, always mention which nixpkgs you build against.

Please don’t be me and forget to specficy the -I command line argument when using nix-shell. :facepalm: I spent a good amount of time feeling like a proper noob until DigitalKiwi in the #nixos freenode chan pointed this out. Long live IRC rooms! :bow:

This environment will provide you the Qt Creator (accessible through the qtcreator command), Qt Designer (accessible through the designer command) as well as the pyrcc5 and pyuic5 command line utils that you’ll use for generating resource and ui code respectively.

I tend to design containers for the projects that I develop (even for buildchains for firmware development) but when dealing with GUI projects, containers become a bit of a hassle1. This is why I turn to nix which allows for nifty developer environments. Since I really hate fiddling with tooling, the ability of nix to declaratively define environments is a real joy with regards to portability (you can move between machines with confidence) and reproducibility (you can onboard new folks to a project with relative ease and considerably less “well it worked for me” problems :shrug:).

X11 sockets into your containers and some other jazz.

The good folks at have already provided a better write-up on poetry2nix than I can, which provides some additional perspectives on the whole Poetry on NixOS stuff, so please go read that if you’d like to know more.

  1. In order to get GUI to work in containers, you will have to mount