lib: init builders subset

Most of it is specifically for NixOS usage but in case that it is
possible for independent desktop sessions entirely made in home-manager
especially in non-NixOS systems, it will be... nice.
This commit is contained in:
Gabriel Arazas 2024-07-10 19:19:48 +08:00
parent f46cec6e11
commit 11806b9c4b
No known key found for this signature in database
GPG Key ID: 62104B43D00AA360
7 changed files with 123 additions and 0 deletions

42
lib/builders.nix Normal file
View File

@ -0,0 +1,42 @@
{ 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";
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-${desktopName}";
text = lib.generators.toINI { } config;
destination = "/share/xdg-desktop-portal/${lib.optionalString (desktopName != "common") "${desktopName}-"}portals.conf";
};
}

View File

@ -12,9 +12,11 @@ pkgs.lib.makeExtensible
inherit (pkgs) lib;
callLib = file: import file { inherit pkgs lib self; };
in {
builders = callLib ./builders.nix;
trivial = callLib ./trivial.nix;
data = callLib ./data.nix;
inherit (self.builders) makeXDGMimeAssociationList makeXDGPortalConfiguration;
inherit (self.trivial) countAttrs;
inherit (self.data) importYAML renderTeraTemplate;
})

67
tests/lib/builders.nix Normal file
View File

@ -0,0 +1,67 @@
{ pkgs, lib, self }:
let
sampleDesktopName = "horizontal-hunger";
in
lib.runTests {
testsBuilderMakeSampleXDGAssociationList = {
expr =
let
xdgAssociations = self.builders.makeXDGMimeAssociationList {
defaultApplications = {
"application/pdf" = "firefox.desktop";
};
};
in builtins.readFile "${xdgAssociations}/share/applications/mimeapps.list";
expected =
builtins.readFile ./data/fixtures/xdg-mime-sample-mimeapps.list;
};
# This should only create the "Default Applications" section of the
# specific-desktop mimeapps.list.
testsBuilderMakeSampleDesktopSpecificXDGAssociationList = {
expr =
let
xdgAssociations = self.builders.makeXDGMimeAssociationList {
desktopName = sampleDesktopName;
defaultApplications = {
"application/pdf" = "firefox.desktop";
};
};
in builtins.readFile "${xdgAssociations}/share/applications/${sampleDesktopName}-mimeapps.list";
expected =
builtins.readFile ./data/fixtures/xdg-mime-sample-desktop-specific-mimeapps.list;
};
testsBuilderMakeSampleXDGPortalCommonConfig = {
expr =
let
xdgPortalConf = self.builders.makeXDGPortalConfiguration {
config.preferred = {
default = "gtk";
"org.freedesktop.impl.portal.Screencast" = "gnome";
};
};
in
builtins.readFile "${xdgPortalConf}/share/xdg-desktop-portal/portals.conf";
expected =
builtins.readFile ./data/fixtures/xdg-portal.conf;
};
# We're just testing out if the destination is correct at this point.
testsBuilderMakeSampleXDGPortalDesktopSpecificConfig = {
expr =
let
xdgPortalConf = self.builders.makeXDGPortalConfiguration {
desktopName = sampleDesktopName;
config.preferred = {
default = "gtk";
"org.freedesktop.impl.portal.Screencast" = "gnome";
};
};
in
builtins.readFile "${xdgPortalConf}/share/xdg-desktop-portal/${sampleDesktopName}-portals.conf";
expected =
builtins.readFile ./data/fixtures/xdg-portal.conf;
};
}

View File

@ -0,0 +1,2 @@
[Default Applications]
application/pdf=firefox.desktop

View File

@ -0,0 +1,6 @@
[Added Associations]
[Default Applications]
application/pdf=firefox.desktop
[Removed Associations]

View File

@ -0,0 +1,3 @@
[preferred]
default=gtk
org.freedesktop.impl.portal.Screencast=gnome

View File

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