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