Skip to content

sergei-mironov/mobile-nixos-cfg

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

78 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

What is it?

This is an umbrella repo for my Pinephone and MobileNixos experiemnts. The repo is a bit oldschoolish as it doesn't use NixFlakes. The dependant repositories are pinned as Git submodules (use git clone --recursive ..).

  1. What is it?
  2. Hardware
  3. Usage
  4. Notes
  5. Resources

Hardware

  • A Pinephone and a USB cable
  • A host PC with the Nix package manager installed
  • 32Gb SDcard and USB cardreader

Usage

How to use this repository

  1. Checkout the Git submodules (host) $ git submodule update --init
  2. Enter the nix-shell to set the PATH and NIX_PATH variable: (host) $ nix-shell.

Flashing external SDcard with Jumpdrive utility

The algorithm:

  1. Insert the TF-sdcard to your cardreader and connect it to the Host PC
  2. Run the flash-jumpdrive.sh script sh ./script/flash-jumpdrive.sh
  3. Make sure the script detects the sdcard correctly, press y
  4. Type in the sudo password if set. The sdcard is now the jumpdrive sdcard
  5. Move the sdcard from the cardreader to the Pinephone
  6. Boot the Pinephone and connect it to the PC with USB cable. eMMC flash will appear in the list of Host PC's block devices (verify it with lsblk).

Now one can flash the phone's internal eMMC from the Host PC.

Flashing the internal eMMC card with a Mobile NixOS example configuration

Here we flash the Pinephone with the bootable MobileNixos configuration example.nix. Currently it has only an absolute minimum of features, like GUI+Wifi.

  1. Enable the binfmt boot option of the Host PC's kernel as described in the Tom'f blog.

  2. Make sure that:

    • External SDcard is flashed with Jumpdrive utility
    • The pinephone is booted from Jumpdrive SDcard, connected to the Host PC and the internal eMMC is listed among the PC block devices (lsblk).
  3. Make sure that mobile-nixos and nixpkgs submodules are checked-out (git submodule update --init --recursive).

  4. Edit the DEVL variable of flash-mobile-nixos. Set it to the correct name of internal eMMC device.

  5. Run flash-mobile-nixos.sh

  6. Upon completion, turn the Pinephone off, remove the Jumpdrive sdcard from the Pinephone and turn it on again. Nixos XFCE example image should boot and the XFCE desktop should finally appear.

  7. Manually resize the partition table on disk and the root filesystem, check the result with df -h.

    (pinephone) $ sudo cfdisk /dev/mmcblk2
    ... Add the remaining free space to the /dev/mmcblk2p4 partition
    ... Write the new partition
    (pinephone) $ sudo resize2fs /dev/mmcblk2p4
  8. The result may look like:

  • Note 1: I only tested the binfmt hack described in Tomf's blog.
  • Note 2: The Original configuration doesn't enable SSH by mistake. This mobile-nixos PR fixes it: mobile-nixos/mobile-nixos#455 In this repo the changes should be already applied.

Determining the version of Pinephone's hardware

(pinephone) $ cat /proc/device-tree/model
1.2

1.1 is the Braveheart and 1.2 is a Community or Beta PinePhone. However Community editions have subversions like 1.2, 1.2a, 1.2b. Those might have additional hardware fixes.

Updating Mobilenixos over SSH

The below method allows updating the Stage-2 software. The idea is to build the toplevel package and then upload it's closure using nix-copy-closure over SSH. A related PR comment with a discussion

  1. Make meaningful changes in the example.nix configuration.
  2. Turn the Pinephone on, connect it to a local WiFi network, figure out its IP address. Say, we got a 192.168.1.38.
  3. Adjust the DEVIP variable of build-switch-toplevel.sh accordingly, and run it. Depending on the current SSH settings, the script may ask for ssh password several times (nixos by default).
    (host) $ build-switch-toplevel.sh switch

The Pinephone software should be switched to the just-built profile. The old profile should be accessable through the recovery menu (shown at reboot+volume up).

Note: build-switch-phosh.sh script works with the phosh.nix config in a similar manner.

Setting up a remote build agent

The source wiki page https://nixos.wiki/wiki/Distributed_build

