{
  description = "Nixpkgs PR request status checker";

  inputs = {
    nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";

    pyproject-nix = {
      url = "github:pyproject-nix/pyproject.nix";
      inputs.nixpkgs.follows = "nixpkgs";
    };

    uv2nix = {
      url = "github:pyproject-nix/uv2nix";
      inputs.pyproject-nix.follows = "pyproject-nix";
      inputs.nixpkgs.follows = "nixpkgs";
    };

    pyproject-build-systems = {
      url = "github:pyproject-nix/build-system-pkgs";
      inputs.pyproject-nix.follows = "pyproject-nix";
      inputs.uv2nix.follows = "uv2nix";
      inputs.nixpkgs.follows = "nixpkgs";
    };
  };

  outputs =
    {
      self,
      nixpkgs,
      uv2nix,
      pyproject-nix,
      pyproject-build-systems,
      ...
    }:
    let
      inherit (nixpkgs) lib;
      forAllSystems = lib.genAttrs lib.systems.flakeExposed;

      workspace = uv2nix.lib.workspace.loadWorkspace { workspaceRoot = ./.; };

      overlay = workspace.mkPyprojectOverlay {
        sourcePreference = "wheel";
      };

      editableOverlay = workspace.mkEditablePyprojectOverlay {
        root = "$REPO_ROOT";
      };

      # Python sets grouped per system
      pythonSets = forAllSystems (
        system:
        let
          pkgs = nixpkgs.legacyPackages.${system};
          inherit (pkgs) stdenv;

          # Base Python package set from pyproject.nix
          baseSet = pkgs.callPackage pyproject-nix.build.packages {
            python = pkgs.python312;
          };

          # An overlay of build fixups & test additions
          pyprojectOverrides = final: prev: {
            nixprstatus = prev.nixprstatus.overrideAttrs (old: {
              # Add tests to passthru.tests
              #
              # These attribute are used in Flake checks.
              passthru = old.passthru // {
                tests = (old.tests or { }) // {
                  # Run mypy checks
                  mypy =
                    let
                      venv = final.mkVirtualEnv "nixprstatus-typing-env" {
                        nixprstatus = [ "typing" ];
                      };
                    in
                    stdenv.mkDerivation {
                      name = "${final.nixprstatus.name}-mypy";
                      inherit (final.nixprstatus) src;
                      nativeBuildInputs = [
                        venv
                      ];
                      dontConfigure = true;
                      dontInstall = true;
                      buildPhase = ''
                        mkdir $out
                        mypy --strict nixprstatus --junit-xml $out/junit.xml
                      '';
                    };

                  # Run unittest with coverage reports installed into build output
                  unittest =
                    let
                      venv = final.mkVirtualEnv "nixprstatus-test-env" {
                        nixprstatus = [ ];
                      };
                    in
                    stdenv.mkDerivation {
                      name = "${final.nixprstatus.name}-test";
                      inherit (final.nixprstatus) src;
                      nativeBuildInputs = [
                        venv
                      ];

                      dontConfigure = true;

                      buildPhase = ''
                        runHook preBuild
                        mkdir -p $out
                        python -m unittest discover tests -v > $out/unittest.txt
                        runHook postBuild
                      '';
                    };
                };
              };
            });

          };

        in
        baseSet.overrideScope (
          lib.composeManyExtensions [
            pyproject-build-systems.overlays.default
            overlay
            pyprojectOverrides
          ]
        )
      );

    in
    # Django static roots grouped per system
    {
      checks = forAllSystems (
        system:
        let
          pythonSet = pythonSets.${system};
        in
        # Inherit tests from passthru.tests into flake checks
        pythonSet.nixprstatus.passthru.tests
      );

      packages = forAllSystems (
        system:
        let
          pkgs = nixpkgs.legacyPackages.${system};
          pythonSet = pythonSets.${system};
          venv = pythonSet.mkVirtualEnv "nixprstatus-env" workspace.deps.default;
        in
        {
          default = pkgs.stdenv.mkDerivation {
            pname = "nixprstatus";
            version = pythonSet.nixprstatus.version;
            dontConfigure = true;
            dontUnpack = true;
            dontBuild = true;
            buildInputs = [ venv ];
            nativeBuildInputs = [ pkgs.installShellFiles ];
            postInstall = ''
              mkdir -p $out/bin
              ln -s ${venv}/bin/nixprstatus $out/bin/nixprstatus
              installShellCompletion --cmd nixprstatus \
              --bash <($out/bin/nixprstatus --show-completion bash) \
              --zsh <($out/bin/nixprstatus --show-completion zsh) \
              --fish <($out/bin/nixprstatus --show-completion fish)
            '';
          };
        }
      );

      overlays.default = final: prev: {
        nixprstatus = self.packages.${prev.system}.default;
      };

      # Use an editable Python set for development.
      devShells = forAllSystems (
        system:
        let
          pkgs = nixpkgs.legacyPackages.${system};
          editablePythonSet = pythonSets.${system}.overrideScope editableOverlay;
          venv = editablePythonSet.mkVirtualEnv "nixprstatus-dev-env" {
            nixprstatus = [ "dev" ];
          };
        in
        {
          default = pkgs.mkShell {
            packages = [
              venv
              pkgs.uv
            ];
            shellHook = ''
              unset PYTHONPATH
              export REPO_ROOT=$(git rev-parse --show-toplevel)
              export UV_NO_SYNC=1
            '';
          };
        }
      );
    };
}
