caddy: clean up withPlugins logic and support replacement

stepbrobd.com 63b06645 5a983011

verified
+53 -25
+53 -25
pkgs/by-name/ca/caddy/plugins.nix
··· 29 29 { 30 30 plugins, 31 31 hash ? fakeHash, 32 + doInstallCheck ? true, 32 33 }: 33 34 34 35 let ··· 83 84 84 85 # xcaddy built output always uses pseudo-version number 85 86 # we enforce user provided plugins are present and have matching tags here 86 - doInstallCheck = true; 87 + inherit doInstallCheck; 87 88 installCheckPhase = '' 88 89 runHook preInstallCheck 89 90 91 + declare -A modules errors 90 92 ${toShellVar "notfound" pluginsSorted} 91 93 92 - while read kind module version; do 93 - [[ "$kind" = "dep" ]] || continue 94 - module="''${module}@''${version}" 95 - for i in "''${!notfound[@]}"; do 96 - if [[ ''${notfound[i]} = ''${module} ]]; then 97 - unset 'notfound[i]' 98 - fi 99 - done 94 + # put build info that we care about into `modules` list 95 + while read -r kind module version _; do 96 + case "$kind" in 97 + 'dep'|'=>') 98 + modules[$module]=$version 99 + ;; 100 + *) 101 + # we only care about 'dep' and '=>' directives for now 102 + ;; 103 + esac 100 104 done < <($out/bin/caddy build-info) 101 105 102 - if (( ''${#notfound[@]} )); then 103 - for plugin in "''${notfound[@]}"; do 104 - base=''${plugin%@*} 105 - specified=''${plugin#*@} 106 - found=0 106 + # compare build-time (Nix side) against runtime (Caddy side) 107 + for spec in "''${notfound[@]}"; do 108 + if [[ $spec == *=* ]]; then 109 + # orig=repl_mod@repl_ver 110 + orig=''${spec%%=*} 111 + repl=''${spec#*=} 112 + repl_mod=''${repl%@*} 113 + repl_ver=''${repl#*@} 107 114 108 - while read kind module expected; do 109 - [[ "$kind" = "dep" && "$module" = "$base" ]] || continue 110 - echo "Plugin \"$base\" have incorrect tag:" 111 - echo " specified: \"$base@$specified\"" 112 - echo " got: \"$base@$expected\"" 113 - found=1 114 - done < <($out/bin/caddy build-info) 115 + if [[ -z ''${modules[$orig]} ]]; then 116 + errors[$spec]="plugin \"$spec\" with replacement not found in build info:\n reason: \"$orig\" missing" 117 + elif [[ -z ''${modules[$repl_mod]} ]]; then 118 + errors[$spec]="plugin \"$spec\" with replacement not found in build info:\n reason: \"$repl_mod\" missing" 119 + elif [[ "''${modules[$repl_mod]}" != "$repl_ver" ]]; then 120 + errors[$spec]="plugin \"$spec\" have incorrect tag:\n specified: \"$spec\"\n got: \"$orig=$repl_mod@''${modules[$repl_mod]}\"" 121 + fi 122 + else 123 + # mod@ver 124 + mod=''${spec%@*} 125 + ver=''${spec#*@} 115 126 116 - if (( found == 0 )); then 117 - echo "Plugin \"$base\" not found in build:" 118 - echo " specified: \"$base@$specified\"" 119 - echo " plugin does not exist in the xcaddy build output, open an issue in nixpkgs or upstream" 127 + if [[ -z ''${modules[$mod]} ]]; then 128 + errors[$spec]="plugin \"$spec\" not found in build info" 129 + elif [[ "''${modules[$mod]}" != "$ver" ]]; then 130 + errors[$spec]="plugin \"$spec\" have incorrect tag:\n specified: \"$spec\"\n got: \"$mod@''${modules[$mod]}\"" 120 131 fi 132 + fi 133 + done 134 + 135 + # print errors if any 136 + if [[ ''${#errors[@]} -gt 0 ]]; then 137 + for spec in "''${!errors[@]}"; do 138 + printf "Error: ''${errors[$spec]}\n" >&2 121 139 done 140 + 141 + echo "Tips:" 142 + echo "If:" 143 + echo " - you are using module replacement (e.g. \`plugin1=plugin2@version\`)" 144 + echo " - the provided Caddy plugin is under a repository's subdirectory, and \`go.{mod,sum}\` files are not in that subdirectory" 145 + echo " - you have custom build logic or other advanced use cases" 146 + echo "Please consider:" 147 + echo " - set \`doInstallCheck = false\`" 148 + echo " - write your own \`installCheckPhase\` and override the default script" 149 + echo "If you are sure this error is caused by packaging, or by caddy/xcaddy, raise an issue with upstream or nixpkgs" 122 150 123 151 exit 1 124 152 fi