{ fetchFromSourcehut
, stdenv
, buildGoModule
, makeWrapper
, scdoc
, pkgs
, lib
, alsa-utils
, libxkbcommon
, gnused
, gawk
, coreutils
, libnotify
, dmenu
, procps
}:
# Source: https://github.com/Lykos153/numen-nix
buildGoModule rec {
  pname = "numen";
  version = "master";
  vendorHash = "sha256-Y3CbAnIK+gEcUfll9IlEGZE/s3wxdhAmTJkj9zlAtoQ=";
  src = fetchFromSourcehut {
    owner = "~geb";
    repo = pname;
    rev = version;
    hash = "sha256-haiaMBq9xbcDd83Kmm00Xc7823U+90DworOZk9H2n9w=";
  };

  allowGoReference = true;
  preBuild = ''
    export CGO_CFLAGS="-I${pkgs.internal.vosk-bin}/include"
    export CGO_LDFLAGS="-L${pkgs.internal.vosk-bin}/lib"
  '';
  nativeBuildInputs = [
    makeWrapper
    scdoc
  ];
  ldflags = [
    "-X main.Version=${version}"
    "-X main.DefaultModelPackage=vosk-model-small-en-us"
    "-X main.DefaultModelPaths=${pkgs.internal.vosk-model-small-en-us}/usr/share/vosk-models/small-en-us"
    "-X main.DefaultPhrasesDir=${placeholder "out"}/etc/numen/phrases"
  ];
  # This is necessary because while the scripts are copied relative to
  # the nix store, the hard-coded paths inside the scripts themselves
  # still point outside of the store.
  patchPhase = ''
    substituteInPlace scripts/* \
      --replace /etc/numen/scripts "$out/etc/numen/scripts" \
      --replace sed ${gnused}/bin/sed \
      --replace awk ${gawk}/bin/awk \
      --replace cat ${coreutils}/bin/cat \
      --replace notify-send ${libnotify}/bin/notify-send
    substituteInPlace scripts/menu \
      --replace "-dmenu" "-${dmenu}/bin/dmenu"
    substituteInPlace scripts/displaying \
      --replace "(pgrep" "(${procps}/bin/pgrep" \
      --replace "(ps" "(${procps}/bin/ps"
    substituteInPlace phrases/* \
      --replace /etc/numen/scripts "$out/etc/numen/scripts" \
      --replace numenc "$out/bin/numenc"
    substituteInPlace numenc \
      --replace /bin/echo "${coreutils}/bin/echo" \
      --replace cat "${coreutils}/bin/cat"
  '';
  installPhase = ''
    runHook preInstall

    install -Dm755 $GOPATH/bin/numen -t "$out/bin"
    install -Dm755 numenc -t "$out/bin"
    install -Dm755 scripts/* -t "$out/scripts"
    install -Dm644 phrases/* -t "$out/prases"
    sed -i "s:/etc/numen/scripts:${placeholder "out"}/scripts:g" \
           $out/scripts/* \
           $out/prases/*
    mkdir -p "$out/usr/share/man/man1" || exit
    scdoc < doc/numen.1.scd > "$out/usr/share/man/man1/numen.1" || exit
    echo Installed Successfully.


    runHook postInstall
  '';
  postFixup = ''
    wrapProgram $out/bin/numen \
      --prefix PATH : ${lib.makeBinPath [ pkgs.internal.dotool alsa-utils ]} \
      --prefix LD_LIBRARY_PATH : ${lib.makeLibraryPath [ libxkbcommon stdenv.cc.cc.lib ]} \
  '';
}