diff mbox series

[bug#42380,v5,9/9] gnu: Add torbrowser-unbundle.

Message ID 347e91d0ce7f96882f70a081b208c388153cd6b8.1622693271.git.nandre@riseup.net
State New
Headers show
Series None | expand

Commit Message

André Batista June 3, 2021, 4:10 a.m. UTC
* gnu/packages/tor.scm (torbrowser-unbundle): New variable.
* gnu/packages/patches/torbrowser-start-desktop.patch: New file.
* gnu/packages/patches/torbrowser-start-script.patch: New file.
* gnu/local.mk (dist_patch_DATA): Adjust accordingly.

 create mode 100644 gnu/packages/patches/torbrowser-start-desktop.patch
 create mode 100644 gnu/packages/patches/torbrowser-start-script.patch

Comments

M June 3, 2021, 9:07 p.m. UTC | #1
Some comments, maybe the have already been addressed previously:

> +++ b/gnu/packages/patches/torbrowser-start-desktop.patch
> @@ -0,0 +1,22 @@
> +Change TorBrowser desktop file in order for it to be agnostic to the
> +path when invoked.
> +
> +--- torbrowser-68.10.0esr-9.5-1/tbb-scripts/RelativeLink/start-tor-browser.desktop.orig    2020-07-05 18:47:40.689484877 -0300
> ++++ torbrowser-68.10.0esr-9.5-1/tbb-scripts/RelativeLink/start-tor-browser.desktop 2020-07-24 14:36:37.720579884 -0300
> +@@ -1,4 +1,4 @@
> +-#!/usr/bin/env ./Browser/execdesktop
> ++#!/usr/bin/env bash
> + #
> + # This file is a self-modifying .desktop file that can be run from the shell.
> + # It preserves arguments and environment for the start-tor-browser script.
> +@@ -28,7 +28,7 @@
> + GenericName=Web Browser
> + Comment=Tor Browser is +1 for privacy and −1 for mass surveillance
> + Categories=Network;WebBrowser;Security;
> +-Exec=sh -c '"$(dirname "$*")"/Browser/start-tor-browser --detach || ([ ! -x "$(dirname "$*")"/Browser/start-tor-browser ] && "$(dirname "$*")"/start-tor-browser --detach)' dummy %k
> +-X-TorBrowser-ExecShell=./Browser/start-tor-browser --detach
> +-Icon=web-browser
> ++Exec=sh -c start-tor-browser
> ++X-TorBrowser-ExecShell=start-tor-browser --detach
> ++Icon=torbrowser

What's the reason for switching the icon from web-browser to torbrowser?
Also, the guixy way would be to simply replace "$(dirname "$*")/STUFF"
by /gnu/store/[...]/MORE-STUF/STUFF.

Otherwise, you're assuming "sh" is in the profile. It would also
be possible to replace "sh" with (string-append (assoc-ref inputs "bash") "/bin/sh")
I guess.

> + StartupWMClass=Tor Browser
> diff --git a/gnu/packages/patches/torbrowser-start-script.patch b/gnu/packages/patches/torbrowser-start-script.patch
> new file mode 100644
> index 0000000000..b8c8d9a26a
> --- /dev/null
> +++ b/gnu/packages/patches/torbrowser-start-script.patch
> @@ -0,0 +1,181 @@
> +Change TorBrowser startup script in order for it to setup needed files
> +outside guix store. Remove tests which are not needed on guix system.
> +
> +--- torbrowser-68.10.0esr-9.5-1/tbb-scripts/RelativeLink/start-tor-browser.orig    2020-07-05 18:47:40.685485004 -0300
> ++++ torbrowser-68.10.0esr-9.5-1/tbb-scripts/RelativeLink/start-tor-browser 2020-07-23 18:13:32.426282743 -0300
> +@@ -5,6 +5,14 @@
> + #
> + # Copyright 2017 The Tor Project.  See LICENSE for licensing information.
> + 
> ++TBB_HOME="${HOME}/.local/share/torbrowser"
> ++TBB_LOGFILE="${TBB_HOME}/torbrowser.log"
> ++TBB_DATA="${TBB_HOME}/Data"
> ++TBB_PROFILE="${TBB_DATA}/Browser/profile.default"
> ++TBB_STORE_PATH=$(dirname $(realpath "$0"))
> ++TBB_STORE_DATA="${TBB_STORE_PATH}/TorBrowser/Data"
> ++TORRC="${TBB_DATA}/Tor/torrc-defaults"
> ++
> + complain_dialog_title="Tor Browser"
> + 
> + # First, make sure DISPLAY is set.  If it isn't, we're hosed; scream
> +@@ -134,8 +142,8 @@
> +           ;;
> +       -l | --log)
> +           if [ -z "$2" -o "${2:0:1}" == "-" ]; then
> +-             printf "Logging Tor Browser debug information to tor-browser.log\n"
> +-             logfile="../tor-browser.log"
> ++             printf "Logging Tor Browser debug information to torbrowser.log\n"

Why rename tor-browser.log to torbrowser.log?

> + [...]
> ++# Try to be agnostic to where we're being started from, check if files are on its
> ++# default paths and chdir to TBB_HOME
> ++if [ -e "${TORRC}" ]; then
> ++   cd "${TBB_HOME}"
> ++else
> ++   mkdir -p "${TBB_HOME}"
> ++   cp -R "${TBB_STORE_DATA}" "${TBB_HOME}"
> ++   chmod -R 700 "${TBB_HOME}"
> ++   mkdir -p "${TBB_PROFILE}"
> ++   echo "user_pref(\"extensions.torlauncher.torrc-defaults_path\", \"${TORRC}\");"\
> ++     > "${TBB_PROFILE}/user.js"
> ++   echo "ClientTransportPlugin meek_lite,obfs2,obfs3,obfs4,scramblesuit exec ${TBB_STORE_PATH}/TorBrowser/Tor/PluggableTransports/obfs4proxy"\
> ++     >> "${TORRC}"
> ++   cd "${TBB_HOME}"
> + fi

"mkdir" and "cp" are from coreutils, which are not guaranteed to be in
the profile. I'd suggest:

(1) (preferred) use substitute* in a build phase to replace
    'mkdir' and 'cp' & co with the absolute store path
(2) or add coreutils to propagated-inputs

Likewise for sed. 

> [...]
> 
> + if [ "$register_desktop_app" -eq 1 ]; then
> + 	mkdir -p "$HOME/.local/share/applications/"
> +-	cp ../start-tor-browser.desktop "$HOME/.local/share/applications/"
> ++	cp "${TBB_STORE_PATH}/start-tor-browser.desktop" "$HOME/.local/share/applications/"
> + 	update-desktop-database "$HOME/.local/share/applications/"
> + 	printf "Tor Browser has been registered as a desktop app for this user in ~/.local/share/applications/\n"
> + 	exit 0

Is this required on Guix and would it work well on Guix? Copying .desktop files around
seems counter to ‘Guix suppots transactional upgrades and roll-backs, [...]. [...]
reproducible operating systems’ and not very functional. Shouldn't
"guix install torbrowser-unbundle" be sufficient?

noscript seems an useful extension for IceCat as well.
Maybe move it to gnuzilla.scm? Maybe move https-everywhere
there as well? (Separate issue: https-everywhere seems to
be bundled in IceCat ...)

