From 51cc55330719be8918cff038b74f5b2c5612995a Mon Sep 17 00:00:00 2001 From: piegames Date: Mon, 29 Jun 2020 23:23:50 +0200 Subject: [PATCH 1/2] make-desktopitem: refactoring, documentation and improvement - New parameter `extraDesktopEntries` to easily add some less usual entries to the desktop file - Rewrite of the core logic. Instead of a key-value-list, use an attribute set with nullable values to make it overridable - Added some comments - Some cosmetic/readability code refactors - I didn't like the doubly nested strings around the `fileValidation` --- .../make-desktopitem/default.nix | 66 +++++++++++-------- 1 file changed, 37 insertions(+), 29 deletions(-) diff --git a/pkgs/build-support/make-desktopitem/default.nix b/pkgs/build-support/make-desktopitem/default.nix index 8355a5ad29b..f5020c036aa 100644 --- a/pkgs/build-support/make-desktopitem/default.nix +++ b/pkgs/build-support/make-desktopitem/default.nix @@ -1,50 +1,58 @@ { 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" , exec , icon ? null , comment ? null , terminal ? "false" -, desktopName +, desktopName # The name of the application , genericName ? null , mimeType ? null , categories ? 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. }: let - optionalEntriesList = [{k="Icon"; v=icon;} - {k="Comment"; v=comment;} - {k="GenericName"; v=genericName;} - {k="MimeType"; v=mimeType;} - {k="Categories"; v=categories;} - {k="StartupNotify"; v=startupNotify;}]; + # like builtins.toString, but null -> null instead of null -> "" + nullableToString = value: if value == null then null else builtins.toString value; - valueNotNull = {k, v}: v != null; - entriesToKeep = builtins.filter valueNotNull optionalEntriesList; + # 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); + "Name" = toString name; + "GenericName" = (nullableToString genericName); + "MimeType" = (nullableToString mimeType); + "Categories" = (nullableToString categories); + "StartupNotify" = (nullableToString startupNotify); + } // extraDesktopEntries; - mkEntry = {k, v}: k + "=" + v; - optionalEntriesString = lib.concatMapStringsSep "\n" mkEntry entriesToKeep; + # Map all entries to a list of lines + 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 runCommandLocal "${name}.desktop" {} - '' + ('' mkdir -p "$out/share/applications" cat > "$out/share/applications/${name}.desktop" < Date: Wed, 14 Oct 2020 01:19:43 +0200 Subject: [PATCH 2/2] make-desktopitem: minor fixes and code style --- .../make-desktopitem/default.nix | 22 ++++++++++--------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/pkgs/build-support/make-desktopitem/default.nix b/pkgs/build-support/make-desktopitem/default.nix index f5020c036aa..fd46e298513 100644 --- a/pkgs/build-support/make-desktopitem/default.nix +++ b/pkgs/build-support/make-desktopitem/default.nix @@ -6,7 +6,7 @@ , exec , icon ? null , comment ? null -, terminal ? "false" +, terminal ? false , desktopName # The name of the application , genericName ? null , mimeType ? null @@ -19,20 +19,22 @@ let # like builtins.toString, but null -> null instead of null -> "" - nullableToString = value: if value == null then null else builtins.toString value; + nullableToString = value: if value == null then null + else if builtins.isBool value then lib.boolToString value + else builtins.toString value; # 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); + "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); + "GenericName" = nullableToString genericName; + "MimeType" = nullableToString mimeType; + "Categories" = nullableToString categories; + "StartupNotify" = nullableToString startupNotify; } // extraDesktopEntries; # Map all entries to a list of lines