Packaging python-adblock for qutebrowser

I’m trying to get adblock on qutebrowser and I see that it needs python-adblock but I can’t find it in nixpkgs so I wanted to try to do it but I can’t get it to work. This is what my python-adblock.nix looks like

{ buildPythonPackage, fetchFromGitHub, rustc, maturin, python3Packages, ... }:

buildPythonPackage rec {
    pname = "python-adblock";
    format = "pyproject";
    version = "0.6.0";

    src = fetchFromGitHub {
        inherit pname version;
        owner = "ArniDagur";
        repo = "python-adblock";
        rev = "0.6.0";
        sha256 = "0994zqzq1s1sbqi7h4dlw6p78nvg8az7s5gfqbai9zf79isp23p6";
    };

    doCheck = false;

    nativeBuildInputs = [
        rustc
        maturin
    ];

    propagatedBuildInputs = [ python3Packages.setuptools ];
}

with the following in my configuration.nix

environment.systemPackages = with pkgs; [
        (import ./python_packages/python-adblock/python-adblock.nix {
            buildPythonPackage = pkgs.python3Packages.buildPythonPackage;
            fetchFromGitHub = pkgs.fetchFromGitHub;
            maturin = pkgs.maturin;
            rustc = pkgs.rustc;
            python3Packages = pkgs.python3Packages;

        })

];

I get this error when I run
nh os test

these 12 derivations will be built:
  /nix/store/gjymp0k7rqwn1bypbk2cp65vkjhb5ggq-python3.12-python-adblock-0.6.0.drv
  /nix/store/mn72hap2439ilv82n7b9kgjg4p389iwy-system-path.drv
  /nix/store/y5c100qykmy0rh0siwanqv3184pd717f-dbus-1.drv
  /nix/store/28mvvxnslyp0nj6nkjs2wq0kqkym2yny-X-Restart-Triggers-dbus.drv
  /nix/store/57pab32c7fy646ly1lqggfdwa0i1k8ns-X-Restart-Triggers-polkit.drv
  /nix/store/k8j3scdmvsf2nv2m0mjjj0379z6qsja5-unit-polkit.service.drv
  /nix/store/snmi0ywbp3yx1z9vd8add3lzv7mdby4z-unit-dbus.service.drv
  /nix/store/7992zcyg6v8w0hj47yhd2mckh8sza4ya-system-units.drv
  /nix/store/cq41mgld0aam0waapbkpd32k6w9gj5zz-unit-dbus.service.drv
  /nix/store/rff7172q5z81p57pmz3gvi4cjn0vv295-user-units.drv
  /nix/store/bd17frsqfas9s616qvp46f9cx3f97q6q-etc.drv
  /nix/store/b2pvn6wmpgbh0s8q4bgf950pqbj6969r-nixos-system-nixos-24.11.20241001.27e30d1.drv