> +(define https-everywhere-lib-wasm
> +  (let ((commit "45b1622f1240659aca4762fa336aad1322d6d50f"))
> +    (package
> +      (name "https-everywhere-lib-wasm")
> +      (version "2021.4.15")
> +      (source
> +       (origin
> +         (method git-fetch)
> +         (uri (git-reference
> +               (url "https://github.com/EFForg/https-everywhere-lib-wasm")
> +               (commit commit)))
> +         (file-name (git-file-name name version))
> +         (sha256
> +          (base32
> +           "1lq62rzypdzmnnzvfns3ccvv1g7p7g9s8jx788zzigr3gnmkpffx"))))
> +      (build-system trivial-build-system)
> +      (arguments
> +       `(#:modules ((guix build utils))
> +         #:builder (begin
> +                     (use-modules (guix build utils))
> +                     (format #t "Copying source ...~%")
> +                     (copy-recursively (assoc-ref %build-inputs "source")
> +                                       %output
> +                                       #:log (%make-void-port "w")))))

Why are you copying the source code to somewhere else?
This doesn't seem to accomplish anything. I would suggest
something like:

;; Source code of ‘HTTPS Everywhere WASM Library’,
;; licensed as license:lgpl2.1+, used in 'https-everywhere'
;; as an input.
(define https-everywhere-lib-wasm/source-code
  (origin (method git-fetch) [...]))

Note that you can use 'origin' objects in 'inputs' and 'native-inputs'.


> +      (home-page "https://github.com/EFForg/https-everywhere-lib-wasm")
> +      (synopsis "Browser extension for protection against known attacks")
> +      (description "Browser extension that protects users from a range of
> +known attacks on web browsing activity such as Cross-site scripting, clickjack and
> +makes possible for the users to block or choose on a per site basis which remote
> +javascript to run while browsing the web.")
> +      (license license:gpl2+))))

The license file seems to tell something different: LGPL2.1+ instead of GPL2+:
https://github.com/EFForg/https-everywhere-lib-wasm/blob/master/LICENSE


> [...]
> +      (native-inputs
> +       `(("https-everywhere" ,https-everywhere)
> +	   ("noscript" ,noscript)

noscript and https-everywhere seem more like 'inputs' than
'native-inputs' to me, but IIUC they are source-code only
and not compiled, so it doesn't really matter here I guess.

> [...]
> +             (add-after 'unpack 'make-bundle
> +               (lambda* (#:key inputs native-inputs #:allow-other-keys)
> +		 (let ((tor-launcher (assoc-ref inputs "tor-launcher"))
> +                       (tor-launcher-dir "browser/extensions/tor-launcher")
> +                       (tbb (assoc-ref inputs "tor-browser-build"))
> +                       (tbb-scripts-dir "tbb-scripts"))
> +
> +                   (format #t "Copying tor-launcher ...~%")
> +                   (copy-recursively tor-launcher tor-launcher-dir
> +                                     #:log (%make-void-port "w"))
> +                   (format #t "Copying tor-browser-build ...~%")
> +                   (mkdir tbb-scripts-dir)
> +                   (copy-recursively tbb tbb-scripts-dir
> +                                     #:log (%make-void-port "w"))
> +		   (make-file-writable "browser/app/profile/000-tor-browser.js")
> +		   (make-file-writable (string-append tbb-scripts-dir
> +						      "/RelativeLink/start-tor-browser"))
> +                   (make-file-writable (string-append tbb-scripts-dir
> +						      "/RelativeLink/start-tor-browser.desktop")))
> +		 #t))

Returning #t at the end of a phase is not required anymore.
The warning will disappear when core-updates is merged.

> +             (replace 'configure
> +               (lambda* (#:key inputs outputs configure-flags #:allow-other-keys)
> +		 (let* ((out (assoc-ref outputs "out"))
> +			(bash (which "bash"))
> +			(flags `(,(string-append "--prefix=" out)
> +				 ,@configure-flags)))
> +                   (setenv "SHELL" bash)
> +                   (setenv "AUTOCONF" (string-append
> +                                       (assoc-ref %build-inputs "autoconf")
> +                                       "/bin/autoconf"))

In build phases, use 'inputs' or 'native-inputs' instead of %build-inputs.
It's more explicit, maybe there are other reasons as well. (Here it should
be native-inputs I guess).


> +                   (setenv "CONFIG_SHELL" bash)
> +                   (setenv "PYTHON" (string-append (assoc-ref inputs "python-2")
> +                                                   "/bin/python"))

This most likely should be (assoc-ref (or native-inputs inputs) "python-2")
instead of (assoc-ref inputs "python-2").

> +                   (setenv "CC" "gcc")  ; needed when Stylo is enabled
This most likely should be ,(cc-for-target) instead of "gcc".

(The native-inputs/inputs and "gcc" / (cc-for-target) distinction is important
when cross-compiling (though not all dependencies are cross-compilable currently,
so it's a bit moot for now.))

> +             (add-after 'install-extensions 'link-binaries
> +               (lambda* (#:key inputs native-inputs outputs #:allow-other-keys)
You're not using 'native-inputs' in this build phase so you can remove it
from the arguments list.

> +              [...]
> +             (add-after 'link-binaries 'copy-bundle-data
> +               (lambda* (#:key inputs native-inputs outputs #:allow-other-keys)

Likewise.

> +      (description
> +       "Tor Browser is the Tor Project version of Firefox browser.  It is the only
> +recommended way to anonymously browse the web that is supported by the project.
> +It modifies Firefox in order to avoid many know application level attacks on
> +the privacy of Tor users.
> +
> +WARNING: This is not the official Tor Browser and is currently on testing.  Use
> +at your own risk and please report back on guix channels if you find any
> +issues.")

This seems unnecessarily scary. All packages in guix are ‘at your own risk’
and every new package is ‘in testing’ for a while, whatever that means.
What about

 "Warning: this is not the official built of Tor Browser from upstream.
As such, the Guix version of Tor Browser may have small differences
that might allow a malicious actor to identify you as a Guix user."

?

Greetings,
Maxime.
M June 3, 2021, 9:07 p.m. UTC | #2
Some comments, maybe the have already been addressed previously:

> +++ b/gnu/packages/patches/torbrowser-start-desktop.patch
> @@ -0,0 +1,22 @@
> +Change TorBrowser desktop file in order for it to be agnostic to the
> +path when invoked.
> +
> +--- torbrowser-68.10.0esr-9.5-1/tbb-scripts/RelativeLink/start-tor-browser.desktop.orig    2020-07-05 18:47:40.689484877 -0300
> ++++ torbrowser-68.10.0esr-9.5-1/tbb-scripts/RelativeLink/start-tor-browser.desktop 2020-07-24 14:36:37.720579884 -0300
> +@@ -1,4 +1,4 @@
> +-#!/usr/bin/env ./Browser/execdesktop
> ++#!/usr/bin/env bash
> + #
> + # This file is a self-modifying .desktop file that can be run from the shell.
> + # It preserves arguments and environment for the start-tor-browser script.
> +@@ -28,7 +28,7 @@
> + GenericName=Web Browser
> + Comment=Tor Browser is +1 for privacy and −1 for mass surveillance
> + Categories=Network;WebBrowser;Security;
> +-Exec=sh -c '"$(dirname "$*")"/Browser/start-tor-browser --detach || ([ ! -x "$(dirname "$*")"/Browser/start-tor-browser ] && "$(dirname "$*")"/start-tor-browser --detach)' dummy %k
> +-X-TorBrowser-ExecShell=./Browser/start-tor-browser --detach
> +-Icon=web-browser
> ++Exec=sh -c start-tor-browser
> ++X-TorBrowser-ExecShell=start-tor-browser --detach
> ++Icon=torbrowser

What's the reason for switching the icon from web-browser to torbrowser?
Also, the guixy way would be to simply replace "$(dirname "$*")/STUFF"
by /gnu/store/[...]/MORE-STUF/STUFF.

Otherwise, you're assuming "sh" is in the profile. It would also
be possible to replace "sh" with (string-append (assoc-ref inputs "bash") "/bin/sh")
I guess.

> + StartupWMClass=Tor Browser
> diff --git a/gnu/packages/patches/torbrowser-start-script.patch b/gnu/packages/patches/torbrowser-start-script.patch
> new file mode 100644
> index 0000000000..b8c8d9a26a
> --- /dev/null
> +++ b/gnu/packages/patches/torbrowser-start-script.patch
> @@ -0,0 +1,181 @@
> +Change TorBrowser startup script in order for it to setup needed files
> +outside guix store. Remove tests which are not needed on guix system.
> +
> +--- torbrowser-68.10.0esr-9.5-1/tbb-scripts/RelativeLink/start-tor-browser.orig    2020-07-05 18:47:40.685485004 -0300
> ++++ torbrowser-68.10.0esr-9.5-1/tbb-scripts/RelativeLink/start-tor-browser 2020-07-23 18:13:32.426282743 -0300
> +@@ -5,6 +5,14 @@
> + #
> + # Copyright 2017 The Tor Project.  See LICENSE for licensing information.
> + 
> ++TBB_HOME="${HOME}/.local/share/torbrowser"
> ++TBB_LOGFILE="${TBB_HOME}/torbrowser.log"
> ++TBB_DATA="${TBB_HOME}/Data"
> ++TBB_PROFILE="${TBB_DATA}/Browser/profile.default"
> ++TBB_STORE_PATH=$(dirname $(realpath "$0"))
> ++TBB_STORE_DATA="${TBB_STORE_PATH}/TorBrowser/Data"
> ++TORRC="${TBB_DATA}/Tor/torrc-defaults"
> ++
> + complain_dialog_title="Tor Browser"
> + 
> + # First, make sure DISPLAY is set.  If it isn't, we're hosed; scream
> +@@ -134,8 +142,8 @@
> +           ;;
> +       -l | --log)
> +           if [ -z "$2" -o "${2:0:1}" == "-" ]; then
> +-             printf "Logging Tor Browser debug information to tor-browser.log\n"
> +-             logfile="../tor-browser.log"
> ++             printf "Logging Tor Browser debug information to torbrowser.log\n"

Why rename tor-browser.log to torbrowser.log?

> + [...]
> ++# Try to be agnostic to where we're being started from, check if files are on its
> ++# default paths and chdir to TBB_HOME
> ++if [ -e "${TORRC}" ]; then
> ++   cd "${TBB_HOME}"
> ++else
> ++   mkdir -p "${TBB_HOME}"
> ++   cp -R "${TBB_STORE_DATA}" "${TBB_HOME}"
> ++   chmod -R 700 "${TBB_HOME}"
> ++   mkdir -p "${TBB_PROFILE}"
> ++   echo "user_pref(\"extensions.torlauncher.torrc-defaults_path\", \"${TORRC}\");"\
> ++     > "${TBB_PROFILE}/user.js"
> ++   echo "ClientTransportPlugin meek_lite,obfs2,obfs3,obfs4,scramblesuit exec ${TBB_STORE_PATH}/TorBrowser/Tor/PluggableTransports/obfs4proxy"\
> ++     >> "${TORRC}"
> ++   cd "${TBB_HOME}"
> + fi

"mkdir" and "cp" are from coreutils, which are not guaranteed to be in
the profile. I'd suggest:

(1) (preferred) use substitute* in a build phase to replace
    'mkdir' and 'cp' & co with the absolute store path
(2) or add coreutils to propagated-inputs

Likewise for sed. 

> [...]
> 
> + if [ "$register_desktop_app" -eq 1 ]; then
> + 	mkdir -p "$HOME/.local/share/applications/"
> +-	cp ../start-tor-browser.desktop "$HOME/.local/share/applications/"
> ++	cp "${TBB_STORE_PATH}/start-tor-browser.desktop" "$HOME/.local/share/applications/"
> + 	update-desktop-database "$HOME/.local/share/applications/"
> + 	printf "Tor Browser has been registered as a desktop app for this user in ~/.local/share/applications/\n"
> + 	exit 0

Is this required on Guix and would it work well on Guix? Copying .desktop files around
seems counter to ‘Guix suppots transactional upgrades and roll-backs, [...]. [...]
reproducible operating systems’ and not very functional. Shouldn't
"guix install torbrowser-unbundle" be sufficient?

noscript seems an useful extension for IceCat as well.
Maybe move it to gnuzilla.scm? Maybe move https-everywhere
there as well? (Separate issue: https-everywhere seems to
be bundled in IceCat ...)

> +(define https-everywhere-lib-wasm
> +  (let ((commit "45b1622f1240659aca4762fa336aad1322d6d50f"))
> +    (package
> +      (name "https-everywhere-lib-wasm")
> +      (version "2021.4.15")
> +      (source
> +       (origin
> +         (method git-fetch)
> +         (uri (git-reference
> +               (url "https://github.com/EFForg/https-everywhere-lib-wasm")
> +               (commit commit)))
> +         (file-name (git-file-name name version))
> +         (sha256
> +          (base32
> +           "1lq62rzypdzmnnzvfns3ccvv1g7p7g9s8jx788zzigr3gnmkpffx"))))
> +      (build-system trivial-build-system)
> +      (arguments
> +       `(#:modules ((guix build utils))
> +         #:builder (begin
> +                     (use-modules (guix build utils))
> +                     (format #t "Copying source ...~%")
> +                     (copy-recursively (assoc-ref %build-inputs "source")
> +                                       %output
> +                                       #:log (%make-void-port "w")))))

Why are you copying the source code to somewhere else?
This doesn't seem to accomplish anything. I would suggest
something like:

;; Source code of ‘HTTPS Everywhere WASM Library’,
;; licensed as license:lgpl2.1+, used in 'https-everywhere'
;; as an input.
(define https-everywhere-lib-wasm/source-code
  (origin (method git-fetch) [...]))

Note that you can use 'origin' objects in 'inputs' and 'native-inputs'.


> +      (home-page "https://github.com/EFForg/https-everywhere-lib-wasm")
> +      (synopsis "Browser extension for protection against known attacks")
> +      (description "Browser extension that protects users from a range of
> +known attacks on web browsing activity such as Cross-site scripting, clickjack and
> +makes possible for the users to block or choose on a per site basis which remote
> +javascript to run while browsing the web.")
> +      (license license:gpl2+))))

The license file seems to tell something different: LGPL2.1+ instead of GPL2+:
https://github.com/EFForg/https-everywhere-lib-wasm/blob/master/LICENSE


> [...]
> +      (native-inputs
> +       `(("https-everywhere" ,https-everywhere)
> +	   ("noscript" ,noscript)

noscript and https-everywhere seem more like 'inputs' than
'native-inputs' to me, but IIUC they are source-code only
and not compiled, so it doesn't really matter here I guess.

> [...]
> +             (add-after 'unpack 'make-bundle
> +               (lambda* (#:key inputs native-inputs #:allow-other-keys)
> +		 (let ((tor-launcher (assoc-ref inputs "tor-launcher"))
> +                       (tor-launcher-dir "browser/extensions/tor-launcher")
> +                       (tbb (assoc-ref inputs "tor-browser-build"))
> +                       (tbb-scripts-dir "tbb-scripts"))
> +
> +                   (format #t "Copying tor-launcher ...~%")
> +                   (copy-recursively tor-launcher tor-launcher-dir
> +                                     #:log (%make-void-port "w"))
> +                   (format #t "Copying tor-browser-build ...~%")
> +                   (mkdir tbb-scripts-dir)
> +                   (copy-recursively tbb tbb-scripts-dir
> +                                     #:log (%make-void-port "w"))
> +		   (make-file-writable "browser/app/profile/000-tor-browser.js")
> +		   (make-file-writable (string-append tbb-scripts-dir
> +						      "/RelativeLink/start-tor-browser"))
> +                   (make-file-writable (string-append tbb-scripts-dir
> +						      "/RelativeLink/start-tor-browser.desktop")))
> +		 #t))

Returning #t at the end of a phase is not required anymore.
The warning will disappear when core-updates is merged.

> +             (replace 'configure
> +               (lambda* (#:key inputs outputs configure-flags #:allow-other-keys)
> +		 (let* ((out (assoc-ref outputs "out"))
> +			(bash (which "bash"))
> +			(flags `(,(string-append "--prefix=" out)
> +				 ,@configure-flags)))
> +                   (setenv "SHELL" bash)
> +                   (setenv "AUTOCONF" (string-append
> +                                       (assoc-ref %build-inputs "autoconf")
> +                                       "/bin/autoconf"))

In build phases, use 'inputs' or 'native-inputs' instead of %build-inputs.
It's more explicit, maybe there are other reasons as well. (Here it should
be native-inputs I guess).


> +                   (setenv "CONFIG_SHELL" bash)
> +                   (setenv "PYTHON" (string-append (assoc-ref inputs "python-2")
> +                                                   "/bin/python"))

This most likely should be (assoc-ref (or native-inputs inputs) "python-2")
instead of (assoc-ref inputs "python-2").

> +                   (setenv "CC" "gcc")  ; needed when Stylo is enabled
This most likely should be ,(cc-for-target) instead of "gcc".

(The native-inputs/inputs and "gcc" / (cc-for-target) distinction is important
when cross-compiling (though not all dependencies are cross-compilable currently,
so it's a bit moot for now.))

> +             (add-after 'install-extensions 'link-binaries
> +               (lambda* (#:key inputs native-inputs outputs #:allow-other-keys)
You're not using 'native-inputs' in this build phase so you can remove it
from the arguments list.

> +              [...]
> +             (add-after 'link-binaries 'copy-bundle-data
> +               (lambda* (#:key inputs native-inputs outputs #:allow-other-keys)

Likewise.

> +      (description
> +       "Tor Browser is the Tor Project version of Firefox browser.  It is the only
> +recommended way to anonymously browse the web that is supported by the project.
> +It modifies Firefox in order to avoid many know application level attacks on
> +the privacy of Tor users.
> +
> +WARNING: This is not the official Tor Browser and is currently on testing.  Use
> +at your own risk and please report back on guix channels if you find any
> +issues.")

This seems unnecessarily scary. All packages in guix are ‘at your own risk’
and every new package is ‘in testing’ for a while, whatever that means.
What about

 "Warning: this is not the official built of Tor Browser from upstream.
As such, the Guix version of Tor Browser may have small differences
that might allow a malicious actor to identify you as a Guix user."

?

Greetings,
Maxime.
André Batista July 10, 2021, 3:10 a.m. UTC | #3
Hi Maxim,

I don't know why but I've overlooked your message when it first arrived,
I'm sorry and thanks for your help.

qui 03 jun 2021 às 23:07:23 (1622772443), maximedevos@telenet.be enviou:
> > + GenericName=Web Browser
> > + Comment=Tor Browser is +1 for privacy and −1 for mass surveillance
> > + Categories=Network;WebBrowser;Security;
> > +-Exec=sh -c '"$(dirname "$*")"/Browser/start-tor-browser --detach || ([ ! -x "$(dirname "$*")"/Browser/start-tor-browser ] && "$(dirname "$*")"/start-tor-browser --detach)' dummy %k
> > +-X-TorBrowser-ExecShell=./Browser/start-tor-browser --detach
> > +-Icon=web-browser
> > ++Exec=sh -c start-tor-browser
> > ++X-TorBrowser-ExecShell=start-tor-browser --detach
> > ++Icon=torbrowser
> 
> What's the reason for switching the icon from web-browser to torbrowser?

If I'm not mistaken, this was done to get the newer icon set from sources.
Without this change it was getting the older icons which were still
available. I'll check this again.

> Also, the guixy way would be to simply replace "$(dirname "$*")/STUFF"
> by /gnu/store/[...]/MORE-STUF/STUFF.
> Otherwise, you're assuming "sh" is in the profile. It would also
> be possible to replace "sh" with (string-append (assoc-ref inputs "bash") "/bin/sh")
> I guess.

You are right, I'm getting warnings on sh or bash when compiling.

> > + # First, make sure DISPLAY is set.  If it isn't, we're hosed; scream
> > +@@ -134,8 +142,8 @@
> > +           ;;
> > +       -l | --log)
> > +           if [ -z "$2" -o "${2:0:1}" == "-" ]; then
> > +-             printf "Logging Tor Browser debug information to tor-browser.log\n"
> > +-             logfile="../tor-browser.log"
> > ++             printf "Logging Tor Browser debug information to torbrowser.log\n"
> 
> Why rename tor-browser.log to torbrowser.log?

I think this had no reason other than my personal choice.

> > + [...]
> > ++# Try to be agnostic to where we're being started from, check if files are on its
> > ++# default paths and chdir to TBB_HOME
> > ++if [ -e "${TORRC}" ]; then
> > ++   cd "${TBB_HOME}"
> > ++else
> > ++   mkdir -p "${TBB_HOME}"
> > ++   cp -R "${TBB_STORE_DATA}" "${TBB_HOME}"
> > ++   chmod -R 700 "${TBB_HOME}"
> > ++   mkdir -p "${TBB_PROFILE}"
> > ++   echo "user_pref(\"extensions.torlauncher.torrc-defaults_path\", \"${TORRC}\");"\
> > ++     > "${TBB_PROFILE}/user.js"
> > ++   echo "ClientTransportPlugin meek_lite,obfs2,obfs3,obfs4,scramblesuit exec ${TBB_STORE_PATH}/TorBrowser/Tor/PluggableTransports/obfs4proxy"\
> > ++     >> "${TORRC}"
> > ++   cd "${TBB_HOME}"
> > + fi
> 
> "mkdir" and "cp" are from coreutils, which are not guaranteed to be in
> the profile. I'd suggest:
> 
> (1) (preferred) use substitute* in a build phase to replace
>     'mkdir' and 'cp' & co with the absolute store path
> (2) or add coreutils to propagated-inputs
> 
> Likewise for sed. 

Noted. I'll write a build phase.

> > [...]
> > 
> > + if [ "$register_desktop_app" -eq 1 ]; then
> > + 	mkdir -p "$HOME/.local/share/applications/"
> > +-	cp ../start-tor-browser.desktop "$HOME/.local/share/applications/"
> > ++	cp "${TBB_STORE_PATH}/start-tor-browser.desktop" "$HOME/.local/share/applications/"
> > + 	update-desktop-database "$HOME/.local/share/applications/"
> > + 	printf "Tor Browser has been registered as a desktop app for this user in ~/.local/share/applications/\n"
> > + 	exit 0
> 
> Is this required on Guix and would it work well on Guix? Copying .desktop files around
> seems counter to ‘Guix suppots transactional upgrades and roll-backs, [...]. [...]
> reproducible operating systems’ and not very functional. Shouldn't
> "guix install torbrowser-unbundle" be sufficient?

Yes, it should. I think I was trying to change things as little as
possible from what the official bundle does/provides, but I agree that
it would be better to just remove this part of the script and have it
behave as an ordinary application.

> noscript seems an useful extension for IceCat as well.
> Maybe move it to gnuzilla.scm? Maybe move https-everywhere
> there as well? (Separate issue: https-everywhere seems to
> be bundled in IceCat ...)

I was thinking the better outcome would be to have something as it
was done for ungoogled-chromium to provide extensions that would
be compatible with both icecat and torbrowser. Unfortunatly, I've
been unable to address most of the things I had intended for now
at least. I'll also keep a note on this and address it as soon as
I can.

> Why are you copying the source code to somewhere else?

Because I did not know any better.

> This doesn't seem to accomplish anything. I would suggest
> something like:
> 
> ;; Source code of ‘HTTPS Everywhere WASM Library’,
> ;; licensed as license:lgpl2.1+, used in 'https-everywhere'
> ;; as an input.
> (define https-everywhere-lib-wasm/source-code
>   (origin (method git-fetch) [...]))
> 
> Note that you can use 'origin' objects in 'inputs' and 'native-inputs'.

Thanks, I did not know this. I'll change accordingly.

> > +      (license license:gpl2+))))
> 
> The license file seems to tell something different: LGPL2.1+ instead of GPL2+:
> https://github.com/EFForg/https-everywhere-lib-wasm/blob/master/LICENSE

Ouch. You are right.

> > [...]
> > +      (native-inputs
> > +       `(("https-everywhere" ,https-everywhere)
> > +	   ("noscript" ,noscript)
> 
> noscript and https-everywhere seem more like 'inputs' than
> 'native-inputs' to me, but IIUC they are source-code only
> and not compiled, so it doesn't really matter here I guess.

Noted.

> > +                   (make-file-writable (string-append tbb-scripts-dir
> > +						      "/RelativeLink/start-tor-browser.desktop")))
> > +		 #t))
> 
> Returning #t at the end of a phase is not required anymore.
> The warning will disappear when core-updates is merged.

Noted.

> > +             (replace 'configure
> > +               (lambda* (#:key inputs outputs configure-flags #:allow-other-keys)
> > +		 (let* ((out (assoc-ref outputs "out"))
> > +			(bash (which "bash"))
> > +			(flags `(,(string-append "--prefix=" out)
> > +				 ,@configure-flags)))
> > +                   (setenv "SHELL" bash)
> > +                   (setenv "AUTOCONF" (string-append
> > +                                       (assoc-ref %build-inputs "autoconf")
> > +                                       "/bin/autoconf"))
> 
> In build phases, use 'inputs' or 'native-inputs' instead of %build-inputs.
> It's more explicit, maybe there are other reasons as well. (Here it should
> be native-inputs I guess).

Noted.

> > +                   (setenv "CONFIG_SHELL" bash)
> > +                   (setenv "PYTHON" (string-append (assoc-ref inputs "python-2")
> > +                                                   "/bin/python"))
> 
> This most likely should be (assoc-ref (or native-inputs inputs) "python-2")
> instead of (assoc-ref inputs "python-2").
> 
> > +                   (setenv "CC" "gcc")  ; needed when Stylo is enabled
> This most likely should be ,(cc-for-target) instead of "gcc".
> 
> (The native-inputs/inputs and "gcc" / (cc-for-target) distinction is important
> when cross-compiling (though not all dependencies are cross-compilable currently,
> so it's a bit moot for now.))

Noted. This and the previous code snippets came from Thunderbird if I
remember correctly and I didn't really give it much tought. I'll try to
address them both.

> > +             (add-after 'install-extensions 'link-binaries
> > +               (lambda* (#:key inputs native-inputs outputs #:allow-other-keys)
> You're not using 'native-inputs' in this build phase so you can remove it
> from the arguments list.
> > +              [...]
> > +             (add-after 'link-binaries 'copy-bundle-data
> > +               (lambda* (#:key inputs native-inputs outputs #:allow-other-keys)
> 
> Likewise.

Noted.

> > +      (description
> > +       "Tor Browser is the Tor Project version of Firefox browser.  It is the only
> > +recommended way to anonymously browse the web that is supported by the project.
> > +It modifies Firefox in order to avoid many know application level attacks on
> > +the privacy of Tor users.
> > +
> > +WARNING: This is not the official Tor Browser and is currently on testing.  Use
> > +at your own risk and please report back on guix channels if you find any
> > +issues.")
> 
> This seems unnecessarily scary. All packages in guix are ‘at your own risk’
> and every new package is ‘in testing’ for a while, whatever that means.
> What about

I wrote this to give some consideration to the fact that TorProject
strongly advises people _NOT_ to build their own TorBrowsers and not
to use anything other than the one they provide. I've also intended to
make it clear that the anonymity set might be reduced and that guix
users could be distinguished from other TB users, specially given the
nature of guix would imply that at sometime or other guix users would
even be using different software versions, which applies to extensions,
libs and even the tor software under the hood. I also gathered that
it would be sometime before this could be merged into guix and that
maybe some users would find this recipe on the mailing lists and
install themselves, so I thought it would be a good thing to be over
scary rather than give a false impression that it was something one
should do without a second thought.

Thanks for your valuable input. I've been a little short on time and
got stuck for a while, but I do intent to go back to it and when I
do I'll contemplate your suggestions.

Cheers,
diff mbox series

Patch

diff --git a/gnu/local.mk b/gnu/local.mk
index a5820b5827..c5fdb8fc16 100644
--- a/gnu/local.mk
+++ b/gnu/local.mk
@@ -42,6 +42,7 @@ 
 # Copyright © 2021 Björn Höfling <bjoern.hoefling@bjoernhoefling.de>
 # Copyright © 2021 Philip McGrath <philip@philipmcgrath.com>
 # Copyright © 2021 Arun Isaac <arunisaac@systemreboot.net>
+# Copyright © 2021 André Batista <nandre@riseup.net>
 #
 # This file is part of GNU Guix.
 #
@@ -1739,6 +1740,8 @@  dist_patch_DATA =						\
   %D%/packages/patches/tipp10-qt5.patch			\
   %D%/packages/patches/tk-find-library.patch			\
   %D%/packages/patches/tla2tools-build-xml.patch		\
+  %D%/packages/patches/torbrowser-start-desktop.patch           \
+  %D%/packages/patches/torbrowser-start-script.patch            \
   %D%/packages/patches/transcode-ffmpeg.patch	\
   %D%/packages/patches/transmission-honor-localedir.patch	\
   %D%/packages/patches/ttf2eot-cstddef.patch			\
diff --git a/gnu/packages/patches/torbrowser-start-desktop.patch b/gnu/packages/patches/torbrowser-start-desktop.patch
new file mode 100644
index 0000000000..6832ed363b
--- /dev/null
+++ b/gnu/packages/patches/torbrowser-start-desktop.patch
@@ -0,0 +1,22 @@ 
+Change TorBrowser desktop file in order for it to be agnostic to the
+path when invoked.
+
+--- torbrowser-68.10.0esr-9.5-1/tbb-scripts/RelativeLink/start-tor-browser.desktop.orig    2020-07-05 18:47:40.689484877 -0300
++++ torbrowser-68.10.0esr-9.5-1/tbb-scripts/RelativeLink/start-tor-browser.desktop 2020-07-24 14:36:37.720579884 -0300
+@@ -1,4 +1,4 @@
+-#!/usr/bin/env ./Browser/execdesktop
++#!/usr/bin/env bash
+ #
+ # This file is a self-modifying .desktop file that can be run from the shell.
+ # It preserves arguments and environment for the start-tor-browser script.
+@@ -28,7 +28,7 @@
+ GenericName=Web Browser
+ Comment=Tor Browser is +1 for privacy and −1 for mass surveillance
+ Categories=Network;WebBrowser;Security;
+-Exec=sh -c '"$(dirname "$*")"/Browser/start-tor-browser --detach || ([ ! -x "$(dirname "$*")"/Browser/start-tor-browser ] && "$(dirname "$*")"/start-tor-browser --detach)' dummy %k
+-X-TorBrowser-ExecShell=./Browser/start-tor-browser --detach
+-Icon=web-browser
++Exec=sh -c start-tor-browser
++X-TorBrowser-ExecShell=start-tor-browser --detach
++Icon=torbrowser
+ StartupWMClass=Tor Browser
diff --git a/gnu/packages/patches/torbrowser-start-script.patch b/gnu/packages/patches/torbrowser-start-script.patch
new file mode 100644
index 0000000000..b8c8d9a26a
--- /dev/null
+++ b/gnu/packages/patches/torbrowser-start-script.patch
@@ -0,0 +1,181 @@ 
+Change TorBrowser startup script in order for it to setup needed files
+outside guix store. Remove tests which are not needed on guix system.
+
+--- torbrowser-68.10.0esr-9.5-1/tbb-scripts/RelativeLink/start-tor-browser.orig    2020-07-05 18:47:40.685485004 -0300
++++ torbrowser-68.10.0esr-9.5-1/tbb-scripts/RelativeLink/start-tor-browser 2020-07-23 18:13:32.426282743 -0300
+@@ -5,6 +5,14 @@
+ #
+ # Copyright 2017 The Tor Project.  See LICENSE for licensing information.
+ 
++TBB_HOME="${HOME}/.local/share/torbrowser"
++TBB_LOGFILE="${TBB_HOME}/torbrowser.log"
++TBB_DATA="${TBB_HOME}/Data"
++TBB_PROFILE="${TBB_DATA}/Browser/profile.default"
++TBB_STORE_PATH=$(dirname $(realpath "$0"))
++TBB_STORE_DATA="${TBB_STORE_PATH}/TorBrowser/Data"
++TORRC="${TBB_DATA}/Tor/torrc-defaults"
++
+ complain_dialog_title="Tor Browser"
+ 
+ # First, make sure DISPLAY is set.  If it isn't, we're hosed; scream
+@@ -134,8 +142,8 @@
+           ;;
+       -l | --log)
+           if [ -z "$2" -o "${2:0:1}" == "-" ]; then
+-             printf "Logging Tor Browser debug information to tor-browser.log\n"
+-             logfile="../tor-browser.log"
++             printf "Logging Tor Browser debug information to torbrowser.log\n"
++             logfile="${TBB_LOGFILE}"
+           elif [ "${2:0:1}" == "/" -o "${2:0:1}" == "~" ]; then
+              printf "Logging Tor Browser debug information to %s\n" "$2"
+              logfile="$2"
+@@ -187,41 +195,22 @@
+ 	export XAUTHORITY
+ fi
+ 
+-# If this script is being run through a symlink, we need to know where
+-# in the filesystem the script itself is, not where the symlink is.
+-myname="$0"
+-if [ -L "$myname" ]; then
+-	# XXX readlink is not POSIX, but is present in GNU coreutils
+-	# and on FreeBSD.  Unfortunately, the -f option (which follows
+-	# a whole chain of symlinks until it reaches a non-symlink
+-	# path name) is a GNUism, so we have to have a fallback for
+-	# FreeBSD.  Fortunately, FreeBSD has realpath instead;
+-	# unfortunately, that's also non-POSIX and is not present in
+-	# GNU coreutils.
+-	#
+-	# If this launcher were a C program, we could just use the
+-	# realpath function, which *is* POSIX.  Too bad POSIX didn't
+-	# make that function accessible to shell scripts.
+-
+-	# If realpath is available, use it; it Does The Right Thing.
+-	possibly_my_real_name="`realpath "$myname" 2>/dev/null`"
+-	if [ "$?" -eq 0 ]; then
+-		myname="$possibly_my_real_name"
+-	else
+-		# realpath is not available; hopefully readlink -f works.
+-		myname="`readlink -f "$myname" 2>/dev/null`"
+-		if [ "$?" -ne 0 ]; then
+-			# Ugh.
+-			complain "start-tor-browser cannot be run using a symlink on this operating system."
+-		fi
+-	fi
++# Try to be agnostic to where we're being started from, check if files are on its
++# default paths and chdir to TBB_HOME
++if [ -e "${TORRC}" ]; then
++   cd "${TBB_HOME}"
++else
++   mkdir -p "${TBB_HOME}"
++   cp -R "${TBB_STORE_DATA}" "${TBB_HOME}"
++   chmod -R 700 "${TBB_HOME}"
++   mkdir -p "${TBB_PROFILE}"
++   echo "user_pref(\"extensions.torlauncher.torrc-defaults_path\", \"${TORRC}\");"\
++     > "${TBB_PROFILE}/user.js"
++   echo "ClientTransportPlugin meek_lite,obfs2,obfs3,obfs4,scramblesuit exec ${TBB_STORE_PATH}/TorBrowser/Tor/PluggableTransports/obfs4proxy"\
++     >> "${TORRC}"
++   cd "${TBB_HOME}"
+ fi
+ 
+-# Try to be agnostic to where we're being started from, chdir to where
+-# the script is.
+-mydir="`dirname "$myname"`"
+-test -d "$mydir" && cd "$mydir"
+-
+ # If ${PWD} results in a zero length string, we can try something else...
+ if [ ! "${PWD}" ]; then
+ 	# "hacking around some braindamage"
+@@ -236,16 +225,9 @@
+   ln -nsf ~/.config/ibus/bus .config/ibus
+ fi
+ 
+-# Fix up .desktop Icon and Exec Paths, and update the .desktop file from the
+-# canonical version if it was changed by the updater.
+-cp start-tor-browser.desktop ../
+-sed -i -e "s,^Name=.*,Name=Tor Browser,g" ../start-tor-browser.desktop
+-sed -i -e "s,^Icon=.*,Icon=$PWD/browser/chrome/icons/default/default128.png,g" ../start-tor-browser.desktop
+-sed -i -e "s,^Exec=.*,Exec=sh -c '\"$PWD/start-tor-browser\" --detach || ([ !  -x \"$PWD/start-tor-browser\" ] \&\& \"\$(dirname \"\$*\")\"/Browser/start-tor-browser --detach)' dummy %k,g" ../start-tor-browser.desktop
+-
+ if [ "$register_desktop_app" -eq 1 ]; then
+ 	mkdir -p "$HOME/.local/share/applications/"
+-	cp ../start-tor-browser.desktop "$HOME/.local/share/applications/"
++	cp "${TBB_STORE_PATH}/start-tor-browser.desktop" "$HOME/.local/share/applications/"
+ 	update-desktop-database "$HOME/.local/share/applications/"
+ 	printf "Tor Browser has been registered as a desktop app for this user in ~/.local/share/applications/\n"
+ 	exit 0
+@@ -265,21 +247,6 @@
+ HOME="${PWD}"
+ export HOME
+ 
+-SYSARCHITECTURE=$(getconf LONG_BIT)
+-TORARCHITECTURE=$(expr "$(file TorBrowser/Tor/tor)" : '.*ELF \([[:digit:]]*\)')
+-
+-if [ $SYSARCHITECTURE -ne $TORARCHITECTURE ]; then
+-   complain "Wrong architecture? 32-bit vs. 64-bit."
+-   exit 1
+-fi
+-
+-[% IF c("var/asan") -%]
+-# We need to disable LSan which is enabled by default now. Otherwise we'll get
+-# a crash during shutdown: https://bugs.torproject.org/10599#comment:59
+-ASAN_OPTIONS="detect_leaks=0"
+-export ASAN_OPTIONS
+-[% END -%]
+-
+ function setControlPortPasswd() {
+     local ctrlPasswd=$1
+ 
+@@ -342,13 +309,15 @@
+ # your password in the following line where the word “secret” is:
+ setControlPortPasswd ${TOR_CONTROL_PASSWD:='"secret"'}
+ 
+-# Set up custom bundled fonts. See fonts-conf(5).
+-export FONTCONFIG_PATH="${HOME}/TorBrowser/Data/fontconfig"
+-export FONTCONFIG_FILE="fonts.conf"
+-
+ # Avoid overwriting user's dconf values. Fixes #27903.
+ export GSETTINGS_BACKEND=memory
+ 
++# Set up custom bundled fonts. See fonts-conf(5).
++export FONTCONFIG_FILE="${HOME}/Data/fontconfig/fonts.conf"
++
++sed -i "${FONTCONFIG_FILE}"\
++    -e "s,<dir>fonts</dir>,<dir>${TBB_STORE_PATH}/fonts</dir>,"
++
+ cd "${HOME}"
+ 
+ # We pass all additional command-line arguments we get to Firefox.
+@@ -357,23 +326,23 @@
+ 
+ if [ "$show_usage" -eq 1 ]; then
+     # Display Firefox help, then our help
+-    TOR_CONTROL_PASSWD=${TOR_CONTROL_PASSWD} ./firefox --class "Tor Browser" \
+-        -profile TorBrowser/Data/Browser/profile.default --help 2>/dev/null
++    TOR_CONTROL_PASSWD=${TOR_CONTROL_PASSWD} ${TBB_STORE_PATH}/firefox --class "Tor Browser" \
++        -profile Data/Browser/profile.default --help 2>/dev/null
+     tbb_usage
+ elif [ "$detach" -eq 1 ] ; then
+-    TOR_CONTROL_PASSWD=${TOR_CONTROL_PASSWD} ./firefox --class "Tor Browser" \
+-       -profile TorBrowser/Data/Browser/profile.default "${@}" > "$logfile" 2>&1 </dev/null &
++    TOR_CONTROL_PASSWD=${TOR_CONTROL_PASSWD} ${TBB_STORE_PATH}/firefox --class "Tor Browser" \
++       -profile Data/Browser/profile.default "${@}" > "$logfile" 2>&1 </dev/null &
+     disown "$!"
+ elif [ "$log_output" -eq 1 -a "$show_output" -eq 1 ]; then
+-    TOR_CONTROL_PASSWD=${TOR_CONTROL_PASSWD} ./firefox --class "Tor Browser" \
+-        -profile TorBrowser/Data/Browser/profile.default "${@}" 2>&1 </dev/null | \
++    TOR_CONTROL_PASSWD=${TOR_CONTROL_PASSWD} ${TBB_STORE_PATH}/firefox --class "Tor Browser" \
++        -profile Data/Browser/profile.default "${@}" 2>&1 </dev/null | \
+         tee "$logfile"
+ elif [ "$show_output" -eq 1 ]; then
+-    TOR_CONTROL_PASSWD=${TOR_CONTROL_PASSWD} ./firefox --class "Tor Browser" \
+-        -profile TorBrowser/Data/Browser/profile.default "${@}" < /dev/null
++    TOR_CONTROL_PASSWD=${TOR_CONTROL_PASSWD} ${TBB_STORE_PATH}/firefox --class "Tor Browser" \
++        -profile Data/Browser/profile.default "${@}" < /dev/null
+ else
+-    TOR_CONTROL_PASSWD=${TOR_CONTROL_PASSWD} ./firefox --class "Tor Browser" \
+-        -profile TorBrowser/Data/Browser/profile.default "${@}" > "$logfile" 2>&1 </dev/null
++    TOR_CONTROL_PASSWD=${TOR_CONTROL_PASSWD} ${TBB_STORE_PATH}/firefox --class "Tor Browser" \
++        -profile Data/Browser/profile.default "${@}" > "$logfile" 2>&1 </dev/null
+ fi
+ 
+ exit $?
diff --git a/gnu/packages/tor.scm b/gnu/packages/tor.scm
index 52d78cba79..d5832b19df 100644
--- a/gnu/packages/tor.scm
+++ b/gnu/packages/tor.scm
@@ -28,31 +28,71 @@ 
 
 (define-module (gnu packages tor)
   #:use-module ((guix licenses) #:prefix license:)
+  #:use-module (guix gexp)
+  #:use-module (guix monads)
   #:use-module (guix packages)
   #:use-module (guix utils)
+  #:use-module (guix store)
   #:use-module (guix download)
   #:use-module (guix git-download)
-  #:use-module (guix build-system gnu)
+  #:use-module (guix build-system cargo)
   #:use-module (guix build-system go)
+  #:use-module (guix build-system gnu)
   #:use-module (guix build-system python)
+  #:use-module (guix build-system trivial)
   #:use-module (gnu packages)
+  #:use-module (gnu packages admin)
+  #:use-module (gnu packages assembly)
+  #:use-module (gnu packages audio)
   #:use-module (gnu packages autotools)
   #:use-module (gnu packages base)
+  #:use-module (gnu packages bash)
   #:use-module (gnu packages check)
   #:use-module (gnu packages compression)
+  #:use-module (gnu packages cups)
+  #:use-module (gnu packages databases)
+  #:use-module (gnu packages fontutils)
+  #:use-module (gnu packages gl)
+  #:use-module (gnu packages glib)
+  #:use-module (gnu packages gnome)
   #:use-module (gnu packages golang)
+  #:use-module (gnu packages gtk)
+  #:use-module (gnu packages gnuzilla)
+  #:use-module (gnu packages icu4c)
+  #:use-module (gnu packages image)
+  #:use-module (gnu packages kerberos)
+  #:use-module (gnu packages libcanberra)
   #:use-module (gnu packages libevent)
+  #:use-module (gnu packages libffi)
   #:use-module (gnu packages linux)
+  #:use-module (gnu packages llvm)
+  #:use-module (gnu packages node)
+  #:use-module (gnu packages nss)
   #:use-module (gnu packages pcre)
+  #:use-module (gnu packages perl)
   #:use-module (gnu packages pkg-config)
+  #:use-module (gnu packages pulseaudio)
   #:use-module (gnu packages python)
   #:use-module (gnu packages python-crypto)
   #:use-module (gnu packages python-web)
   #:use-module (gnu packages python-xyz)
   #:use-module (gnu packages qt)
-  #:use-module (gnu packages autotools)
+  #:use-module (gnu packages readline)
+  #:use-module (gnu packages rsync) ; for httpse
+  #:use-module (gnu packages rust)
+  #:use-module (gnu packages rust-apps)
+  #:use-module (gnu packages sqlite)
   #:use-module (gnu packages tls)
-  #:use-module (gnu packages w3m))
+  #:use-module (gnu packages version-control)
+  #:use-module (gnu packages video)
+  #:use-module (gnu packages vim) ; for xxd
+  #:use-module (gnu packages w3m)
+  #:use-module (gnu packages xdisorg)
+  #:use-module (gnu packages xiph)
+  #:use-module (gnu packages xorg)
+  #:use-module (gnu packages xml) ; for httpse
+  #:use-module (ice-9 match)
+  #:use-module ((srfi srfi-1) #:hide (zip)))
 
 (define-public tor
   (package
@@ -498,3 +538,677 @@  incorporates ideas and concepts from Philipp Winter's ScrambleSuit protocol.
 The obfs naming was chosen primarily because it was shorter, in terms of
 protocol ancestery obfs4 is much closer to ScrambleSuit than obfs2/obfs3.")
     (license license:gpl3+)))
+
+;; torbrowser and tor-browser-builder build versions are not always in sync
+(define %torbrowser-version "78.11.0esr-10.0-1")
+(define %tbb-build-version "10.0.17")
+(define %torbrowser-build "build1")
+(define %tbb-build "build1")
+(define %torbrowser-build-id "20210602000000");must be of the form YYYYMMDDhhmmss
+
+;; Fonts for Tor Browser. Avoid downloading 4Gb+ git repo on
+;; https://github.com/googlei18n/noto-fonts.git to use just a handful.
+;; Use the fonts on Tor Browser release tarball.
+(define torbrowser-fonts
+  (package
+    (name "torbrowser-fonts")
+    (version %tbb-build-version)
+    (source
+     (origin
+       (method url-fetch)
+       (uri (string-append "https://archive.torproject.org/tor-package-archive/torbrowser/"
+                           version "/tor-browser-linux64-"
+                           version "_en-US.tar.xz"))
+       (sha256
+        (base32
+         "13x38n1cvqmxjz0jf2fda8lx2k25szzmg7gvv08z3q5na7109m2m"))))
+    (build-system trivial-build-system)
+    (native-inputs
+     `(("tar" ,tar)
+       ("xz" ,xz)))
+    (arguments
+     `(#:modules ((guix build utils))
+       #:builder (begin
+                   (use-modules (guix build utils))
+                   (let ((src (assoc-ref %build-inputs "source"))
+                         (src-dir "tor-browser_en-US/Browser/fonts")
+                         (fonts (string-append %output "/share/fonts"))
+                         (tar (assoc-ref %build-inputs "tar"))
+                         (xz (assoc-ref %build-inputs "xz")))
+                     (mkdir-p fonts)
+                     (format #t "Untaring torbrowser ball ...~%")
+                     (invoke (string-append tar "/bin/tar") "-xf" src
+                             "-C" fonts "--strip-components=3"
+                             (string-append "--use-compress-program=" xz "/bin/xz")
+                             src-dir)
+                     #t))))
+    (home-page "https://github.com/googlei18n/noto-fonts")
+    (synopsis "Tor Browser bundled fonts")
+    (description "Free fonts bundled with Tor Browser.  Includes a subset of Noto,
+Arimo, Cousine, Tinos and STIX fonts.")
+    (license license:silofl1.1)))
+
+(define tor-browser-build
+  (let ((commit (string-append "tbb-desktop-" %tbb-build-version
+                               "-" %tbb-build)))
+    (package
+      (name "tor-browser-build")
+      (version %tbb-build-version)
+      (source
+       (origin
+         (method git-fetch)
+         (uri (git-reference
+               (url "https://git.torproject.org/builders/tor-browser-build.git")
+               (commit commit)))
+         (file-name (git-file-name name version))
+         (sha256
+          (base32
+           "1qwgghy79wx0w1yz132yyaln4g42s72133n6gbdf07rkf5n44izc"))))
+      (build-system trivial-build-system)
+      (arguments
+       `(#:modules ((guix build utils))
+         #:builder (begin
+                     (use-modules (guix build utils))
+                     (format #t "Copying build scripts ...~%")
+                     (copy-recursively (string-append
+                                        (assoc-ref %build-inputs "source")
+                                        "/projects/tor-browser")
+                                       %output
+                                       #:log (%make-void-port "w")))))
+      (home-page "https://www.torproject.org")
+      (synopsis "Tor Browser build scripts")
+      (description "Tor Browser runtime scripts.")
+      (license (license:non-copyleft "file://LICENSE")))))
+
+(define tor-launcher
+  (package
+    (name "tor-launcher")
+    (version "0.2.28")
+    (source
+     (origin
+       (method url-fetch)
+       (uri (string-append "https://archive.torproject.org/tor-package-archive/torbrowser/"
+                           %tbb-build-version "/src-" name "-"
+                           version ".tar.xz"))
+       (sha256
+        (base32
+         "0mbd1q46d8nqisn6n79sp6m29332ymb2pf13xzgq1ml7rfcy6jjy"))))
+    (build-system trivial-build-system)
+    (native-inputs
+     `(("tar" ,tar)
+       ("xz" ,xz)))
+    (arguments
+     `(#:modules ((guix build utils))
+       #:builder (begin
+                   (use-modules (guix build utils))
+		   (format #t "Extracting source ...~%")
+                   (let ((src (assoc-ref %build-inputs "source"))
+                         (tar (assoc-ref %build-inputs "tar"))
+                         (xz (assoc-ref %build-inputs "xz")))
+                     (mkdir-p %output)
+                     (format #t "Extracting source ...~%")
+                     (invoke (string-append tar "/bin/tar") "-xf" src
+                             "-C" %output "--strip-components=1"
+                             (string-append "--use-compress-program=" xz "/bin/xz"))))))
+    (home-page "https://www.torproject.org")
+    (synopsis "Tor Browser built-in controler extension")
+    (description "Browser extension that starts the tor process (which
+connects the browser and other applications to the Tor Network), and
+which helps people configure and use @code{tor}. The first window that
+you see when you start Tor Browser is displayed by this extension.")
+    (license (license:non-copyleft "file://src/LICENSE"))))
+
+(define https-everywhere-lib-wasm
+  (let ((commit "45b1622f1240659aca4762fa336aad1322d6d50f"))
+    (package
+      (name "https-everywhere-lib-wasm")
+      (version "2021.4.15")
+      (source
+       (origin
+         (method git-fetch)
+         (uri (git-reference
+               (url "https://github.com/EFForg/https-everywhere-lib-wasm")
+               (commit commit)))
+         (file-name (git-file-name name version))
+         (sha256
+          (base32
+           "1lq62rzypdzmnnzvfns3ccvv1g7p7g9s8jx788zzigr3gnmkpffx"))))
+      (build-system trivial-build-system)
+      (arguments
+       `(#:modules ((guix build utils))
+         #:builder (begin
+                     (use-modules (guix build utils))
+                     (format #t "Copying source ...~%")
+                     (copy-recursively (assoc-ref %build-inputs "source")
+                                       %output
+                                       #:log (%make-void-port "w")))))
+      (home-page "https://github.com/EFForg/https-everywhere-lib-wasm")
+      (synopsis "Browser extension for protection against known attacks")
+      (description "Browser extension that protects users from a range of
+known attacks on web browsing activity such as Cross-site scripting, clickjack and
+makes possible for the users to block or choose on a per site basis which remote
+javascript to run while browsing the web.")
+      (license license:gpl2+))))
+
+;; Both https-everywhere and noscript are rellying on some precompiled code for now.
+;; Also read on the work on chromium extensions on gnu/build/chromium-extensions.scm
+;; to see if can be adapted.
+(define-public https-everywhere
+  (package
+    (name "https-everywhere")
+    (version "2021.4.15")
+    (source
+     (origin
+       (method url-fetch)
+       (uri (string-append "https://github.com/EFForg/" name "/archive/"
+                           version ".tar.gz"))
+       (file-name (string-append name "-" version ".tar.gz"))
+       (sha256
+        (base32
+         "1bknx8l8gxmpwb13pvn6pdbavknci8q0jhygdaz50ilc1xld89i3"))))
+    (build-system trivial-build-system)
+    (native-inputs
+     `(("bash" ,bash)
+       ("coreutils" ,coreutils)
+       ("findutils" ,findutils)
+       ("git" ,git)
+       ("grep" ,grep)
+       ("gzip" ,gzip)
+       ("https-everywhere-lib-wasm"
+        ,https-everywhere-lib-wasm)
+       ("libxml2" ,libxml2)
+       ("libxslt" ,libxslt)
+       ("openssl" ,openssl)
+       ("python" ,python)
+       ("rsync" ,rsync)
+       ("sed" ,sed)
+       ("tar" ,tar)
+       ("util-linux" ,util-linux) ; for getopt
+       ("xxd" ,xxd)
+       ("which" ,which)
+       ("zip" ,zip)))
+    (arguments
+     `(#:modules ((guix build utils))
+       #:builder (begin
+                   (use-modules (guix build utils))
+                   (let ((src (assoc-ref %build-inputs "source"))
+                         (httpse-libwasm (assoc-ref %build-inputs
+                                                    "https-everywhere-lib-wasm"))
+                         (bash (assoc-ref %build-inputs "bash"))
+                         (coreutils (assoc-ref %build-inputs "coreutils"))
+                         (python (assoc-ref %build-inputs "python"))
+                         (openssl (assoc-ref %build-inputs "openssl"))
+                         (git (assoc-ref %build-inputs "git"))
+                         (grep (assoc-ref %build-inputs "grep"))
+                         (findutils (assoc-ref %build-inputs "findutils"))
+                         (sed (assoc-ref %build-inputs "sed"))
+                         (which (assoc-ref %build-inputs "which"))
+                         (rsync (assoc-ref %build-inputs "rsync"))
+                         (libxml2 (assoc-ref %build-inputs "libxml2"))
+                         (libxslt (assoc-ref %build-inputs "libxslt"))
+                         (util-linux (assoc-ref %build-inputs "util-linux"))
+                         (xxd (assoc-ref %build-inputs "xxd"))
+                         (zip (assoc-ref %build-inputs "zip"))
+                         (tar (assoc-ref %build-inputs "tar"))
+                         (gzip (assoc-ref %build-inputs "gzip")))
+                     (setenv "SHELL" (string-append bash "/bin/bash"))
+                     (set-path-environment-variable
+                      "PATH" '("bin")
+                      (list bash sed findutils which git python tar openssl rsync
+                            libxml2 libxslt util-linux grep xxd gzip zip coreutils))
+                     (set-path-environment-variable
+                      "LIBRARY_PATH" '("lib")
+                      (list bash sed findutils which git python tar openssl rsync
+                            libxml2 libxslt util-linux grep xxd gzip zip coreutils))
+                     (format #t "Untaring source tarball ...~%")
+                     (invoke "tar" "-xf" src "--strip-components=1")
+                     ;; Python3.6 is hardcoded on these scripts. Using v3.8 appears to
+                     ;; be harmless.
+                     (substitute* '("install-dev-dependencies.sh"
+                                    "make.sh"
+                                    "hooks/precommit"
+                                    "test/firefox.sh"
+                                    "test/manual.sh"
+                                    "test/rules/src/https_everywhere_checker/check_rules.py"
+                                    "test/script.py"
+                                    "test/validations.sh"
+                                    "test/validations/filename/run.py"
+                                    "test/validations/relaxng/run.py"
+                                    "test/validations/securecookie/run.py"
+                                    "test/validations/special/run.py"
+                                    "utils/create_zip.py"
+                                    "utils/chromium-translations.py"
+                                    "utils/create-platform-certs/split_combined_cert_file.py"
+                                    ;; "utils/mk-client-whitelist/dbconnect.py"
+                                    ;; "utils/mk-client-whitelist/run.py"
+                                    "utils/merge-rulesets.py"
+                                    "utils/setversion.py"
+                                    "utils/zipfile_deterministic.py")
+                       (("python3.6") "python3"))
+                     (make-file-writable "lib-wasm")
+                     (copy-recursively httpse-libwasm
+                                       "lib-wasm"
+                                       #:log (%make-void-port "w"))
+                     ;; Remove precompiled binaries from source. This breaks
+                     ;; http-everywhere at runtime, but building is successful.
+                     ;; For now building takes the precompiled version included with sources.
+		     ;; Tor Browser upstream does the same. In case you want to remove the
+		     ;; precompiled version, use the routine below. It successfully builds
+		     ;; but at run time rules seem to be broken.
+                     ;(with-directory-excursion "lib-wasm/pkg"
+                     ;  (for-each (lambda (file)
+                     ;              (if (file-exists? file)
+                     ;                  (delete-file file)
+                     ;                  (display (string-append
+                     ;                            "Warning: file " file
+                     ;                            " not found! Skipping...\n"))))
+                     ;            '("https_everywhere_lib_wasm.js"
+                     ;              "https_everywhere_lib_wasm_bg.wasm")))
+                     (for-each patch-shebang
+                               (find-files "."
+                                           (lambda (file stat)
+                                             ;; Filter out symlinks.
+                                             (eq? 'regular (stat:type stat)))
+                                           #:stat lstat))
+                     ;; Failing to generate the xpi, but copy-dir appears to be enough.
+                     ;; Failing on missing 'wasm'? Not generating rulesets. Once lib-wasm
+		     ;; gets cross-compiled, try to move the '.xpi' extension instead.
+                     (invoke "./make.sh")
+                     (copy-recursively "pkg/xpi-eff" %output
+                                       #:log (%make-void-port "w"))
+                     #t))))
+    (home-page "https://www.eff.org/https-everywhere")
+    (synopsis "Browser extension for automatic HTTPS usage")
+    (description "Browser extension that automatically makes the browser to use
+HTTPS instead of plain HTTP when the remote destination makes it available to users.")
+    (license license:gpl2+)))
+
+(define noscript
+  (package
+    (name "noscript")
+    (version "11.2.8")
+    (source
+     (origin
+       (method url-fetch)
+       (uri (string-append "https://secure.informaction.com/download/releases/"
+                           name "-" version ".xpi"))
+       (sha256
+        (base32
+         "0rrlhlzljlmgns7j49c43ilb8wij2zcysrbpap1xxsfbkbczji27"))))
+    (build-system trivial-build-system)
+    (arguments
+     `(#:modules ((guix build utils))
+       #:builder (begin
+		   ;; Fake build for now. Upstream Tor Browser does not build it as well.
+                   (format #t "Copying source ...~%")
+                   (copy-file (assoc-ref %build-inputs "source")
+                              %output))))
+    (home-page "https://noscript.net")
+    (synopsis "Browser extension for protection against known attacks")
+    (description "Browser extension that protects users from a range of
+known attacks on web browsing activity such as Cross-site scripting, clickjack and
+makes possible for the users to block or choose on a per site basis which remote
+javascript to run while browsing the web.")
+    (license license:gpl2+)))
+
+;; (Un)fortunatly Tor Browser has it's own reproducible build system - RBM - which
+;; automates the build process for them and compiles Tor Browser from a range of
+;; repositories and produces a range of tarballs for different architectures and
+;; locales. So we need to cherry-pick what is needed for guix and produce our own
+;; tarball. See https://gitweb.torproject.org/builders/tor-browser-build.git/projects/\
+;; {tor-browser,firefox}/{build,config} for the rationale applied here. See also
+;; the Hacking on Tor Browser document for a high level introduction at
+;; https://trac.torproject.org/projects/tor/wiki/doc/Tor Browser/Hacking).
+;;
+;; TODO: Import langpacks from https://archive.torproject.org/tor-package-archive/\
+;; torbrowser/'version'/langpacks-tor-browser-linux64-version.tar.xz
+(define-public torbrowser-unbundle
+  (let ((commit (string-append "tor-browser-" %torbrowser-version
+                               "-" %torbrowser-build)))
+    (package
+      (inherit icecat)
+      (name "torbrowser-unbundle")
+      (version %tbb-build-version)
+      (source
+       (origin
+	 (method url-fetch)
+	 (uri (string-append "https://archive.torproject.org/tor-package-archive/torbrowser/"
+			     %tbb-build-version
+			     "/src-firefox-tor-browser-"
+			     %torbrowser-version "-" %torbrowser-build
+			     ".tar.xz"))
+	 (sha256
+          (base32
+           "1jnb7v9zn7y2fk7gqvq1ijlwl16i7wf83y1xn2djqcp1j42n1yvq"))))
+      (build-system gnu-build-system)
+      (inputs
+       `(("obfs4" ,obfs4)
+	 ("tor" ,tor-client)
+	 ,@(package-inputs icecat)))
+      (native-inputs
+       `(("https-everywhere" ,https-everywhere)
+	 ("noscript" ,noscript)
+	 ("tor-browser-build" ,tor-browser-build)
+	 ("tor-launcher" ,tor-launcher)
+	 ("torbrowser-fonts" ,torbrowser-fonts)
+	 ("torbrowser-start-script.patch"
+          ,(search-patch "torbrowser-start-script.patch"))
+	 ("torbrowser-start-desktop.patch"
+          ,(search-patch "torbrowser-start-desktop.patch"))
+	 ,@(package-native-inputs icecat)))
+      (properties '())
+      (arguments
+       (substitute-keyword-arguments (package-arguments icecat)
+	 ((#:phases phases)
+          `(modify-phases ,phases
+             ;; On mach build system this is done on configure.
+             (delete 'bootstrap)
+             ;; Doesn't seem to be needed for TorBrowser
+             (delete 'fix-ffmpeg-runtime-linker)
+             ;; Tried to make use of these using let, but inheritance appears to block it.
+             (delete 'install-icons)
+             (delete 'install-desktop-entry)
+             (add-after 'unpack 'make-bundle
+               (lambda* (#:key inputs native-inputs #:allow-other-keys)
+		 (let ((tor-launcher (assoc-ref inputs "tor-launcher"))
+                       (tor-launcher-dir "browser/extensions/tor-launcher")
+                       (tbb (assoc-ref inputs "tor-browser-build"))
+                       (tbb-scripts-dir "tbb-scripts"))
+
+                   (format #t "Copying tor-launcher ...~%")
+                   (copy-recursively tor-launcher tor-launcher-dir
+                                     #:log (%make-void-port "w"))
+                   (format #t "Copying tor-browser-build ...~%")
+                   (mkdir tbb-scripts-dir)
+                   (copy-recursively tbb tbb-scripts-dir
+                                     #:log (%make-void-port "w"))
+		   (make-file-writable "browser/app/profile/000-tor-browser.js")
+		   (make-file-writable (string-append tbb-scripts-dir
+						      "/RelativeLink/start-tor-browser"))
+                   (make-file-writable (string-append tbb-scripts-dir
+						      "/RelativeLink/start-tor-browser.desktop")))
+		 #t))
+             (replace 'apply-guix-specific-patches
+               (lambda* (#:key inputs native-inputs #:allow-other-keys)
+		 (let ((patch (string-append (assoc-ref (or native-inputs inputs)
+							"patch")
+                                             "/bin/patch")))
+                   (for-each (match-lambda
+                               ((label . file)
+				;; Only difference to icecat is browser name.
+				(when (and (string-prefix? "torbrowser-" label)
+                                           (string-suffix? ".patch" label))
+                                  (format #t "applying '~a'...~%" file)
+                                  (invoke patch "--force" "--no-backup-if-mismatch"
+                                          "-p1" "--input" file))))
+                             (or native-inputs inputs)))
+		 #t))
+             (add-after 'apply-guix-specific-patches 'merge-bridge-preferences
+               (lambda* (#:key inputs native-inputs #:allow-other-keys)
+		 (begin
+                   (let ((tbb-prefs (open-file
+                                     "browser/app/profile/000-tor-browser.js" "a"))
+			 (bridge-prefs
+                          (open-input-file
+                           "tbb-scripts/Bundle-Data/PTConfigs/bridge_prefs.js")))
+                     (display (get-string-all bridge-prefs) tbb-prefs)
+                     (close-output-port tbb-prefs)))
+		 #t))
+             (replace 'configure
+               (lambda* (#:key inputs outputs configure-flags #:allow-other-keys)
+		 (let* ((out (assoc-ref outputs "out"))
+			(bash (which "bash"))
+			(flags `(,(string-append "--prefix=" out)
+				 ,@configure-flags)))
+                   (setenv "SHELL" bash)
+                   (setenv "AUTOCONF" (string-append
+                                       (assoc-ref %build-inputs "autoconf")
+                                       "/bin/autoconf"))
+                   (setenv "CONFIG_SHELL" bash)
+                   (setenv "PYTHON" (string-append (assoc-ref inputs "python-2")
+                                                   "/bin/python"))
+                   (setenv "CC" "gcc")  ; needed when Stylo is enabled
+                   (setenv "MOZ_BUILD_DATE" ,%torbrowser-build-id) ; avoid timestamp.
+                   (setenv "LDFLAGS" (string-append "-Wl,-rpath="
+                                                    (assoc-ref outputs "out")
+                                                    "/lib"))
+                   (substitute* ".mozconfig"
+                     ;; Arch independent builddir.
+                     (("(mk_add_options MOZ_OBJDIR=@TOPSRCDIR@/obj).*" _ m)
+                      (string-append m "dir\n"))
+                     (("ac_add_options --disable-tor-launcher") "")
+                     ;; We won't be building incrementals.
+                     (("ac_add_options --enable-signmar") "")
+                     (("ac_add_options --enable-verify-mar") "")
+                     (("ac_add_options --with-tor-browser-version=dev-build")
+		      ;; If we decide to rebrand this browser, here is the place to do it.
+                      (string-append
+                       "ac_add_options --with-tor-browser-version=org.gnu\n"
+                       "ac_add_options --with-unsigned-addon-scopes=app,system\n"
+                       "ac_add_options --allow-addon-sideload\n"
+                       "ac_add_options --enable-pulseaudio\n"
+                       "ac_add_options --disable-debug-symbols\n"
+                       "ac_add_options --disable-updater\n"
+                       "ac_add_options --enable-system-pixman\n"
+                       "ac_add_options --enable-system-ffi\n"
+                       "ac_add_options --with-system-icu\n"
+                       "ac_add_options --with-system-jpeg\n"
+                       "ac_add_options --with-system-libevent\n"
+                       ;;"ac_add_options --with-system-nspr\n" ;; broken for now, see icecat.
+                       ;;"ac_add_options --with-system-nss\n"
+                       "ac_add_options --with-system-zlib\n"
+                       ;; Without these clang is not found.
+                       "ac_add_options --with-clang-path="
+                       (assoc-ref %build-inputs "clang") "/bin/clang\n"
+                       "ac_add_options --with-libclang-path="
+                       (assoc-ref %build-inputs "clang") "/lib\n")))
+                   (substitute* "browser/app/profile/000-tor-browser.js"
+                     ;; Tor Browser updates are disabled on mozconfig, but let's make sure.
+                     (("(pref\\(\"extensions.torbutton.versioncheck_enabled\").*" _ m)
+                      (string-append m ",false);\n"))
+                     ;; Snowflake is not built on gnu targets.
+                     (("pref\\(\"extensions.torlauncher.default_bridge.snowflake.1\".*")
+                      "\n"))
+                   (substitute*
+                       "browser/extensions/tor-launcher/src/defaults/preferences/torlauncher-prefs.js"
+                     ;; Not multilingual. See tor-browser/build:141. Currently disabled on
+                     ;; tor-launcher, but let's make sure while missing langpacks.
+                     (("(pref\\(\"extensions.torlauncher.prompt_for_locale\").*" _ m)
+                      (string-append m ", false);\n")))
+                   ;; For user data outside the guix store.
+                   (substitute* "xpcom/io/TorFileUtils.cpp"
+                     (("ANDROID") "GNUGUIX"))
+                   (substitute* "old-configure.in"
+                     (("(AC_SUBST\\(TOR_BROWSER_DISABLE_TOR_LAUNCHER\\))" _ m)
+                      (string-append m "\n AC_DEFINE(GNUGUIX)\n")))
+                   (format #t "Invoking mach configure ...~%")
+                   (invoke "./mach" "configure"))
+		 #t))
+             (add-after 'configure 'install-desk-icons
+               (lambda* (#:key outputs #:allow-other-keys)
+		 (let ((out (assoc-ref outputs "out")))
+                   (with-directory-excursion "browser/branding/official"
+                     (for-each
+                      (lambda (file)
+			(let* ((size (string-filter char-numeric? file))
+                               (icons (string-append out "/share/icons/hicolor/"
+                                                     size "x" size "/apps")))
+                          (mkdir-p icons)
+                          (copy-file file (string-append icons "/torbrowser.png"))))
+                      '("default16.png" "default22.png" "default24.png"
+			"default32.png" "default48.png" "default64.png"
+			"default128.png" "default256.png" "default512.png"))
+                     #t))))
+             (replace 'build
+               (lambda _
+		 (make-file-writable "Cargo.lock")
+		 (invoke "./mach" "build")
+                 #t))
+             (replace 'neutralise-store-references
+               (lambda _
+		 ;; difference from icecat "objdir" preffix. We could use find-files there.
+		 (substitute*
+                     "objdir/dist/bin/chrome/toolkit/content/global/buildconfig.html"
+                   (((format #f "(~a/)([0-9a-df-np-sv-z]{32})"
+                             (regexp-quote (%store-directory))) _ store hash)
+                    (string-append store
+                                   (string-take hash 8)
+                                   "<!-- Guix: not a runtime dependency -->"
+                                   (string-drop hash 8))))
+		 #t))
+             ;; Tor Browser just do a stage-package here and copy files to its places.
+             (replace 'install
+               (lambda* (#:key inputs native-inputs outputs
+			 configure-flags #:allow-other-keys)
+		 (let* ((out (assoc-ref outputs "out"))
+			(applications (string-append out "/share/applications"))
+			(build "objdir/dist/firefox")
+			(bin (string-append out "/bin"))
+			(lib (string-append out "/lib"))
+			(start-script "tbb-scripts/RelativeLink/start-tor-browser")
+			(desktop-file "tbb-scripts/RelativeLink/start-tor-browser.desktop"))
+                   (invoke "./mach" "build" "stage-package")
+                   ;; Tor Browser doesn't use those.
+                   ;; See: tor-browser-build.git/projects/firefox/build:167
+                   (format #t "Deleting spurious files ...~%")
+                   (with-directory-excursion build
+                     (for-each (lambda (file)
+				 (if (file-exists? file)
+                                     (delete-file file)
+                                     (display (string-append
+                                               "Warning: file " file
+                                               " not found! Skipping...\n"))))
+                               '("firefox-bin" "fonts/TwemojiMozilla.ttf")))
+                   (rmdir (string-append build "/fonts"))
+                   (format #t "Creating install dirs ...~%")
+                   (mkdir-p applications)
+                   (mkdir-p lib)
+                   (mkdir bin)
+                   (format #t "Copying files to install dirs ...~%")
+                   (copy-recursively build (string-append lib "/")
+                                     #:log (%make-void-port "w"))
+                   (copy-file start-script
+                              (string-append lib "/start-tor-browser"))
+                   (copy-file desktop-file
+                              (string-append lib "/start-tor-browser.desktop"))
+                   (chmod (string-append lib "/start-tor-browser") #o555)
+                   (chmod (string-append lib "/start-tor-browser.desktop") #o555)
+                   (format #t "Linking start-tor-browser script ...~%")
+                   (symlink (string-append lib "/start-tor-browser")
+                            (string-append bin "/start-tor-browser"))
+                   (format #t "Installing desktop file ...~%")
+                   (install-file desktop-file applications))
+		 #t))
+             (add-after 'install 'install-fonts
+               (lambda* (#:key inputs native-inputs outputs #:allow-other-keys)
+		 (let* ((out (assoc-ref outputs "out"))
+			(lib (string-append out "/lib/"))
+			(fonts  (string-append (or (assoc-ref native-inputs
+                                                              "torbrowser-fonts")
+                                                   (assoc-ref inputs
+                                                              "torbrowser-fonts"))
+                                               "/share")))
+                   (copy-recursively fonts lib
+                                     #:log (%make-void-port "w"))
+                   (symlink (string-append lib "/fonts")
+                            (string-append out "/share/fonts")))
+		 #t))
+             (add-after 'install-fonts 'install-extensions
+               (lambda* (#:key inputs native-inputs outputs #:allow-other-keys)
+		 (let* ((out (assoc-ref outputs "out"))
+			(lib (string-append out "/lib"))
+			(ext (string-append lib "/browser/extensions"))
+			(torb-ext (string-append lib
+						 "/chrome/torbutton/content/extensions"))
+			(httpse-id "https-everywhere")
+			(noscript-id "{73a6fe31-595d-460b-a920-fcc0f8843232}")
+			(httpse (assoc-ref inputs "https-everywhere"))
+			(noscript (assoc-ref inputs "noscript")))
+                   (mkdir-p ext)
+                   (mkdir-p torb-ext)
+                   (copy-file noscript (string-append
+					ext "/" noscript-id ".xpi"))
+                   (copy-recursively httpse
+                                     (string-append torb-ext "/" httpse-id)
+                                     #:log (%make-void-port "w"))
+                   (chmod (string-append ext "/" noscript-id ".xpi") #o555)
+                   ;; Make httpse available as a builtin extension. This step is
+		   ;; changing some timestamps and making everything non-deterministic.
+		   ;; To check insert 'resource://torbutton/content/extensions/https-everywhere/'
+		   ;; on the browser address field. Check 'reset-gzip-timestamps' to see what
+		   ;; can be done.
+		   ;; https://gitweb.torproject.org/builders/tor-browser-build.git/tree/projects/\
+		   ;; tor-browser/build?h=tbb-desktop-10.0.17-build1&id=16fa8c3bf91a9c1a827a7d23be344de599cd10bb#n77g
+                   (with-directory-excursion lib
+                     (make-file-writable "omni.ja")
+                     (for-each (lambda (file)
+				 (invoke "zip" "-X" "omni.ja" file))
+                               (find-files "chrome" ".*"))))
+		 #t))
+             (add-after 'install-extensions 'link-binaries
+               (lambda* (#:key inputs native-inputs outputs #:allow-other-keys)
+		 (let* ((out (assoc-ref outputs "out"))
+			(tordir (string-append out "/lib/TorBrowser/Tor"))
+			(ptdir (string-append tordir "/PluggableTransports"))
+			(obfs4 (string-append (assoc-ref inputs "obfs4")
+                                              "/bin/obfs4proxy"))
+			(tor (string-append (assoc-ref inputs "tor")
+                                            "/bin/tor")))
+                   (mkdir-p ptdir)
+                   (symlink tor (string-append tordir "/tor"))
+                   (symlink obfs4 (string-append ptdir "/obfs4proxy")))
+		 #t))
+             (add-after 'link-binaries 'copy-bundle-data
+               (lambda* (#:key inputs native-inputs outputs #:allow-other-keys)
+		 (let* ((out (assoc-ref outputs "out"))
+			(lib (string-append out "/lib"))
+			(tbb "tbb-scripts")
+			(ptconf (string-append tbb "/Bundle-Data/PTConfigs"))
+			(docs (string-append lib "/TorBrowser/Docs"))
+			(data (string-append lib "/TorBrowser/Data")))
+                   (mkdir-p data)
+                   (mkdir docs)
+                   (with-directory-excursion
+                       (string-append tbb "/Bundle-Data/linux/Data")
+                     (for-each (lambda (file)
+				 (copy-recursively file
+                                                   (string-append data "/" file)
+                                                   #:log (%make-void-port "w")))
+                               '("Browser" "fontconfig" "Tor")))
+                   (copy-file (string-append ptconf "/linux/torrc-defaults-appendix")
+                              (string-append data "/Tor/torrc-defaults-appendix"))
+                   (copy-recursively (string-append tbb "/Bundle-Data/Docs")
+                                     (string-append docs "/")
+                                     #:log (%make-void-port "w")))
+		 #t))
+             ;; This fixes the file chooser crash that happens with GTK 3
+             (replace 'wrap-program
+               (lambda* (#:key inputs outputs #:allow-other-keys)
+		 (let* ((out (assoc-ref outputs "out"))
+			(lib (string-append out "/lib"))
+			(gtk (assoc-ref inputs "gtk+"))
+			(gtk-share (string-append gtk "/share"))
+			(mesa (assoc-ref inputs "mesa"))
+			(mesa-lib (string-append mesa "/lib"))
+			(pulseaudio (assoc-ref inputs "pulseaudio"))
+			(pulseaudio-lib (string-append pulseaudio "/lib"))
+			(libxscrnsaver (assoc-ref inputs "libxscrnsaver"))
+			(libxscrnsaver-lib (string-append libxscrnsaver "/lib")))
+                   ;; Only difference to icecat is browser name.
+                   (wrap-program (car (find-files lib "^firefox$"))
+                     `("XDG_DATA_DIRS" prefix (,gtk-share))
+                     `("LD_LIBRARY_PATH" prefix (,pulseaudio-lib ,mesa-lib
+								 ,libxscrnsaver-lib))))
+		 #t))))))
+      (home-page "https://www.torproject.org")
+      (synopsis "Anonymous browser derived from Mozilla Firefox")
+      (description
+       "Tor Browser is the Tor Project version of Firefox browser.  It is the only
+recommended way to anonymously browse the web that is supported by the project.
+It modifies Firefox in order to avoid many know application level attacks on
+the privacy of Tor users.
+
+WARNING: This is not the official Tor Browser and is currently on testing.  Use
+at your own risk and please report back on guix channels if you find any
+issues.")
+      (license license:mpl2.0)))) ;and others, see toolkit/content/license.html