{ pkgs, lib }: let generator = pkgs.rustPlatform.buildRustPackage { pname = "openwrt-gen"; version = "0.1.0"; src = ./generator; cargoLock.lockFile = ./generator/Cargo.lock; }; # Remote apply script: backs up the configs it overrides, applies the new # firewall/dnsmasq/ethers, ensures bridge-netfilter sysctls are enabled, # validates, and auto-restores on failure. Runs on the router as-is (no Nix # templating needed), so it's just referenced directly from disk. applyScript = ./skyg-apply.sh; in config: let # Nix-rendered DNS records from the global skyg.internalNetworkingMap. Merged # into the decrypted secret's generalMappings by the scripts below. # Empty fragment when the caller doesn't supply one. dnsRecords = if config ? dnsRecords && config.dnsRecords != null then config.dnsRecords else builtins.toFile "skyg-dns-records-empty.json" (builtins.toJSON { generalMappings = [ ]; }); # Config JSON is read from stdin at deploy time — caller decrypts the age secret. # Usage: age -d secrets/glmain.json.age | openwrt-deploy # # The bulk of the logic lives in ./openwrt-deploy.sh; this just exports the # Nix-computed values it needs (router address, generator/apply-script store # paths) and inlines the file's content after them. deployScript = pkgs.writeShellScriptBin "openwrt-deploy" '' export PATH="${lib.makeBinPath [ pkgs.jq ]}:$PATH" export ROUTER="${config.router.ip}" export ROUTER_USER="${config.router.user}" export GENERATOR="${generator}" export APPLY_SCRIPT="${applyScript}" export NIX_DNS_RECORDS="${dnsRecords}" ${builtins.readFile ./openwrt-deploy.sh} ''; # Dry-run: render the configs locally and write them to .tmp/openwrt-/ # for review. No SSH, no uci, no router changes. Run from the repo root. # The router name is passed as $1 (defaults to glmain) — see ./openwrt-dry-run.sh. dryRunScript = pkgs.writeShellScriptBin "openwrt-dry-run" '' export PATH="${lib.makeBinPath [ pkgs.jq ]}:$PATH" export GENERATOR="${generator}" export NIX_DNS_RECORDS="${dnsRecords}" ${builtins.readFile ./openwrt-dry-run.sh} ''; in { inherit deployScript dryRunScript; }