Trouble Registering a Myapp Link Protocol Handler on NixOS with KDE 6

Hello everyone,

I’m trying to create a NixOS module that handles myapp: URLs by forwarding them. However, despite trying numerous standard methods, I consistently get the following error from KDE Plasma 6 when clicking a myapp link:

Unable to create KIO worker. Unknown protocol ‘myapp’.

This error suggests that KDE’s KIO subsystem doesn’t even recognize the myapp: protocol, rather than just failing to find an application for it.

Minimal Reproducible Example

Here is a minimal version of the NixOS module that demonstrates the problem. This version uses a simple script that just logs the passed URL to a file for easy debugging, removing dependencies from the equation.

myapp-handler-test.nix

{ config, lib, pkgs, ... }:

let
  cfg = config.programs.myapp-handler-test;

  # A self-contained package that provides a simple script and a .desktop file.
  # This mimics the structure of standard application packages.
  handlerPkg = pkgs.stdenv.mkDerivation {
    pname = "myapp-handler-test";
    version = "1.0.0";

    # No src needed, we create files directly.
    dontUnpack = true;

    # The install phase creates the script and the desktop file.
    installPhase = ''
      runHook preInstall
      
      # Create the executable script
      mkdir -p $out/bin
      cat > $out/bin/myapp-handler-test << EOF
      #!${pkgs.runtimeShell}
      # Simple debug script: just log the passed argument.
      echo "Myapp handler called with: \$1" >> /tmp/myapp-debug.log
      EOF
      chmod +x $out/bin/myapp-handler-test

      # Create the .desktop file for URL scheme registration
      mkdir -p $out/share/applications
      cat > $out/share/applications/myapp-handler-test.desktop << EOF
      [Desktop Entry]
      Name=Myapp Handler Test
      GenericName=Myapp Handler
      Comment=A test for myapp URL handling
      Exec=$out/bin/myapp-handler-test %u
      Terminal=false
      Type=Application
      # Register this application as a handler for myapp links
      MimeType=x-scheme-handler/myapp;
      # Added KDE categories based on suggestions
      Categories=Qt;KDE;Network;
      EOF

      runHook postInstall
    '';
  };

in
{
  options.programs.myapp-handler-test.enable = lib.mkEnableOption "Myapp handler test";

  config = lib.mkIf cfg.enable {
    # Install the package into the system environment
    environment.systemPackages = [ handlerPkg ];

    # Set our application as the default handler for the myapp scheme
    xdg.mime.defaultApplications = {
      "x-scheme-handler/myapp" = [ "myapp-handler-test.desktop" ];
    };
  };
}

To use this, I import it into my NixOS configuration.nix and rebuild.

What has been tried

The core of the issue seems to be getting KDE 6 to recognize the x-scheme-handler/myapp MIME type in the .desktop file. So far, we have attempted:

  1. Multiple .desktop file installation methods:

    • Manually placing the file in /etc/xdg/applications/ via environment.etc.
    • Using pkgs.makeDesktopItem and adding it to environment.systemPackages.
    • The current method: building a complete, self-contained package with stdenv.mkDerivation that includes the script and the .desktop file, to precisely mimic how standard applications are packaged.
  2. Desktop File Contents:

    • Ensured Type=Application and MimeType=x-scheme-handler/myapp; are present.
    • Switched between Exec=... %U and Exec=... %u.
    • Added Qt;KDE; to the Categories= line, mirroring what applications do.
  3. MIME Association:

    • Used xdg.mime.defaultApplications to explicitly set the default handler.

Despite these attempts, the “Unknown protocol ‘myapp’” error persists, indicating that the registration is failing at a fundamental level. After each change, I have run nixos-rebuild switch and have also tried logging out and restarting the system.

The Question

What is the correct and most robust way to register a custom URL scheme handler in a NixOS module for KDE Plasma 6? Is there a new mechanism or a specific detail I’m missing that would cause KIO to ignore an apparently valid .desktop file?

Any help or insight would be greatly appreciated. Thank you!