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