Unverified Commit 716f0a73 authored by Artturin's avatar Artturin Committed by GitHub
Browse files

nixos/firefox: add autoConfigFiles (#321254)

parents 96a9741b ac3e0dba
Loading
Loading
Loading
Loading
+196 −149
Original line number Diff line number Diff line
{ pkgs, config, lib, ... }:
{
  pkgs,
  config,
  lib,
  ...
}:

let
  cfg = config.programs.firefox;
@@ -99,7 +104,13 @@ in
    };

    preferences = lib.mkOption {
      type = with lib.types; attrsOf (oneOf [ bool int str ]);
      type =
        with lib.types;
        attrsOf (oneOf [
          bool
          int
          str
        ]);
      default = { };
      description = ''
        Preferences to set from `about:config`.
@@ -112,7 +123,12 @@ in
    };

    preferencesStatus = lib.mkOption {
      type = lib.types.enum [ "default" "locked" "user" "clear" ];
      type = lib.types.enum [
        "default"
        "locked"
        "user"
        "clear"
      ];
      default = "locked";
      description = ''
        The status of `firefox.preferences`.
@@ -127,7 +143,8 @@ in

    languagePacks = lib.mkOption {
      # Available languages can be found in https://releases.mozilla.org/pub/firefox/releases/${cfg.package.version}/linux-x86_64/xpi/
      type = lib.types.listOf (lib.types.enum ([
      type = lib.types.listOf (
        lib.types.enum ([
          "ach"
          "af"
          "an"
@@ -231,7 +248,8 @@ in
          "xh"
          "zh-CN"
          "zh-TW"
      ]));
        ])
      );
      default = [ ];
      description = ''
        The language packs to install.
@@ -249,6 +267,19 @@ in
      '';
    };

    autoConfigFiles = lib.mkOption {
      type = with lib.types; listOf path;
      default = [ ];
      description = ''
        AutoConfig files can be used to set and lock preferences that are not covered
        by the policies.json for Mac and Linux. This method can be used to automatically
        change user preferences or prevent the end user from modifiying specific
        preferences by locking them. More info can be found in https://support.mozilla.org/en-US/kb/customizing-firefox-using-autoconfig.

        Files are concated and autoConfig is appended.
      '';
    };

    nativeMessagingHosts = ({
      packages = lib.mkOption {
        type = lib.types.listOf lib.types.package;
@@ -260,18 +291,28 @@ in
    }) // (builtins.mapAttrs (k: v: lib.mkEnableOption "${v.name} support") nmhOptions);
  };

  config = let
    forEachEnabledNmh = fn: lib.flatten (lib.mapAttrsToList (k: v: lib.optional cfg.nativeMessagingHosts.${k} (fn k v)) nmhOptions);
  in lib.mkIf cfg.enable {
    warnings = forEachEnabledNmh (k: v:
      "The `programs.firefox.nativeMessagingHosts.${k}` option is deprecated, " +
      "please add `${v.package.pname}` to `programs.firefox.nativeMessagingHosts.packages` instead."
  config =
    let
      forEachEnabledNmh =
        fn:
        lib.flatten (
          lib.mapAttrsToList (k: v: lib.optional cfg.nativeMessagingHosts.${k} (fn k v)) nmhOptions
        );
    in
    lib.mkIf cfg.enable {
      warnings = forEachEnabledNmh (
        k: v:
        "The `programs.firefox.nativeMessagingHosts.${k}` option is deprecated, "
        + "please add `${v.package.pname}` to `programs.firefox.nativeMessagingHosts.packages` instead."
      );
      programs.firefox.nativeMessagingHosts.packages = forEachEnabledNmh (_: v: v.package);

      environment.systemPackages = [
        (cfg.package.override (old: {
        extraPrefsFiles = old.extraPrefsFiles or [] ++ [(pkgs.writeText "firefox-autoconfig.js" cfg.autoConfig)];
          extraPrefsFiles =
            old.extraPrefsFiles or [ ]
            ++ cfg.autoConfigFiles
            ++ [ (pkgs.writeText "firefox-autoconfig.js" cfg.autoConfig) ];
          nativeMessagingHosts = old.nativeMessagingHosts or [ ] ++ cfg.nativeMessagingHosts.packages;
          cfg = (old.cfg or { }) // cfg.wrapperConfig;
        }))
@@ -288,20 +329,26 @@ in
      # Preferences are converted into a policy
      programs.firefox.policies = {
        DisableAppUpdate = true;
      Preferences = (builtins.mapAttrs
        (_: value: { Value = value; Status = cfg.preferencesStatus; })
        cfg.preferences);
      ExtensionSettings = builtins.listToAttrs (builtins.map
        (lang: lib.attrsets.nameValuePair
          "langpack-${lang}@firefox.mozilla.org"
          {
        Preferences = (
          builtins.mapAttrs (_: value: {
            Value = value;
            Status = cfg.preferencesStatus;
          }) cfg.preferences
        );
        ExtensionSettings = builtins.listToAttrs (
          builtins.map (
            lang:
            lib.attrsets.nameValuePair "langpack-${lang}@firefox.mozilla.org" {
              installation_mode = "normal_installed";
              install_url = "https://releases.mozilla.org/pub/firefox/releases/${cfg.package.version}/linux-x86_64/xpi/${lang}.xpi";
            }
        )
        cfg.languagePacks);
          ) cfg.languagePacks
        );
      };
    };

  meta.maintainers = with lib.maintainers; [ danth ];
  meta.maintainers = with lib.maintainers; [
    danth
    linsui
  ];
}