python-adblock> building '/nix/store/gjymp0k7rqwn1bypbk2cp65vkjhb5ggq-python3.12-python-adblock-0.6.0.drv'
python-adblock> Sourcing python-remove-tests-dir-hook
python-adblock> Sourcing python-catch-conflicts-hook.sh
python-adblock> Sourcing python-remove-bin-bytecode-hook.sh
python-adblock> Sourcing pypa-build-hook
python-adblock> Using pypaBuildPhase
python-adblock> Sourcing python-runtime-deps-check-hook
python-adblock> Using pythonRuntimeDepsCheckHook
python-adblock> Sourcing pypa-install-hook
python-adblock> Using pypaInstallPhase
python-adblock> Sourcing python-imports-check-hook.sh
python-adblock> Using pythonImportsCheckPhase
python-adblock> Sourcing python-namespaces-hook
python-adblock> Sourcing python-catch-conflicts-hook.sh
python-adblock> Running phase: unpackPhase
python-adblock> unpacking source archive /nix/store/klys344ddyc3yjnj02qy27c5aidcfm01-python-adblock-0.6.0
python-adblock> source root is python-adblock-0.6.0
python-adblock> setting SOURCE_DATE_EPOCH to timestamp 315619200 of file python-adblock-0.6.0/web/static/index.html
python-adblock> Running phase: patchPhase
python-adblock> Running phase: updateAutotoolsGnuConfigScriptsPhase
python-adblock> Running phase: configurePhase
python-adblock> no configure script, doing nothing
python-adblock> Running phase: buildPhase
python-adblock> Executing pypaBuildPhase
python-adblock> Creating a wheel...
python-adblock> * Getting build dependencies for wheel...
python-adblock> 
python-adblock> Traceback (most recent call last):
python-adblock>   File "/nix/store/h9c4wnwbzjr2vg12pqf2n4vn9lh2hhjq-python3.12-pyproject-hooks-1.0.0/lib/python3.12/site-packages/pyproject_hooks/_impl.py", line 321, in _call_hook
python-adblock>     raise BackendUnavailable(data.get('traceback', ''))
python-adblock> pyproject_hooks._impl.BackendUnavailable: Traceback (most recent call last):
python-adblock>   File "/nix/store/h9c4wnwbzjr2vg12pqf2n4vn9lh2hhjq-python3.12-pyproject-hooks-1.0.0/lib/python3.12/site-packages/pyproject_hooks/_in_process/_in_process.py", line 77, in _build_backend
python-adblock>     obj = import_module(mod_path)
python-adblock>           ^^^^^^^^^^^^^^^^^^^^^^^
python-adblock>   File "/nix/store/h3i0acpmr8mrjx07519xxmidv8mpax4y-python3-3.12.5/lib/python3.12/importlib/__init__.py", line 90, in import_module
python-adblock>     return _bootstrap._gcd_import(name[level:], package, level)
python-adblock>            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
python-adblock>   File "<frozen importlib._bootstrap>", line 1387, in _gcd_import
python-adblock>   File "<frozen importlib._bootstrap>", line 1360, in _find_and_load
python-adblock>   File "<frozen importlib._bootstrap>", line 1324, in _find_and_load_unlocked
python-adblock> ModuleNotFoundError: No module named 'maturin'
python-adblock> 
python-adblock> ERROR Backend 'maturin' is not available.
error: builder for '/nix/store/gjymp0k7rqwn1bypbk2cp65vkjhb5ggq-python3.12-python-adblock-0.6.0.drv' failed with exit code 1;
       last 10 log lines:
       >           ^^^^^^^^^^^^^^^^^^^^^^^
       >   File "/nix/store/h3i0acpmr8mrjx07519xxmidv8mpax4y-python3-3.12.5/lib/python3.12/importlib/__init__.py", line 90, in import_module
       >     return _bootstrap._gcd_import(name[level:], package, level)
       >            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
       >   File "<frozen importlib._bootstrap>", line 1387, in _gcd_import
       >   File "<frozen importlib._bootstrap>", line 1360, in _find_and_load
       >   File "<frozen importlib._bootstrap>", line 1324, in _find_and_load_unlocked
       > ModuleNotFoundError: No module named 'maturin'
       >
       > ERROR Backend 'maturin' is not available.
       For full logs, run 'nix-store -l /nix/store/gjymp0k7rqwn1bypbk2cp65vkjhb5ggq-python3.12-python-adblock-0.6.0.drv'.
error: 1 dependencies of derivation '/nix/store/mn72hap2439ilv82n7b9kgjg4p389iwy-system-path.drv' failed to build
error: 1 dependencies of derivation '/nix/store/b2pvn6wmpgbh0s8q4bgf950pqbj6969r-nixos-system-nixos-24.11.20241001.27e30d1.drv' failed to build
┏━ 1 Errors: 
┃ error: builder for '/nix/store/gjymp0k7rqwn1bypbk2cp65vkjhb5ggq-python3.12-python-adblock-0.6.0.drv' failed with exit code 1;
┃        last 10 log lines:
┃        >           ^^^^^^^^^^^^^^^^^^^^^^^
┃        >   File "/nix/store/h3i0acpmr8mrjx07519xxmidv8mpax4y-python3-3.12.5/lib/python3.12/importlib/__init__.py", line 90, in import_module
┃        >     return _bootstrap._gcd_import(name[level:], package, level)
┃        >            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
┃        >   File "<frozen importlib._bootstrap>", line 1387, in _gcd_import
┃        >   File "<frozen importlib._bootstrap>", line 1360, in _find_and_load
┃        >   File "<frozen importlib._bootstrap>", line 1324, in _find_and_load_unlocked
┃        > ModuleNotFoundError: No module named 'maturin'
┃        >
┃        > ERROR Backend 'maturin' is not available.
┃        For full logs, run 'nix-store -l /nix/store/gjymp0k7rqwn1bypbk2cp65vkjhb5ggq-python3.12-python-adblock-0.6.0.drv'.
┣━ 1 Traces: 
┃ trace: evaluation warning: swiProlog has been renamed to swi-prolog
┣━ Dependency Graph:
┃       ┌─ ⏸ unit-dbus.service waiting for 1 ⚠
┃    ┌─ ⏸ user-units
┃    │        ┌─ ⏸ dbus-1 waiting for 1 ⚠
┃    │     ┌─ ⏸ X-Restart-Triggers-dbus
┃    │  ┌─ ⏸ unit-dbus.service
┃    │  │        ┌─ ⚠ python3.12-python-adblock-0.6.0 failed with exit code 1 after ⏱ 0s in buildPhase
┃    │  │     ┌─ ⏸ system-path
┃    │  │  ┌─ ⏸ X-Restart-Triggers-polkit
┃    │  ├─ ⏸ unit-polkit.service
┃    ├─ ⏸ system-units
┃ ┌─ ⏸ etc
┃ ⏸ nixos-system-nixos-24.11.20241001.27e30d1
┣━━━ Builds          
┗━ ∑ ⏵ 0 │ ✔ 0 │ ⏸ 11 │ ⚠ Exited after 1 build failures at 20:53:38 after 29s
Error: 
   0: Command exited with status Exited(1)