Here we teach the Host PC's nixos to use the Pinephone as a remote build agent. Not sure, but it seems that it works faster than host-based build using qemu which also works by default.

  1. Determine which user runs builds on the Host PC. For NixOS hosts, the user is root.
  2. Manyally setup the passwordless SSH loging from the Host PC's root user to the pinephone, typically asa follows:
    • Add the root's public SSH keys to the root@pinephone (done in the example.nix.
    • Rebuild and switch the Pinephone's configuration with build-switch-toplevel.sh switch
    • Add the pinephone-builder section into the /root/.ssh/config of the Host.
  3. Assuming that the Host PC is a NixOS, we need to update it's config with a snippet like the following:
    { config, pkgs, ... }:
    {
      nix.buildMachines = [
        { hostName = "pinephone-builder";
          sshUser = "root";
          systems = ["aarch64-linux"];
          maxJobs = 1;
          speedFactor = 2;
          mandatoryFeatures = [ ];
        }
      ] ;
    
      nix.distributedBuilds = true;
    }
  4. To test that the Pinephone's store is available:
    (host) $ sudo nix ping-store --store ssh://pinephone-builder && echo ok
    ok
  5. To make sure that the builder does ditribute builds to the Pinephone:
    (host) $ vim modules/nixpkgs/pkgs/tools/misc/mc/default.nix
    ... edit smth to force rebuilding (TODO: how to --check distributed build?)
    (host) $ build-mobile-nixos.sh -j0 -A pkgs.mc
  6. Watch the host's log and the pinephone's htop.

On updating the MobileNixos and Nixpkgs submodules

Switching to a Phosh configuration

  1. Make sure that the Pinephone is on, its Wifi is working and the passwordless SSH login is set up

  2. Run the build-switch-phosh.sh

    (host) $ build-switch-phosh.sh switch
  3. The result may look like

Running applications from the Phosh configuraiton via SSH

GUI on the phone's screen

  1. Login tho the Pinephone using SSH
  2. Set variables
    • For the wayland apps: export WAYLAND_DISPLAY=wayland-0
    • For the Telegram export QT_QPA_PLATFORM=wayland
  3. Run apps from the command line, they should appear on the phone's screen.

GUI on the host

  1. Install weston and waypipe
  2. Run weston as host's X-client. Click on the termina icon
  3. Run (host+weston) $ waypipe ssh nixos@pinephone <WAYLAND_APP>. See the application's screen appear inside the weston window on the host.

Running the Phosh config in a QEMU emulator

  1. Run run-qemu-phosh.sh.

    (host) $ run-qemu-phosh.sh
  2. The result may look like

Notes:

  • Actually, building the qemu image takes some time. It may be faster to run simple things on a real device.
  • TODO: figure out how to adjust the screen size to that of real pinephone.

Updating the modem firmware

This is a hard one.

  1. In general, we follow the Quectel's guide.
  2. Make sure pkgs.android-tools presents in the system packages section of the pinephone configuration.
  3. Temporary disable the modem-control service: systemd.services."modem-control".enable = false;
  4. Reboot the pinephone. Modem should be powered off.
  5. Stop the ModemManager (pinephone) $ systemctl stop ModemManager
  6. Power the modem on using dumb method echo 2 > /sys/class/modem-power/modem-power/device/powered
  7. Generate the magic AT commands
    (pinephone) $ journalctl -b | grep 'ADB KEY'
    ... ADB KEY is '31281228' ...
    (pinephone) $ curl https://xnux.eu/devices/feature/qadbkey-unlock.c > qadbkey-unlock.c
    (pinephone) $ gcc qadbkey-unlock.c -lcrypt -o qadbkey-unlock
    (pinephone) $ ./qadbkey-unlock 31281228
    ... Prints three AT commands: two to unlock the ADB and one to lock it back.
  8. Send the first two AT commands to the Modem's serial port /dev/ttyUSB2 (use minicom, or start the ModemManager with --debug and use mmcli or use other serial communicatin methods)
  9. An additional usb device should appear. adb should be able to recognize it as a unnamed device. Proceed with Quectel's manual: (pinephone) # adb reboot edl.
  10. Proceed with Quectel's manual to the ./qfirehose -f ./ part.
  11. Upon successful update, disable the adb by sending the third magic AT command, generated by qadbkey-unlock.
  12. Re-enable the modem-control service and reboot the pinephone.

Notes

Shallo fetches

  • To make a shallow fetch of a specific commit, do git fetch [remote] --depth 1 origin <full_commit_hash>

Power consumption

Hotspot issues

USB network issues

  • Sometimes host can't receive the IP addres. One could set it manually to 172.16.42.2 and connect to 172.16.42.1

    sudo ifconfig enp0s20f0u1 172.16.42.2 netmask 255.255.255.0
    

Downloading kernel source

build-switch-phosh.sh -A config.system.build.kernel.src

Time issue

Author's phone probably has a defected crystall on a low-precision clock.

Resources

MobileNixos on Pinephone

MobileNixos general

PinePhone general

Mobile nixers

Fun

Releases

No releases published

Packages

No packages published