blog: nixos pinephone: turn these notes into a structured article

This commit is contained in:
colin 2022-05-30 03:38:47 -07:00
parent 04f48def04
commit 74750128ee
1 changed files with 78 additions and 30 deletions

View File

@ -1,37 +1,61 @@
+++
title = "NixOS on my Pinephone"
date = 2022-01-01
description = "remove extra.hidden to link this from the index"
description = "a flake-based quickstart and some tuning"
extra.hidden = true
+++
step 1: provision a nix machine
there is no official, easy-to-grab NixOS image to download and flash to devices like the pinephone today. although there is [a way](https://news.ycombinator.com/item?id=30010178) to do that via the hydra build cache, it's a bit tortured and since the images built in automation don't have a user, you'll have to manually edit `/etc/fstab` (from a different machine) to add a user and then login with a USB-C keyboard.
step 2: convert to a flake
step 3: install jumpdrive to a sd card
step 4: build a minimal mobile-nixos
validate the image (optional; debugging):
```sh
$ sudo losetup -Pf /nix/store/rp9nsr9zympcc1vqah4rckija2lb34xx-pine64-pinephone_full-disk-image.img
$ sudo fsck -f /dev/loop0p4
$ sudo losetup -d /dev/loop0
so first, provision an existing Nix install. the architecture doesn't matter -- i'll assume it's x86_64 and show you how to cross-compile. i recommend using a flake for this. i'm basing this around nix-22.05, so something like this:
```flake.nix
TODO
```
step 5: flash the image. oflag=direct is less likely to result in corruption
```sh
$ sudo umount /dev/sdb* ; sudo dd if=/nix/store/qalvicwwl80sfjx0rxmyi85wg30x257f-pine64-pinephone_full-disk-image.img of=/dev/sdb bs=1M status=progress conv=sync oflag=direct;
```configuration.nix
TODO
```
validate with `sudo fsck -f /dev/sdb4`. you might see a single, inconsequential error about `Padding at end of inode bitmap is not set.`
## Bare-minimal Build
step 6: resize the rootfs:
for our Pinephone machine, we'll let [mobile-nixos](https://github.com/NixOS/mobile-nixos/) do the heavy lifting.
RESIZE THE PARTITION USING CFDISK. `parted` and the like mess with the bootloader. mobile-nixos is switching from
u-boot to towboot, so at some point this won't be problematic. you can resize the filesystem using resize2fs,
or just skip that and the phone will do that the next time you boot.
```diff -u a/flake.nix b/flake.nix
TODO: minimal NixOS install
```
if your host machine isn't aarch64, you'll have to enable cross compiling.
```diff -u a/flake.nix b/flake.nix
TODO: enable cross compiling
```
then build the image with:
```sh
nix build './#imgs.moby'
```
the resulting image is effectively the same as what Hydra spits out in the automation: no users, and no way to login. if you're brand new to the Pinephone, i recommend flashing the image and booting as a sanity check. otherwise, skip to _Making the Image More Usable_ where we'll build a more usable image. at any time, consult the _Troubleshooting_ section if you hit something unexpected.
### Flashing the Image
you might be tempted to flash this to the eMMC instead of an SD card, thinking that the former will be more reliable storage. it's not: my eMMC began to fail within 20 write cycles. i highly recommend you use an SD card for this.
```sh
sudo dd if=$(readlink ./result)$ of=/dev/sdb bs=4M oflag=direct conv=sync status=progress
```
`oflag=direct` makes the progress bar actually usable -- otherwise it'll just show how much data has been passed to the kernel -- and `conv=sync` seems to deal better with low quality SD cards (it feels like paranoia, but if i `fsck -f` the result it sometimes shows corruption without this flag).
insert the card to the Pinephone and boot it. the SD slot takes precedent over the eMMC in its boot sequence, so nothing more is needed. you should see the power indicator turn red (indicating that the builtin bootloader is active), then yellow (u-boot), then green (NixOS stage 1) (TODO: verify this). you'll see a mobile-NixOS splash screen, it'll take a minute to validate the fs, and then boot into stage 2 where you'll be stuck at a login prompt.
## Making the Image More Usable
we'll want to rebuild the image and include a user, desktop environment, and some basic applications. i tried Phosh, Plasma Mobile, and Gnome: of these, Phosh works the best OOTB _by far_ (Plasma Mobile is _slow_ and crash-prone, Gnome lacks an on-screen keyboard outside the core apps and its navigation is less tailored to phones).
```diff -u a/flake.nix b/flake.nix
TODO: show how to setup Phosh
```
build and flash the image as before. this is the final image we'll flash, so before you eject the card resize the rootfs so you can make use of the full device. do NOT use `fdisk` or `parted` for this. it disrupts some on-disk structures required by the bootloader (this won't be a worry after mobile-nixos officially switches to tow-boot). instead, use the ncurses frontend to fdisk: `cfdisk`:
```sh
$ sudo cfdisk /dev/sdb
@ -40,33 +64,57 @@ scroll to /dev/sdb4
leave at default (28.8G)
> Write
type "yes"
> Quite
> Quit
```
step 7: poweroff, remove SD card, boot phone.
eject the card and boot the phone, login, and toy around with it. open the display settings and mess with the scale (it defaults to 200%). i find 150% is best, particularly so that Firefox doesn't overflow.
step 8: add gnome and re-flash; connect to wifi (`nmcli device wifi connect <wifiname>`)
## Building Generations on the Device
we don't want to re-flash the device _every_ time we change something. let's update the flake to allow on-device updates.
step 9: ssh into the machine. clone/rebuild config:
```diff -u a/flake.nix b/flake.nix
TODO: add toplevel nixosConfigurations.pinephone
```
copy your flake over to the phone at `/etc/nixos/flake.nix` somehow. i use git for this. on the phone:
```sh
~$ git clone https://git.uninsane.org/colin/nix-files.git
~$ sudo rmdir /etc/nixos && sudo mv nix-files /etc/nixos
~$ cd /etc/nixos
/etc/nixos$ sudo git config --global --add safe.directory /etc/nixos
/etc/nixos$ sudo nixos-rebuild --flake "./#moby" switch # XXX: check /etc/fstab for compatibility first?
/etc/nixos$ sudo nixos-rebuild --flake "./#pinephone" switch
```
if everything goes right, the build shouldn't take more than 5-10 minutes.
`sudo reboot now` and you should be right back to where you were.
you're set!
validate this with a reboot, and you should be golden! happy nixing :-)
# Troubleshooting
TODO
unbootable; unflashable (e.g. eMMC not exposed over USB)... tend to be power related issues.
the base jumpdrive image does NOT appear to charge the battery (definitely not when connected to a PC; unsure about when connected to the wall).
flash postmarketOS to SD & boot: this will charge and you'll see battery percentage
# Resources:
use dd flags.
eMMC has a dozen or two write cycles: prefer SD card.
WiFi without GUI: `nmcli device wifi connect <wifiname>`
jumpdrive
validating the built, but unflashed, image:
```sh
$ sudo losetup -Pf /nix/store/rp9nsr9zympcc1vqah4rckija2lb34xx-pine64-pinephone_full-disk-image.img
$ sudo fsck -f /dev/loop0p4
$ sudo losetup -d /dev/loop0
```
validate a flashed image with `sudo fsck -f /dev/sdb4`. you might see a single, inconsequential error about `Padding at end of inode bitmap is not set.`
# Additional Resources:
TODO: clean this section up
[NixOS from a working install](https://git.sr.ht/~tomf/notes/tree/master/item/pinephone-nixos-getting-started.md)
configuration is from a mobile-nixos local.nix repo. workable (leverages the hydra cache), but unclear how one would configure this after install (flake support?)
[HN cached build](https://news.ycombinator.com/item?id=30010178)