3
0
Fork 0
forked from mirrors/nixpkgs
nixpkgs/pkgs/applications/networking/browsers/firefox/wrapper.nix

376 lines
14 KiB
Nix
Raw Normal View History

2020-12-02 08:50:59 +00:00
{ stdenv, lib, makeDesktopItem, makeWrapper, lndir, config
2021-02-01 08:05:09 +00:00
, replace, fetchurl, zip, unzip, jq, xdg-utils, writeText
## various stuff that can be plugged in
, ffmpeg, xorg, alsaLib, libpulseaudio, libcanberra-gtk2, libglvnd, libnotify
, gnome3/*.gnome-shell*/
, browserpass, chrome-gnome-shell, uget-integrator, plasma5Packages, bukubrow, pipewire
, tridactyl-native
, fx_cast_bridge
, udev
2017-09-27 14:30:29 +01:00
, kerberos
2020-03-04 07:21:06 +00:00
, libva
, mesa # firefox wants gbm for drm+dmabuf
}:
## configurability of the wrapper itself
2017-11-16 15:39:16 +00:00
browser:
let
2017-11-16 15:39:16 +00:00
wrapper =
{ browserName ? browser.browserName or (lib.getName browser)
, pname ? browserName
, version ? lib.getVersion browser
2017-11-16 15:39:16 +00:00
, desktopName ? # browserName with first letter capitalized
(lib.toUpper (lib.substring 0 1 browserName) + lib.substring 1 (-1) browserName)
, nameSuffix ? ""
, icon ? browserName
, extraNativeMessagingHosts ? []
, pkcs11Modules ? []
, forceWayland ? false
, useGlvnd ? true
, cfg ? config.${browserName} or {}
2020-06-28 14:55:07 +01:00
## Following options are needed for extra prefs & policies
2020-11-30 15:30:46 +00:00
# For more information about anti tracking (german website)
# visit https://wiki.kairaven.de/open/app/firefox
2020-06-28 14:55:07 +01:00
, extraPrefs ? ""
2020-11-30 15:30:46 +00:00
# For more information about policies visit
# https://github.com/mozilla/policy-templates#enterprisepoliciesenabled
2020-06-28 14:55:07 +01:00
, extraPolicies ? {}
2020-11-30 15:30:46 +00:00
, firefoxLibName ? "firefox" # Important for tor package or the like
, nixExtensions ? null
2017-11-16 15:39:16 +00:00
}:
assert forceWayland -> (browser ? gtk3); # Can only use the wayland backend if gtk3 is being used
2017-11-16 15:39:16 +00:00
let
ffmpegSupport = browser.ffmpegSupport or false;
gssSupport = browser.gssSupport or false;
alsaSupport = browser.alsaSupport or false;
pipewireSupport = browser.pipewireSupport or false;
2017-11-16 15:39:16 +00:00
2021-02-07 22:02:11 +00:00
# FIXME: This should probably be an assertion now?
2017-11-16 15:39:16 +00:00
plugins =
let
removed = lib.filter (a: builtins.hasAttr a cfg) [
2021-02-07 22:02:11 +00:00
"enableAdobeFlash"
"enableAdobeReader"
"enableBluejeans"
"enableDjvu"
2021-02-07 22:02:11 +00:00
"enableFriBIDPlugin"
"enableGoogleTalkPlugin"
"enableMPlayer"
2021-02-07 22:02:11 +00:00
"enableVLC"
"icedtea"
2021-02-07 22:02:11 +00:00
"jre"
];
2021-02-07 22:02:11 +00:00
in if removed != [] then
throw "Your configuration mentions ${lib.concatMapStringsSep ", " (p: browserName + "." + p) removed}. All plugin related options have been removed, since Firefox from version 52 onwards no longer supports npapi plugins (see https://support.mozilla.org/en-US/kb/npapi-plugins)."
else
[]
;
2017-11-16 15:39:16 +00:00
nativeMessagingHosts =
([ ]
++ lib.optional (cfg.enableBrowserpass or false) (lib.getBin browserpass)
++ lib.optional (cfg.enableBukubrow or false) bukubrow
++ lib.optional (cfg.enableTridactylNative or false) tridactyl-native
++ lib.optional (cfg.enableGnomeExtensions or false) chrome-gnome-shell
2018-04-30 03:41:36 +01:00
++ lib.optional (cfg.enableUgetIntegrator or false) uget-integrator
++ lib.optional (cfg.enablePlasmaBrowserIntegration or false) plasma5Packages.plasma-browser-integration
++ lib.optional (cfg.enableFXCastBridge or false) fx_cast_bridge
++ extraNativeMessagingHosts
2017-11-16 15:39:16 +00:00
);
libs = lib.optionals stdenv.isLinux [ udev libva mesa libnotify xorg.libXScrnSaver ]
++ lib.optional (pipewireSupport && lib.versionAtLeast version "83") pipewire
2020-06-13 03:31:01 +01:00
++ lib.optional ffmpegSupport ffmpeg
2017-11-16 15:39:16 +00:00
++ lib.optional gssSupport kerberos
++ lib.optional useGlvnd libglvnd
2017-11-16 15:39:16 +00:00
++ lib.optionals (cfg.enableQuakeLive or false)
2018-06-25 02:49:30 +01:00
(with xorg; [ stdenv.cc libX11 libXxf86dga libXxf86vm libXext libXt alsaLib zlib ])
++ lib.optional (config.pulseaudio or true) libpulseaudio
++ lib.optional alsaSupport alsaLib
++ pkcs11Modules;
gtk_modules = [ libcanberra-gtk2 ];
2017-11-16 15:39:16 +00:00
2020-06-28 14:55:07 +01:00
#########################
# #
# EXTRA PREF CHANGES #
# #
#########################
policiesJson = writeText "policies.json" (builtins.toJSON enterprisePolicies);
2020-06-28 14:55:07 +01:00
usesNixExtensions = nixExtensions != null;
nameArray = builtins.map(a: a.name) (if usesNixExtensions then nixExtensions else []);
# Check that every extension has a unqiue .name attribute
# and an extid attribute
extensions = if nameArray != (lib.unique nameArray) then
throw "Firefox addon name needs to be unique"
else builtins.map (a:
2020-11-30 15:30:46 +00:00
if ! (builtins.hasAttr "extid" a) then
throw "nixExtensions has an invalid entry. Missing extid attribute. Please use fetchfirefoxaddon"
2020-11-30 15:30:46 +00:00
else
a
) (if usesNixExtensions then nixExtensions else []);
2020-06-28 14:55:07 +01:00
enterprisePolicies =
{
policies = lib.optionalAttrs usesNixExtensions {
2020-06-28 14:55:07 +01:00
DisableAppUpdate = true;
} //
lib.optionalAttrs usesNixExtensions {
2020-06-28 14:55:07 +01:00
ExtensionSettings = {
"*" = {
blocked_install_message = "You can't have manual extension mixed with nix extensions";
installation_mode = "blocked";
};
} // lib.foldr (e: ret:
ret // {
"${e.extid}" = {
installation_mode = "allowed";
};
}
) {} extensions;
} //
{
Extensions = {
Install = lib.foldr (e: ret:
ret ++ [ "${e.outPath}/${e.extid}.xpi" ]
) [] extensions;
};
}
2020-06-28 14:55:07 +01:00
// extraPolicies;
};
mozillaCfg = writeText "mozilla.cfg" ''
// First line must be a comment
2020-06-28 14:55:07 +01:00
// Disables addon signature checking
// to be able to install addons that do not have an extid
// Security is maintained because only user whitelisted addons
// with a checksum can be installed
${ lib.optionalString usesNixExtensions ''lockPref("xpinstall.signatures.required", false)'' };
2020-06-28 14:55:07 +01:00
${extraPrefs}
'';
#############################
# #
# END EXTRA PREF CHANGES #
# #
#############################
2017-11-16 15:39:16 +00:00
in stdenv.mkDerivation {
inherit pname version;
2017-11-16 15:39:16 +00:00
desktopItem = makeDesktopItem {
name = browserName;
exec = "${browserName}${nameSuffix} %U";
inherit icon;
comment = "";
desktopName = "${desktopName}${nameSuffix}${lib.optionalString forceWayland " (Wayland)"}";
2017-11-16 15:39:16 +00:00
genericName = "Web Browser";
categories = "Network;WebBrowser;";
2021-01-15 05:42:41 +00:00
mimeType = lib.concatStringsSep ";" [
2017-11-16 15:39:16 +00:00
"text/html"
"text/xml"
"application/xhtml+xml"
"application/vnd.mozilla.xul+xml"
"x-scheme-handler/http"
"x-scheme-handler/https"
"x-scheme-handler/ftp"
];
};
nativeBuildInputs = [ makeWrapper lndir ];
buildInputs = lib.optional (browser ? gtk3) browser.gtk3;
2017-11-16 15:39:16 +00:00
2020-06-28 14:55:07 +01:00
buildCommand = lib.optionalString stdenv.isDarwin ''
mkdir -p $out/Applications
cp -R --no-preserve=mode,ownership ${browser}/Applications/${browserName}.app $out/Applications
rm -f $out${browser.execdir or "/bin"}/${browserName}
'' + ''
if [ ! -x "${browser}${browser.execdir or "/bin"}/${browserName}" ]
2017-11-16 15:39:16 +00:00
then
echo "cannot find executable file \`${browser}${browser.execdir or "/bin"}/${browserName}'"
2017-11-16 15:39:16 +00:00
exit 1
fi
2020-06-28 14:55:07 +01:00
#########################
# #
# EXTRA PREF CHANGES #
# #
#########################
# Link the runtime. The executable itself has to be copied,
# because it will resolve paths relative to its true location.
# Any symbolic links have to be replicated as well.
cd "${browser}"
find . -type d -exec mkdir -p "$out"/{} \;
find . -type f \( -not -name "${browserName}" \) -exec ln -sT "${browser}"/{} "$out"/{} \;
find . -type f -name "${browserName}" -print0 | while read -d $'\0' f; do
cp -P --no-preserve=mode,ownership "${browser}/$f" "$out/$f"
chmod a+rwx "$out/$f"
done
# fix links and absolute references
cd "${browser}"
find . -type l -print0 | while read -d $'\0' l; do
target="$(readlink "$l" | ${replace}/bin/replace-literal -es -- "${browser}" "$out")"
ln -sfT "$target" "$out/$l"
done
# This will not patch binaries, only "text" files.
# Its there for the wrapper mostly.
cd "$out"
${replace}/bin/replace-literal -esfR -- "${browser}" "$out"
# create the wrapper
executablePrefix="$out${browser.execdir or "/bin"}"
executablePath="$executablePrefix/${browserName}"
if [ ! -x "$executablePath" ]
then
echo "cannot find executable file \`${browser}${browser.execdir or "/bin"}/${browserName}'"
exit 1
fi
if [ ! -L "$executablePath" ]
then
# Careful here, the file at executablePath may already be
# a wrapper. That is why we postfix it with -old instead
# of -wrapped.
oldExe="$executablePrefix"/".${browserName}"-old
mv "$executablePath" "$oldExe"
else
oldExe="$(readlink -v --canonicalize-existing "$executablePath")"
fi
if [ ! -x "${browser}${browser.execdir or "/bin"}/${browserName}" ]
then
echo "cannot find executable file \`${browser}${browser.execdir or "/bin"}/${browserName}'"
exit 1
fi
makeWrapper "$oldExe" \
"$out${browser.execdir or "/bin"}/${browserName}${nameSuffix}" \
2017-11-16 15:39:16 +00:00
--suffix-each MOZ_PLUGIN_PATH ':' "$plugins" \
--suffix LD_LIBRARY_PATH ':' "$libs" \
--suffix-each GTK_PATH ':' "$gtk_modules" \
--suffix-each LD_PRELOAD ':' "$(cat $(filterExisting $(addSuffix /extra-ld-preload $plugins)))" \
2021-02-01 08:05:09 +00:00
--prefix PATH ':' "${xdg-utils}/bin" \
2017-11-16 15:39:16 +00:00
--prefix-contents PATH ':' "$(filterExisting $(addSuffix /extra-bin-path $plugins))" \
--suffix PATH ':' "$out${browser.execdir or "/bin"}" \
2017-11-16 15:39:16 +00:00
--set MOZ_APP_LAUNCHER "${browserName}${nameSuffix}" \
--set MOZ_SYSTEM_DIR "$out/lib/mozilla" \
--set SNAP_NAME "firefox" \
--set MOZ_LEGACY_PROFILES 1 \
--set MOZ_ALLOW_DOWNGRADE 1 \
${lib.optionalString forceWayland ''
--set MOZ_ENABLE_WAYLAND "1" \
''}${lib.optionalString (browser ? gtk3)
2017-11-16 15:39:16 +00:00
''--prefix XDG_DATA_DIRS : "$GSETTINGS_SCHEMAS_PATH" \
2019-02-13 21:47:50 +00:00
--suffix XDG_DATA_DIRS : '${gnome3.adwaita-icon-theme}/share'
2017-11-16 15:39:16 +00:00
''
}
2020-06-28 14:55:07 +01:00
#############################
# #
# END EXTRA PREF CHANGES #
# #
#############################
2017-11-16 15:39:16 +00:00
if [ -e "${browser}/share/icons" ]; then
mkdir -p "$out/share"
ln -s "${browser}/share/icons" "$out/share/icons"
else
for res in 16 32 48 64 128; do
mkdir -p "$out/share/icons/hicolor/''${res}x''${res}/apps"
icon=( "${browser}/lib/"*"/browser/chrome/icons/default/default''${res}.png" )
if [ -e "$icon" ]; then ln -s "$icon" \
"$out/share/icons/hicolor/''${res}x''${res}/apps/${browserName}.png"
fi
done
2017-11-16 15:39:16 +00:00
fi
install -D -t $out/share/applications $desktopItem/share/applications/*
mkdir -p $out/lib/mozilla/native-messaging-hosts
2017-11-16 15:39:16 +00:00
for ext in ${toString nativeMessagingHosts}; do
ln -sLt $out/lib/mozilla/native-messaging-hosts $ext/lib/mozilla/native-messaging-hosts/*
2017-11-16 15:39:16 +00:00
done
mkdir -p $out/lib/mozilla/pkcs11-modules
for ext in ${toString pkcs11Modules}; do
ln -sLt $out/lib/mozilla/pkcs11-modules $ext/lib/mozilla/pkcs11-modules/*
done
2017-11-16 15:39:16 +00:00
# For manpages, in case the program supplies them
mkdir -p $out/nix-support
echo ${browser} > $out/nix-support/propagated-user-env-packages
2020-06-28 14:55:07 +01:00
#########################
# #
# EXTRA PREF CHANGES #
# #
#########################
# user customization
mkdir -p $out/lib/${firefoxLibName}
# creating policies.json
mkdir -p "$out/lib/${firefoxLibName}/distribution"
POL_PATH="$out/lib/${firefoxLibName}/distribution/policies.json"
rm -f "$POL_PATH"
cat ${policiesJson} >> "$POL_PATH"
# preparing for autoconfig
mkdir -p "$out/lib/${firefoxLibName}/defaults/pref"
echo 'pref("general.config.filename", "mozilla.cfg");' > "$out/lib/${firefoxLibName}/defaults/pref/autoconfig.js"
echo 'pref("general.config.obscure_value", 0);' >> "$out/lib/${firefoxLibName}/defaults/pref/autoconfig.js"
2020-06-28 14:55:07 +01:00
cat > "$out/lib/${firefoxLibName}/mozilla.cfg" < ${mozillaCfg}
mkdir -p $out/lib/${firefoxLibName}/distribution/extensions
#############################
# #
# END EXTRA PREF CHANGES #
# #
#############################
2017-11-16 15:39:16 +00:00
'';
preferLocalBuild = true;
# Let each plugin tell us (through its `mozillaPlugin') attribute
# where to find the plugin in its tree.
plugins = map (x: x + x.mozillaPlugin) plugins;
libs = lib.makeLibraryPath libs + ":" + lib.makeSearchPathOutput "lib" "lib64" libs;
gtk_modules = map (x: x + x.gtkModule) gtk_modules;
passthru = { unwrapped = browser; };
disallowedRequisites = [ stdenv.cc ];
meta = browser.meta // {
description =
browser.meta.description
+ " (with plugins: "
+ lib.concatStrings (lib.intersperse ", " (map (x: x.name) plugins))
+ ")";
hydraPlatforms = [];
priority = (browser.meta.priority or 0) - 1; # prefer wrapper over the package
};
};
in
lib.makeOverridable wrapper