-
-
Notifications
You must be signed in to change notification settings - Fork 14.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Caddy 2: custom plugins support #89268
Comments
Below are 2 Nix files that I have modified and wrote for personal use. It's not styled properly, and it's missing a fallback Edited to reflect recent Service implementation{ config, lib, pkgs, ... }:
with lib;
let cfg = config.services.caddy;
configFile = pkgs.writeText "caddyconfig" cfg.config;
in {
options.services.caddy = {
enable = mkEnableOption "Caddy web server";
config = mkOption {
default = "";
example = ''
example.com {
gzip
minify
log syslog
root /srv/http
}
'';
type = types.lines;
description = "Configuration file to use with adapter";
};
adapter = mkOption {
default = "caddyfile";
type = types.str;
description = "Type of config given";
};
dataDir = mkOption {
default = "/var/lib/caddy";
type = types.path;
description = ''
The data directory, for storing certificates. Before 17.09, this
would create a .caddy directory. With 17.09 the contents of the
.caddy directory are in the specified data directory instead.
'';
};
plugins = mkOption {
default = [];
type = types.listOf types.str;
example = [
"github.com/tarent/loginsrv/caddy"
];
description = "List of plugins to use";
};
vendorSha256 = mkOption {
default = lib.fakeSha256;
type = types.str;
description = "Only fill this if custom plugins are added";
};
package = mkOption {
default = (pkgs.callPackage ./default.nix {
plugins = cfg.plugins;
vendorSha256 = cfg.vendorSha256;
});
type = types.package;
description = "Caddy package to use.";
};
};
config = mkIf cfg.enable {
environment.systemPackages = [ cfg.package ];
systemd.services.caddy = {
description = "Caddy web server";
after = [ "network-online.target" ];
wantedBy = [ "multi-user.target" ];
serviceConfig = {
ExecStart = ''
${cfg.package}/bin/caddy run \
--config ${configFile} \
--adapter ${cfg.adapter} \
'';
ExecReload = "${cfg.package} reload";
ExecStop = "${cfg.package} stop";
Type = "simple";
User = "caddy";
Group = "caddy";
Restart = "on-failure";
StartLimitInterval = 86400;
StartLimitBurst = 1;
AmbientCapabilities = "cap_net_bind_service";
CapabilityBoundingSet = "cap_net_bind_service";
NoNewPrivileges = true;
LimitNPROC = 8192;
LimitNOFILE = 1048576;
PrivateTmp = true;
PrivateDevices = true;
ProtectHome = true;
ProtectSystem = "full";
ReadWriteDirectories = cfg.dataDir;
};
};
users.users.caddy = {
group = "caddy";
uid = config.ids.uids.caddy;
home = cfg.dataDir;
createHome = true;
};
users.groups.caddy.gid = config.ids.uids.caddy;
};
} Package file implementation{ stdenv, lib, buildGoModule, plugins ? [], vendorSha256 ? "" }:
with lib;
let imports = flip concatMapStrings plugins (pkg: "\t\t\t_ \"${pkg}\"\n");
main = ''
package main
import (
caddycmd "github.com/caddyserver/caddy/v2/cmd"
_ "github.com/caddyserver/caddy/v2/modules/standard"
${imports}
)
func main() {
caddycmd.Main()
}
'';
in buildGoModule rec {
pname = "caddy";
version = "2.0.0";
goPackagePath = "github.com/caddyserver/caddy/v2";
subPackages = [ "cmd/caddy" ];
src = builtins.fetchGit {
url = "https://github.com/caddyserver/caddy.git";
rev = "e051e119d1dff75972ed9b07cf97bbb989ba8daa";
};
inherit vendorSha256;
overrideModAttrs = (_: {
preBuild = "echo '${main}' > cmd/caddy/main.go";
postInstall = "cp go.sum go.mod $out/ && ls $out/";
});
postPatch = ''
echo '${main}' > cmd/caddy/main.go
cat cmd/caddy/main.go
'';
postConfigure = ''
cp vendor/go.sum ./
cp vendor/go.mod ./
'';
meta = with stdenv.lib; {
homepage = https://caddyserver.com;
description = "Fast, cross-platform HTTP/2 web server with automatic HTTPS";
license = licenses.asl20;
maintainers = with maintainers; [ rushmorem fpletz zimbatm ];
};
} |
duplicate of #14671 @diamondburned can you create a pull request? |
My PR would be exposing |
this can already be achieved with existing
services.caddy = {
package = (pkgs.callPackage ./custom-caddy.nix {
plugins = [ "github.com/tarent/loginsrv/caddy" ];
vendorSha256 = "xxx";
});
};
|
@diamondburned I'm wondering if you have any issue on 22.11? I got this error after upgrading from 22.05, same with fresh 22.11.
|
Posting here since this page still shows up prominently on search results. I struggled with this recently, and I found two workarounds that do not require setting At first I got it working by following @curbengh's guide along with the heretic's way: https://zimbatm.com/notes/nix-packaging-the-heretic-way. Essentially, that meant setting Then I found a solution which does not require messing with sandbox settings at all: #14671 (comment). |
Hmm, @thenbe the blog post is not available (cloudflare 502), messing with the sandbox settings should be avoided and the linked PR is just a draft. The cleanest way in my opinion (currently) is to package a patched Caddy version with Go. This project or one of its forks show how to do that with flakes: https://github.com/pinpox/nixos-caddy-patched |
There should be exposed
vendorSha256
andplugins
options to allow generating and compiling Caddy v2 with additional plugins.Example:
The text was updated successfully, but these errors were encountered: