{
  marleylib,
  config,
  osConfig,
  lib,
  pkgs,
  ...
}: let
  inherit (marleylib.module) mkEnableOption';

  cfg = config.marleyos.programs.floorp;
  osCfg = osConfig.marleyos.programs.floorp.enable or false;
in {
  options.marleyos.programs.floorp.enable = mkEnableOption' "floorp" osCfg;

  config = lib.mkIf cfg.enable {
    nixpkgs.overlays = [
      (import ../../../overlays/firefox-addons {
        inherit lib;
        inherit pkgs;
      })
    ];

    # FIXME: switch to this once upgraded to 25.05
    # stylix.targets.floorp = {
    #   enable = true;
    #   colorTheme.enable = true;
    #   profileNames = [config.marleyos.my.name];
    # };

    programs.floorp = {
      enable = true;
      package = lib.mkIf pkgs.stdenv.isDarwin pkgs.emptyDirectory;

      profiles = {
        "${config.marleycfg.my.name}" = {
          extensions =
            (with pkgs.nur.repos.rycee.firefox-addons; [
              archivebox-exporter
              bitwarden
              catppuccin-web-file-icons
              clearurls
              consent-o-matic
              darkreader
              facebook-container
              firefox-color
              kagi-search
              # libredirect
              # proton-vpn
              raindropio
              refined-github
              shinigami-eyes
              snowflake
              stylus
              tampermonkey
              ublock-origin
              # vue-js-devtools
              # wappalyzer
              wayback-machine
              # xdebug-helper-for-firefox
            ])
            ++ (with pkgs.marleyos.firefox-addons; [
              axe-devtools
              # clockwork-dev-tools
              medium-parser
              readwise
              readwise-highlighter
            ]);

          settings = {
            # https://arkenfox.github.io/gui/
            #
            # Introduction - - - - - - - - - - - - - - - - - - - - - - - - - - -
            "browser.aboutConfig.showWarning" = false;

            # Startup - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
            "browser.startup.page" = 1; # homepage
            "browser.startup.homepage" = "https://punkfairie.net/start/";
            "browser.newtabpage.enabled" = false;
            "browser.newtabpage.activity-stream.showSponsored" = false;
            "browser.newtabpage.activity-stream.showSponsoredTopSites" = false;
            "browser.newtabpage.activity-stream.default.sites" = "";

            # Geolocation - - - - - - - - - - - - - - - - - - - - - - - - - - -
            "geo.provider.use_corelocation" = false; # mac
            "geo.provider.use_geoclue" = false; # linux

            # Quieter Fox - - - - - - - - - - - - - - - - - - - - - - - - - - -
            # Recommendations
            "extensions.getAddons.showPane" = false; # uses google analytics
            "extensions.htmlaboutaddons.recommendations.enabled" = false;
            "browser.discovery.enabled" = false;
            "browser.shopping.experience2023.enabled" = false;

            # Telemetry
            "datareporting.policy.dataSubmissionEnabled" = false;
            "datareporting.healthreport.uploadEnabled" = false;
            "toolkit.telemetry.unified" = false;
            "toolkit.telemetry.enabled" = false;
            "toolkit.telemetry.server" = "data:,";
            "toolkit.telemetry.archive.enabled" = false;
            "toolkit.telemetry.newProfilePing.enabled" = false;
            "toolkit.telemetry.shutdownPingSender.enabled" = false;
            "toolkit.telemetry.updatePing.enabled" = false;
            "toolkit.telemetry.bhrPing.enabled" = false;
            "toolkit.telemetry.firstShutdownPing.enabled" = false;
            "toolkit.telemetry.coverage.opt-out" = true;
            "toolkit.coverage.opt-out" = true;
            "toolkit.coverage.endpoint.base" = "";
            "browser.newtabpage.activity-stream.feeds.telemetry" = false;
            "browser.newtabpage.activity-stream.telemetry" = false;

            # Studies
            "app.shield.optoutstudies.enabled" = false;
            "app.normandy.enabled" = false;
            "app.normandy.api_url" = "";

            # Crash Reports
            "breakpad.reportURL" = "";
            "browser.tabs.crashReporting.sendReport" = false;
            "browser.crashReports.unsubmittedCheck.autoSubmit2" = false;

            # Other
            "captivedetect.canonicalURL" = "";
            "network.captive-portal-service.enabled" = false;
            "netowkr.connectivity-service.enabled" = false;

            # Safe Browsing - - - - - - - - - - - - - - - - - - - - - - - - - -
            # Sends info to google.
            "browser.safebrowsing.downloads.remote.enabled" = false;

            # Block Implicit Outbound - - - - - - - - - - - - - - - - - - - - -
            # Disables behavior related to non-explicitly asked for browsing
            # (speculative fetching of links not clicked on).
            "network.prefetch-next" = false;
            "network.dns.disablePrefetch" = true;
            "network.dns.disablePrefetchFromHTTPS" = true;
            "network.predictor.enabled" = false;
            "network.predictor.enable-prefetch" = false;
            "network.http.speculative-parallel-limit" = 0;
            "browser.places.speculativeConnect.enabled" = false;

            # DNS / DoH / PROXY / SOCKS - - - - - - - - - - - - - - - - - - - -
            "network.proxy.socks_remote_dns" = true;
            "network.file.disable_unc_paths" = true;
            "network.gio.supported-protocols" = "";

            # Location Bar / Search Bar / Suggestions / History / Forms - - - -
            "browser.urlbar.speculativeConnect.enabled" = false;
            "browser.urlbar.quicksuggest.enabled" = false;
            "browser.urlbar.suggest.quicksuggest.nonsponsored" = false;
            "browser.urlbar.suggest.quicksuggest.sponsored" = false;
            "browser.urlbar.trending.featureGate" = false;
            "browser.urlbar.addons.featureGate" = false;
            "browser.urlbar.mdn.featureGate" = false;
            "browser.urlbar.pocket.featureGate" = false;
            "browser.urlbar.weather.featureGate" = false;
            "browser.urlbar.yelp.featureGate" = false;
            "browser.formfill.enable" = false;

            # Passwords - - - - - - - - - - - - - - - - - - - - - - - - - - - -
            "signon.autofillForms" = false;
            "signon.formlessCapture.enabled" = false;
            "network.auth.subresource-http-auth-allow" = 1;

            # Disk Avoidance - - - - - - - - - - - - - - - - - - - - - - - - - -
            "browser.privatebrowsing.forceMediaMemoryCache" = true;
            "media.memory_cache_max_size" = 65536;
            "browser.sessionstore.privacy_level" = 2;

            # HTTPS (SSL/TLS / OCSP / CERTS / HPKP) - - - - - - - - - - - - - -
            # SSL (Secure Sockets Layer) / TLS (Transport Layer Security)
            "security.ssl.require_safe_negotiation" = true;
            "security.tls.enable_0rtt_data" = false;

            # OCSP (Online Certificate Status Protocol)
            "security.OCSP.enabled" = 1;
            "security.OCSP.require" = true;

            # CERTS / HPKP (HTTP Public Key Pinning)
            "security.cert_pinning.enforcement_level" = 2;
            "security.remote_settings.crlite_filters.enabled" = true;
            "security.pki.crlite_mode" = 2;

            # Mixed Content
            "dom.security.https_only_mode" = true;
            "dom.security.https_only_mode_send_http_background_request" = false;

            # UI (User Interface)
            "security.ssl.treat_unsafe_negotiation_as_broken" = true;
            "browser.xul.error_pages.expert_bad_cert" = true;

            # Referrers - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
            "network.http.referer.XOriginTrimmingPolicy" = 2;

            # Containers - - - - - - - - - - - - - - - - - - - - - - - - - - - -
            "privacy.userContext.enabled" = true;
            "privacy.userContext.ui.enabled" = true;

            # Plugins / Media / WebRTC - - - - - - - - - - - - - - - - - - - - -
            "media.peerconnection.ice.proxy_only_if_behind_proxy" = true;
            "media.peerconnection.ice.default_address_only" = true;

            # DOM (Document Object Model) - - - - - - - - - - - - - - - - - - -
            "dom.disable_window_move_resize" = true;

            # Miscellaneous - - - - - - - - - - - - - - - - - - - - - - - - - -
            "browser.download.start_downloads_in_tmp_dir" = true;
            "browser.helperApps.deleteTempFileOnExit" = true;
            "browser.uitour.enabled" = false;
            "devtools.debugger.remote-enabled" = false;
            "permissions.manager.defaultsUrl" = "";
            "webchannel.allowObject.urlWhiteList" = "";
            "network.IDN_show_punycode" = true;
            "browser.tabs.searchclipboardfor.middleclick" = false;
            "browser.contentanalysis.enabled" = false;
            "browser.contentanalysis.default_result" = 0;

            # Downloads
            "browser.download.alwaysOpenPanel" = false;
            "browser.download.manager.addToRecentDocs" = false;
            "browser.download.always_ask_before_handling_new_types" = true;

            # Extensions
            "extensions.enabledScopes" = 5;
            "extensions.autoDisableScopes" = 0; # auto-enable extensions
            "extensions.postDownloadThirdPartyPrompt" = false;

            # ETP (Enhanced Tracking Protection) - - - - - - - - - - - - - - - -
            "browser.contentblocking.category" = "strict";

            # Shutdown & Sanitizing - - - - - - - - - - - - - - - - - - - - - -
            "privacy.sanitize.sanitizeOnShutdown" = true;

            # Sanitize On Shutdown: Ignores "Allow" Site Exceptions
            "privacy.clearOnShutdown.cache" = true;
            "privacy.clearOnShutdown_v2.cache" = true;
            "privacy.clearOnShutdown.downloads" = false;
            "privacy.clearOnShutdown.formdata" = true;
            "privacy.clearOnShutdown.history" = false;
            "privacy.clearOnShutdown_v2.historyFormDataAndDownloads" = false;

            # Sanitize On Shutdown: Respects "Allow" Site Exceptions
            "privacy.clearOnShutdown.cookies" = true;
            "privacy.clearOnShutdown.offlineApps" = true;
            "privacy.clearOnShutdown.sessions" = true;
            "privacy.clearOnShutdown_v2.cookiesAndStorage" = true;

            # Sanitize Site Data: Ignores "Allow" Site Exceptions
            "privacy.clearSiteData.cache" = true;
            "privacy.clearSiteData.cookiesAndStorage" = false;
            "privacy.clearSiteData.historyFormDataAndDownloads" = false;
            "privacy.cpd.cache" = true;
            "privacy.clearHistory.cache" = true;
            "privacy.cpd.formdata" = true;
            "privacy.cpd.history" = false;
            "privacy.clearHistory.historyFormDataAndDownloads" = true;
            "privacy.cpd.cookies" = false;
            "privacy.cpd.sessions" = true;
            "privacy.cpd.offlineApps" = false;
            "privacy.clearHistory.cookiesAndStorage" = false;
            "privacy.cpd.passwords" = false;

            # Sanitize Manual: Timerange
            "privacy.sanitize.timeSpan" = 0; # everything

            # Optional RFP (resistFingerprinting) - - - - - - - - - - - - - - -
            "privacy.window.maxInnerWidth" = 1200;
            "privacy.window.maxInnerHeight" = 900;
            "privacy.resistFingerprinting.block_mozAddonManager" = true;
            "privacy.spoof_english" = 1;
            "browser.display.use_system_colors" = false;
            "browser.link.open_newwindow" = 3; # open in new tab instead
            "browser.link.open_newwindow.restriction" = 0;

            # Optional OpSec - - - - - - - - - - - - - - - - - - - - - - - - - -
            "signon.rememberSignons" = false;
            "extensions.formautofill.addresses.enabled" = false;
            "extensions.formautofill.creditCards.enabled" = false;

            # Don't Touch - - - - - - - - - - - - - - - - - - - - - - - - - - -
            "extensions.blocklist.enabled" = true;
            "network.http.referer.spoofSource" = false;
            "security.dialog_enable_delay" = 1000;
            "privacy.firstparty.isolate" = false;
            "extensions.webcompat.enable_shims" = true;
            "security.tls.version.enable-deprecated" = false;
            "extensions.quarantinedDomains.enabled" = false;

            # Non-Project Related - - - - - - - - - - - - - - - - - - - - - - -
            "browser.startup.homepage_override.mstone" = "ignore";
            "browser.newtabpage.activity-stream.asrouter.userprefs.cfr.addons" =
              false;
            "browser.newtabpage.activity-stream.asrouter.userprefs.cfr.features" =
              false;
            "browser.urlbar.showSearchTerms.enabled" = false;

            # Floorp - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
            "floorp.lepton.interface" = 3;
            "userChrome.tab.lepton_like_padding" = false;
            "userChrome.autohide.back_button" = true;
            "userChrome.autohide.forward_button" = true;
            "userChrome.autohide.navbar" = false;
            "userChrome.autohide.page_action" = true;
            "floorp.browser.sidebar.enable" = false;
            "floorp.browser.sidebar2.data" = ''
              {"data":{"floorp__history":{"url":"floorp//history","width":415},"floorp__downloads":{"url":"floorp//downloads","width":415},"floorp__notes":{"url":"floorp//notes","width":550}},"index":["floorp__history","floorp__downloads","floorp__notes"]}
            '';
            "floorp.tabbar.style" = 2;
            "floorp.verticaltab.hover.enabled" = false;
            "floorp.verticaltab.show.newtab.button" = true;

            # marleyOS - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
            "app.update.channel" = "default"; # disable updates
            "browser.search.region" = "US";
            "browser.shell.checkDefaultBrowser" = false;
            "browser.toolbars.bookmarks.visibility" = "never";
            "browser.uiCustomization.state" = ''
              {"placements":{"widget-overflow-fixed-list":[],"unified-extensions-area":["search_kagi_com-browser-action","_a4c4eda4-fb84-4a84-b4a1-f7c1cbf2a1ad_-browser-action","gdpr_cavi_au_dk-browser-action","_bbb880ce-43c9-47ae-b746-c3e0096c5b76_-browser-action","_contain-facebook-browser-action","addon_darkreader_org-browser-action","_b11bea1f-a888-4332-8d8a-cec2be7d24b9_-browser-action","firefox_tampermonkey_net-browser-action","_74145f27-f039-47ce-a470-a662b129930a_-browser-action","_7a7a4a92-a2a0-41d1-9fd7-1e92480d612d_-browser-action","_f7619bc3-ed22-44a3-83ad-e79a78416737_-browser-action","vpn_proton_ch-browser-action","_1c5e4c6f-5530-49a3-b216-31ce7d744db0_-browser-action","user-agent-switcher_ninetailed_ninja-browser-action","medium-parser_example_com-browser-action","pywalfox_frewacom_org-browser-action","wappalyzer_crunchlabz_com-browser-action","team_readwise_io-browser-action","_e4a8a97b-f2ed-450b-b12d-ee082ba24781_-browser-action","_axe-firefox-devtools-browser-action"],"nav-bar":["back-button","forward-button","stop-reload-button","customizableui-special-spring1","urlbar-container","customizableui-special-spring2","save-to-pocket-button","downloads-button","unified-extensions-button","ublock0_raymondhill_net-browser-action","newtaboverride_agenedia_com-browser-action","wayback_machine_mozilla_org-browser-action","_446900e4-71c2-419f-a6a7-df9c091e268b_-browser-action","jid0-adyhmvsp91nuo8prv0mn2vkeb84_jetpack-browser-action"],"toolbar-menubar":["menubar-items"],"TabsToolbar":["tabbrowser-tabs","new-tab-button","alltabs-button"],"PersonalToolbar":["import-button","personal-bookmarks"],"statusBar":["screenshot-button","fullscreen-button","status-text"]},"seen":["search_kagi_com-browser-action","wayback_machine_mozilla_org-browser-action","_a4c4eda4-fb84-4a84-b4a1-f7c1cbf2a1ad_-browser-action","gdpr_cavi_au_dk-browser-action","_bbb880ce-43c9-47ae-b746-c3e0096c5b76_-browser-action","_contain-facebook-browser-action","addon_darkreader_org-browser-action","jid0-adyhmvsp91nuo8prv0mn2vkeb84_jetpack-browser-action","ublock0_raymondhill_net-browser-action","_b11bea1f-a888-4332-8d8a-cec2be7d24b9_-browser-action","firefox_tampermonkey_net-browser-action","_74145f27-f039-47ce-a470-a662b129930a_-browser-action","_446900e4-71c2-419f-a6a7-df9c091e268b_-browser-action","_7a7a4a92-a2a0-41d1-9fd7-1e92480d612d_-browser-action","developer-button","profile-manager","sidebar-reverse-position-toolbar","undo-closed-tab","workspaces-toolbar-button","_f7619bc3-ed22-44a3-83ad-e79a78416737_-browser-action","vpn_proton_ch-browser-action","_1c5e4c6f-5530-49a3-b216-31ce7d744db0_-browser-action","user-agent-switcher_ninetailed_ninja-browser-action","medium-parser_example_com-browser-action","newtaboverride_agenedia_com-browser-action","pywalfox_frewacom_org-browser-action","wappalyzer_crunchlabz_com-browser-action","team_readwise_io-browser-action","_e4a8a97b-f2ed-450b-b12d-ee082ba24781_-browser-action","_axe-firefox-devtools-browser-action"],"dirtyAreaCache":["unified-extensions-area","nav-bar","statusBar","PersonalToolbar","toolbar-menubar","TabsToolbar"],"currentVersion":20,"newElementCount":5}
            '';
            "identity.fxaccounts.account.device.name" = "Floorp @ Nyx";
            "services.sync.declinedEngines" = "passwords,addons,history,forms,addresses,tabs,creditcards";
            "services.sync.engine.addons" = false;
            "services.sync.engine.history" = false;
            "services.sync.engine.passwords" = false;
            "services.sync.engine.tabs" = false;

            # 2 = auto; 1 = light; 0 = dark
            "layout.css.prefers-color-scheme.content-override" = 0;

            # FIXME: Switch to stylix module once it's available for Floorp.
            "font.name.monospace.x-western" =
              config.stylix.fonts.monospace.name;
            "font.name.sans-serif-x-western" =
              config.stylix.fonts.sansSerif.name;
            "font.name.serif.x-western" = config.stylix.fonts.serif.name;
          };
        };
      };
    };
  };
}
