diff --git a/lib/builders.nix b/lib/builders.nix index 76b0f5f1..2e16a471 100644 --- a/lib/builders.nix +++ b/lib/builders.nix @@ -39,4 +39,33 @@ text = lib.generators.toINI { } config; destination = "/share/xdg-desktop-portal/${lib.optionalString (desktopName != "common") "${desktopName}-"}portals.conf"; }; + + /* Create an XDG desktop entry file. Unlike the `makeDesktopItem`, it doesn't + have a required schema as long as it is valid data to be converted to. + Furthermore, the validation process can be disabled in case you want to + create something like an entry for a desktop session. + */ + makeXDGDesktopEntry = { + name, + config, + validate ? true, + destination ? "/share/applications/${name}.desktop", + }: + pkgs.writeTextFile { + name = "xdg-desktop-entry-${name}"; + text = lib.generators.toINI { + listsAsDuplicateKeys = false; + mkKeyValue = lib.generators.mkKeyValueDefault { + mkValueString = v: + if lib.isList v then lib.concatStringsSep ";" v + else lib.generators.mkValueStringDefault { } v; + } "="; + } config; + inherit destination; + checkPhase = + lib.optionalString validate + '' + ${lib.getExe' pkgs.desktop-file-utils "desktop-file-validate"} "$target" + ''; + }; } diff --git a/tests/lib/builders.nix b/tests/lib/builders.nix index 747b04aa..060e4918 100644 --- a/tests/lib/builders.nix +++ b/tests/lib/builders.nix @@ -64,4 +64,42 @@ lib.runTests { expected = builtins.readFile ./data/fixtures/xdg-portal.conf; }; + + # We're just testing out if the destination is correct at this point. + testsBuilderMakeSampleXDGDesktopEntry = { + expr = + let + xdgDesktopEntry = self.builders.makeXDGDesktopEntry { + name = sampleDesktopName; + validate = false; + config = { + "Desktop Entry".Exec = "Hello"; + }; + }; + in + builtins.readFile "${xdgDesktopEntry}/share/applications/${sampleDesktopName}.desktop"; + expected = + builtins.readFile ./data/fixtures/xdg-desktop-entry.desktop; + }; + + # We're just testing out if the destination is correct at this point. + testsBuilderMakeSampleDesktopSessionFile = { + expr = + let + xdgDesktopEntry = self.builders.makeXDGDesktopEntry { + name = sampleDesktopName; + validate = false; + destination = "/share/wayland-sessions/${sampleDesktopName}.desktop"; + config = { + "Desktop Entry" = { + Exec = "Hello"; + DesktopNames = [ "GNOME" "Sway" ]; + }; + }; + }; + in + builtins.readFile "${xdgDesktopEntry}/share/wayland-sessions/${sampleDesktopName}.desktop"; + expected = + builtins.readFile ./data/fixtures/xdg-desktop-session.desktop; + }; } diff --git a/tests/lib/data/fixtures/xdg-desktop-entry.desktop b/tests/lib/data/fixtures/xdg-desktop-entry.desktop new file mode 100644 index 00000000..00cf3f53 --- /dev/null +++ b/tests/lib/data/fixtures/xdg-desktop-entry.desktop @@ -0,0 +1,2 @@ +[Desktop Entry] +Exec=Hello diff --git a/tests/lib/data/fixtures/xdg-desktop-session.desktop b/tests/lib/data/fixtures/xdg-desktop-session.desktop new file mode 100644 index 00000000..66fe60a0 --- /dev/null +++ b/tests/lib/data/fixtures/xdg-desktop-session.desktop @@ -0,0 +1,3 @@ +[Desktop Entry] +DesktopNames=GNOME;Sway +Exec=Hello