diff --git a/modules/programs/firefox/mkFirefoxModule.nix b/modules/programs/firefox/mkFirefoxModule.nix index 26f9046b..c94f8aba 100644 --- a/modules/programs/firefox/mkFirefoxModule.nix +++ b/modules/programs/firefox/mkFirefoxModule.nix @@ -158,6 +158,8 @@ let }) else (pkgs.wrapFirefox.override { config = bcfg; }) package { }; + + bookmarkTypes = import ./profiles/bookmark-types.nix { inherit lib; }; in { options = setAttrByPath modulePath { enable = mkOption { @@ -380,7 +382,7 @@ in { bookmarks = mkOption { type = (with types; - coercedTo (listOf anything) (bookmarks: + coercedTo bookmarkTypes.settingsType (bookmarks: warn '' ${cfg.name} bookmarks have been refactored into a submodule that now explicitly require a 'force' option to be enabled. diff --git a/modules/programs/firefox/profiles/bookmark-types.nix b/modules/programs/firefox/profiles/bookmark-types.nix new file mode 100644 index 00000000..ff6b88ce --- /dev/null +++ b/modules/programs/firefox/profiles/bookmark-types.nix @@ -0,0 +1,70 @@ +{ lib, ... }: + +with lib; + +rec { + settingsType = with types; + coercedTo (addCheck (attrsOf nodeType) (attrs: !(attrs ? settings))) + attrValues (listOf nodeType); + + bookmarkSubmodule = types.submodule ({ name, ... }: { + options = { + name = mkOption { + type = types.str; + default = name; + description = "Bookmark name."; + }; + + tags = mkOption { + type = types.listOf types.str; + default = [ ]; + description = "Bookmark tags."; + }; + + keyword = mkOption { + type = types.nullOr types.str; + default = null; + description = "Bookmark search keyword."; + }; + + url = mkOption { + type = types.str; + description = "Bookmark url, use %s for search terms."; + }; + }; + }) // { + description = "bookmark submodule"; + }; + + bookmarkType = types.addCheck bookmarkSubmodule (x: x ? "url"); + + directoryType = types.submodule ({ name, ... }: { + options = { + name = mkOption { + type = types.str; + default = name; + description = "Directory name."; + }; + + bookmarks = mkOption { + type = types.listOf nodeType; + default = [ ]; + description = "Bookmarks within directory."; + }; + + toolbar = mkOption { + type = types.bool; + default = false; + description = '' + Make this the toolbar directory. Note, this does _not_ + mean that this directory will be added to the toolbar, + this directory _is_ the toolbar. + ''; + }; + }; + }) // { + description = "directory submodule"; + }; + + nodeType = types.either bookmarkType directoryType; +} diff --git a/modules/programs/firefox/profiles/bookmarks.nix b/modules/programs/firefox/profiles/bookmarks.nix index 469d543b..130677f5 100644 --- a/modules/programs/firefox/profiles/bookmarks.nix +++ b/modules/programs/firefox/profiles/bookmarks.nix @@ -3,66 +3,9 @@ with lib; let - bookmarkSubmodule = types.submodule ({ name, ... }: { - options = { - name = mkOption { - type = types.str; - default = name; - description = "Bookmark name."; - }; + bookmarkTypes = import ./bookmark-types.nix { inherit lib; }; - tags = mkOption { - type = types.listOf types.str; - default = [ ]; - description = "Bookmark tags."; - }; - - keyword = mkOption { - type = types.nullOr types.str; - default = null; - description = "Bookmark search keyword."; - }; - - url = mkOption { - type = types.str; - description = "Bookmark url, use %s for search terms."; - }; - }; - }) // { - description = "bookmark submodule"; - }; - - bookmarkType = types.addCheck bookmarkSubmodule (x: x ? "url"); - - directoryType = types.submodule ({ name, ... }: { - options = { - name = mkOption { - type = types.str; - default = name; - description = "Directory name."; - }; - - bookmarks = mkOption { - type = types.listOf nodeType; - default = [ ]; - description = "Bookmarks within directory."; - }; - - toolbar = mkOption { - type = types.bool; - default = false; - description = '' - Make this the toolbar directory. Note, this does _not_ - mean that this directory will be added to the toolbar, - this directory _is_ the toolbar. - ''; - }; - }; - }) // { - description = "directory submodule"; - }; - - nodeType = types.either bookmarkType directoryType; + inherit (bookmarkTypes) settingsType; bookmarksFile = bookmarks: let @@ -141,8 +84,7 @@ in { }; settings = mkOption { - type = with types; - coercedTo (attrsOf nodeType) attrValues (listOf nodeType); + type = settingsType; default = [ ]; example = literalExpression '' [ diff --git a/tests/modules/programs/firefox/common.nix b/tests/modules/programs/firefox/common.nix index 7dca728a..f086702d 100644 --- a/tests/modules/programs/firefox/common.nix +++ b/tests/modules/programs/firefox/common.nix @@ -5,6 +5,7 @@ builtins.mapAttrs (test: module: import module [ "programs" name ]) { "${name}-final-package" = ./final-package.nix; "${name}-policies" = ./policies.nix; "${name}-profiles-bookmarks" = ./profiles/bookmarks; + "${name}-profiles-bookmarks-attrset" = ./profiles/bookmarks/attrset.nix; "${name}-profiles-containers" = ./profiles/containers; "${name}-profiles-containers-duplicate-ids" = ./profiles/containers/duplicate-ids.nix; diff --git a/tests/modules/programs/firefox/profiles/bookmarks/attrset.nix b/tests/modules/programs/firefox/profiles/bookmarks/attrset.nix new file mode 100644 index 00000000..c4e3c436 --- /dev/null +++ b/tests/modules/programs/firefox/profiles/bookmarks/attrset.nix @@ -0,0 +1,82 @@ +modulePath: +{ config, lib, ... }: + +let + + cfg = lib.getAttrFromPath modulePath config; + + firefoxMockOverlay = import ../../setup-firefox-mock-overlay.nix modulePath; + +in { + imports = [ firefoxMockOverlay ]; + + config = lib.mkIf config.test.enableBig (lib.setAttrByPath modulePath { + enable = true; + profiles.bookmarks = { + settings = { "general.smoothScroll" = false; }; + bookmarks = { + home-manager = { + toolbar = true; + bookmarks = [{ + name = "Home Manager"; + url = "https://wiki.nixos.org/wiki/Home_Manager"; + }]; + }; + wikipedia = { + name = "wikipedia"; + tags = [ "wiki" ]; + keyword = "wiki"; + url = "https://en.wikipedia.org/wiki/Special:Search?search=%s&go=Go"; + }; + kernel-org = { + name = "kernel.org"; + url = "https://www.kernel.org"; + }; + nix-sites = { + name = "Nix sites"; + bookmarks = [ + { + name = "homepage"; + url = "https://nixos.org/"; + } + { + name = "wiki"; + tags = [ "wiki" "nix" ]; + url = "https://wiki.nixos.org/"; + } + { + name = "Nix sites"; + bookmarks = [ + { + name = "homepage"; + url = "https://nixos.org/"; + } + { + name = "wiki"; + url = "https://wiki.nixos.org/"; + } + ]; + } + ]; + }; + }; + }; + } // { + nmt.script = '' + bookmarksUserJs=$(normalizeStorePaths \ + home-files/${cfg.configPath}/bookmarks/user.js) + + assertFileContent \ + $bookmarksUserJs \ + ${./expected-bookmarks-user.js} + + bookmarksFile="$(sed -n \ + '/browser.bookmarks.file/ {s|^.*\(/nix/store[^"]*\).*|\1|;p}' \ + $TESTED/home-files/${cfg.configPath}/bookmarks/user.js)" + + assertFileContent \ + $bookmarksFile \ + ${./expected-bookmarks.html} + ''; + }); +} diff --git a/tests/modules/programs/firefox/profiles/bookmarks/default.nix b/tests/modules/programs/firefox/profiles/bookmarks/default.nix index 2d647d03..635b760e 100644 --- a/tests/modules/programs/firefox/profiles/bookmarks/default.nix +++ b/tests/modules/programs/firefox/profiles/bookmarks/default.nix @@ -24,13 +24,6 @@ in { url = "https://wiki.nixos.org/wiki/Home_Manager"; }]; } - { - name = "wikipedia"; - tags = [ "wiki" ]; - keyword = "wiki"; - url = - "https://en.wikipedia.org/wiki/Special:Search?search=%s&go=Go"; - } { name = "kernel.org"; url = "https://www.kernel.org"; @@ -62,6 +55,13 @@ in { } ]; } + { + name = "wikipedia"; + tags = [ "wiki" ]; + keyword = "wiki"; + url = + "https://en.wikipedia.org/wiki/Special:Search?search=%s&go=Go"; + } ]; }; }; diff --git a/tests/modules/programs/firefox/profiles/bookmarks/expected-bookmarks.html b/tests/modules/programs/firefox/profiles/bookmarks/expected-bookmarks.html index 93bc195f..4f22e86c 100644 --- a/tests/modules/programs/firefox/profiles/bookmarks/expected-bookmarks.html +++ b/tests/modules/programs/firefox/profiles/bookmarks/expected-bookmarks.html @@ -10,7 +10,6 @@

Home Manager

-

wikipedia
kernel.org

Nix sites

@@ -22,4 +21,5 @@

wiki

+

wikipedia