nixos/varnish: reduce overusage of lib (#208242)

This commit is contained in:
Christian Theune 2025-01-16 18:47:14 +01:00
parent 75ad720f77
commit ba79b6d68b

View File

@ -5,65 +5,62 @@
...
}:
with lib;
let
cfg = config.services.varnish;
commandLine =
"-f ${pkgs.writeText "default.vcl" cfg.config}"
+
optionalString (cfg.extraModules != [ ])
lib.optionalString (cfg.extraModules != [ ])
" -p vmod_path='${
makeSearchPathOutput "lib" "lib/varnish/vmods" ([ cfg.package ] ++ cfg.extraModules)
lib.makeSearchPathOutput "lib" "lib/varnish/vmods" ([ cfg.package ] ++ cfg.extraModules)
}' -r vmod_path";
in
{
options = {
services.varnish = {
enable = mkEnableOption "Varnish Server";
enable = lib.mkEnableOption "Varnish Server";
enableConfigCheck = mkEnableOption "checking the config during build time" // {
enableConfigCheck = lib.mkEnableOption "checking the config during build time" // {
default = true;
};
package = mkPackageOption pkgs "varnish" { };
package = lib.mkPackageOption pkgs "varnish" { };
http_address = mkOption {
type = types.str;
http_address = lib.mkOption {
type = lib.types.str;
default = "*:6081";
description = ''
HTTP listen address and port.
'';
};
config = mkOption {
type = types.lines;
config = lib.mkOption {
type = lib.types.lines;
description = ''
Verbatim default.vcl configuration.
'';
};
stateDir = mkOption {
type = types.path;
stateDir = lib.mkOption {
type = lib.types.path;
default = "/run/varnish/${config.networking.hostName}";
defaultText = literalExpression ''"/run/varnish/''${config.networking.hostName}"'';
defaultText = lib.literalExpression ''"/run/varnish/''${config.networking.hostName}"'';
description = ''
Directory holding all state for Varnish to run. Note that this should be a tmpfs in order to avoid performance issues and crashes.
'';
};
extraModules = mkOption {
type = types.listOf types.package;
extraModules = lib.mkOption {
type = lib.types.listOf lib.types.package;
default = [ ];
example = literalExpression "[ pkgs.varnishPackages.geoip ]";
example = lib.literalExpression "[ pkgs.varnishPackages.geoip ]";
description = ''
Varnish modules (except 'std').
'';
};
extraCommandLine = mkOption {
type = types.str;
extraCommandLine = lib.mkOption {
type = lib.types.str;
default = "";
example = "-s malloc,256M";
description = ''
@ -74,17 +71,16 @@ in
};
config = mkIf cfg.enable {
config = lib.mkIf cfg.enable {
systemd.services.varnish = {
description = "Varnish";
wantedBy = [ "multi-user.target" ];
after = [ "network.target" ];
preStart = mkIf (!(lib.hasPrefix "/run/" cfg.stateDir)) ''
preStart = lib.mkIf (!(lib.hasPrefix "/run/" cfg.stateDir)) ''
mkdir -p ${cfg.stateDir}
chown -R varnish:varnish ${cfg.stateDir}
'';
postStop = mkIf (!(lib.hasPrefix "/run/" cfg.stateDir)) ''
postStop = lib.mkIf (!(lib.hasPrefix "/run/" cfg.stateDir)) ''
rm -rf ${cfg.stateDir}
'';
serviceConfig = {
@ -95,7 +91,7 @@ in
RestartSec = "5s";
User = "varnish";
Group = "varnish";
RuntimeDirectory = mkIf (lib.hasPrefix "/run/" cfg.stateDir) (
RuntimeDirectory = lib.mkIf (lib.hasPrefix "/run/" cfg.stateDir) (
lib.removePrefix "/run/" cfg.stateDir
);
AmbientCapabilities = "cap_net_bind_service";
@ -107,7 +103,7 @@ in
environment.systemPackages = [ cfg.package ];
# check .vcl syntax at compile time (e.g. before nixops deployment)
system.checks = mkIf cfg.enableConfigCheck [
system.checks = lib.mkIf cfg.enableConfigCheck [
(pkgs.runCommand "check-varnish-syntax" { } ''
${cfg.package}/bin/varnishd -C ${commandLine} 2> $out || (cat $out; exit 1)
'')