Location:
   src/commands.rs:151

But I have maturin in my nativeBuildInputs?

running
nix-store -l /nix/store/gjymp0k7rqwn1bypbk2cp65vkjhb5ggq-python3.12-python-adblock-0.6.0.drv

gvies me this

Sourcing python-remove-tests-dir-hook
Sourcing python-catch-conflicts-hook.sh
Sourcing python-remove-bin-bytecode-hook.sh
Sourcing pypa-build-hook
Using pypaBuildPhase
Sourcing python-runtime-deps-check-hook
Using pythonRuntimeDepsCheckHook
Sourcing pypa-install-hook
Using pypaInstallPhase
Sourcing python-imports-check-hook.sh
Using pythonImportsCheckPhase
Sourcing python-namespaces-hook
Sourcing python-catch-conflicts-hook.sh
Running phase: unpackPhase
@nix { "action": "setPhase", "phase": "unpackPhase" }
unpacking source archive /nix/store/klys344ddyc3yjnj02qy27c5aidcfm01-python-adblock-0.6.0
source root is python-adblock-0.6.0
setting SOURCE_DATE_EPOCH to timestamp 315619200 of file python-adblock-0.6.0/web/static/index.html
Running phase: patchPhase
@nix { "action": "setPhase", "phase": "patchPhase" }
Running phase: updateAutotoolsGnuConfigScriptsPhase
@nix { "action": "setPhase", "phase": "updateAutotoolsGnuConfigScriptsPhase" }
Running phase: configurePhase
@nix { "action": "setPhase", "phase": "configurePhase" }
no configure script, doing nothing
Running phase: buildPhase
@nix { "action": "setPhase", "phase": "buildPhase" }
Executing pypaBuildPhase
Creating a wheel...
* Getting build dependencies for wheel...

Traceback (most recent call last):
  File "/nix/store/h9c4wnwbzjr2vg12pqf2n4vn9lh2hhjq-python3.12-pyproject-hooks-1.0.0/lib/python3.12/site-packages/pyproject_hooks/_impl.py", line 321, in _call_hook
    raise BackendUnavailable(data.get('traceback', ''))
pyproject_hooks._impl.BackendUnavailable: Traceback (most recent call last):
  File "/nix/store/h9c4wnwbzjr2vg12pqf2n4vn9lh2hhjq-python3.12-pyproject-hooks-1.0.0/lib/python3.12/site-packages/pyproject_hooks/_in_process/_in_process.py", line 77, in _build_backend
    obj = import_module(mod_path)
          ^^^^^^^^^^^^^^^^^^^^^^^
  File "/nix/store/h3i0acpmr8mrjx07519xxmidv8mpax4y-python3-3.12.5/lib/python3.12/importlib/__init__.py", line 90, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<frozen importlib._bootstrap>", line 1387, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1360, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1324, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'maturin'

ERROR Backend 'maturin' is not available.

This is the github repo

Any help would be appreciated, thanks.

use callPackage not import. In this case the callPackage from python3Packages would be the best bet. then you don’t need all those args.
and an error about a python module means you need to provide the dep properly, i.e. use dependenciesnot nativeBuildInputs

