2020-04-24 23:36:52 +00:00
|
|
|
{ config, lib, pkgs, ... }:
|
|
|
|
|
|
|
|
let
|
|
|
|
|
|
|
|
inherit (lib) mkDefault mkEnableOption mkForce mkIf mkMerge mkOption;
|
2021-10-06 13:57:05 +00:00
|
|
|
inherit (lib) literalExpression mapAttrs optional optionalString types;
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
cfg = config.services.limesurvey;
|
|
|
|
fpm = config.services.phpfpm.pools.limesurvey;
|
|
|
|
|
|
|
|
user = "limesurvey";
|
|
|
|
group = config.services.httpd.group;
|
|
|
|
stateDir = "/var/lib/limesurvey";
|
|
|
|
|
|
|
|
pkg = pkgs.limesurvey;
|
|
|
|
|
|
|
|
configType = with types; oneOf [ (attrsOf configType) str int bool ] // {
|
|
|
|
description = "limesurvey config type (str, int, bool or attribute set thereof)";
|
|
|
|
};
|
|
|
|
|
|
|
|
limesurveyConfig = pkgs.writeText "config.php" ''
|
|
|
|
<?php
|
|
|
|
return json_decode('${builtins.toJSON cfg.config}', true);
|
|
|
|
?>
|
|
|
|
'';
|
|
|
|
|
|
|
|
mysqlLocal = cfg.database.createLocally && cfg.database.type == "mysql";
|
|
|
|
pgsqlLocal = cfg.database.createLocally && cfg.database.type == "pgsql";
|
|
|
|
|
|
|
|
in
|
|
|
|
{
|
|
|
|
# interface
|
|
|
|
|
|
|
|
options.services.limesurvey = {
|
2024-04-21 15:54:59 +00:00
|
|
|
enable = mkEnableOption "Limesurvey web application";
|
2020-04-24 23:36:52 +00:00
|
|
|
|
2023-03-24 00:07:29 +00:00
|
|
|
encryptionKey = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
default = "E17687FC77CEE247F0E22BB3ECF27FDE8BEC310A892347EC13013ABA11AA7EB5";
|
2024-04-21 15:54:59 +00:00
|
|
|
description = ''
|
2023-03-24 00:07:29 +00:00
|
|
|
This is a 32-byte key used to encrypt variables in the database.
|
|
|
|
You _must_ change this from the default value.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
encryptionNonce = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
default = "1ACC8555619929DB91310BE848025A427B0F364A884FFA77";
|
2024-04-21 15:54:59 +00:00
|
|
|
description = ''
|
2023-03-24 00:07:29 +00:00
|
|
|
This is a 24-byte nonce used to encrypt variables in the database.
|
|
|
|
You _must_ change this from the default value.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2020-04-24 23:36:52 +00:00
|
|
|
database = {
|
|
|
|
type = mkOption {
|
|
|
|
type = types.enum [ "mysql" "pgsql" "odbc" "mssql" ];
|
|
|
|
example = "pgsql";
|
|
|
|
default = "mysql";
|
2024-04-21 15:54:59 +00:00
|
|
|
description = "Database engine to use.";
|
2020-04-24 23:36:52 +00:00
|
|
|
};
|
|
|
|
|
2023-03-24 00:07:29 +00:00
|
|
|
dbEngine = mkOption {
|
|
|
|
type = types.enum [ "MyISAM" "InnoDB" ];
|
|
|
|
default = "InnoDB";
|
2024-04-21 15:54:59 +00:00
|
|
|
description = "Database storage engine to use.";
|
2023-03-24 00:07:29 +00:00
|
|
|
};
|
|
|
|
|
2020-04-24 23:36:52 +00:00
|
|
|
host = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
default = "localhost";
|
2024-04-21 15:54:59 +00:00
|
|
|
description = "Database host address.";
|
2020-04-24 23:36:52 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
port = mkOption {
|
2022-12-17 10:02:37 +00:00
|
|
|
type = types.port;
|
2020-04-24 23:36:52 +00:00
|
|
|
default = if cfg.database.type == "pgsql" then 5442 else 3306;
|
2021-10-06 13:57:05 +00:00
|
|
|
defaultText = literalExpression "3306";
|
2024-04-21 15:54:59 +00:00
|
|
|
description = "Database host port.";
|
2020-04-24 23:36:52 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
name = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
default = "limesurvey";
|
2024-04-21 15:54:59 +00:00
|
|
|
description = "Database name.";
|
2020-04-24 23:36:52 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
user = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
default = "limesurvey";
|
2024-04-21 15:54:59 +00:00
|
|
|
description = "Database user.";
|
2020-04-24 23:36:52 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
passwordFile = mkOption {
|
|
|
|
type = types.nullOr types.path;
|
|
|
|
default = null;
|
|
|
|
example = "/run/keys/limesurvey-dbpassword";
|
2024-04-21 15:54:59 +00:00
|
|
|
description = ''
|
2020-04-24 23:36:52 +00:00
|
|
|
A file containing the password corresponding to
|
2022-08-21 13:32:41 +00:00
|
|
|
{option}`database.user`.
|
2020-04-24 23:36:52 +00:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
socket = mkOption {
|
|
|
|
type = types.nullOr types.path;
|
|
|
|
default =
|
|
|
|
if mysqlLocal then "/run/mysqld/mysqld.sock"
|
|
|
|
else if pgsqlLocal then "/run/postgresql"
|
|
|
|
else null
|
|
|
|
;
|
2021-10-06 13:57:05 +00:00
|
|
|
defaultText = literalExpression "/run/mysqld/mysqld.sock";
|
2024-04-21 15:54:59 +00:00
|
|
|
description = "Path to the unix socket file to use for authentication.";
|
2020-04-24 23:36:52 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
createLocally = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = cfg.database.type == "mysql";
|
2021-10-06 13:57:05 +00:00
|
|
|
defaultText = literalExpression "true";
|
2024-04-21 15:54:59 +00:00
|
|
|
description = ''
|
2020-04-24 23:36:52 +00:00
|
|
|
Create the database and database user locally.
|
|
|
|
This currently only applies if database type "mysql" is selected.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
virtualHost = mkOption {
|
|
|
|
type = types.submodule (import ../web-servers/apache-httpd/vhost-options.nix);
|
2021-10-06 13:57:05 +00:00
|
|
|
example = literalExpression ''
|
2020-04-24 23:36:52 +00:00
|
|
|
{
|
|
|
|
hostName = "survey.example.org";
|
|
|
|
adminAddr = "webmaster@example.org";
|
|
|
|
forceSSL = true;
|
|
|
|
enableACME = true;
|
|
|
|
}
|
|
|
|
'';
|
2024-04-21 15:54:59 +00:00
|
|
|
description = ''
|
2022-08-21 13:32:41 +00:00
|
|
|
Apache configuration can be done by adapting `services.httpd.virtualHosts.<name>`.
|
|
|
|
See [](#opt-services.httpd.virtualHosts) for further information.
|
2020-04-24 23:36:52 +00:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
poolConfig = mkOption {
|
|
|
|
type = with types; attrsOf (oneOf [ str int bool ]);
|
|
|
|
default = {
|
|
|
|
"pm" = "dynamic";
|
|
|
|
"pm.max_children" = 32;
|
|
|
|
"pm.start_servers" = 2;
|
|
|
|
"pm.min_spare_servers" = 2;
|
|
|
|
"pm.max_spare_servers" = 4;
|
|
|
|
"pm.max_requests" = 500;
|
|
|
|
};
|
2024-04-21 15:54:59 +00:00
|
|
|
description = ''
|
2022-08-21 13:32:41 +00:00
|
|
|
Options for the LimeSurvey PHP pool. See the documentation on `php-fpm.conf`
|
2020-04-24 23:36:52 +00:00
|
|
|
for details on configuration directives.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
config = mkOption {
|
|
|
|
type = configType;
|
|
|
|
default = {};
|
2024-04-21 15:54:59 +00:00
|
|
|
description = ''
|
2020-04-24 23:36:52 +00:00
|
|
|
LimeSurvey configuration. Refer to
|
2022-08-21 13:32:41 +00:00
|
|
|
<https://manual.limesurvey.org/Optional_settings>
|
2020-04-24 23:36:52 +00:00
|
|
|
for details on supported values.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
# implementation
|
|
|
|
|
|
|
|
config = mkIf cfg.enable {
|
|
|
|
|
|
|
|
assertions = [
|
|
|
|
{ assertion = cfg.database.createLocally -> cfg.database.type == "mysql";
|
|
|
|
message = "services.limesurvey.createLocally is currently only supported for database type 'mysql'";
|
|
|
|
}
|
|
|
|
{ assertion = cfg.database.createLocally -> cfg.database.user == user;
|
|
|
|
message = "services.limesurvey.database.user must be set to ${user} if services.limesurvey.database.createLocally is set true";
|
|
|
|
}
|
|
|
|
{ assertion = cfg.database.createLocally -> cfg.database.socket != null;
|
|
|
|
message = "services.limesurvey.database.socket must be set if services.limesurvey.database.createLocally is set to true";
|
|
|
|
}
|
|
|
|
{ assertion = cfg.database.createLocally -> cfg.database.passwordFile == null;
|
|
|
|
message = "a password cannot be specified if services.limesurvey.database.createLocally is set to true";
|
|
|
|
}
|
|
|
|
];
|
|
|
|
|
|
|
|
services.limesurvey.config = mapAttrs (name: mkDefault) {
|
|
|
|
runtimePath = "${stateDir}/tmp/runtime";
|
|
|
|
components = {
|
|
|
|
db = {
|
|
|
|
connectionString = "${cfg.database.type}:dbname=${cfg.database.name};host=${if pgsqlLocal then cfg.database.socket else cfg.database.host};port=${toString cfg.database.port}" +
|
|
|
|
optionalString mysqlLocal ";socket=${cfg.database.socket}";
|
|
|
|
username = cfg.database.user;
|
|
|
|
password = mkIf (cfg.database.passwordFile != null) "file_get_contents(\"${toString cfg.database.passwordFile}\");";
|
|
|
|
tablePrefix = "limesurvey_";
|
|
|
|
};
|
|
|
|
assetManager.basePath = "${stateDir}/tmp/assets";
|
|
|
|
urlManager = {
|
|
|
|
urlFormat = "path";
|
|
|
|
showScriptName = false;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
config = {
|
|
|
|
tempdir = "${stateDir}/tmp";
|
|
|
|
uploaddir = "${stateDir}/upload";
|
2023-03-24 00:07:29 +00:00
|
|
|
encryptionnonce = cfg.encryptionNonce;
|
|
|
|
encryptionsecretboxkey = cfg.encryptionKey;
|
2020-04-24 23:36:52 +00:00
|
|
|
force_ssl = mkIf (cfg.virtualHost.addSSL || cfg.virtualHost.forceSSL || cfg.virtualHost.onlySSL) "on";
|
|
|
|
config.defaultlang = "en";
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
services.mysql = mkIf mysqlLocal {
|
|
|
|
enable = true;
|
|
|
|
package = mkDefault pkgs.mariadb;
|
|
|
|
ensureDatabases = [ cfg.database.name ];
|
|
|
|
ensureUsers = [
|
|
|
|
{ name = cfg.database.user;
|
|
|
|
ensurePermissions = {
|
|
|
|
"${cfg.database.name}.*" = "SELECT, CREATE, INSERT, UPDATE, DELETE, ALTER, DROP, INDEX";
|
|
|
|
};
|
|
|
|
}
|
|
|
|
];
|
|
|
|
};
|
|
|
|
|
|
|
|
services.phpfpm.pools.limesurvey = {
|
|
|
|
inherit user group;
|
2023-05-24 13:37:59 +00:00
|
|
|
phpPackage = pkgs.php81;
|
2023-03-24 00:07:29 +00:00
|
|
|
phpEnv.DBENGINE = "${cfg.database.dbEngine}";
|
2020-04-24 23:36:52 +00:00
|
|
|
phpEnv.LIMESURVEY_CONFIG = "${limesurveyConfig}";
|
|
|
|
settings = {
|
|
|
|
"listen.owner" = config.services.httpd.user;
|
|
|
|
"listen.group" = config.services.httpd.group;
|
|
|
|
} // cfg.poolConfig;
|
|
|
|
};
|
|
|
|
|
|
|
|
services.httpd = {
|
|
|
|
enable = true;
|
|
|
|
adminAddr = mkDefault cfg.virtualHost.adminAddr;
|
|
|
|
extraModules = [ "proxy_fcgi" ];
|
|
|
|
virtualHosts.${cfg.virtualHost.hostName} = mkMerge [ cfg.virtualHost {
|
|
|
|
documentRoot = mkForce "${pkg}/share/limesurvey";
|
|
|
|
extraConfig = ''
|
|
|
|
Alias "/tmp" "${stateDir}/tmp"
|
|
|
|
<Directory "${stateDir}">
|
|
|
|
AllowOverride all
|
|
|
|
Require all granted
|
|
|
|
Options -Indexes +FollowSymlinks
|
|
|
|
</Directory>
|
|
|
|
|
|
|
|
Alias "/upload" "${stateDir}/upload"
|
|
|
|
<Directory "${stateDir}/upload">
|
|
|
|
AllowOverride all
|
|
|
|
Require all granted
|
|
|
|
Options -Indexes
|
|
|
|
</Directory>
|
|
|
|
|
|
|
|
<Directory "${pkg}/share/limesurvey">
|
|
|
|
<FilesMatch "\.php$">
|
|
|
|
<If "-f %{REQUEST_FILENAME}">
|
|
|
|
SetHandler "proxy:unix:${fpm.socket}|fcgi://localhost/"
|
|
|
|
</If>
|
|
|
|
</FilesMatch>
|
|
|
|
|
|
|
|
AllowOverride all
|
|
|
|
Options -Indexes
|
|
|
|
DirectoryIndex index.php
|
|
|
|
</Directory>
|
|
|
|
'';
|
|
|
|
} ];
|
|
|
|
};
|
|
|
|
|
|
|
|
systemd.tmpfiles.rules = [
|
|
|
|
"d ${stateDir} 0750 ${user} ${group} - -"
|
|
|
|
"d ${stateDir}/tmp 0750 ${user} ${group} - -"
|
|
|
|
"d ${stateDir}/tmp/assets 0750 ${user} ${group} - -"
|
|
|
|
"d ${stateDir}/tmp/runtime 0750 ${user} ${group} - -"
|
|
|
|
"d ${stateDir}/tmp/upload 0750 ${user} ${group} - -"
|
|
|
|
"C ${stateDir}/upload 0750 ${user} ${group} - ${pkg}/share/limesurvey/upload"
|
|
|
|
];
|
|
|
|
|
|
|
|
systemd.services.limesurvey-init = {
|
|
|
|
wantedBy = [ "multi-user.target" ];
|
|
|
|
before = [ "phpfpm-limesurvey.service" ];
|
|
|
|
after = optional mysqlLocal "mysql.service" ++ optional pgsqlLocal "postgresql.service";
|
2023-03-24 00:07:29 +00:00
|
|
|
environment.DBENGINE = "${cfg.database.dbEngine}";
|
2020-04-24 23:36:52 +00:00
|
|
|
environment.LIMESURVEY_CONFIG = limesurveyConfig;
|
|
|
|
script = ''
|
|
|
|
# update or install the database as required
|
2023-05-24 13:37:59 +00:00
|
|
|
${pkgs.php81}/bin/php ${pkg}/share/limesurvey/application/commands/console.php updatedb || \
|
|
|
|
${pkgs.php81}/bin/php ${pkg}/share/limesurvey/application/commands/console.php install admin password admin admin@example.com verbose
|
2020-04-24 23:36:52 +00:00
|
|
|
'';
|
|
|
|
serviceConfig = {
|
|
|
|
User = user;
|
|
|
|
Group = group;
|
|
|
|
Type = "oneshot";
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
systemd.services.httpd.after = optional mysqlLocal "mysql.service" ++ optional pgsqlLocal "postgresql.service";
|
|
|
|
|
|
|
|
users.users.${user} = {
|
|
|
|
group = group;
|
|
|
|
isSystemUser = true;
|
|
|
|
};
|
|
|
|
|
|
|
|
};
|
|
|
|
}
|