2020-04-24 23:36:52 +00:00
|
|
|
{ lib, runCommandLocal, desktop-file-utils }:
|
|
|
|
|
2020-10-16 20:44:37 +00:00
|
|
|
# See https://specifications.freedesktop.org/desktop-entry-spec/desktop-entry-spec-latest.html
|
|
|
|
{ name # The name of the desktop file
|
2020-04-24 23:36:52 +00:00
|
|
|
, type ? "Application"
|
|
|
|
, exec
|
|
|
|
, icon ? null
|
|
|
|
, comment ? null
|
2020-10-16 20:44:37 +00:00
|
|
|
, terminal ? false
|
|
|
|
, desktopName # The name of the application
|
2020-04-24 23:36:52 +00:00
|
|
|
, genericName ? null
|
|
|
|
, mimeType ? null
|
|
|
|
, categories ? null
|
|
|
|
, startupNotify ? null
|
2020-11-30 08:33:03 +00:00
|
|
|
, extraDesktopEntries ? { } # Extra key-value pairs to add to the [Desktop Entry] section. This may override other values
|
2020-10-16 20:44:37 +00:00
|
|
|
, extraEntries ? "" # Extra configuration. Will be appended to the end of the file and may thus contain extra sections
|
2020-04-24 23:36:52 +00:00
|
|
|
, fileValidation ? true # whether to validate resulting desktop file.
|
|
|
|
}:
|
|
|
|
let
|
2020-10-16 20:44:37 +00:00
|
|
|
# like builtins.toString, but null -> null instead of null -> ""
|
2020-11-30 08:33:03 +00:00
|
|
|
nullableToString = value:
|
|
|
|
if value == null then null
|
|
|
|
else if builtins.isBool value then lib.boolToString value
|
|
|
|
else builtins.toString value;
|
2020-04-24 23:36:52 +00:00
|
|
|
|
2020-10-16 20:44:37 +00:00
|
|
|
# The [Desktop entry] section of the desktop file, as attribute set.
|
|
|
|
mainSection = {
|
|
|
|
"Type" = toString type;
|
|
|
|
"Exec" = nullableToString exec;
|
|
|
|
"Icon" = nullableToString icon;
|
|
|
|
"Comment" = nullableToString comment;
|
|
|
|
"Terminal" = nullableToString terminal;
|
2020-10-19 00:13:06 +00:00
|
|
|
"Name" = toString desktopName;
|
2020-10-16 20:44:37 +00:00
|
|
|
"GenericName" = nullableToString genericName;
|
|
|
|
"MimeType" = nullableToString mimeType;
|
|
|
|
"Categories" = nullableToString categories;
|
|
|
|
"StartupNotify" = nullableToString startupNotify;
|
|
|
|
} // extraDesktopEntries;
|
2020-04-24 23:36:52 +00:00
|
|
|
|
2020-10-16 20:44:37 +00:00
|
|
|
# Map all entries to a list of lines
|
|
|
|
desktopFileStrings =
|
2020-11-30 08:33:03 +00:00
|
|
|
[ "[Desktop Entry]" ]
|
2020-10-16 20:44:37 +00:00
|
|
|
++ builtins.filter
|
|
|
|
(v: v != null)
|
|
|
|
(lib.mapAttrsToList
|
|
|
|
(name: value: if value != null then "${name}=${value}" else null)
|
|
|
|
mainSection
|
|
|
|
)
|
2020-11-30 08:33:03 +00:00
|
|
|
++ (if extraEntries == "" then [ ] else [ "${extraEntries}" ]);
|
2020-04-24 23:36:52 +00:00
|
|
|
in
|
2020-11-30 08:33:03 +00:00
|
|
|
runCommandLocal "${name}.desktop"
|
|
|
|
{
|
|
|
|
nativeBuildInputs = [ desktop-file-utils ];
|
|
|
|
}
|
2020-10-16 20:44:37 +00:00
|
|
|
(''
|
2020-04-24 23:36:52 +00:00
|
|
|
mkdir -p "$out/share/applications"
|
|
|
|
cat > "$out/share/applications/${name}.desktop" <<EOF
|
2020-10-16 20:44:37 +00:00
|
|
|
${builtins.concatStringsSep "\n" desktopFileStrings}
|
|
|
|
EOF
|
|
|
|
'' + lib.optionalString fileValidation ''
|
|
|
|
echo "Running desktop-file validation"
|
2020-11-30 08:33:03 +00:00
|
|
|
desktop-file-validate "$out/share/applications/${name}.desktop"
|
2020-10-16 20:44:37 +00:00
|
|
|
'')
|