From 11806b9c4be61d5cf6996658c25ddc3e7e4ee771 Mon Sep 17 00:00:00 2001 From: Gabriel Arazas Date: Wed, 10 Jul 2024 19:19:48 +0800 Subject: [PATCH] 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. --- lib/builders.nix | 42 ++++++++++++ lib/default.nix | 2 + tests/lib/builders.nix | 67 +++++++++++++++++++ ...mime-sample-desktop-specific-mimeapps.list | 2 + .../fixtures/xdg-mime-sample-mimeapps.list | 6 ++ tests/lib/data/fixtures/xdg-portal.conf | 3 + tests/lib/default.nix | 1 + 7 files changed, 123 insertions(+) create mode 100644 lib/builders.nix create mode 100644 tests/lib/builders.nix create mode 100644 tests/lib/data/fixtures/xdg-mime-sample-desktop-specific-mimeapps.list create mode 100644 tests/lib/data/fixtures/xdg-mime-sample-mimeapps.list create mode 100644 tests/lib/data/fixtures/xdg-portal.conf diff --git a/lib/builders.nix b/lib/builders.nix new file mode 100644 index 00000000..76b0f5f1 --- /dev/null +++ b/lib/builders.nix @@ -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"; + }; +} diff --git a/lib/default.nix b/lib/default.nix index 4ef51ab5..7fd6a5c6 100644 --- a/lib/default.nix +++ b/lib/default.nix @@ -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; }) diff --git a/tests/lib/builders.nix b/tests/lib/builders.nix new file mode 100644 index 00000000..747b04aa --- /dev/null +++ b/tests/lib/builders.nix @@ -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; + }; +} diff --git a/tests/lib/data/fixtures/xdg-mime-sample-desktop-specific-mimeapps.list b/tests/lib/data/fixtures/xdg-mime-sample-desktop-specific-mimeapps.list new file mode 100644 index 00000000..c0ec4545 --- /dev/null +++ b/tests/lib/data/fixtures/xdg-mime-sample-desktop-specific-mimeapps.list @@ -0,0 +1,2 @@ +[Default Applications] +application/pdf=firefox.desktop diff --git a/tests/lib/data/fixtures/xdg-mime-sample-mimeapps.list b/tests/lib/data/fixtures/xdg-mime-sample-mimeapps.list new file mode 100644 index 00000000..11b84a81 --- /dev/null +++ b/tests/lib/data/fixtures/xdg-mime-sample-mimeapps.list @@ -0,0 +1,6 @@ +[Added Associations] + +[Default Applications] +application/pdf=firefox.desktop + +[Removed Associations] diff --git a/tests/lib/data/fixtures/xdg-portal.conf b/tests/lib/data/fixtures/xdg-portal.conf new file mode 100644 index 00000000..682d9ce7 --- /dev/null +++ b/tests/lib/data/fixtures/xdg-portal.conf @@ -0,0 +1,3 @@ +[preferred] +default=gtk +org.freedesktop.impl.portal.Screencast=gnome diff --git a/tests/lib/default.nix b/tests/lib/default.nix index 1d966475..69865906 100644 --- a/tests/lib/default.nix +++ b/tests/lib/default.nix @@ -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;