{ config, lib, pkgs, nur, ... }:
{
  config = lib.mkIf config.home.desktop.enable {
    programs.firefox = {
      enable = true;
      package = pkgs.librewolf;
      profiles = let
        # FireFox css, based on https://github.com/Dook97/firefox-qutebrowser-userchrome
        themeFont = ''
          --tab-font: '${config.look.fonts.main}';
          --urlbar-font: '${config.look.fonts.main}';
        '';
        themeMain = ''
          :root {
            --tab-active-bg-color: #${config.look.colors.prime};
            --tab-hover-bg-color: #${config.look.colors.accent};
            --tab-inactive-bg-color: #${config.look.colors.dark};
            --tab-active-fg-fallback-color: #FFFFFF;
            --tab-inactive-fg-fallback-color: #${config.look.colors.text};
            --urlbar-focused-bg-color: #${config.look.colors.dark};
            --urlbar-not-focused-bg-color: #${config.look.colors.dark};
            --toolbar-bgcolor: #${config.look.colors.dark} !important;
        '';
        themeAlt = ''
          :root {
            --tab-active-bg-color: #${config.look.colors.dark};
            --tab-hover-bg-color: #${config.look.colors.accent};
            --tab-inactive-bg-color: #${config.look.colors.prime};
            --tab-active-fg-fallback-color: #${config.look.colors.text};
            --tab-inactive-fg-fallback-color: #FFFFFF;
            --urlbar-focused-bg-color: #${config.look.colors.prime};
            --urlbar-not-focused-bg-color: #${config.look.colors.prime};
            --toolbar-bgcolor: #${config.look.colors.prime} !important;
        '';

        # Profile settings
        commonExtensions = with pkgs.nur.repos.rycee.firefox-addons; [
          bitwarden
          darkreader
          return-youtube-dislikes
          simple-tab-groups
          sponsorblock
          ublock-origin
          violentmonkey
        ];
        commonSearch = {
          force = true;
          default = "Ecosia";
          order = [
            "Ecosia"
            "NixPKGs"
            "MyNixOS"
            "YouTube"
            "Spotify"
          ];
          engines = {
            "Ecosia" = {
              urls = [{
                template = "https://www.ecosia.org/search";
                params = [{
                  name = "q";
                  value = "{searchTerms}";
                }];
              }];
              definedAliases = [ "@e" ];
            };
            "NixPKGs" = {
              urls = [{
                template = "https://search.nixos.org/packages";
                params = [
                  {
                    name = "type";
                    value = "packages";
                  }
                  {
                    name = "query";
                    value = "{searchTerms}";
                  }
                ];
              }];
              definedAliases = [ "@p" ];
            };
            "MyNixOS" = {
              urls = [{
                template = "https://mynixos.com/search";
                params = [{
                  name = "q";
                  value = "{searchTerms}";
                }];
              }];
              definedAliases = [ "@my" ];
            };
            "YouTube" = {
              urls = [{
                template = "https://www.youtube.com/results";
                params = [{
                  name = "search_query";
                  value = "{searchTerms}";
                }];
              }];
              definedAliases = [ "@y" ];
            };
            "Spotify" = {
              urls = [{ template = "https://open.spotify.com/search/{searchTerms}"; }];
              definedAliases = [ "@s" ];
            };
          };
        };
        commonSettings = {
          # Generic
          "browser.download.useDownloadDir" = true;
          "browser.shell.checkDefaultBrowser" = false;
          "browser.startup.page" = 3;
          "browser.urlbar.speculativeConnect.enabled" = false;

          # Hardware tweaks
          "clipboard.autocopy" = false;
          "general.autoScroll" = true;
          "middlemouse.paste" = false;
          "toolkit.tabbox.switchByScrolling" = true;
          "webgl.disabled" = false;

          # Fonts
          "font.name.serif.x-western" = config.look.fonts.main;
          "font.name.sans-serif.x-western" = config.look.fonts.main;
          "font.name.monospace.x-western" = config.look.fonts.mono;

          # Theming and visibility
          "browser.aboutConfig.showWarning" = false;
          "browser.compactmode.show" = true;
          "browser.tabs.inTitlebar" = 1;
          "browser.theme.content-theme" = 0;
          "browser.theme.toolbar-theme" = 0;
          "browser.toolbars.bookmarks.visibility" = "never";
          "browser.uidensity" = 1;
          "extensions.activeThemeID" = "firefox-compact-dark@mozilla.org";
          "toolkit.legacyUserProfileCustomizations.stylesheets" = true;

          # New tab
          "browser.newtabpage.activity-stream.asrouter.userprefs.cfr.addons" = false;
          "browser.newtabpage.activity-stream.asrouter.userprefs.cfr.features" = false;
          "browser.newtabpage.activity-stream.showSponsoredTopSites" = false;
          "browser.newtabpage.enabled" = false;

          # Safe browsing
          "browser.safebrowsing.downloads.enabled" = false;
          "browser.safebrowsing.downloads.remote.enabled" = false;
          "browser.safebrowsing.downloads.remote.block_uncommon" = false;
          "browser.safebrowsing.downloads.remote.block_potentially_unwanted" = false;
          "browser.safebrowsing.malware.enabled" = false;
          "browser.safebrowsing.phishing.enabled" = false;
          "browser.safebrowsing.blockedURIs.enabled" = false;
          "browser.safebrowsing.provider.google4.gethashURL" = false;
          "browser.safebrowsing.provider.google4.updateURL" = false;
          "browser.safebrowsing.provider.google.gethashURL" = false;
          "browser.safebrowsing.provider.google.updateURL" = false;

          # Extensions
          "extensions.pocket.enabled" = false;
          "extensions.formautofill.addresses.enabled" = false;
          "extensions.formautofill.creditCards.enabled" = false;
          "extensions.getAddons.showPane" = false;
          "extensions.htmlaboutaddons.discover.enabled" = false;
          "extensions.htmlaboutaddons.recommendations.enabled" = false;

          # Force extensions to work
          "extensions.autoDisableScopes" = 0;
          "extensions.enabledScopes" = 15;

          # Privacy
          "privacy.resistFingerprinting" = true;
          "privacy.firstparty.isolate" = true;
          "privacy.fingerprintingProtection" = true;
          "privacy.globalprivacycontrol.enabled" = true;
          "privacy.globalprivacycontrol.functionality.enabled" = true;
          "privacy.clearOnShutdown.cookies" = false;
          "privacy.clearOnShutdown_v2.cookiesAndStorage" = false;

          # Tracking URL blocking
          "urlclassifier.trackingSkipURLs" = "*.reddit.com, *.twitter.com, *.twimg.com, *.tiktok.com";
          "urlclassifier.features.socialtracking.skipURLs" = "*.instagram.com, *.twitter.com, *.twimg.com";

          # Network tracking
          "browser.contentblocking.category" = "strict";
          "network.cookie.cookieBehavior" = 1;
          "network.cookie.sameSite.noneRequiresSecure" = true;
          "network.http.referer.XOriginPolicy" = 2;
          "network.http.referer.XOriginTrimmingPolicy" = 2;
          "network.http.referer.trimmingPolicy" = 2;
          "network.trr.mode" = 3;
          "network.trr.uri" = "https://wikimedia-dns.org/dns-query";

          # HTTPS upgrading
          "dom.security.https_only_mode" = true;
          "dom.security.https_only_mode_ever_enabled" = true;
          "dom.security.https_only_mode_ever_enabled_pbm" = true;

          # Sensors
          "device.sensors.motion.enabled" = false;
          "dom.battery.enabled" = false;
          "dom.private-attribution.submission.enabled" = false;

          # Telemetry
          "browser.discovery.enabled" = false;
          "browser.send_pings" = false;
          "datareporting.healthreport.uploadEnabled" = false;
          "identity.fxaccounts.enabled" = false;
          "security.OCSP.require" = true;
          "services.sync.engine.addresses.available" = false;
          "svg.context-properties.content.enabled" = true;
        };
      in {
        Main = {
          id = 0;
          isDefault = true;
          extensions = commonExtensions;
          search = commonSearch;
          settings = commonSettings;
          userChrome = ''
            ${themeMain}
            ${themeFont}
            ${builtins.readFile ./quteFox.css}
          '';
        };
        Alt = {
          id = 1;
          extensions = commonExtensions;
          search = commonSearch;
          settings = commonSettings;
          userChrome = ''
            ${themeAlt}
            ${themeFont}
            ${builtins.readFile ./quteFox.css}
          '';
        };
      };
    };

    # Symlinks to Librewolf
    home = {
      file = {
        ".librewolf".source = config.lib.file.mkOutOfStoreSymlink "${config.home.homeDirectory}/.mozilla/firefox";
        ".mozilla/librewolf".source = config.lib.file.mkOutOfStoreSymlink "${config.home.homeDirectory}/.mozilla/firefox";
      };
      persistence."/persist${config.home.homeDirectory}".directories = [ ".mozilla" ];
    };
  };
}
