nixos/tomcat: remove `with lib;` and use mkEnableOption

Changed files
+70 -95
nixos
modules
services
web-servers
+70 -95
nixos/modules/services/web-servers/tomcat.nix
···
{ config, lib, pkgs, ... }:
-
with lib;
-
let
cfg = config.services.tomcat;
···
in
{
-
meta = {
-
maintainers = with maintainers; [ danbst ];
};
###### interface
options = {
-
services.tomcat = {
-
enable = mkEnableOption (lib.mdDoc "Apache Tomcat");
-
package = mkOption {
-
type = types.package;
-
default = pkgs.tomcat9;
-
defaultText = literalExpression "pkgs.tomcat9";
-
example = lib.literalExpression "pkgs.tomcat9";
-
description = lib.mdDoc ''
-
Which tomcat package to use.
-
'';
-
};
-
purifyOnStart = mkOption {
-
type = types.bool;
default = false;
description = lib.mdDoc ''
On startup, the `baseDir` directory is populated with various files,
···
'';
};
-
baseDir = mkOption {
type = lib.types.path;
default = "/var/tomcat";
description = lib.mdDoc ''
···
'';
};
-
logDirs = mkOption {
-
default = [];
-
type = types.listOf types.path;
description = lib.mdDoc "Directories to create in baseDir/logs/";
};
-
extraConfigFiles = mkOption {
-
default = [];
-
type = types.listOf types.path;
description = lib.mdDoc "Extra configuration files to pull into the tomcat conf directory";
};
-
extraEnvironment = mkOption {
-
type = types.listOf types.str;
-
default = [];
example = [ "ENVIRONMENT=production" ];
description = lib.mdDoc "Environment Variables to pass to the tomcat service";
};
-
extraGroups = mkOption {
-
default = [];
-
type = types.listOf types.str;
example = [ "users" ];
description = lib.mdDoc "Defines extra groups to which the tomcat user belongs.";
};
-
user = mkOption {
-
type = types.str;
default = "tomcat";
description = lib.mdDoc "User account under which Apache Tomcat runs.";
};
-
group = mkOption {
-
type = types.str;
default = "tomcat";
description = lib.mdDoc "Group account under which Apache Tomcat runs.";
};
-
javaOpts = mkOption {
-
type = types.either (types.listOf types.str) types.str;
default = "";
description = lib.mdDoc "Parameters to pass to the Java Virtual Machine which spawns Apache Tomcat";
};
-
catalinaOpts = mkOption {
-
type = types.either (types.listOf types.str) types.str;
default = "";
description = lib.mdDoc "Parameters to pass to the Java Virtual Machine which spawns the Catalina servlet container";
};
-
sharedLibs = mkOption {
-
type = types.listOf types.str;
-
default = [];
description = lib.mdDoc "List containing JAR files or directories with JAR files which are libraries shared by the web applications";
};
-
serverXml = mkOption {
-
type = types.lines;
default = "";
description = lib.mdDoc ''
Verbatim server.xml configuration.
···
'';
};
-
commonLibs = mkOption {
-
type = types.listOf types.str;
-
default = [];
description = lib.mdDoc "List containing JAR files or directories with JAR files which are libraries shared by the web applications and the servlet container";
};
-
webapps = mkOption {
-
type = types.listOf types.path;
default = [ tomcat.webapps ];
-
defaultText = literalExpression "[ config.services.tomcat.package.webapps ]";
description = lib.mdDoc "List containing WAR files or directories with WAR files which are web applications to be deployed on Tomcat";
};
-
virtualHosts = mkOption {
-
type = types.listOf (types.submodule {
options = {
-
name = mkOption {
-
type = types.str;
description = lib.mdDoc "name of the virtualhost";
};
-
aliases = mkOption {
-
type = types.listOf types.str;
description = lib.mdDoc "aliases of the virtualhost";
-
default = [];
};
-
webapps = mkOption {
-
type = types.listOf types.path;
description = lib.mdDoc ''
List containing web application WAR files and/or directories containing
web applications and configuration files for the virtual host.
'';
-
default = [];
};
};
});
-
default = [];
description = lib.mdDoc "List consisting of a virtual host name and a list of web applications to deploy on each virtual host";
};
-
logPerVirtualHost = mkOption {
-
type = types.bool;
default = false;
description = lib.mdDoc "Whether to enable logging per virtual host.";
};
-
jdk = mkOption {
-
type = types.package;
-
default = pkgs.jdk;
-
defaultText = literalExpression "pkgs.jdk";
-
description = lib.mdDoc "Which JDK to use.";
-
};
axis2 = {
-
-
enable = mkOption {
-
default = false;
-
type = types.bool;
-
description = lib.mdDoc "Whether to enable an Apache Axis2 container";
-
};
-
services = mkOption {
-
default = [];
-
type = types.listOf types.str;
description = lib.mdDoc "List containing AAR files or directories with AAR files which are web services to be deployed on Axis2";
};
-
};
-
};
-
};
-
###### implementation
-
config = mkIf config.services.tomcat.enable {
users.groups.tomcat.gid = config.ids.gids.tomcat;
users.users.tomcat =
-
{ uid = config.ids.uids.tomcat;
description = "Tomcat user";
home = "/homeless-shelter";
group = "tomcat";
···
ln -sfn ${tomcat}/conf/$i ${cfg.baseDir}/conf/`basename $i`
done
-
${optionalString (cfg.extraConfigFiles != []) ''
for i in ${toString cfg.extraConfigFiles}; do
ln -sfn $i ${cfg.baseDir}/conf/`basename $i`
done
···
hostElementForVirtualHost = virtualHost: ''
<Host name="${virtualHost.name}" appBase="virtualhosts/${virtualHost.name}/webapps"
unpackWARs="true" autoDeploy="true" xmlValidation="false" xmlNamespaceAware="false">
-
'' + concatStrings (innerElementsForVirtualHost virtualHost) + ''
</Host>
'';
innerElementsForVirtualHost = virtualHost:
(map (alias: ''
<Alias>${alias}</Alias>
'') virtualHost.aliases)
-
++ (optional cfg.logPerVirtualHost ''
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs/${virtualHost.name}"
prefix="${virtualHost.name}_access_log." pattern="combined" resolveHosts="false"/>
'');
-
hostElementsString = concatMapStringsSep "\n" hostElementForVirtualHost cfg.virtualHosts;
-
hostElementsSedString = replaceStrings ["\n"] ["\\\n"] hostElementsString;
in ''
# Create a modified server.xml which also includes all virtual hosts
-
sed -e "/<Engine name=\"Catalina\" defaultHost=\"localhost\">/a\\"${escapeShellArg hostElementsSedString} \
${tomcat}/conf/server.xml > ${cfg.baseDir}/conf/server.xml
''
}
-
${optionalString (cfg.logDirs != []) ''
for i in ${toString cfg.logDirs}; do
mkdir -p ${cfg.baseDir}/logs/$i
chown ${cfg.user}:${cfg.group} ${cfg.baseDir}/logs/$i
done
''}
-
${optionalString cfg.logPerVirtualHost (toString (map (h: ''
mkdir -p ${cfg.baseDir}/logs/${h.name}
chown ${cfg.user}:${cfg.group} ${cfg.baseDir}/logs/${h.name}
'') cfg.virtualHosts))}
···
# Symlink all the given web applications files or paths into the webapps/ directory
# of this virtual host
-
for i in "${optionalString (virtualHost ? webapps) (toString virtualHost.webapps)}"; do
if [ -f $i ]; then
# If the given web application is a file, symlink it into the webapps/ directory
ln -sfn $i ${cfg.baseDir}/virtualhosts/${virtualHost.name}/webapps/`basename $i`
···
done
'') cfg.virtualHosts)}
-
${optionalString cfg.axis2.enable ''
# Copy the Axis2 web application
cp -av ${pkgs.axis2}/webapps/axis2 ${cfg.baseDir}/webapps
···
serviceConfig = {
Type = "forking";
PermissionsStartOnly = true;
-
PIDFile="/run/tomcat/tomcat.pid";
RuntimeDirectory = "tomcat";
User = cfg.user;
-
Environment=[
"CATALINA_BASE=${cfg.baseDir}"
"CATALINA_PID=/run/tomcat/tomcat.pid"
"JAVA_HOME='${cfg.jdk}'"
···
{ config, lib, pkgs, ... }:
let
cfg = config.services.tomcat;
···
in
{
meta = {
+
maintainers = with lib.maintainers; [ danbst ];
};
###### interface
options = {
services.tomcat = {
+
enable = lib.mkEnableOption (lib.mdDoc "Apache Tomcat");
+
package = lib.mkPackageOptionMD pkgs "tomcat9" { };
+
purifyOnStart = lib.mkOption {
+
type = lib.types.bool;
default = false;
description = lib.mdDoc ''
On startup, the `baseDir` directory is populated with various files,
···
'';
};
+
baseDir = lib.mkOption {
type = lib.types.path;
default = "/var/tomcat";
description = lib.mdDoc ''
···
'';
};
+
logDirs = lib.mkOption {
+
default = [ ];
+
type = lib.types.listOf lib.types.path;
description = lib.mdDoc "Directories to create in baseDir/logs/";
};
+
extraConfigFiles = lib.mkOption {
+
default = [ ];
+
type = lib.types.listOf lib.types.path;
description = lib.mdDoc "Extra configuration files to pull into the tomcat conf directory";
};
+
extraEnvironment = lib.mkOption {
+
type = lib.types.listOf lib.types.str;
+
default = [ ];
example = [ "ENVIRONMENT=production" ];
description = lib.mdDoc "Environment Variables to pass to the tomcat service";
};
+
extraGroups = lib.mkOption {
+
default = [ ];
+
type = lib.types.listOf lib.types.str;
example = [ "users" ];
description = lib.mdDoc "Defines extra groups to which the tomcat user belongs.";
};
+
user = lib.mkOption {
+
type = lib.types.str;
default = "tomcat";
description = lib.mdDoc "User account under which Apache Tomcat runs.";
};
+
group = lib.mkOption {
+
type = lib.types.str;
default = "tomcat";
description = lib.mdDoc "Group account under which Apache Tomcat runs.";
};
+
javaOpts = lib.mkOption {
+
type = lib.types.either (lib.types.listOf lib.types.str) lib.types.str;
default = "";
description = lib.mdDoc "Parameters to pass to the Java Virtual Machine which spawns Apache Tomcat";
};
+
catalinaOpts = lib.mkOption {
+
type = lib.types.either (lib.types.listOf lib.types.str) lib.types.str;
default = "";
description = lib.mdDoc "Parameters to pass to the Java Virtual Machine which spawns the Catalina servlet container";
};
+
sharedLibs = lib.mkOption {
+
type = lib.types.listOf lib.types.str;
+
default = [ ];
description = lib.mdDoc "List containing JAR files or directories with JAR files which are libraries shared by the web applications";
};
+
serverXml = lib.mkOption {
+
type = lib.types.lines;
default = "";
description = lib.mdDoc ''
Verbatim server.xml configuration.
···
'';
};
+
commonLibs = lib.mkOption {
+
type = lib.types.listOf lib.types.str;
+
default = [ ];
description = lib.mdDoc "List containing JAR files or directories with JAR files which are libraries shared by the web applications and the servlet container";
};
+
webapps = lib.mkOption {
+
type = lib.types.listOf lib.types.path;
default = [ tomcat.webapps ];
+
defaultText = lib.literalExpression "[ config.services.tomcat.package.webapps ]";
description = lib.mdDoc "List containing WAR files or directories with WAR files which are web applications to be deployed on Tomcat";
};
+
virtualHosts = lib.mkOption {
+
type = lib.types.listOf (lib.types.submodule {
options = {
+
name = lib.mkOption {
+
type = lib.types.str;
description = lib.mdDoc "name of the virtualhost";
};
+
aliases = lib.mkOption {
+
type = lib.types.listOf lib.types.str;
description = lib.mdDoc "aliases of the virtualhost";
+
default = [ ];
};
+
webapps = lib.mkOption {
+
type = lib.types.listOf lib.types.path;
description = lib.mdDoc ''
List containing web application WAR files and/or directories containing
web applications and configuration files for the virtual host.
'';
+
default = [ ];
};
};
});
+
default = [ ];
description = lib.mdDoc "List consisting of a virtual host name and a list of web applications to deploy on each virtual host";
};
+
logPerVirtualHost = lib.mkOption {
+
type = lib.types.bool;
default = false;
description = lib.mdDoc "Whether to enable logging per virtual host.";
};
+
jdk = lib.mkPackageOptionMD pkgs "jdk" { };
axis2 = {
+
enable = lib.mkEnableOption "Apache Axis2 container";
+
services = lib.mkOption {
+
default = [ ];
+
type = lib.types.listOf lib.types.str;
description = lib.mdDoc "List containing AAR files or directories with AAR files which are web services to be deployed on Axis2";
};
};
};
};
###### implementation
+
config = lib.mkIf config.services.tomcat.enable {
users.groups.tomcat.gid = config.ids.gids.tomcat;
users.users.tomcat =
+
{
+
uid = config.ids.uids.tomcat;
description = "Tomcat user";
home = "/homeless-shelter";
group = "tomcat";
···
ln -sfn ${tomcat}/conf/$i ${cfg.baseDir}/conf/`basename $i`
done
+
${lib.optionalString (cfg.extraConfigFiles != []) ''
for i in ${toString cfg.extraConfigFiles}; do
ln -sfn $i ${cfg.baseDir}/conf/`basename $i`
done
···
hostElementForVirtualHost = virtualHost: ''
<Host name="${virtualHost.name}" appBase="virtualhosts/${virtualHost.name}/webapps"
unpackWARs="true" autoDeploy="true" xmlValidation="false" xmlNamespaceAware="false">
+
'' + lib.concatStrings (innerElementsForVirtualHost virtualHost) + ''
</Host>
'';
innerElementsForVirtualHost = virtualHost:
(map (alias: ''
<Alias>${alias}</Alias>
'') virtualHost.aliases)
+
++ (lib.optional cfg.logPerVirtualHost ''
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs/${virtualHost.name}"
prefix="${virtualHost.name}_access_log." pattern="combined" resolveHosts="false"/>
'');
+
hostElementsString = lib.concatMapStringsSep "\n" hostElementForVirtualHost cfg.virtualHosts;
+
hostElementsSedString = lib.replaceStrings ["\n"] ["\\\n"] hostElementsString;
in ''
# Create a modified server.xml which also includes all virtual hosts
+
sed -e "/<Engine name=\"Catalina\" defaultHost=\"localhost\">/a\\"${lib.escapeShellArg hostElementsSedString} \
${tomcat}/conf/server.xml > ${cfg.baseDir}/conf/server.xml
''
}
+
${lib.optionalString (cfg.logDirs != []) ''
for i in ${toString cfg.logDirs}; do
mkdir -p ${cfg.baseDir}/logs/$i
chown ${cfg.user}:${cfg.group} ${cfg.baseDir}/logs/$i
done
''}
+
${lib.optionalString cfg.logPerVirtualHost (toString (map (h: ''
mkdir -p ${cfg.baseDir}/logs/${h.name}
chown ${cfg.user}:${cfg.group} ${cfg.baseDir}/logs/${h.name}
'') cfg.virtualHosts))}
···
# Symlink all the given web applications files or paths into the webapps/ directory
# of this virtual host
+
for i in "${lib.optionalString (virtualHost ? webapps) (toString virtualHost.webapps)}"; do
if [ -f $i ]; then
# If the given web application is a file, symlink it into the webapps/ directory
ln -sfn $i ${cfg.baseDir}/virtualhosts/${virtualHost.name}/webapps/`basename $i`
···
done
'') cfg.virtualHosts)}
+
${lib.optionalString cfg.axis2.enable ''
# Copy the Axis2 web application
cp -av ${pkgs.axis2}/webapps/axis2 ${cfg.baseDir}/webapps
···
serviceConfig = {
Type = "forking";
PermissionsStartOnly = true;
+
PIDFile = "/run/tomcat/tomcat.pid";
RuntimeDirectory = "tomcat";
User = cfg.user;
+
Environment = [
"CATALINA_BASE=${cfg.baseDir}"
"CATALINA_PID=/run/tomcat/tomcat.pid"
"JAVA_HOME='${cfg.jdk}'"