Build Caddy with plugins using NixOS
Find a file
2025-08-26 10:51:00 +01:00
caddy-with-plugins.nix Override any version of buildGoModule for Caddy 2025-08-26 10:51:00 +01:00
flake.lock Initial commit 2024-04-05 19:45:15 +01:00
flake.nix Fix package detection with wrapPackage option 2024-04-08 04:22:37 +01:00
README.md Update Git URL in documentation 2025-01-08 17:40:59 +00:00

caddy-plugins-nix

Easily build Caddy server with any plugins (official or unofficial) easily with the Nix package manager.

Usage

This repo uses Nix Flakes, so your setup must support using them.

Using this repo requires 2 steps. Firstly, add this flake as an input:

{
  inputs = {
    caddy-plugins-nix = {
      url = "git+https://git.spad.io/Spadio/caddy-plugins-nix.git";
      inputs.nixpkgs.follows = "nixpkgs"; # Optional, but recommended.
    };
  };

  outputs = { self, nixpkgs, caddy-plugins-nix }: {
    # Change `yourHostname` to your actual hostname.
    nixosConfigurations.yourHostname = nixpkgs.lib.nixosSystem {
      system = "x86_64-linux"; # Change `system` to your actual system.
      modules = [
        ./configuration.nix
        caddy-plugins-nix.nixosModules.default
      ];
    };
  };
}

Then, simply set the services.caddy.plugins.plugins and services.caddy.plugins.vendorHash options in your NixOS configuration to enable any Caddy plugins. For example:

{ config, lib, pkgs, ... }:

{
  services.caddy = {
    enable = true;
    plugins = {
      plugins = [ "github.com/caddy-dns/cloudflare@v0.0.0-00000000000000-000000000000" ];
      vendorHash = lib.fakeHash;
    };
  };
}

The plugins.vendorHash value will change based upon the base Caddy version used (sourced from the nixpkgs input) and the plugins and plugin versions applied. The easiest way to calculate this is to set the services.caddy.plugins.plugins value as you desire, and then set services.caddy.plugins.vendorHash to lib.fakeHash in your configuration (as is done in the above example) and fetch the correct hash from the resulting build error.

Wrapper Script

If you need to wrap the resulting package (for example, to set an environment variable to the contents of a file), you can make use of the config.services.caddy.plugins.wrapPackage option:

{ config, lib, pkgs, ... }:

{
  services.caddy = {
    enable = true;
    plugins = {
      plugins = [ "github.com/caddy-dns/cloudflare@v0.0.0-00000000000000-000000000000" ];
      vendorHash = lib.fakeHash;
      wrapPackage = package: pkgs.writeShellScriptBin "caddy" ''
        set -euo pipefail
        export SOME_API_KEY="$(cat ${config.mySecretsPaths.some_api_key})"
        exec ${package}/bin/caddy "$@"
      '';
    };
  };
}