Merge pull request #91790 from piegamesde/improve-makedesktopitem

make-desktopitem: refactoring, documentation and improvement
This commit is contained in:
maralorn 2020-10-15 02:42:12 +02:00 committed by GitHub
commit ae2630c96a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 40 additions and 30 deletions

View File

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