lib/builders: update folder structure

This is to organize for future builders that may be more comprehensive.
This commit is contained in:
Gabriel Arazas 2024-08-29 13:00:20 +08:00
parent e56c0a6136
commit 0750c10a0d
No known key found for this signature in database
GPG Key ID: 62104B43D00AA360
6 changed files with 102 additions and 72 deletions

View File

@ -1,71 +0,0 @@
{ pkgs, lib, self }:
{
/* Create an XDG MIME Association listing. This should also take care of
generating desktop-specific mimeapps.list if `desktopName` is given. The
given desktop name is already assumed to be in suitable casing which is
typically in lowercase ASCII.
*/
makeXDGMimeAssociationList = {
desktopName ? "",
addedAssociations ? { },
removedAssociations ? { },
defaultApplications ? { },
}:
pkgs.writeTextFile {
name = "xdg-mime-associations${lib.optionalString (desktopName != "") "-${desktopName}"}";
text =
# Non-desktop-specific mimeapps.list are only allowed to specify
# default applications.
lib.generators.toINI { } ({
"Default Applications" = defaultApplications;
} // (lib.optionalAttrs (desktopName == "") {
"Added Associations" = addedAssociations;
"Removed Associations" = removedAssociations;
}));
destination = "/share/applications/${lib.optionalString (desktopName != "") "${desktopName}-"}mimeapps.list";
};
/* Create an XDG Portals configuration with the given desktop name and its
configuration. Similarly, the given desktop name is assumed to be already
in its suitable form of a lowercase ASCII.
*/
makeXDGPortalConfiguration = {
desktopName ? "common",
config,
}:
pkgs.writeTextFile {
name = "xdg-portal-config${lib.optionalString (desktopName != "common") "-${desktopName}"}";
text = lib.generators.toINI { } config;
destination = "/share/xdg-desktop-portal/${lib.optionalString (desktopName != "common") "${desktopName}-"}portals.conf";
};
/* Create an XDG desktop entry file. Unlike the `makeDesktopItem`, it doesn't
have a required schema as long as it is valid data to be converted to.
Furthermore, the validation process can be disabled in case you want to
create something like an entry for a desktop session.
*/
makeXDGDesktopEntry = {
name,
config,
validate ? true,
destination ? "/share/applications/${name}.desktop",
}:
pkgs.writeTextFile {
name = "xdg-desktop-entry-${name}";
text = lib.generators.toINI {
listsAsDuplicateKeys = false;
mkKeyValue = lib.generators.mkKeyValueDefault {
mkValueString = v:
if lib.isList v then lib.concatStringsSep ";" v
else lib.generators.mkValueStringDefault { } v;
} "=";
} config;
inherit destination;
checkPhase =
lib.optionalString validate
''
${lib.getExe' pkgs.desktop-file-utils "desktop-file-validate"} "$target"
'';
};
}

7
lib/builders/default.nix Normal file
View File

@ -0,0 +1,7 @@
{ pkgs, lib, self }:
{
makeXDGMimeAssociationList = pkgs.callPackage ./xdg/make-association-list.nix { };
makeXDGPortalConfiguration = pkgs.callPackage ./xdg/make-portal-config.nix { };
makeXDGDesktopEntry = pkgs.callPackage ./xdg/make-desktop-entry.nix { };
}

View File

@ -0,0 +1,38 @@
{ lib, writeTextFile }:
/* Create an XDG MIME Association listing. This should also take care of
generating desktop-specific mimeapps.list if `desktopName` is given. The
given desktop name is already assumed to be in suitable casing which is
typically in lowercase ASCII.
*/
{
# An optional string containing the name of the desktop to be associated
# with.
desktopName ? "",
# Applications to be put in `Added Associations`. This is not set when the
# database is desktop-specific (when the `desktopName` is non-empty.)
addedAssociations ? { },
# Associations to be put in `Removed Associations` in the file. Similar to
# `addedAssociations`, this will not be added when it is desktop-specific.
removedAssociations ? { },
# Set of applications to be opened associated with the MIME type.
defaultApplications ? { },
}:
writeTextFile {
name = "xdg-mime-associations${lib.optionalString (desktopName != "") "-${desktopName}"}";
text =
# Non-desktop-specific mimeapps.list are only allowed to specify
# default applications.
lib.generators.toINI { } ({
"Default Applications" = defaultApplications;
} // (lib.optionalAttrs (desktopName == "") {
"Added Associations" = addedAssociations;
"Removed Associations" = removedAssociations;
}));
destination = "/share/applications/${lib.optionalString (desktopName != "") "${desktopName}-"}mimeapps.list";
}

View File

@ -0,0 +1,39 @@
{ lib, writeTextFile, desktop-file-utils }:
/* Create an XDG desktop entry file. Unlike the `makeDesktopItem`, it doesn't
have a required schema as long as it is valid data to be converted to.
Furthermore, the validation process can be disabled in case you want to
create something like an entry for a desktop session.
*/
{
# Name of the desktop entry. Only used as part of the package name and the
# default value of the destination path.
name,
# Nix-representable data to be exported as the desktop entry.
config,
# Add a validation check for the exported desktop entry.
validate ? true,
# Destination path relative to the output path.
destination ? "/share/applications/${name}.desktop",
}:
writeTextFile {
name = "xdg-desktop-entry-${name}";
text = lib.generators.toINI {
listsAsDuplicateKeys = false;
mkKeyValue = lib.generators.mkKeyValueDefault {
mkValueString = v:
if lib.isList v then lib.concatStringsSep ";" v
else lib.generators.mkValueStringDefault { } v;
} "=";
} config;
inherit destination;
checkPhase =
lib.optionalString validate
''
${lib.getExe' desktop-file-utils "desktop-file-validate"} "$target"
'';
}

View File

@ -0,0 +1,17 @@
{ lib, writeTextFile }:
/* Create an XDG Portals configuration with the given desktop name and its
configuration. Similarly, the given desktop name is assumed to be already
in its suitable form of a lowercase ASCII.
*/
{
desktopName ? "common",
# Nix-representable data to be exported as the portal configuration.
config,
}:
writeTextFile {
name = "xdg-portal-config${lib.optionalString (desktopName != "common") "-${desktopName}"}";
text = lib.generators.toINI { } config;
destination = "/share/xdg-desktop-portal/${lib.optionalString (desktopName != "common") "${desktopName}-"}portals.conf";
}

View File

@ -14,7 +14,7 @@ pkgs.lib.makeExtensible
let
callLib = file: import file { inherit pkgs lib self; };
in {
builders = callLib ./builders.nix;
builders = callLib ./builders;
trivial = callLib ./trivial.nix;
data = callLib ./data.nix;
fetchers = callLib ./fetchers;