From 858fe2f09a4149a8ec5494a501324a8b97e2e51c Mon Sep 17 00:00:00 2001 From: Damien Cassou Date: Wed, 11 Oct 2023 08:47:17 +0200 Subject: [PATCH] firefox: refactor duplicate profile ID detection --- modules/programs/firefox.nix | 35 +++++++++++++------ .../firefox/duplicate-profile-ids.nix | 2 +- 2 files changed, 25 insertions(+), 12 deletions(-) diff --git a/modules/programs/firefox.nix b/modules/programs/firefox.nix index 368d43f4c..568472c54 100644 --- a/modules/programs/firefox.nix +++ b/modules/programs/firefox.nix @@ -111,6 +111,29 @@ let

''; + mkNoDuplicateAssertion = entities: entityKind: + (let + # Return an attribute set with entity IDs as keys and a list of + # entity names with corresponding ID as value. An ID is present in + # the result only if more than one entity has it. The argument + # entities is a list of AttrSet of one id/name pair. + findDuplicateIds = entities: + filterAttrs (_entityId: entityNames: length entityNames != 1) + (zipAttrs entities); + + duplicates = findDuplicateIds (mapAttrsToList + (entityName: entity: { "${toString entity.id}" = entityName; }) + entities); + + mkMsg = entityId: entityNames: + " - ID ${entityId} is used by " + concatStringsSep ", " entityNames; + in { + assertion = duplicates == { }; + message = '' + Must not have a Firefox ${entityKind} with an existing ID but + '' + concatStringsSep "\n" (mapAttrsToList mkMsg duplicates); + }); + in { meta.maintainers = [ maintainers.rycee maintainers.kira-bruneau ]; @@ -490,17 +513,7 @@ in { (", namely " + concatStringsSep ", " defaults); }) - (let - duplicates = filterAttrs (_: v: length v != 1) (zipAttrs - (mapAttrsToList (n: v: { "${toString v.id}" = n; }) (cfg.profiles))); - - mkMsg = n: v: " - ID ${n} is used by ${concatStringsSep ", " v}"; - in { - assertion = duplicates == { }; - message = '' - Must not have Firefox profiles with duplicate IDs but - '' + concatStringsSep "\n" (mapAttrsToList mkMsg duplicates); - }) + (mkNoDuplicateAssertion cfg.profiles "profile") ]; warnings = optional (cfg.enableGnomeExtensions or false) '' diff --git a/tests/modules/programs/firefox/duplicate-profile-ids.nix b/tests/modules/programs/firefox/duplicate-profile-ids.nix index 41c09436e..9b0b7c060 100644 --- a/tests/modules/programs/firefox/duplicate-profile-ids.nix +++ b/tests/modules/programs/firefox/duplicate-profile-ids.nix @@ -5,7 +5,7 @@ config = lib.mkIf config.test.enableBig { test.asserts.assertions.expected = ['' - Must not have Firefox profiles with duplicate IDs but + Must not have a Firefox profile with an existing ID but - ID 1 is used by first, second'']; programs.firefox = {