From 9f4268e6b630497e289b18473775dff9c2d6635d Mon Sep 17 00:00:00 2001 From: Kira Bruneau Date: Sun, 5 Feb 2023 05:23:22 -0500 Subject: [PATCH] firefox: support passing any json value to settings (#3580) Firefox internally only supports bool, int, and string types for preferences, but often stores objects, arrays and floats as strings. This change makes it nicer to specify those type of preferences in Nix, and it also makes it possible to merge objects & arrays across multiple modules. --- modules/programs/firefox.nix | 25 ++++++++++++++++--- .../firefox/profile-settings-expected-user.js | 1 + .../programs/firefox/profile-settings.nix | 8 +++++- 3 files changed, 30 insertions(+), 4 deletions(-) diff --git a/modules/programs/firefox.nix b/modules/programs/firefox.nix index bf1c57790..4b3f288c7 100644 --- a/modules/programs/firefox.nix +++ b/modules/programs/firefox.nix @@ -42,6 +42,12 @@ let profilesIni = generators.toINI { } profiles; + userPrefValue = pref: + builtins.toJSON (if isBool pref || isInt pref || isString pref then + pref + else + builtins.toJSON pref); + mkUserJs = prefs: extraPrefs: bookmarks: let prefs' = lib.optionalAttrs ([ ] != bookmarks) { @@ -52,7 +58,7 @@ let // Generated by Home Manager. ${concatStrings (mapAttrsToList (name: value: '' - user_pref("${name}", ${builtins.toJSON value}); + user_pref("${name}", ${userPrefValue value}); '') prefs')} ${extraPrefs} @@ -201,7 +207,10 @@ in { }; settings = mkOption { - type = with types; attrsOf (either bool (either int str)); + type = types.attrsOf (jsonFormat.type // { + description = + "Firefox preference (int, bool, string, and also attrs, list, float as a JSON string)"; + }); default = { }; example = literalExpression '' { @@ -211,9 +220,19 @@ in { "distribution.searchplugins.defaultLocale" = "en-GB"; "general.useragent.locale" = "en-GB"; "browser.bookmarks.showMobileBookmarks" = true; + "browser.newtabpage.pinned" = [{ + title = "NixOS"; + url = "https://nixos.org"; + }]; } ''; - description = "Attribute set of Firefox preferences."; + description = '' + Attribute set of Firefox preferences. + + Firefox only supports int, bool, and string types for + preferences, but home-manager will automatically + convert all other JSON-compatible values into strings. + ''; }; extraConfig = mkOption { diff --git a/tests/modules/programs/firefox/profile-settings-expected-user.js b/tests/modules/programs/firefox/profile-settings-expected-user.js index 0edd47b91..d929df2b3 100644 --- a/tests/modules/programs/firefox/profile-settings-expected-user.js +++ b/tests/modules/programs/firefox/profile-settings-expected-user.js @@ -1,5 +1,6 @@ // Generated by Home Manager. +user_pref("browser.newtabpage.pinned", "[{\"title\":\"NixOS\",\"url\":\"https://nixos.org\"}]"); user_pref("general.smoothScroll", false); diff --git a/tests/modules/programs/firefox/profile-settings.nix b/tests/modules/programs/firefox/profile-settings.nix index b28e64597..278f3dbb9 100644 --- a/tests/modules/programs/firefox/profile-settings.nix +++ b/tests/modules/programs/firefox/profile-settings.nix @@ -9,7 +9,13 @@ lib.mkIf config.test.enableBig { profiles.test = { id = 1; - settings = { "general.smoothScroll" = false; }; + settings = { + "general.smoothScroll" = false; + "browser.newtabpage.pinned" = [{ + title = "NixOS"; + url = "https://nixos.org"; + }]; + }; }; profiles.bookmarks = {