aboutsummaryrefslogtreecommitdiff
path: root/infra/libkookie/nixpkgs/pkgs/build-support/make-desktopitem/default.nix
blob: fd46e2985131aa67c545204243da3629cb110721 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
{ lib, runCommandLocal, desktop-file-utils }:

# 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 # The name of the application
, genericName ? null
, mimeType ? null
, categories ? null
, startupNotify ? 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
  # like builtins.toString, but null -> null instead of null -> ""
  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;
    "Name" = toString name;
    "GenericName" = nullableToString genericName;
    "MimeType" = nullableToString mimeType;
    "Categories" = nullableToString categories;
    "StartupNotify" = nullableToString startupNotify;
  } // extraDesktopEntries;

  # 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" <<EOF
    ${builtins.concatStringsSep "\n" desktopFileStrings}
    EOF
  '' + lib.optionalString fileValidation ''
    echo "Running desktop-file validation"
    ${desktop-file-utils}/bin/desktop-file-validate "$out/share/applications/${name}.desktop"
  '')