Thanks for replying. I’ve tried making the suggested changes but I still get the same errors. This is what it looks like now. I hope I understoood your reply.

python-adblock.nix:

{ buildPythonPackage, fetchFromGitHub, rustc, maturin, python3Packages, ... }:

buildPythonPackage rec {
    pname = "python-adblock";
    format = "pyproject";
    version = "0.6.0";

    src = fetchFromGitHub {
        inherit pname version;
        owner = "ArniDagur";
        repo = "python-adblock";
        rev = "0.6.0";
        sha256 = "0994zqzq1s1sbqi7h4dlw6p78nvg8az7s5gfqbai9zf79isp23p6";
    };

    doCheck = false;

    dependencies = [
        rustc
        maturin
    ];

    propagatedBuildInputs = [ python3Packages.setuptools ];
}

configuration.nix

environment.systemPackages = with pkgs; [
        (python3Packages.callPackage ./python_packages/python-adblock/python-adblock.nix {})
];

rustc would stay on nativeBuildInputs as it’s meant to be ysd at compile-time only. And given that maturin which I just checked is also a rust package that is meant to build packages and not used at runtime, so it should also stay in nativeBuildInputs.

However looking in nixpkgs it appears there’s an explicit hook for maturin instead of using maturin directly. Can you try the following?(And remove dependencies, my earlier suggestion was incorrect).

nativeBuildInputs = [
  rustPlatform.maturinBuildHook
  rustPlatform.cargoSetupHook
];

  cargoDeps = rustPlatform.fetchCargoTarball {
    inherit pname version src;
    hash = ""; # replace with hash from error
  };

I tried your new suggestion and got this error

💥 maturin failed
  Caused by: The following metadata fields in `package.metadata.maturin` section of Cargo.toml are removed since maturin 0.14.0: classifier, requires-python, please set them in pyproject

I thought that the solution might be to downgrade maturin to a version before 0.14.0 but I couldn’t find version 0.14.0 on the maturin github page. I could find version 0.1.4, which I assume is the same?

The version right before that one is 1.3.2 so I tried reading Overriding | nixpkgs to downgrade maturin in nativeBuildInputs.

I tried different things from that wiki but I just can’t get the syntax right. I think this is best attempt

{ buildPythonPackage, fetchFromGitHub, rustc, python3Packages, rustPlatform, pkgs, ... }:
let 
    # Override maturin to use version 1.3.2
    maturin = pkgs.maturin.overrideAttrs (oldAttrs: rec {
        version = "1.3.2";
        src = fetchFromGitHub {
            owner = "PyO3";
            repo = "maturin";
            rev = "v${version}";
            sha256 = "0b2dkiqx683mz4vha3maj8hp181v8ff5gmdm57hish48pq7941j5";
        };
        cargoHash = "sha256-sb1PSp+hS8IDyA6O4AS37xdkYBRGoX+sc0Oo8p1Si9g=";
    });
in
buildPythonPackage rec {
    pname = "python-adblock";
    format = "pyproject";
    version = "0.6.0";

    src = fetchFromGitHub {
        inherit pname version;
        owner = "ArniDagur";
        repo = "python-adblock";
        rev = "0.6.0";
        sha256 = "0994zqzq1s1sbqi7h4dlw6p78nvg8az7s5gfqbai9zf79isp23p6";
    };

    doCheck = false;

    nativeBuildInputs = [
        rustc
        maturin
        rustPlatform.cargoSetupHook
    ];
    cargoDeps = rustPlatform.fetchCargoTarball {
        inherit pname version src;
        hash = "sha256-XF+BojY7uzvfgakM0Lwqka6FqC10yf2rhHZugCUZZLg=";
    };
    propagatedBuildInputs = [ python3Packages.setuptools ];
}

I get this error though

┃ error: hash mismatch in fixed-output derivation '/nix/store/mvfl0ax1pjjhmz6d1lnqwv7v1lyfk94p-source.drv':
┃          specified: sha256-RQaSDr6IQB3hKbXVV5xDO6BwIZKqDgU3+XUg03GcTSw=
┃             got:    sha256-sb1PSp+hS8IDyA6O4AS37xdkYBRGoX+sc0Oo8p1Si9g=

but this confuses me because I haven’t specified sha256-RQaSDr6IQB3hKbXVV5xDO6BwIZKqDgU3+XUg03GcTSw= anywhere