coreDNS externalPlugins built success with error in run time

Hi,

I need to build coreDNS with alternate plugin. here is the build command.

nix build -I nixpkgs=flake:github:NixOS/nixpkgs/nixpkgs-unstable \
  --impure --expr 'let pkgs = import <nixpkgs> { }; in pkgs.coredns.override {
    externalPlugins = [
      {
        name = "alternate ";
        repo = "github.com/coredns/alternate";
        version = "v0.2.12";
      }
    ];
    vendorHash = "sha256-FlPCD11tP0N1jtnNNkU/G1QDpc3tttW5DMpd+dYK028=";
  }'

The build worked fine as I can see the plugin is listed when I run ./coredns --plugins command.

➜ result/bin/coredns --plugins
Server types:
  dns

Caddyfile loaders:
  flag
  default

Other plugins:
  dns.acl
  dns.alternate

However, when I tried to use it with below Corefile

.:{{ .CoreDNSPort }} {
    forward . 127.0.0.1:{{ .EnvoyDNSPort }}
    alternate NOTIMP,FORMERR,NXDOMAIN,SERVFAIL,REFUSED . /etc/resolv.conf
    prometheus localhost:{{ .PrometheusPort }}
    errors{{ if .CoreDNSLogging}}
    log{{end}}
}

I am getting

Corefile:6 - Error during parsing: Unknown directive 'alternate'

Does anyone know what could be the problem?

I think I got it !

The order plugins are included in coredns clearly matters, especially the alternate plugin which requires to be included before the forward one, while the derivation does not take care of that.

I modified the existing preBuild to move alternate before forward once it had been added at the end of plugin.cfg. And now it works fine.

So this is a bug, the derivation should be modified to handle plugin ordering.

Note: For some reason that does not seem clear to me at a first glance, the plugin string is added split on two lines instead of on a single line, so I adapted the sed command but it should be fixed too I guess.

Override code snippet:

coredns = pkgs-kuma.coredns.override {
  externalPlugins = [
    {
      name = "alternate ";
      repo = "github.com/coredns/alternate";
      version = "v0.2.12";
    }
  ];
  vendorHash = "sha256-FlPCD11tP0N1jtnNNkU/G1QDpc3tttW5DMpd+dYK028=";
  buildGoModule = args: pkgs-kuma.buildGoModule (args // {
    doCheck = false;
    preBuild = ''
      chmod -R u+w vendor
      mv -t . vendor/go.{mod,sum} vendor/plugin.cfg

      cat plugin.cfg
      ${pkgs.gnused}/bin/sed \
        -i plugin.cfg \
        -e '/alternate/d' \
        -e '/:github.com\/coredns\/alternate/d' \
        -e '/^forward:.*/i alternate:github.com/coredns/alternate'
      cat plugin.cfg

      GOOS= GOARCH= go generate
    '';
  });
};

The first cat output:

[...]

coredns> loop:loop
coredns> forward:forward
coredns> grpc:grpc
coredns> erratic:erratic
coredns> whoami:whoami
coredns> on:github.com/coredns/caddy/onevent
coredns> sign:sign
coredns> view:view
coredns> alternate
coredns> :github.com/coredns/alternate

Second output:

[...]

coredns> loop:loop
coredns> alternate:github.com/coredns/alternate
coredns> forward:forward
coredns> grpc:grpc
coredns> erratic:erratic
coredns> whoami:whoami
coredns> on:github.com/coredns/caddy/onevent
coredns> sign:sign
coredns> view:view
1 Like

FYI, I submitted a PR that resolves this issue: coredns: allow specifying position for externalPlugins by dotboris · Pull Request #360798 · NixOS/nixpkgs · GitHub

1 Like