deploy: add build and deploy script as flake app

This commit is contained in:
Tobias Happ 2022-10-28 20:33:49 +02:00
parent 0a3b09df75
commit 28e2fde133
6 changed files with 116 additions and 155 deletions

4
.gitignore vendored
View file

@ -1,4 +1,4 @@
/.fakedroid
/source.tar.gz
result
result-*
out
.fakedroid

View file

@ -124,10 +124,20 @@ is probably not interesting for you, just download and use a prebuilt one.
If you really want to rebuild it, you can just use Android Studio for that.
The zipball generation is probably what you are after.
Get an x86_64 computer with flake-enabled Nix. Run
Get an x86_64 computer with flake-enabled Nix.
> **tl;dr**: Use the deploy app like the following which executes all steps mentioned below:
>
> ```sh
> nix run ".#deploy" -- <public_url> <rsync_target>
> # or run the following for explanation of this script
> nix run ".#deploy"
> ```
Run
```sh
nix build .#bootstrapZip --impure
nix build ".#bootstrapZip" --impure
```
Put the zip file from `result` on some HTTP server

151
build.sh
View file

@ -1,151 +0,0 @@
#!/usr/bin/env bash
# Copyright (c) 2019-2022, see AUTHORS. Licensed under MIT License, see LICENSE.
# If you want to change something in this repo and now you're wondering
# how to recompile Nix-on-Droid with your changes and put it to your device,
# wonder no more!
# One of the simplest scenarios would be forking it on GitHub:
# * fork it to github.com/YOUR_USERNAME/nix-on-droid
# * change something, commit your changes to some BRANCH
# * push it
# * NIX_ON_DROID_CHANNEL_URL=https://github.com/YOUR_USERNAME/nix-on-droid/archive/BRANCH.tar.gz \
# nix build --show-trace ".#bootstrapZip" --impure -o out/nix-on-droid-aarch64
# * put out/nix-on-droid-aarch64/bootstrap-aarch64.zip to some HTTP server
# * install the app and, on first startup, enter the URL of the directory
# containing the resulting zip file, start the installation
# The zipfile will be used to provide proot and kickstart the installation,
# it will build what's available from NIX_ON_DROID_CHANNEL_URL you've specified,
# (i.e., what you've pushed to BRANCH), and the resulting system will be
# subscribed to the updates that you push to the specified BRANCH of your fork.
# But note: this way proot is not updatable without reinstalling, see below.
# If you just want to change something and test it, this should be enough.
# This probably doesn't warrant a script, you can just run the command above
# and call it a day. If you don't need to maintain a long-term fork, do it.
# You can stop reading here.
# But, in some cases, you need to be concerned with more things than that.
# Maybe you don't want to use GitHub.
# Maybe you don't want to bother with forking or pushing.
# Maybe you need to maintain a long-term fork and ship updates to proot.
# Maybe you want to automate and streamline the whole process.
# If that's the case, read more to find out what to do and how to do it.
# ---
# There are three distinct things built from this repo:
# 1. A zipfile with the installer (zipball),
# which has to be downloaded from some web server during the installation,
# the user is asked for location on the initial app startup.
# (https://nix-on-droid.unboiled.info/bootstrap + /bootstrap-$arch.zip
# for the official builds, the user can override the first part).
# Needed only once, doesn't matter after the installation.
# 2. Sources for building nix-on-droid-specific packages on device,
# which also have to be put somewhere on the web. If you push to GitHub,
# you already have them published in a nix-channel usable form.
# But it's also possible to point to an arbitrary URL.
# The initial location is baked into the installer tarball,
# it's later reconfigurable with 'nix-channel'.
# This is how most updates are shipped.
# 3. Proot binaries. Unfortunately, they can't be built natively and still work
# (and nobody knows why), so the mess above is not enough.
# Installer zipball can be used to deliver the initial proot binary,
# but if you want to deliver updates, you need some third option.
# The official builds address that by having all the installations trust
# `nix-on-droid.cachix.org` and pushing binaries there.
# If you want to maintain a long-term fork, you should create your own
# and do the same, or the only way of updating proot
# would be through reinstallation.
# That's a lot of stuff, and the developers have come up with workflows
# progressively more rich and twisted than `nix build ... && rsync ...`.
# Feel free to pick up tricks below and/or modify the script to suit your needs.
set -e
arches=${arches:-aarch64} # whitespace-separated list of architectures to build
# Create/clear the output directory.
mkdir -p out
rm -f out/*
for arch in $arches; do
# Build proot for the target architecture.
# We don't really need to compile it separately...
echo $arch: building proot...
nix build ".#prootTermux" -o out/proot-$arch
proot=$(realpath out/proot-$arch)
# ... except for the rare case you've advanced pinned nixpkgs
# or changed something regarding proot compilation,
# and now the hashes have changed and we have to update them.
echo $arch: patching proot path in modules/environment/login/default.nix...
# used to be different per architecture, aarch64-only now under prootStatic
grep "prootStatic = \"/nix/store/" modules/environment/login/default.nix
sed -i "s|prootStatic = \"/nix/store/.*\";|prootStatic = \"$proot\";|" \
modules/environment/login/default.nix
grep "prootStatic = \"/nix/store/" modules/environment/login/default.nix
# Since proot has to be cross-compiled and cannot be built on device,
# it's also a good idea to push it to cachix, or existing installations
# wouldn't be able to pick up the changes.
if [[ -z $channel_url ]]; then
# It is enough to push your changes to GitHub to
# you have nix-on-droid sources downloadable from there.
# Let's figure out the URL.
branch=${branch:-$(git rev-parse --abbrev-ref HEAD)} # overrideable
tracking=$(git config branch.${branch}.remote)
url=$(git remote get-url $tracking)
if [[ -z "$channel_url" ]]; then
if [[ -z "$github_repository" ]]; then
if [[ $url =~ git@github.com:* ]]; then
autodetected_repository=${url##git@github.com:}
elif [[ $url =~ https://github.com/* ]]; then
autodetected_repository=${url##https://github.com}
fi
if [[ $autodetected_repository =~ \.git ]]; then
autodetected_repository=${autodetected_repository%%.git}
fi
echo $arch: autodetected repository: $autodetected_repository
if [[ $autodetected_repository == 't184256/nix-on-droid' ]]; then
if [[ $USER != 'monk' ]]; then
echo 'Failed to autodetect the URL of your fork.'
echo 'Set either github_repository or channel_url.'
exit 1
fi
fi
channel_url=${channel_url:-https://github.com/$autodetected_repository/archive/$branch.tar.gz}
echo $arch: autodetected channel URL: $channel_url
fi
fi
else
# Build channel tarball,
# later optionally push it somewhere along with the installation zipball
:
fi
### not annotated yet ###
nixOnDroidChannelURL=${nixOnDroidChannelURL:-$channel_url}
echo $arch: building nix-on-droid...
NIX_ON_DROID_CHANNEL_URL="$nixOnDroidChannelURL" nix build ".#bootstrapZip" --impure -o out/nix-on-droid-$arch
if [ -z "$rsync_to" ]; then
echo "Done. Now put out/nix-on-droid-$arch/bootstrap-$arch.zip on some HTTP server and point the app to it. Good luck!"
else
if [ $branch == master ]; then
tgt="$rsync_to/bootstrap/"
else
tgt="$rsync_to/bootstrap-$branch/"
fi
echo rsyncing to $tgt...
rsync -vP out/nix-on-droid-$arch/bootstrap-*.zip $tgt
fi
done

View file

@ -54,6 +54,11 @@
program = "${self.packages.${system}.nix-on-droid}/bin/nix-on-droid";
};
deploy = {
type = "app";
program = toString (import ./scripts/deploy.nix { inherit nixpkgs system; });
};
fakedroid = {
type = "app";
program = toString self.packages.${system}.fakedroid;

34
scripts/deploy.nix Normal file
View file

@ -0,0 +1,34 @@
# Copyright (c) 2019-2022, see AUTHORS. Licensed under MIT License, see LICENSE.
{ nixpkgs, system }:
let
pkgs = nixpkgs.legacyPackages.${system};
runtimePackages = with pkgs; [
coreutils
git
gnugrep
gnused
gnutar
gzip
jq
nix
openssh
rsync
];
in
pkgs.runCommand
"deploy"
{
preferLocalBuild = true;
allowSubstitutes = false;
}
''
install -D -m755 ${./deploy.sh} $out
substituteInPlace $out \
--subst-var-by bash "${pkgs.bash}" \
--subst-var-by path "${pkgs.lib.makeBinPath runtimePackages}"
''

63
scripts/deploy.sh Executable file
View file

@ -0,0 +1,63 @@
#!@bash@/bin/bash
set -euo pipefail
PATH=@path@
if [[ $# -ne 2 ]]; then
cat >&2 <<EOF
USAGE: nix run .#deploy -- <public_url> <rsync_target>
Builds bootstrap zip ball and source code tar ball (for usage as a channel or
flake) and uploads it to the directory specified in <rsync_target>. The
contents of this directory should be reachable by the android device with
<public_url>.
Example:
$ nix run .#deploy -- 'https://example.com/bootstrap' 'user@host:/path/to/bootstrap'
EOF
exit 1
fi
PUBLIC_URL="$1"
RSYNC_TARGET="$2"
# this allows to run this script from every place in this git repo
REPO_DIR="$(git rev-parse --show-toplevel)"
cd "$REPO_DIR"
SOURCE_FILE="source.tar.gz"
function log() {
echo "> $*"
}
log "building proot..."
PROOT="$(nix build --no-link --print-out-paths ".#prootTermux")"
PROOT_HASH_FILE="modules/environment/login/default.nix"
log "patching proot path in $PROOT_HASH_FILE..."
grep "prootStatic = \"/nix/store/" "$PROOT_HASH_FILE"
sed -i "s|prootStatic = \"/nix/store/.*\";|prootStatic = \"$PROOT\";|" "$PROOT_HASH_FILE"
grep "prootStatic = \"/nix/store/" "$PROOT_HASH_FILE"
log "building bootstrapZip..."
export NIX_ON_DROID_CHANNEL_URL="$PUBLIC_URL/$SOURCE_FILE"
export NIX_ON_DROID_FLAKE_URL="$PUBLIC_URL/$SOURCE_FILE"
BOOTSTRAP_ZIP="$(nix build --no-link --print-out-paths --impure ".#bootstrapZip")"
log "creating tar ball of current HEAD..."
git archive --prefix nix-on-droid/ --output "$SOURCE_FILE" HEAD
log "uploading artifacts..."
rsync --progress \
"$SOURCE_FILE" \
"$BOOTSTRAP_ZIP/bootstrap-aarch64.zip" \
"$RSYNC_TARGET"