nixpkgs/pkgs/applications/networking/browsers/firefox/wrapper.nix

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

412 lines
15 KiB
Nix
Raw Normal View History

{ stdenv, lib, makeDesktopItem, makeWrapper, makeBinaryWrapper, lndir, config
, jq, xdg-utils, writeText
## various stuff that can be plugged in
, ffmpeg_5, xorg, alsa-lib, libpulseaudio, libcanberra-gtk3, libglvnd, libnotify, opensc
, gnome/*.gnome-shell*/
, browserpass, gnome-browser-connector, uget-integrator, plasma5Packages, bukubrow, pipewire
, tridactyl-native
, fx_cast_bridge
, udev
2021-03-14 16:11:48 +00:00
, libkrb5
2020-03-04 07:21:06 +00:00
, libva
, mesa # firefox wants gbm for drm+dmabuf
, cups
, pciutils
, sndio
, libjack2
, speechd
}:
## configurability of the wrapper itself
2017-11-16 15:39:16 +00:00
browser:
let
2017-11-16 15:39:16 +00:00
wrapper =
{ applicationName ? browser.binaryName or (lib.getName browser)
, pname ? applicationName
, version ? lib.getVersion browser
, desktopName ? # applicationName with first letter capitalized
(lib.toUpper (lib.substring 0 1 applicationName) + lib.substring 1 (-1) applicationName)
2017-11-16 15:39:16 +00:00
, nameSuffix ? ""
, icon ? applicationName
, wmClass ? applicationName
, extraNativeMessagingHosts ? []
, pkcs11Modules ? []
, useGlvnd ? true
, cfg ? config.${applicationName} or {}
2020-06-28 13:55:07 +00: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 13:55:07 +00:00
, extraPrefs ? ""
2022-02-11 18:05:45 +00:00
, extraPrefsFiles ? []
2020-11-30 15:30:46 +00:00
# For more information about policies visit
# https://github.com/mozilla/policy-templates#enterprisepoliciesenabled
2020-06-28 13:55:07 +00:00
, extraPolicies ? {}
2022-02-11 18:05:45 +00:00
, extraPoliciesFiles ? []
, libName ? browser.libName or "firefox" # Important for tor package or the like
, nixExtensions ? null
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;
sndioSupport = browser.sndioSupport or false;
jackSupport = browser.jackSupport or false;
# PCSC-Lite daemon (services.pcscd) also must be enabled for firefox to access smartcards
smartcardSupport = cfg.smartcardSupport or false;
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) gnome-browser-connector
2018-04-30 02:41:36 +00: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
;
libs = lib.optionals stdenv.isLinux [ udev libva mesa libnotify xorg.libXScrnSaver cups pciutils ]
++ lib.optional pipewireSupport pipewire
++ lib.optional ffmpegSupport ffmpeg_5
2021-03-14 16:11:48 +00:00
++ lib.optional gssSupport libkrb5
++ lib.optional useGlvnd libglvnd
2017-11-16 15:39:16 +00:00
++ lib.optionals (cfg.enableQuakeLive or false)
(with xorg; [ stdenv.cc libX11 libXxf86dga libXxf86vm libXext libXt alsa-lib zlib ])
++ lib.optional (config.pulseaudio or true) libpulseaudio
++ lib.optional alsaSupport alsa-lib
++ lib.optional sndioSupport sndio
++ lib.optional jackSupport libjack2
++ lib.optional smartcardSupport opensc
++ lib.optional (cfg.speechSynthesisSupport or false) speechd
++ pkcs11Modules;
gtk_modules = [ libcanberra-gtk3 ];
2017-11-16 15:39:16 +00:00
launcherName = "${applicationName}${nameSuffix}";
2020-06-28 13:55:07 +00:00
#########################
# #
# EXTRA PREF CHANGES #
# #
#########################
policiesJson = writeText "policies.json" (builtins.toJSON enterprisePolicies);
2020-06-28 13:55:07 +00:00
usesNixExtensions = nixExtensions != null;
2023-02-14 18:11:59 +00:00
nameArray = builtins.map(a: a.name) (lib.optionals usesNixExtensions nixExtensions);
firefox: Improve detecting signing requirements Firefox 61 started to enforce signatures for add-ons and since commit d031843a1eee244172570c64c9e238641563e68e, we get an evaluation error that recommends the user to switch to Firefox ESR. This isn't an option for everyone and as I also pointed out in the pull request[1] introducing the above commit, I've been building Firefox like this: let firefoxNoSigning = firefox-unwrapped.overrideAttrs (lib.const { MOZ_REQUIRE_SIGNING = false; }); in wrapFirefox firefoxNoSigning { nixExtensions = ...; } However, this only works after manually modifying nixpkgs (or copy & paste wrapper.nix elsewhere) every time I want to have a new Firefox version. Of course, this gets annoying and tedious after a while, so this motivated me to properly fix this to not only check for an ESR version but also check the value of MOZ_REQUIRE_SIGNING. Note that I'm using toString here to check for the value because there are several ways (false, null, "", ...) to set the environment variable to an empty string and toString makes sure that it really is the desired behaviour. I specifically checked the Firefox source and also tested this with multiple values and only building with MOZ_REQUIRE_SIGNING set to an empty string seems to work (no "0", "false" or other variants). Additionally, there is another method to allow unsigned add-ons, which is by using the --with-unsigned-addon-scopes configure option[2]. Unfortunately, this does not work with nixExtensions because we don't have (or want) a central directory where those add-ons reside. Given that nixExtensions disallows manually installing add-ons, setting MOZ_REQUIRE_SIGNING to false should be safe in this case. [1]: https://github.com/NixOS/nixpkgs/pull/133504 [2]: https://bugs.archlinux.org/task/63075 Signed-off-by: aszlig <aszlig@nix.build>
2022-06-20 23:51:31 +00:00
requiresSigning = browser ? MOZ_REQUIRE_SIGNING
-> toString browser.MOZ_REQUIRE_SIGNING != "";
# 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"
firefox: Improve detecting signing requirements Firefox 61 started to enforce signatures for add-ons and since commit d031843a1eee244172570c64c9e238641563e68e, we get an evaluation error that recommends the user to switch to Firefox ESR. This isn't an option for everyone and as I also pointed out in the pull request[1] introducing the above commit, I've been building Firefox like this: let firefoxNoSigning = firefox-unwrapped.overrideAttrs (lib.const { MOZ_REQUIRE_SIGNING = false; }); in wrapFirefox firefoxNoSigning { nixExtensions = ...; } However, this only works after manually modifying nixpkgs (or copy & paste wrapper.nix elsewhere) every time I want to have a new Firefox version. Of course, this gets annoying and tedious after a while, so this motivated me to properly fix this to not only check for an ESR version but also check the value of MOZ_REQUIRE_SIGNING. Note that I'm using toString here to check for the value because there are several ways (false, null, "", ...) to set the environment variable to an empty string and toString makes sure that it really is the desired behaviour. I specifically checked the Firefox source and also tested this with multiple values and only building with MOZ_REQUIRE_SIGNING set to an empty string seems to work (no "0", "false" or other variants). Additionally, there is another method to allow unsigned add-ons, which is by using the --with-unsigned-addon-scopes configure option[2]. Unfortunately, this does not work with nixExtensions because we don't have (or want) a central directory where those add-ons reside. Given that nixExtensions disallows manually installing add-ons, setting MOZ_REQUIRE_SIGNING to false should be safe in this case. [1]: https://github.com/NixOS/nixpkgs/pull/133504 [2]: https://bugs.archlinux.org/task/63075 Signed-off-by: aszlig <aszlig@nix.build>
2022-06-20 23:51:31 +00:00
else if requiresSigning && !lib.hasSuffix "esr" browser.name then
throw "Nix addons are only supported without signature enforcement (eg. Firefox ESR)"
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
2023-02-14 18:11:59 +00:00
) (lib.optionals usesNixExtensions nixExtensions);
2020-06-28 13:55:07 +00:00
enterprisePolicies =
{
2022-03-06 15:54:10 +00:00
policies = {
2020-06-28 13:55:07 +00:00
DisableAppUpdate = true;
} //
lib.optionalAttrs usesNixExtensions {
2020-06-28 13:55:07 +00: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";
2020-06-28 13:55:07 +00:00
};
}
) {} extensions;
2020-06-28 13:55:07 +00:00
Extensions = {
Install = lib.foldr (e: ret:
ret ++ [ "${e.outPath}/${e.extid}.xpi" ]
) [] extensions;
};
} // lib.optionalAttrs smartcardSupport {
SecurityDevices = {
"OpenSC PKCS#11 Module" = "opensc-pkcs11.so";
};
}
2020-06-28 13:55:07 +00:00
// extraPolicies;
};
mozillaCfg = ''
// First line must be a comment
2020-06-28 13:55:07 +00: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 13:55:07 +00:00
'';
#############################
# #
# END EXTRA PREF CHANGES #
# #
#############################
in stdenv.mkDerivation {
inherit pname version;
2017-11-16 15:39:16 +00:00
desktopItem = makeDesktopItem ({
name = launcherName;
exec = "${launcherName} --name ${wmClass} %U";
2017-11-16 15:39:16 +00:00
inherit icon;
inherit desktopName;
startupNotify = true;
startupWMClass = wmClass;
terminal = false;
} // (if libName == "thunderbird"
then {
genericName = "Email Client";
comment = "Read and write e-mails or RSS feeds, or manage tasks on calendars.";
categories = [
"Network" "Chat" "Email" "Feed" "GTK" "News"
];
keywords = [
"mail" "email" "e-mail" "messages" "rss" "calendar"
"address book" "addressbook" "chat"
];
mimeTypes = [
"message/rfc822"
"x-scheme-handler/mailto"
"text/calendar"
"text/x-vcard"
];
actions = {
profile-manager-window = {
name = "Profile Manager";
exec = "${launcherName} --ProfileManager";
};
};
}
else {
genericName = "Web Browser";
categories = [ "Network" "WebBrowser" ];
mimeTypes = [
"text/html"
"text/xml"
"application/xhtml+xml"
"application/vnd.mozilla.xul+xml"
"x-scheme-handler/http"
"x-scheme-handler/https"
];
actions = {
new-window = {
name = "New Window";
exec = "${launcherName} --new-window %U";
};
new-private-window = {
name = "New Private Window";
exec = "${launcherName} --private-window %U";
};
profile-manager-window = {
name = "Profile Manager";
exec = "${launcherName} --ProfileManager";
};
};
}));
2017-11-16 15:39:16 +00:00
nativeBuildInputs = [ makeWrapper lndir jq ];
buildInputs = [ browser.gtk3 ];
2017-11-16 15:39:16 +00:00
2020-06-28 13:55:07 +00:00
buildCommand = ''
if [ ! -x "${browser}/bin/${applicationName}" ]
2017-11-16 15:39:16 +00:00
then
echo "cannot find executable file \`${browser}/bin/${applicationName}'"
2017-11-16 15:39:16 +00:00
exit 1
fi
2020-06-28 13:55:07 +00: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 "${applicationName}" \) -exec ln -sT "${browser}"/{} "$out"/{} \;
2020-06-28 13:55:07 +00:00
find . -type f \( -name "${applicationName}" -o -name "${applicationName}-bin" \) -print0 | while read -d $'\0' f; do
cp -P --no-preserve=mode,ownership --remove-destination "${browser}/$f" "$out/$f"
2020-06-28 13:55:07 +00:00
chmod a+rwx "$out/$f"
done
# fix links and absolute references
find . -type l -print0 | while read -d $'\0' l; do
target="$(readlink "$l")"
target=''${target/#"${browser}"/"$out"}
2020-06-28 13:55:07 +00:00
ln -sfT "$target" "$out/$l"
done
2022-05-10 11:18:05 +00:00
cd "$out"
2020-06-28 13:55:07 +00:00
# create the wrapper
executablePrefix="$out/bin"
executablePath="$executablePrefix/${applicationName}"
oldWrapperArgs=()
2020-06-28 13:55:07 +00:00
if [[ -L $executablePath ]]; then
# Symbolic link: wrap the link's target.
2020-06-28 13:55:07 +00:00
oldExe="$(readlink -v --canonicalize-existing "$executablePath")"
rm "$executablePath"
elif wrapperCmd=$(${makeBinaryWrapper.extractCmd} "$executablePath"); [[ $wrapperCmd ]]; then
# If the executable is a binary wrapper, we need to update its target to
# point to $out, but we can't just edit the binary in-place because of length
# issues. So we extract the command used to create the wrapper and add the
# arguments to our wrapper.
parseMakeCWrapperCall() {
shift # makeCWrapper
oldExe=$1; shift
oldWrapperArgs=("$@")
}
eval "parseMakeCWrapperCall ''${wrapperCmd//"${browser}"/"$out"}"
rm "$executablePath"
else
if read -rn2 shebang < "$executablePath" && [[ $shebang == '#!' ]]; then
# Shell wrapper: patch in place to point to $out.
sed -i "s@${browser}@$out@g" "$executablePath"
fi
# Suffix the executable with -old, because -wrapped might already be used by the old wrapper.
oldExe="$executablePrefix/.${applicationName}"-old
mv "$executablePath" "$oldExe"
2020-06-28 13:55:07 +00:00
fi
# make xdg-open overrideable at runtime
2020-06-28 13:55:07 +00:00
makeWrapper "$oldExe" \
"''${executablePath}${nameSuffix}" \
--prefix LD_LIBRARY_PATH ':' "$libs" \
2017-11-16 15:39:16 +00:00
--suffix-each GTK_PATH ':' "$gtk_modules" \
--suffix PATH ':' "${xdg-utils}/bin" \
--suffix PATH ':' "$out/bin" \
--set MOZ_APP_LAUNCHER "${launcherName}" \
2017-11-16 15:39:16 +00:00
--set MOZ_SYSTEM_DIR "$out/lib/mozilla" \
--set MOZ_LEGACY_PROFILES 1 \
--set MOZ_ALLOW_DOWNGRADE 1 \
--prefix XDG_DATA_DIRS : "$GSETTINGS_SCHEMAS_PATH" \
--suffix XDG_DATA_DIRS : '${gnome.adwaita-icon-theme}/share' \
--set-default MOZ_ENABLE_WAYLAND 1 \
"''${oldWrapperArgs[@]}"
2020-06-28 13:55:07 +00: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=$( find "${browser}/lib/" -name "default''${res}.png" )
if [ -e "$icon" ]; then ln -s "$icon" \
"$out/share/icons/hicolor/''${res}x''${res}/apps/${icon}.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
2020-06-28 13:55:07 +00:00
#########################
# #
# EXTRA PREF CHANGES #
# #
#########################
# user customization
mkdir -p $out/lib/${libName}
2020-06-28 13:55:07 +00:00
# creating policies.json
mkdir -p "$out/lib/${libName}/distribution"
2020-06-28 13:55:07 +00:00
POL_PATH="$out/lib/${libName}/distribution/policies.json"
2020-06-28 13:55:07 +00:00
rm -f "$POL_PATH"
cat ${policiesJson} >> "$POL_PATH"
2022-02-11 18:05:45 +00:00
extraPoliciesFiles=(${builtins.toString extraPoliciesFiles})
for extraPoliciesFile in "''${extraPoliciesFiles[@]}"; do
jq -s '.[0] + .[1]' "$POL_PATH" $extraPoliciesFile > .tmp.json
mv .tmp.json "$POL_PATH"
done
2020-06-28 13:55:07 +00:00
# preparing for autoconfig
mkdir -p "$out/lib/${libName}/defaults/pref"
2020-06-28 13:55:07 +00:00
echo 'pref("general.config.filename", "mozilla.cfg");' > "$out/lib/${libName}/defaults/pref/autoconfig.js"
echo 'pref("general.config.obscure_value", 0);' >> "$out/lib/${libName}/defaults/pref/autoconfig.js"
2020-06-28 13:55:07 +00:00
cat > "$out/lib/${libName}/mozilla.cfg" << EOF
${mozillaCfg}
EOF
2020-06-28 13:55:07 +00:00
2022-02-11 18:05:45 +00:00
extraPrefsFiles=(${builtins.toString extraPrefsFiles})
for extraPrefsFile in "''${extraPrefsFiles[@]}"; do
cat "$extraPrefsFile" >> "$out/lib/${libName}/mozilla.cfg"
done
cat >> "$out/lib/${libName}/mozilla.cfg" << EOF
${extraPrefs}
EOF
mkdir -p $out/lib/${libName}/distribution/extensions
2020-06-28 13:55:07 +00:00
#############################
# #
# END EXTRA PREF CHANGES #
# #
#############################
2017-11-16 15:39:16 +00:00
'';
preferLocalBuild = true;
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 // {
inherit (browser.meta) description;
2017-11-16 15:39:16 +00:00
hydraPlatforms = [];
priority = (browser.meta.priority or 0) - 1; # prefer wrapper over the package
};
};
in lib.makeOverridable wrapper