self: {
  config,
  lib,
  pkgs,
  ...
}: let
  inherit (pkgs.scopedPackages) firefoxAddons;

  inherit (lib) attrsToList attrValues mkIf mkOption singleton types;

  mainProfile = "dev-edition-default";
  cfg = config.programs.firefox;

  custom-css = pkgs.callPackage ./custom-css {};
in {
  options.programs.firefox.enableCustomConf = mkOption {
    type = types.bool;
    default = false;
  };

  config = mkIf cfg.enableCustomConf {
    programs.firefox = {
      enable = true;

      package = pkgs.firefox-devedition;

      profiles.${mainProfile} = {
        isDefault = true;
        id = 0;

        userChrome = ''
          @import url("file://${custom-css}");
        '';

        settings = {
          # Developer Edition Settings
          "xpinstall.signatures.required" = false;
          "extensions.experiments.enabled" = true;

          # Use the normal file picker
          "widget.use-xdg-desktop-portal.file-picker" = 0;

          # Firefox-gx user.js
          /*
          Default rules
          */
          "toolkit.legacyUserProfileCustomizations.stylesheets" = true;
          "svg.context-properties.content.enabled" = true;
          "layout.css.color-mix.enabled" = true;
          "browser.tabs.delayHidingAudioPlayingIconMS" = 0;
          "layout.css.backdrop-filter.enabled" = true;
          "browser.newtabpage.activity-stream.improvesearch.handoffToAwesomebar" = false;
          "browser.newtabpage.activity-stream.newtabWallpapers.enabled" = true;
          "browser.newtabpage.activity-stream.newtabWallpapers.v2.enabled" = true;

          /*
          To activate container tabs without any extension
          */
          "privacy.userContext.enabled" = true;
          "privacy.userContext.ui.enabled" = true;
          "privacy.userContext.longPressBehavior" = 2;

          # Open previous windows and tabs
          "browser.startup.page" = 3;

          # Prefs
          "apz.overscroll.enabled" = false;
          "layout.css.devPixelsPerPx" = 1.12;
          "browser.search.widget.inNavBar" = true;
          "browser.toolbars.bookmarks.visibility" = "always";
          "browser.toolbars.bookmarks.showInPrivateBrowsing" = true;
          "ui.key.menuAccessKey" = 0;

          # remove telemetry
          "datareporting.policy.dataSubmissionEnabled" = false;
          "datareporting.healthreport.uploadEnabled" = false;
          "datareporting.healthreport.infoURL" = "";
          "dom.security.https_only_mode" = true;

          # remove first run and warning stuff
          "datareporting.policy.firstRunURL" = "";
          "extensions.autoDisableScopes" = 0;
          "browser.aboutwelcome.enabled" = false;
          "browser.aboutConfig.showWarning" = false;

          # Disable firefox autofill
          "signon.rememberSignons" = false;
          "extensions.formautofill.addresses.enabled" = false;
          "extensions.formautofill.creditCards.enabled" = false;

          # remove new tab stuff
          "extensions.pocket.enabled" = false;
          "services.sync.prefs.sync.browser.newtabpage.activity-stream.feeds.section.topstories" = false;
          "browser.newtabpage.activity-stream.section.highlights.includePocket" = false;
          "browser.newtabpage.activity-stream.feeds.system.topstories" = false;
          "browser.newtabpage.activity-stream.feeds.section.topstories" = false;
          "browser.newtabpage.activity-stream.feeds.topsites" = false;
          "browser.newtabpage.activity-stream.asrouter.userprefs.cfr.addons" = false;
          "browser.newtabpage.activity-stream.asrouter.userprefs.cfr.features" = false;
        };

        search = {
          default = "searxng";
          force = true;

          engines = {
            searxng = {
              name = "SearXNG";
              urls = singleton {
                template = "https://search.nelim.org/search";
                params = attrsToList {
                  "q" = "{searchTerms}";
                };
              };
              icon = "https://search.nelim.org/favicon.ico";
              definedAliases = ["@s"];
            };

            code = {
              name = "Github Search Code";
              urls = singleton {
                template = "https://github.com/search";
                params = attrsToList {
                  "type" = "code";
                  "q" = "NOT is:fork {searchTerms}";
                };
              };
              icon = "https://icon.horse/icon/github.com";
              definedAliases = ["@gs"];
            };

            nixcode = {
              name = "Github Nix Code";
              urls = singleton {
                template = "https://github.com/search";
                params = attrsToList {
                  "type" = "code";
                  "q" = "lang:Nix NOT is:fork {searchTerms}";
                };
              };
              icon = "https://icon.horse/icon/github.com";
              definedAliases = ["@gn"];
            };

            nixpkgs = {
              name = "Nixpkgs";
              urls = singleton {
                template = "https://github.com/search";
                params = attrsToList {
                  "type" = "code";
                  "q" = "repo:NixOS/nixpkgs {searchTerms}";
                };
              };
              icon = "https://icon.horse/icon/github.com";
              definedAliases = ["@pkgs"];
            };

            nixwiki = {
              name = "NixOS Wiki";
              urls = singleton {
                template = "https://wiki.nixos.org/w/index.php";
                params = attrsToList {
                  "search" = "{searchTerms}";
                };
              };
              icon = "https://wiki.nixos.org/favicon.ico";
              definedAliases = ["@nw"];
            };

            mynixos = {
              name = "MyNixos";
              urls = singleton {
                template = "https://mynixos.com/search";
                params = attrsToList {
                  "q" = "{searchTerms}";
                };
              };
              icon = "https://mynixos.com/favicon.ico";
              definedAliases = ["@mn"];
            };

            noogle = {
              name = "Noogle";
              urls = singleton {
                template = "https://noogle.dev/q";
                params = attrsToList {
                  "term" = "{searchTerms}";
                };
              };
              icon = "https://noogle.dev/favicon.ico";
              definedAliases = ["@ng"];
            };

            extensions = {
              name = "Firefox Add-ons";
              urls = singleton {
                template = "https://addons.mozilla.org/en-US/firefox/search";
                params = attrsToList {
                  "q" = "{searchTerms}";
                };
              };
              icon = "https://addons.mozilla.org/favicon.ico";
              definedAliases = ["@fa"];
            };

            protondb = {
              name = "ProtonDB";
              urls = singleton {
                template = "https://www.protondb.com/search";
                params = attrsToList {
                  "q" = "{searchTerms}";
                };
              };
              icon = "https://www.protondb.com/favicon.ico";
              definedAliases = ["@pdb"];
            };

            youtube = {
              name = "YouTube";
              urls = singleton {
                template = "https://www.youtube.com/results";
                params = attrsToList {
                  "search_query" = "{searchTerms}";
                };
              };
              icon = "https://www.youtube.com/favicon.ico";
              definedAliases = ["@yt" "@youtube"];
            };

            bing.metaData.hidden = true;
            google.metaData.hidden = true;
            ebay.metaData.hidden = true;
          };

          order = [
            "searxng"
            "ddg"
            "mynixos"
            "nixwiki"
            "code"
            "nixcode"
            "nixpkgs"
            "noogle"
            "wikipedia"
            "youtube"
            "extensions"
            "protondb"
          ];
        };

        extensions.packages = attrValues {
          inherit
            (firefoxAddons)
            auto-refresh-page
            bitwarden
            darkreader
            floccus
            frankerfacez
            google-container
            image-search-options
            istilldontcareaboutcookies
            return-youtube-dislikes
            sponsorblock
            sound-volume
            stylus
            ttv-lol-pro
            ublock-origin
            undoclosetabbutton
            ;
        };
      };
    };
  };

  # For accurate stack trace
  _file = ./default.nix;
}