From 122285b4047a29c3ab86da01a9e0498e89f04d7d Mon Sep 17 00:00:00 2001 From: Gregor Kleen Date: Tue, 11 Jan 2022 19:56:46 +0100 Subject: ... --- flake.nix | 109 +++++++++++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 90 insertions(+), 19 deletions(-) (limited to 'flake.nix') diff --git a/flake.nix b/flake.nix index 471c3a6..dbffac2 100644 --- a/flake.nix +++ b/flake.nix @@ -197,7 +197,89 @@ install -m 0555 -t $out/bin ubnt-mkfwimage ubnt-fwsplit ''; }; - in { + + unpackWrt = pkgs.writeShellScript "unpack" '' + ${pkgs.rsync}/bin/rsync --chmod=u+wX -rlptD --exclude /tmp --exclude /build_dir --exclude /bin --exclude /staging_dir ${openwrtWithPackages}/. ${./files}/. ./. + + patchDir=$(mktemp -d -t patches.XXXXXXXXXX) + ${pkgs.rsync}/bin/rsync --chmod=u+wX -rlptD ${./patches}/. "''${patchDir}/." + + while IFS= read -r -d $'\0' sopsFile; do + ${pkgs.sops}/bin/sops --output="''${sopsFile%.sops}" --decrypt "''${sopsFile}" + rm "''${sopsFile}" + done < <(find . "$patchDir" -not \( \( -path ./tmp -o -path ./build_dir -o -path ./bin -o -path ./staging_dir \) -a -prune \) -a \( -type f -a -name '*.sops' -a -print0 \)) + + while IFS= read -r -d $'\0' patchFile; do + if [[ "''${patchFile##*.}" = "patch" ]]; then + patch -p1 --batch <$patchFile + else + . $patchFile + fi + done < <(find "$patchDir" -type f -print0) + ''; + buildWrt = pkgs.writeShellScript "build" "make -j V=sc"; + + buildWrtApp = { + type = "app"; + program = toString (pkgs.writeShellScript "build-wrapper" (let + buildPy = pkgs.writers.writePython3 "build" {} '' + from tempfile import TemporaryDirectory + import os + import subprocess + import argparse + from pathlib import Path + + parser = argparse.ArgumentParser(prog="build", + formatter_class=argparse.ArgumentDefaultsHelpFormatter # noqa: E501 + ) + parser.add_argument('-C', '--directory', + dest='dir', + metavar='DIRECTORY', + type=Path, + default=Path('./result'), + help='Change to this directory before doing anything' + ) + parser.add_argument('--copy-to', + dest='copyTo', + metavar='FILE', + type=Path, + default='/srv/tftp/' + Path.cwd().name + '.bin', + help='Copy result image here' + ) + parser.add_argument('--copy-to-no-sudo', + dest='copyToSudo', + action='store_false', + help='Use sudo to copy result image' + ) + parser.add_argument('--copy-image', + dest='image', + default='bin/targets/ath79/generic/openwrt-ath79-generic-ubnt_unifiac-pro-initramfs-kernel.bin', # noqa: E501 + help='Which file to copy' + ) + args = parser.parse_args() + + if not args.dir: + args.dir = Path() + if not args.copyTo: + args.copyTo = None + + args.dir.mkdir(parents=True, exist_ok=True) + os.chdir(args.dir) + + with TemporaryDirectory() as tmpdir: + os.environ["TMPDIR"] = f"{tmpdir}" + subprocess.check_call('${unpackWrt}') + subprocess.check_call('${buildWrt}') + + if args.copyTo: + command = ['cp', args.image, args.copyTo] + if args.copyToSudo: + command = ['sudo', '--', *command] + subprocess.check_call(command) + ''; + in "exec -- ${fhs { runScript = toString buildPy; }}/bin/openwrt-env $@")); + }; + in rec { devShell = pkgs.mkShell { name = "openwrt"; shellHook = let @@ -205,27 +287,11 @@ set -ex function unpackPhase() { - ${pkgs.rsync}/bin/rsync --chmod=u+wX -rlptD --exclude /tmp --exclude /build_dir --exclude /bin --exclude /staging_dir ${openwrtWithPackages}/. ${./files}/. ./. - - patchDir=$(mktemp -d -t patches.XXXXXXXXXX) - ${pkgs.rsync}/bin/rsync --chmod=u+wX -rlptD ${./patches}/. "''${patchDir}/." - - while IFS= read -r -d $'\0' sopsFile; do - ${pkgs.sops}/bin/sops --output="''${sopsFile%.sops}" --decrypt "''${sopsFile}" - rm "''${sopsFile}" - done < <(find . "$patchDir" -not \( \( -path ./tmp -o -path ./build_dir -o -path ./bin -o -path ./staging_dir \) -a -prune \) -a \( -type f -a -name '*.sops' -a -print0 \)) - - while IFS= read -r -d $'\0' patchFile; do - if [[ "''${patchFile##*.}" = "patch" ]]; then - patch -p1 --batch <$patchFile - else - . $patchFile - fi - done < <(find "$patchDir" -type f -print0) + ${unpackWrt} } function buildPhase() { - make -j V=sc + ${buildWrt} } set +ex @@ -236,5 +302,10 @@ packages = { inherit openwrtWithPackages ubnt-mkfwimage; }; + + apps = { + inherit buildWrtApp; + }; + defaultApp = apps.buildWrtApp; }); } -- cgit v1.2.3