Skip to content

Commit

Permalink
nixos/varnish: reduce overusage of lib (#208242)
Browse files Browse the repository at this point in the history
  • Loading branch information
ctheune committed Jan 16, 2025
1 parent 75ad720 commit ba79b6d
Showing 1 changed file with 22 additions and 26 deletions.
48 changes: 22 additions & 26 deletions nixos/modules/services/web-servers/varnish/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ''
Expand All @@ -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 = {
Expand All @@ -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";
Expand All @@ -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)
'')
Expand Down

0 comments on commit ba79b6d

Please sign in to comment.