diff options
| -rw-r--r-- | flake.nix | 109 |
1 files changed, 90 insertions, 19 deletions
| @@ -197,7 +197,89 @@ | |||
| 197 | install -m 0555 -t $out/bin ubnt-mkfwimage ubnt-fwsplit | 197 | install -m 0555 -t $out/bin ubnt-mkfwimage ubnt-fwsplit |
| 198 | ''; | 198 | ''; |
| 199 | }; | 199 | }; |
| 200 | in { | 200 | |
| 201 | unpackWrt = pkgs.writeShellScript "unpack" '' | ||
| 202 | ${pkgs.rsync}/bin/rsync --chmod=u+wX -rlptD --exclude /tmp --exclude /build_dir --exclude /bin --exclude /staging_dir ${openwrtWithPackages}/. ${./files}/. ./. | ||
| 203 | |||
| 204 | patchDir=$(mktemp -d -t patches.XXXXXXXXXX) | ||
| 205 | ${pkgs.rsync}/bin/rsync --chmod=u+wX -rlptD ${./patches}/. "''${patchDir}/." | ||
| 206 | |||
| 207 | while IFS= read -r -d $'\0' sopsFile; do | ||
| 208 | ${pkgs.sops}/bin/sops --output="''${sopsFile%.sops}" --decrypt "''${sopsFile}" | ||
| 209 | rm "''${sopsFile}" | ||
| 210 | 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 \)) | ||
| 211 | |||
| 212 | while IFS= read -r -d $'\0' patchFile; do | ||
| 213 | if [[ "''${patchFile##*.}" = "patch" ]]; then | ||
| 214 | patch -p1 --batch <$patchFile | ||
| 215 | else | ||
| 216 | . $patchFile | ||
| 217 | fi | ||
| 218 | done < <(find "$patchDir" -type f -print0) | ||
| 219 | ''; | ||
| 220 | buildWrt = pkgs.writeShellScript "build" "make -j V=sc"; | ||
| 221 | |||
| 222 | buildWrtApp = { | ||
| 223 | type = "app"; | ||
| 224 | program = toString (pkgs.writeShellScript "build-wrapper" (let | ||
| 225 | buildPy = pkgs.writers.writePython3 "build" {} '' | ||
| 226 | from tempfile import TemporaryDirectory | ||
| 227 | import os | ||
| 228 | import subprocess | ||
| 229 | import argparse | ||
| 230 | from pathlib import Path | ||
| 231 | |||
| 232 | parser = argparse.ArgumentParser(prog="build", | ||
| 233 | formatter_class=argparse.ArgumentDefaultsHelpFormatter # noqa: E501 | ||
| 234 | ) | ||
| 235 | parser.add_argument('-C', '--directory', | ||
| 236 | dest='dir', | ||
| 237 | metavar='DIRECTORY', | ||
| 238 | type=Path, | ||
| 239 | default=Path('./result'), | ||
| 240 | help='Change to this directory before doing anything' | ||
| 241 | ) | ||
| 242 | parser.add_argument('--copy-to', | ||
| 243 | dest='copyTo', | ||
| 244 | metavar='FILE', | ||
| 245 | type=Path, | ||
| 246 | default='/srv/tftp/' + Path.cwd().name + '.bin', | ||
| 247 | help='Copy result image here' | ||
| 248 | ) | ||
| 249 | parser.add_argument('--copy-to-no-sudo', | ||
| 250 | dest='copyToSudo', | ||
| 251 | action='store_false', | ||
| 252 | help='Use sudo to copy result image' | ||
| 253 | ) | ||
| 254 | parser.add_argument('--copy-image', | ||
| 255 | dest='image', | ||
| 256 | default='bin/targets/ath79/generic/openwrt-ath79-generic-ubnt_unifiac-pro-initramfs-kernel.bin', # noqa: E501 | ||
| 257 | help='Which file to copy' | ||
| 258 | ) | ||
| 259 | args = parser.parse_args() | ||
| 260 | |||
| 261 | if not args.dir: | ||
| 262 | args.dir = Path() | ||
| 263 | if not args.copyTo: | ||
| 264 | args.copyTo = None | ||
| 265 | |||
| 266 | args.dir.mkdir(parents=True, exist_ok=True) | ||
| 267 | os.chdir(args.dir) | ||
| 268 | |||
| 269 | with TemporaryDirectory() as tmpdir: | ||
| 270 | os.environ["TMPDIR"] = f"{tmpdir}" | ||
| 271 | subprocess.check_call('${unpackWrt}') | ||
| 272 | subprocess.check_call('${buildWrt}') | ||
| 273 | |||
| 274 | if args.copyTo: | ||
| 275 | command = ['cp', args.image, args.copyTo] | ||
| 276 | if args.copyToSudo: | ||
| 277 | command = ['sudo', '--', *command] | ||
| 278 | subprocess.check_call(command) | ||
| 279 | ''; | ||
| 280 | in "exec -- ${fhs { runScript = toString buildPy; }}/bin/openwrt-env $@")); | ||
| 281 | }; | ||
| 282 | in rec { | ||
| 201 | devShell = pkgs.mkShell { | 283 | devShell = pkgs.mkShell { |
| 202 | name = "openwrt"; | 284 | name = "openwrt"; |
| 203 | shellHook = let | 285 | shellHook = let |
| @@ -205,27 +287,11 @@ | |||
| 205 | set -ex | 287 | set -ex |
| 206 | 288 | ||
| 207 | function unpackPhase() { | 289 | function unpackPhase() { |
| 208 | ${pkgs.rsync}/bin/rsync --chmod=u+wX -rlptD --exclude /tmp --exclude /build_dir --exclude /bin --exclude /staging_dir ${openwrtWithPackages}/. ${./files}/. ./. | 290 | ${unpackWrt} |
| 209 | |||
| 210 | patchDir=$(mktemp -d -t patches.XXXXXXXXXX) | ||
| 211 | ${pkgs.rsync}/bin/rsync --chmod=u+wX -rlptD ${./patches}/. "''${patchDir}/." | ||
| 212 | |||
| 213 | while IFS= read -r -d $'\0' sopsFile; do | ||
| 214 | ${pkgs.sops}/bin/sops --output="''${sopsFile%.sops}" --decrypt "''${sopsFile}" | ||
| 215 | rm "''${sopsFile}" | ||
| 216 | 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 \)) | ||
| 217 | |||
| 218 | while IFS= read -r -d $'\0' patchFile; do | ||
| 219 | if [[ "''${patchFile##*.}" = "patch" ]]; then | ||
| 220 | patch -p1 --batch <$patchFile | ||
| 221 | else | ||
| 222 | . $patchFile | ||
| 223 | fi | ||
| 224 | done < <(find "$patchDir" -type f -print0) | ||
| 225 | } | 291 | } |
| 226 | 292 | ||
| 227 | function buildPhase() { | 293 | function buildPhase() { |
| 228 | make -j V=sc | 294 | ${buildWrt} |
| 229 | } | 295 | } |
| 230 | 296 | ||
| 231 | set +ex | 297 | set +ex |
| @@ -236,5 +302,10 @@ | |||
| 236 | packages = { | 302 | packages = { |
| 237 | inherit openwrtWithPackages ubnt-mkfwimage; | 303 | inherit openwrtWithPackages ubnt-mkfwimage; |
| 238 | }; | 304 | }; |
| 305 | |||
| 306 | apps = { | ||
| 307 | inherit buildWrtApp; | ||
| 308 | }; | ||
| 309 | defaultApp = apps.buildWrtApp; | ||
| 239 | }); | 310 | }); |
| 240 | } | 311 | } |
