How do I create a custom application launcher in gnome shell?

howdy!

I’m trying to create a .desktop file to launch a GUI application with custom arguments.

Where do I put the .desktop file so gnome shell sees it?

I tried symlinking my ~/.local/share/applications/myapp.desktop to /run/current-system/sw/share/applications but I cannot since that is mounted read only.

I would prefer to not use home manager or a complicated setup.

Any extra tips and context would be most appreciated. I’m at a loss how to translate traditional GNU/Linux imperative configuration to the nixOS idiom.

Wrap it in a simple derivation and write the desktop file to $out/share/applications, then install it by your favored means.

2 Likes

I’m using following overlay found in someones Gist to add custom Firefox launcher:

  (self: super: {
      firejailedFirefox = super.stdenv.mkDerivation rec {
        name = "firejailed-firefox-desktop-item";
        dontBuild = true;
        unpackPhase = "true";

        desktopItem = super.makeDesktopItem {
          name = "firefox";
          exec = "/run/current-system/sw/bin/firefox";
          genericName = "Web Browser";
          categories = "Application;Network;WebBrowser;";
          desktopName = "firefox";
          mimeType = lib.concatStringsSep ";" [
            "text/html"
            "text/xml"
            "application/xhtml+xml"
            "application/vnd.mozilla.xul+xml"
            "x-scheme-handler/http"
            "x-scheme-handler/https"
            "x-scheme-handler/ftp"
          ];
        };

        installPhase = ''
          mkdir -p $out/share
          cp -r ${desktopItem}/share/applications $out/share
        '';
      };
    })

Then you add firejailedFirefox to users/system packages.

2 Likes

Thank you both @NobbZ and @sorki!
Helped me find the solution.
To get what I needed, I ended up with the following steps.

  1. Create ~/.config/nixpkgs/overlays.nix

  2. add the following

    [
      (self: super: {
        gnumericWrapped = super.stdenv.mkDerivation rec {
          name = "gnumeric-launcher";
          dontBuild = true;
          unpackPhase = "true";
    
          desktopItem = super.makeDesktopItem {
    	name = "gnumeric-launcher";
    	exec = "gnumeric calculations.gnumeric";
    	genericName = "Calculations Spreadsheet";
    	categories = "Math;Science";
    	desktopName = "bills-launcher";
    	icon = "gnumeric";
          };
    
          installPhase = ''
    	mkdir -p $out/share
    	cp -r ${desktopItem}/share/applications $out/share
          '';
        };
      })
    ]
    
  3. Run nix-env -iA nixpkgs.gnumericWrapped

  4. Log out and log in again

Then, I can search for ‘gnumeric-launcher’ and see the pretty icon and shortcut!

I learned that exec could be a relative path. I also was unsure of where libs was defined in the above snippet. lib.concatStringsSep gave me a syntax error.

2 Likes

Hi, did you do something else to get a .desktop file for a firejailed app?

I have this issue with brave and it is not shown in rofi, so i have to run it from the terminal which is annoying.

I tried the following:

  security.chromiumSuidSandbox.enable = true;
  programs.firejail = {
    enable = true;
    wrappedBinaries = {
      brave = {
        executable = "${pkgs.brave}/bin/brave";
        desktop = "${pkgs.brave}/share/applications/brave.desktop"; # TODO
        profile = pkgs.writeText "brave.local" ''
          noblacklist ''${DOWNLOADS}
          whitelist ''${DOWNLOADS}
          include brave.profile
        '';
      };

However, i then get:

building '/nix/store/ybwmpkmpdbv2kjy4f3ycgfhxgyqlhms9-firejail-wrapped-binaries.drv'...
substitute(): ERROR: file '/nix/store/znwyw38nvn0xskzfbjnzx3w7ba5sli29-brave-1.57.57/share/applications/brave.desktop' does not exist
error: builder for '/nix/store/ybwmpkmpdbv2kjy4f3ycgfhxgyqlhms9-firejail-wrapped-binaries.drv' failed with exit code 1
error: 1 dependencies of derivation '/nix/store/fsv2c8s0si8qmhhrclfw46wd0iz4qn4p-firejail-wrapped-binaries_fish-completions.drv' failedto build
error: 1 dependencies of derivation '/nix/store/b3azzi8j5hbjk128jm25scssf1kcdzrk-man-paths.drv' failed to build
error: 1 dependencies of derivation '/nix/store/9r2xsni5vhp6c85mzafk2w6bzw3wls5r-system-path.drv' failed to build
error: 1 dependencies of derivation '/nix/store/1kz8khlkzl6g2j8d4dviad358pk8sj2i-nixos-system-nixos-23.05.3242.da5adce0ffaf.drv' failedto build

I tried to adapt your solution like this but it didn’t help:

      (final: prev: {
        brave-firejail = prev.stdenv.mkDerivation rec {
          name = "Brave-Desktop-Item";
          dontBuild = true;
          unpackPhase = "true";

          desktopItem = prev.makeDesktopItem {
            name = "brave";
            exec = "/run/current-system/sw/bin/brave";
            genericName = "Web Browser";
            categories = ["Network" "WebBrowser"];
            desktopName = "brave";
            # mimeTypes = prev.lib.concatStringsSep ";" [
            # "text/html"
            # "text/xml"
            # "application/xhtml+xml"
            # "application/vnd.mozilla.xul+xml"
            # "x-scheme-handler/http"
            # "x-scheme-handler/https"
            # "x-scheme-handler/ftp"
            # ];
          };

          installPhase = ''
            mkdir -p $out/share
            cp -r ${desktopItem}/share/applications $out/share
          '';
        };
      })

Do you know a solution?