Subject: [bug#51737] [PATCH v2 1/7] gnu: Add linux-libre-waydroid.

From: phodina <[email-masked]>
Date: Sat, 25 Jun 2022 13:08:48 +0000

Hi Ludo',

here's updated patch set.

Indeede it's adding another linux-libre kernel since this opens the attack surface for anybody not using the binder and ashmem interface. However, it bring the support for Android apps on Linux which might be necessary on mobile platforms such as PinePhone.

Any thoughts about upstreaming this?

FIY There are also open source implementation of Andorid apps that can be downloaded from F-Droid [1]

[1] https://f-droid.org/

Kind regards,
Petr

From 0e0d1655ebc3474454471449298bd7b6c8d739da Mon Sep 17 00:00:00 2001
From: Petr Hodina <[email-masked]>
Date: Thu, 11 Nov 2021 19:26:41 +0100
Subject: [PATCH v2 1/7] gnu: Add linux-libre-waydroid.

* gnu/packages/linux.scm (linux-libre-waydroid,
  waydroid-extra-linux-options): New variables.

diff --git a/gnu/packages/linux.scm b/gnu/packages/linux.scm
index 8f7b4f4f5b..d094c529cb 100644
--- a/gnu/packages/linux.scm
+++ b/gnu/packages/linux.scm
@@ -729,6 +729,14 @@ (define %bpf-extra-linux-options
     ;; kheaders module
     ("CONFIG_IKHEADERS" . #t)))
 
+(define %waydroid-extra-linux-options
+  `(;; Modules required for waydroid:
+   ("CONFIG_ASHMEM" . #t)
+   ("CONFIG_ANDROID" . #t)
+   ("CONFIG_ANDROID_BINDER_IPC" . #t)
+   ("CONFIG_ANDROID_BINDERFS" . #t)
+   ("CONFIG_ANDROID_BINDER_DEVICES" . "binder,hwbinder,vndbinder")))
+
 (define (config->string options)
   (string-join (map (match-lambda
                       ((option . 'm)
@@ -1156,6 +1164,24 @@ (define-public linux-libre-mips64el-fuloong2e
                       `(("CONFIG_OVERLAY_FS" . m))
                       %default-extra-linux-options)))
 
+(define-public linux-libre-waydroid
+  (let ((base-linux-libre
+         (make-linux-libre*
+          linux-libre-version
+          linux-libre-gnu-revision
+          linux-libre-source
+          '("x86_64-linux" "i686-linux" "armhf-linux"
+            "aarch64-linux" "riscv64-linux")
+          #:extra-version "linux"
+          #:configuration-file kernel-config
+          #:extra-options
+          (append %waydroid-extra-linux-options
+                  %default-extra-linux-options))))
+    (package
+      (inherit base-linux-libre)
+         (name "linux-libre-waydroid")
+      (inputs `(("cpio" ,cpio) ,@(package-inputs base-linux-libre))))))
+
 (define-public linux-libre-with-bpf
   (let ((base-linux-libre
          (make-linux-libre*
-- 
2.36.1

From 316f4cd48468ee5d2230e976aca24135fbb6af99 Mon Sep 17 00:00:00 2001
From: Petr Hodina <[email-masked]>
Date: Tue, 9 Nov 2021 22:34:39 +0100
Subject: [PATCH v2 3/7] gnu: Add libglibutil.

* gnu/packages/glib.scm (libglibutil): New variable.

diff --git a/gnu/packages/glib.scm b/gnu/packages/glib.scm
index 98267fcee5..6686a51fa1 100644
--- a/gnu/packages/glib.scm
+++ b/gnu/packages/glib.scm
@@ -17,6 +17,7 @@
 ;;; Copyright © 2020 Jan (janneke) Nieuwenhuizen <[email-masked]>
 ;;; Copyright © 2020 Arthur Margerit <[email-masked]>
 ;;; Copyright © 2021 Maxime Devos <[email-masked]>
+;;; Copyright © 2021 Petr Hodina <[email-masked]>
 ;;;
 ;;; This file is part of GNU Guix.
 ;;;
@@ -75,6 +76,7 @@ (define-module (gnu packages glib)
   #:use-module (guix build-system perl)
   #:use-module (guix build-system python)
   #:use-module (guix download)
+  #:use-module (guix git-download)
   #:use-module ((guix licenses) #:prefix license:)
   #:use-module (guix packages)
   #:use-module (guix utils)
@@ -848,6 +850,54 @@ (define-public glibmm-2.64
       (modify-inputs (package-propagated-inputs glibmm)
         (replace "libsigc++" libsigc++-2)))))
 
+(define-public libglibutil
+  (package
+    (name "libglibutil")
+    (version "1.0.65")
+    (source (origin
+              (method git-fetch)
+              (uri (git-reference
+                    (url "https://git.sailfishos.org/mer-core/libglibutil")
+                    (commit version)))
+              (file-name (git-file-name name version))
+              (sha256
+               (base32
+                "0sc8xw5cbxcicipjp6ycbgqppn31lzsll4r9j6b0zxd747dziv54"))))
+    (build-system gnu-build-system)
+    (arguments
+     (list #:make-flags #~(list (string-append "CC="
+                                               #$(cc-for-target))
+                                (string-append "DESTDIR="
+                                               #$output))
+           #:phases #~(modify-phases %standard-phases
+                        (delete 'configure)
+                        (add-after 'unpack 'remove-usr-prefix
+                          (lambda* _
+                            (substitute* "libglibutil.pc.in"
+                              (("/usr/include") (string-append #$output
+                                                               "/include")))
+                            (substitute* "Makefile"
+                              (("usr/") ""))))
+                        (add-after 'install 'install-dev
+                          (lambda* _
+                            (invoke "make" "install-dev"
+                                    (string-append "DESTDIR="
+                                                   #$output))))
+                        (replace 'check
+                          (lambda* (#:key tests? #:allow-other-keys)
+                            (when tests?
+                              (chdir "test")
+                              (invoke "make"
+                                      (string-append "CC="
+                                                     #$(cc-for-target)))
+                              (chdir "..")))))))
+    (native-inputs (list pkg-config))
+    (inputs (list glib))
+    (home-page "https://git.sailfishos.org/mer-core/libglibutil")
+    (synopsis "GLib utilites")
+    (description "This package provides library of glib utilities.")
+    (license license:bsd-3)))
+
 (define-public python-pygobject
   (package
     (name "python-pygobject")
-- 
2.36.1

From e7e3af766279d4e3e87cfe9eeb6458afb1f3bfe4 Mon Sep 17 00:00:00 2001
From: Petr Hodina <[email-masked]>
Date: Wed, 22 Dec 2021 21:31:59 +0100
Subject: [PATCH v2 7/7] services: Add lxd-service-type.

* gnu/services/virtualization.scm (lxd-configuration): New type.
(%lxd-accounts, lxd-service-type): New variables.
(%lxd-activation, lxd-shepherd-service): New procedures.
* gnu/system/file-systems.scm (%elogind-file-systems): Add
"/sys/fs/cgroup/systemd" file-system.

diff --git a/gnu/services/virtualization.scm b/gnu/services/virtualization.scm
index 41afe451c1..15ee30f8e0 100644
--- a/gnu/services/virtualization.scm
+++ b/gnu/services/virtualization.scm
@@ -76,6 +76,9 @@ (define-module (gnu services virtualization)
             virtlog-configuration
             virtlog-service-type
 
+            lxd-configuration
+            lxd-service-type
+
             %qemu-platforms
             lookup-qemu-platforms
             qemu-platform?
@@ -562,6 +565,69 @@ (define (generate-libvirt-documentation)
    `((libvirt-configuration ,libvirt-configuration-fields))
    'libvirt-configuration))
 
+
+;;;
+;;; LXD linux container daemon.
+;;;
+
+(define-configuration lxd-configuration
+  (lxd
+   (package lxd)
+   "LXD package.")
+  (debug?
+   (boolean #f)
+   "Enable or disable debug messages.")
+  (verbose?
+   (boolean #f)
+   "Enable or disable information messages."))
+
+(define %lxd-accounts
+  (list (user-group (name "lxd") (system? #t))))
+
+(define (%lxd-activation config)
+  #~(begin
+      (use-modules (guix build utils))
+      (mkdir-p "/var/log/lxd")))
+
+(define (lxd-shepherd-service config)
+  (let* ((lxd (lxd-configuration-lxd config))
+         (debug? (lxd-configuration-debug? config))
+         (verbose? (lxd-configuration-verbose? config)))
+    (list
+     (shepherd-service
+      (documentation "LXD daemon.")
+      (provision '(lxd))
+      (requirement '(dbus-system
+                     elogind
+                     file-system-/sys/fs/cgroup/blkio
+                     file-system-/sys/fs/cgroup/cpu
+                     file-system-/sys/fs/cgroup/cpuset
+                     file-system-/sys/fs/cgroup/devices
+                     file-system-/sys/fs/cgroup/memory
+                     file-system-/sys/fs/cgroup/pids
+                     file-system-/sys/fs/cgroup/systemd
+                     networking
+                     udev))
+      (start #~(make-forkexec-constructor
+                (list (string-append #$lxd "/bin/lxd")
+                      "--group=lxd"
+                      "--logfile=/var/log/lxd/lxd.log"
+                      #$@(if debug? '("--debug") '())
+                      #$@(if verbose? '("--verbose") '()))))
+      (stop #~(make-kill-destructor))))))
+
+(define lxd-service-type
+  (service-type
+   (name 'lxd)
+   (extensions
+    (list (service-extension activation-service-type
+                             %lxd-activation)
+          (service-extension shepherd-root-service-type
+                             lxd-shepherd-service)
+          (service-extension account-service-type
+                             (const %lxd-accounts))))
+   (default-value (lxd-configuration))))
+
 
 ;;;
 ;;; Transparent QEMU emulation via binfmt_misc.
diff --git a/gnu/system/file-systems.scm b/gnu/system/file-systems.scm
index f8f4276283..01e36268df 100644
--- a/gnu/system/file-systems.scm
+++ b/gnu/system/file-systems.scm
@@ -561,6 +561,17 @@ (define %elogind-file-systems
            (check? #f)
            (options "none,name=elogind")
            (create-mount-point? #t)
+           (dependencies (list (car %control-groups))))
+         ;; The systemd cgroup needs to exist to run systemd inside linux
+         ;; containers (eg. via LXD).  This is *not* required for elogind, but
+         ;; keeping it with the other systemd hacks seemed sensible, for now.
+         (file-system
+           (device "cgroup")
+           (mount-point "/sys/fs/cgroup/systemd")
+           (type "cgroup")
+           (check? #f)
+           (options "none,name=systemd")
+           (create-mount-point? #t)
            (dependencies (list (car %control-groups)))))
    %control-groups))
 
-- 
2.36.1

From 7fecdc80b10c9a9b64b461486b1117acb7785ca1 Mon Sep 17 00:00:00 2001
From: Petr Hodina <[email-masked]>
Date: Tue, 9 Nov 2021 22:41:30 +0100
Subject: [PATCH v2 4/7] gnu: Add libgbinder.

* gnu/packages/glib.scm (libgbinder): New variable.

diff --git a/gnu/packages/glib.scm b/gnu/packages/glib.scm
index 6686a51fa1..82765f60f0 100644
--- a/gnu/packages/glib.scm
+++ b/gnu/packages/glib.scm
@@ -850,6 +850,62 @@ (define-public glibmm-2.64
       (modify-inputs (package-propagated-inputs glibmm)
         (replace "libsigc++" libsigc++-2)))))
 
+(define-public libgbinder
+  (package
+    (name "libgbinder")
+    (version "1.1.23")
+    (source (origin
+              (method git-fetch)
+              (uri (git-reference
+                    (url "https://github.com/mer-hybris/libgbinder")
+                    (commit version)))
+              (file-name (git-file-name name version))
+              (sha256
+               (base32
+                "12nw2ihd2xhpvdh0jlyacskqmbdxhnrm5pnz30v4mkyg9kz4xhdc"))))
+    (build-system gnu-build-system)
+    (arguments
+     (list #:make-flags #~(list (string-append "CC="
+                                               #$(cc-for-target))
+                                (string-append "DESTDIR="
+                                               #$output))
+           #:phases #~(modify-phases %standard-phases
+                        (delete 'configure)
+                        (add-after 'unpack 'fix-pkg-config-in
+                          (lambda* _
+                            (substitute* "Makefile"
+                              (("usr/") ""))
+                            (substitute* "libgbinder.pc.in"
+                              (("@libdir@") (string-append #$output "/lib"))
+                              (("/usr/include") (string-append #$output
+                                                               "/include")))))
+                        (add-after 'install 'install-dev
+                          (lambda* _
+                            (invoke "make" "install-dev"
+                                    (string-append "DESTDIR="
+                                                   #$output))))
+                        (replace 'check
+                          (lambda* (#:key tests? #:allow-other-keys)
+                            (when tests?
+                              (chdir "test")
+                              (invoke "make"
+                                      (string-append "CC="
+                                                     #$(cc-for-target)))
+                              (chdir "..")))))))
+    (native-inputs (list bison flex pkg-config))
+    (inputs (list glib libglibutil))
+    (home-page "https://github.com/mer-hybris/libgbinder")
+    (synopsis "GLib-style interface to binder")
+    (description
+     "This package provides GLib-style interface to binder:
+@enumerate
+@item Integration with GLib event loop
+@item Detection of 32 vs 64 bit kernel at runtime
+@item Asynchronous transactions that don't block the event thread
+@item Stable service manager and low-level transation APIs
+@end enumerate")
+    (license license:bsd-3)))
+
 (define-public libglibutil
   (package
     (name "libglibutil")
-- 
2.36.1

From b0c14635a2ee8afee561ce7c34904ecd15ac59b5 Mon Sep 17 00:00:00 2001
From: Petr Hodina <[email-masked]>
Date: Wed, 10 Nov 2021 01:25:52 +0100
Subject: [PATCH v2 5/7] gnu: Add python-gbinder.

* gnu/packages/glib.scm (python-gbinder): New variable.

diff --git a/gnu/packages/glib.scm b/gnu/packages/glib.scm
index 82765f60f0..adad0313b2 100644
--- a/gnu/packages/glib.scm
+++ b/gnu/packages/glib.scm
@@ -954,6 +954,33 @@ (define-public libglibutil
     (description "This package provides library of glib utilities.")
     (license license:bsd-3)))
 
+(define-public python-gbinder
+  (package
+    (name "python-gbinder")
+    (version "1.0.0")
+    (source (origin
+              (method git-fetch)
+              (uri (git-reference
+                    (url "https://github.com/erfanoabdi/gbinder-python")
+                    (commit version)))
+              (file-name (git-file-name name version))
+              (sha256
+               (base32
+                "0jgblzakjgsy0cj93bmh5gr7qnl2xgsrm0wzc6xjvzry9lrbs360"))))
+    (build-system python-build-system)
+    (arguments
+     (list #:phases #~(modify-phases %standard-phases
+                        (replace 'build
+                          (lambda* _
+                            (invoke "python" "setup.py" "build_ext"
+                                    "--inplace" "--cython"))))))
+    (native-inputs (list python-cython pkg-config))
+    (inputs (list glib libgbinder libglibutil))
+    (home-page "https://github.com/erfanoabdi/gbinder-python")
+    (synopsis "Python bindings for libgbinder")
+    (description "This package provides Python bindings for libgbinder.")
+    (license license:gpl3+)))
+
 (define-public python-pygobject
   (package
     (name "python-pygobject")
-- 
2.36.1

From 6c835528014d8e9545e3e28a3fe390683b5b46f4 Mon Sep 17 00:00:00 2001
From: Petr Hodina <[email-masked]>
Date: Wed, 10 Nov 2021 02:21:08 +0100
Subject: [PATCH v2 6/7] gnu: Add waydroid.

* gnu/packages/virtualization.scm (waydroid): New variable.

diff --git a/gnu/packages/virtualization.scm b/gnu/packages/virtualization.scm
index 4a065e4ef4..e624fe8a61 100644
--- a/gnu/packages/virtualization.scm
+++ b/gnu/packages/virtualization.scm
@@ -2091,6 +2091,94 @@ (define-public bochs
 DOS or Microsoft Windows.")
     (license license:lgpl2.0+)))
 
+(define-public waydroid
+  (package
+    (name "waydroid")
+    (version "1.2.1")
+    (source (origin
+              (method git-fetch)
+              (uri (git-reference
+                    (url "https://github.com/waydroid/waydroid")
+                    (commit version)))
+              (file-name (git-file-name name version))
+              (sha256
+               (base32
+                "1ssqlsnhmpzaq2n9rbkfn3d2ml4vmima0az6gakknjc2q6bnpza9"))))
+    (build-system python-build-system)
+    (arguments
+     (list #:phases #~(modify-phases %standard-phases
+                        (delete 'build)
+                        (delete 'check)
+                        (replace 'install
+                          (lambda* (#:key outputs inputs #:allow-other-keys)
+                            (let* ((lib (string-append #$output
+                                                       "/lib/waydroid")) (tools
+                                                                          (string-append
+                                                                           lib
+                                                                           "/tools"))
+                                   (data (string-append lib "/data"))
+                                   (apps (string-append out
+                                                        "/share/applications"))
+                                   (bin (string-append out "/bin"))
+                                   (paths-bin #~(map (lambda (input)
+                                                     (string-append
+													 #$input "/bin"))
+                                                   '(iptables nftables
+                                                     glibc dnsmasq)))
+                                   (paths-sbin #$(map (lambda (input)
+                                                      (string-append #$input "/sbin"))
+                                                    '(dnsmasq)))
+                                   (site (string-append out "/lib/python"
+                                                        #$(version-major+minor
+                                                           (package-version
+                                                            python))
+                                                        "/site-packages")))
+                              (mkdir-p tools)
+                              (mkdir-p data)
+                              (mkdir-p apps)
+                              (mkdir-p bin)
+                              (copy-recursively "tools" tools)
+                              (copy-recursively "data" data)
+                              (install-file (string-append data
+                                             "/Waydroid.desktop")
+                                            (string-append apps))
+                              (substitute* (string-append apps
+                                                          "/Waydroid.desktop")
+                                (("/usr") lib))
+                              (install-file "waydroid.py" lib)
+                              (symlink (string-append lib "/waydroid.py")
+                                       (string-append bin "/waydroid"))
+                              (wrap-program (string-append bin "/waydroid")
+                                            #~("PYTHONPATH" ":" prefix
+                                               #$paths-bin))
+                              (substitute* (string-append out
+                                            "/lib/waydroid/data/scripts/waydroid-net.sh")
+                                (("/misc") ""))
+                              (wrap-program (string-append out
+                                             "/lib/waydroid/data/scripts/waydroid-net.sh")
+                                            #~("PATH" ":" prefix
+                                               #$(append paths-bin paths-sbin)))))))))
+    (inputs (list bash-minimal
+                  dnsmasq
+                  libgbinder
+                  glibc
+                  lxc
+                  nftables
+                  iptables
+                  python
+                  python-gbinder
+                  python-pygobject))
+    (home-page "https://waydro.id")
+    (synopsis "Container-based approach to boot a full Android system")
+    (description
+     "Waydroid uses Linux namespaces @code{(user, pid, uts, net,
+mount, ipc)} to run a full Android system in a container and provide Android
+applications.  The Android inside the container has direct access to needed
+underlying hardware.  The Android runtime environment ships with a minimal
+customized Android system image based on LineageOS.  The used image is
+currently based on Android 11.")
+    (license license:gpl3)))
+
 (define-public xen
   (package
     (name "xen")
-- 
2.36.1

From cc27e2dbe2d68a337db8706d701c3cc43a4e0b1f Mon Sep 17 00:00:00 2001
From: Petr Hodina <[email-masked]>
Date: Wed, 22 Dec 2021 02:35:46 +0100
Subject: [PATCH v2 2/7] gnu: Add linux-libre-arm64-waydroid.

* gnu/packages/linux.scm (linux-libre-arm64-waydroid): New variable.

diff --git a/gnu/packages/linux.scm b/gnu/packages/linux.scm
index d094c529cb..30738bbad9 100644
--- a/gnu/packages/linux.scm
+++ b/gnu/packages/linux.scm
@@ -1080,6 +1080,33 @@ (define-public linux-libre-arm-omap2plus-4.14
                      #:defconfig "omap2plus_defconfig"
                      #:extra-version "arm-omap2plus"))
 
+(define-public linux-libre-arm64-waydroid
+  (make-linux-libre* linux-libre-version
+                     linux-libre-gnu-revision
+                     linux-libre-source
+                     '("aarch64-linux")
+                     #:defconfig "defconfig"
+                     #:extra-version "arm64-generic"
+                     #:extra-options
+                     (append
+                      `(;; needed to fix the RTC on rockchip platforms
+                        ("CONFIG_RTC_DRV_RK808" . #t)
+                        ;; Pinebook display, battery, charger and usb
+                        ("CONFIG_DRM_ANALOGIX_ANX6345" . m)
+                        ("CONFIG_CHARGER_AXP20X" . m)
+                        ("CONFIG_INPUT_AXP20X_PEK" . m)
+                        ("CONFIG_CHARGER_AXP20X" . m)
+                        ("CONFIG_BATTERY_AXP20X" . m)
+                        ("CONFIG_PINCTRL_AXP209" . m)
+                        ("CONFIG_AXP20X_POWER" . m)
+                        ("CONFIG_AXP20X_ADC" . m)
+                        ;; Pinebook PRO battery and sound support
+                        ("CONFIG_BATTERY_CW2015" . m)
+                        ("CONFIG_CHARGER_GPIO" . m)
+                        ("CONFIG_SND_SOC_ES8316" . m))
+                        %waydroid-extra-linux-options
+                      %default-extra-linux-options)))
+
 (define-public linux-libre-arm64-generic
   (make-linux-libre* linux-libre-version
                      linux-libre-gnu-revision
-- 
2.36.1