diff --git a/pkgs/by-name/co/coredns/package.nix b/pkgs/by-name/co/coredns/package.nix index 19c6e7592d74..8fa46e05caa9 100644 --- a/pkgs/by-name/co/coredns/package.nix +++ b/pkgs/by-name/co/coredns/package.nix @@ -32,60 +32,61 @@ buildGoModule (finalAttrs: { "man" ]; - # Override the go-modules fetcher derivation to fetch plugins - modBuildPhase = '' - cp plugin.cfg plugin.cfg.orig - ${ - (lib.concatMapStringsSep "\n" ( - plugin: - let - position = plugin.position or "end-of-file"; - formatPlugin = { name, repo, ... }: "${name}:${repo}"; - in - if position == "end-of-file" then - "echo '${formatPlugin plugin}' >> plugin.cfg" - else if position == "start-of-file" then - "sed -i '1i ${formatPlugin plugin}' plugin.cfg" - else if lib.hasAttrByPath [ "before" ] position then - '' - if ! grep -q '^${position.before}:' plugin.cfg; then - echo 'Failed to insert ${plugin.name} before ${position.before} in plugin.cfg: ${position.before} is not in plugin.cfg' - exit 1 - fi - sed -i '/^${position.before}:/i ${formatPlugin plugin}' plugin.cfg - '' - else if lib.hasAttrByPath [ "after" ] position then - '' - if ! grep -q '^${position.after}:' plugin.cfg; then - echo 'Failed to insert ${plugin.name} after ${position.after} in plugin.cfg: ${position.after} is not in plugin.cfg' - exit 1 - fi - sed -i '/^${position.after}:/a ${formatPlugin plugin}' plugin.cfg - '' - else - throw '' - Unsupported position value in externalPlugin: - ${builtins.toJSON plugin}. - Valid values for position attr are: - - position = "end-of-file" (the default) - - position = "start-of-file" - - position.before = "{other plugin}" - - position.after = "{other plugin}" - '' - ) externalPlugins) - } - diff -u plugin.cfg.orig plugin.cfg || true - for src in ${toString (attrsToSources externalPlugins)}; do go get $src; done - go mod vendor - CC= GOOS= GOARCH= go generate - go mod vendor - go mod tidy - ''; + overrideModAttrs = { + # Add plugins before vendoring the modules. + preBuild = '' + cp plugin.cfg plugin.cfg.orig + ${ + (lib.concatMapStringsSep "\n" ( + plugin: + let + position = plugin.position or "end-of-file"; + formatPlugin = { name, repo, ... }: "${name}:${repo}"; + in + if position == "end-of-file" then + "echo '${formatPlugin plugin}' >> plugin.cfg" + else if position == "start-of-file" then + "sed -i '1i ${formatPlugin plugin}' plugin.cfg" + else if lib.hasAttrByPath [ "before" ] position then + '' + if ! grep -q '^${position.before}:' plugin.cfg; then + echo 'Failed to insert ${plugin.name} before ${position.before} in plugin.cfg: ${position.before} is not in plugin.cfg' + exit 1 + fi + sed -i '/^${position.before}:/i ${formatPlugin plugin}' plugin.cfg + '' + else if lib.hasAttrByPath [ "after" ] position then + '' + if ! grep -q '^${position.after}:' plugin.cfg; then + echo 'Failed to insert ${plugin.name} after ${position.after} in plugin.cfg: ${position.after} is not in plugin.cfg' + exit 1 + fi + sed -i '/^${position.after}:/a ${formatPlugin plugin}' plugin.cfg + '' + else + throw '' + Unsupported position value in externalPlugin: + ${builtins.toJSON plugin}. + Valid values for position attr are: + - position = "end-of-file" (the default) + - position = "start-of-file" + - position.before = "{other plugin}" + - position.after = "{other plugin}" + '' + ) externalPlugins) + } + diff -u plugin.cfg.orig plugin.cfg || true + for src in ${toString (attrsToSources externalPlugins)}; do go get $src; done + GOFLAGS=''${GOFLAGS//-mod=vendor/} CC= GOOS= GOARCH= go generate + go mod tidy + ''; - modInstallPhase = '' - mv -t vendor go.mod go.sum plugin.cfg - cp -r --reflink=auto vendor "$out" - ''; + # Move the modified `go.mod`, `go.sum`, and `plugin.cfg` files into the + # vendor directory so we can retrieve them later in the `preBuild` hook. + postBuild = '' + mv -t vendor go.mod go.sum plugin.cfg + ''; + }; preBuild = '' chmod -R u+w vendor