{ stdenv
, lib
, autoreconfHook
, autoconf-archive
, pkg-config
, systemd
, glib
, dbus
, libnl
, pcsclite
, python3
, fetchFromGitHub
, gobject-introspection
, wrapGAppsHook
}:

stdenv.mkDerivation rec {
  pname = "neard";
  version = "git-2024-07-02";

  outputs = [ "out" "dev" ];

  src = fetchFromGitHub {
    owner = "linux-nfc";
    repo = "neard";
    rev = "a0a7d4d677800a39346f0c89d93d0fe43a95efad";
    hash = "sha256-6BgX7cJwxX+1RX3wU+HY/PIBgzomzOKemnl0SDLJNro=";
  };

  nativeBuildInputs = [
    autoreconfHook
    autoconf-archive
    pkg-config
    python3
    wrapGAppsHook
  ];

  buildInputs = [
    systemd
    glib
    dbus
    libnl
    pcsclite
    pcsclite.dev
    python3
    python3.pkgs.wrapPython
    glib
    gobject-introspection
  ];

  propagatedBuildInputs = [
    glib
    gobject-introspection
  ];

  pythonPath = with python3.pkgs; [
		pygobject3
		dbus-python
		glib
		gobject-introspection
  ];

  enableParallelBuilding = true;

  configureFlags = [
    "--disable-debug"
    "--enable-tools"
    "--enable-ese"
    "--with-systemdsystemunitdir=${placeholder "out"}/lib/systemd/system"
  ];

  postInstall = ''
    install -m 0755 tools/snep-send $out/bin/

    install -D -m644 src/main.conf $out/etc/neard/main.conf

    # INFO: the config option "--enable-test" would copy the apps to $out/lib/neard/test/ instead
    install -d $out/lib/neard
    install -m 0755 test/* $out/bin

    wrapPythonPrograms
    mv $out/bin/neard-ui.py $out/bin/neard-ui
  '';

  postFixup = ''
		mv $out/bin/.neardutils.py-wrapped $out/bin/neardutils.py
  '';

  meta = with lib; {
    description = "Near Field Communication manager";
    homepage = "https://01.org/linux-nfc";
    license = licenses.gpl2Only;
    platforms = platforms.unix;
  };
}
