lib: init builder for XDG desktop entry

This commit is contained in:
Gabriel Arazas 2024-07-14 11:15:06 +08:00
parent 7e025bd9cf
commit 8138f70d44
No known key found for this signature in database
GPG Key ID: 62104B43D00AA360
4 changed files with 72 additions and 0 deletions

View File

@ -39,4 +39,33 @@
text = lib.generators.toINI { } config; text = lib.generators.toINI { } config;
destination = "/share/xdg-desktop-portal/${lib.optionalString (desktopName != "common") "${desktopName}-"}portals.conf"; 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"
'';
};
} }

View File

@ -64,4 +64,42 @@ lib.runTests {
expected = expected =
builtins.readFile ./data/fixtures/xdg-portal.conf; 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;
};
} }

View File

@ -0,0 +1,2 @@
[Desktop Entry]
Exec=Hello

View File

@ -0,0 +1,3 @@
[Desktop Entry]
DesktopNames=GNOME;Sway
Exec=Hello