2019-06-21 14:09:15 +00:00
|
|
|
{ config, lib, pkgs, ... }:
|
|
|
|
|
|
|
|
let
|
2023-11-27 00:19:27 +00:00
|
|
|
inherit (lib) mkDefault mkEnableOption mkPackageOption mkForce mkIf mkMerge mkOption types;
|
2021-10-03 16:06:03 +00:00
|
|
|
inherit (lib) concatStringsSep literalExpression mapAttrsToList optional optionalString;
|
2019-06-21 14:09:15 +00:00
|
|
|
|
|
|
|
cfg = config.services.moodle;
|
|
|
|
fpm = config.services.phpfpm.pools.moodle;
|
|
|
|
|
|
|
|
user = "moodle";
|
|
|
|
group = config.services.httpd.group;
|
|
|
|
stateDir = "/var/lib/moodle";
|
|
|
|
|
|
|
|
moodleConfig = pkgs.writeText "config.php" ''
|
|
|
|
<?php // Moodle configuration file
|
|
|
|
|
|
|
|
unset($CFG);
|
|
|
|
global $CFG;
|
|
|
|
$CFG = new stdClass();
|
|
|
|
|
2019-09-08 23:38:31 +00:00
|
|
|
$CFG->dbtype = '${ { mysql = "mariadb"; pgsql = "pgsql"; }.${cfg.database.type} }';
|
2019-06-21 14:09:15 +00:00
|
|
|
$CFG->dblibrary = 'native';
|
|
|
|
$CFG->dbhost = '${cfg.database.host}';
|
|
|
|
$CFG->dbname = '${cfg.database.name}';
|
|
|
|
$CFG->dbuser = '${cfg.database.user}';
|
|
|
|
${optionalString (cfg.database.passwordFile != null) "$CFG->dbpass = file_get_contents('${cfg.database.passwordFile}');"}
|
|
|
|
$CFG->prefix = 'mdl_';
|
|
|
|
$CFG->dboptions = array (
|
|
|
|
'dbpersist' => 0,
|
|
|
|
'dbport' => '${toString cfg.database.port}',
|
|
|
|
${optionalString (cfg.database.socket != null) "'dbsocket' => '${cfg.database.socket}',"}
|
|
|
|
'dbcollation' => 'utf8mb4_unicode_ci',
|
|
|
|
);
|
|
|
|
|
2019-11-04 21:24:55 +00:00
|
|
|
$CFG->wwwroot = '${if cfg.virtualHost.addSSL || cfg.virtualHost.forceSSL || cfg.virtualHost.onlySSL then "https" else "http"}://${cfg.virtualHost.hostName}';
|
2019-06-21 14:09:15 +00:00
|
|
|
$CFG->dataroot = '${stateDir}';
|
|
|
|
$CFG->admin = 'admin';
|
|
|
|
|
|
|
|
$CFG->directorypermissions = 02777;
|
|
|
|
$CFG->disableupdateautodeploy = true;
|
|
|
|
|
|
|
|
$CFG->pathtogs = '${pkgs.ghostscript}/bin/gs';
|
2020-07-14 10:56:41 +00:00
|
|
|
$CFG->pathtophp = '${phpExt}/bin/php';
|
2019-06-21 14:09:15 +00:00
|
|
|
$CFG->pathtodu = '${pkgs.coreutils}/bin/du';
|
|
|
|
$CFG->aspellpath = '${pkgs.aspell}/bin/aspell';
|
|
|
|
$CFG->pathtodot = '${pkgs.graphviz}/bin/dot';
|
|
|
|
|
2019-09-16 12:03:37 +00:00
|
|
|
${cfg.extraConfig}
|
|
|
|
|
2019-06-21 14:09:15 +00:00
|
|
|
require_once('${cfg.package}/share/moodle/lib/setup.php');
|
|
|
|
|
|
|
|
// There is no php closing tag in this file,
|
|
|
|
// it is intentional because it prevents trailing whitespace problems!
|
|
|
|
'';
|
|
|
|
|
|
|
|
mysqlLocal = cfg.database.createLocally && cfg.database.type == "mysql";
|
|
|
|
pgsqlLocal = cfg.database.createLocally && cfg.database.type == "pgsql";
|
2020-07-14 10:56:41 +00:00
|
|
|
|
2023-04-03 16:14:09 +00:00
|
|
|
phpExt = pkgs.php81.buildEnv {
|
2022-07-24 08:23:20 +00:00
|
|
|
extensions = { all, ... }: with all; [ iconv mbstring curl openssl tokenizer soap ctype zip gd simplexml dom intl sqlite3 pgsql pdo_sqlite pdo_pgsql pdo_odbc pdo_mysql pdo mysqli session zlib xmlreader fileinfo filter opcache exif sodium ];
|
|
|
|
extraConfig = "max_input_vars = 5000";
|
|
|
|
};
|
2019-06-21 14:09:15 +00:00
|
|
|
in
|
|
|
|
{
|
|
|
|
# interface
|
|
|
|
options.services.moodle = {
|
|
|
|
enable = mkEnableOption "Moodle web application";
|
|
|
|
|
2023-11-27 00:19:27 +00:00
|
|
|
package = mkPackageOption pkgs "moodle" { };
|
2019-06-21 14:09:15 +00:00
|
|
|
|
|
|
|
initialPassword = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
example = "correcthorsebatterystaple";
|
|
|
|
description = ''
|
|
|
|
Specifies the initial password for the admin, i.e. the password assigned if the user does not already exist.
|
|
|
|
The password specified here is world-readable in the Nix store, so it should be changed promptly.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
database = {
|
|
|
|
type = mkOption {
|
|
|
|
type = types.enum [ "mysql" "pgsql" ];
|
|
|
|
default = "mysql";
|
2021-01-24 09:19:10 +00:00
|
|
|
description = "Database engine to use.";
|
2019-06-21 14:09:15 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
host = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
default = "localhost";
|
|
|
|
description = "Database host address.";
|
|
|
|
};
|
|
|
|
|
|
|
|
port = mkOption {
|
2022-11-30 16:15:00 +00:00
|
|
|
type = types.port;
|
2019-06-21 14:09:15 +00:00
|
|
|
description = "Database host port.";
|
|
|
|
default = {
|
2019-09-08 23:38:31 +00:00
|
|
|
mysql = 3306;
|
|
|
|
pgsql = 5432;
|
2019-06-21 14:09:15 +00:00
|
|
|
}.${cfg.database.type};
|
2021-10-03 16:06:03 +00:00
|
|
|
defaultText = literalExpression "3306";
|
2019-06-21 14:09:15 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
name = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
default = "moodle";
|
|
|
|
description = "Database name.";
|
|
|
|
};
|
|
|
|
|
|
|
|
user = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
default = "moodle";
|
|
|
|
description = "Database user.";
|
|
|
|
};
|
|
|
|
|
|
|
|
passwordFile = mkOption {
|
|
|
|
type = types.nullOr types.path;
|
|
|
|
default = null;
|
|
|
|
example = "/run/keys/moodle-dbpassword";
|
|
|
|
description = ''
|
|
|
|
A file containing the password corresponding to
|
|
|
|
{option}`database.user`.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
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-03 16:06:03 +00:00
|
|
|
defaultText = literalExpression "/run/mysqld/mysqld.sock";
|
2019-06-21 14:09:15 +00:00
|
|
|
description = "Path to the unix socket file to use for authentication.";
|
|
|
|
};
|
|
|
|
|
|
|
|
createLocally = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = true;
|
|
|
|
description = "Create the database and database user locally.";
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
virtualHost = mkOption {
|
2020-01-25 01:36:48 +00:00
|
|
|
type = types.submodule (import ../web-servers/apache-httpd/vhost-options.nix);
|
2021-10-03 16:06:03 +00:00
|
|
|
example = literalExpression ''
|
2019-11-04 21:24:55 +00:00
|
|
|
{
|
|
|
|
hostName = "moodle.example.org";
|
|
|
|
adminAddr = "webmaster@example.org";
|
|
|
|
forceSSL = true;
|
|
|
|
enableACME = true;
|
|
|
|
}
|
|
|
|
'';
|
2019-06-21 14:09:15 +00:00
|
|
|
description = ''
|
|
|
|
Apache configuration can be done by adapting {option}`services.httpd.virtualHosts`.
|
|
|
|
See [](#opt-services.httpd.virtualHosts) for further information.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
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;
|
|
|
|
};
|
|
|
|
description = ''
|
|
|
|
Options for the Moodle PHP pool. See the documentation on `php-fpm.conf`
|
|
|
|
for details on configuration directives.
|
|
|
|
'';
|
|
|
|
};
|
2019-09-16 12:03:37 +00:00
|
|
|
|
|
|
|
extraConfig = mkOption {
|
|
|
|
type = types.lines;
|
|
|
|
default = "";
|
|
|
|
description = ''
|
|
|
|
Any additional text to be appended to the config.php
|
|
|
|
configuration file. This is a PHP script. For configuration
|
|
|
|
details, see <https://docs.moodle.org/37/en/Configuration_file>.
|
|
|
|
'';
|
|
|
|
example = ''
|
|
|
|
$CFG->disableupdatenotifications = true;
|
|
|
|
'';
|
|
|
|
};
|
2019-06-21 14:09:15 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
# implementation
|
|
|
|
config = mkIf cfg.enable {
|
|
|
|
|
|
|
|
assertions = [
|
nixos/postgresql: drop ensurePermissions, fix ensureUsers for postgresql15
Closes #216989
First of all, a bit of context: in PostgreSQL, newly created users don't
have the CREATE privilege on the public schema of a database even with
`ALL PRIVILEGES` granted via `ensurePermissions` which is how most of
the DB users are currently set up "declaratively"[1]. This means e.g. a
freshly deployed Nextcloud service will break early because Nextcloud
itself cannot CREATE any tables in the public schema anymore.
The other issue here is that `ensurePermissions` is a mere hack. It's
effectively a mixture of SQL code (e.g. `DATABASE foo` is relying on how
a value is substituted in a query. You'd have to parse a subset of SQL
to actually know which object are permissions granted to for a user).
After analyzing the existing modules I realized that in every case with
a single exception[2] the UNIX system user is equal to the db user is
equal to the db name and I don't see a compelling reason why people
would change that in 99% of the cases. In fact, some modules would even
break if you'd change that because the declarations of the system user &
the db user are mixed up[3].
So I decided to go with something new which restricts the ways to use
`ensure*` options rather than expanding those[4]. Effectively this means
that
* The DB user _must_ be equal to the DB name.
* Permissions are granted via `ensureDBOwnerhip` for an attribute-set in
`ensureUsers`. That way, the user is actually the owner and can
perform `CREATE`.
* For such a postgres user, a database must be declared in
`ensureDatabases`.
For anything else, a custom state management should be implemented. This
can either be `initialScript`, doing it manual, outside of the module or
by implementing proper state management for postgresql[5], but the
current state of `ensure*` isn't even declarative, but a convergent tool
which is what Nix actually claims to _not_ do.
Regarding existing setups: there are effectively two options:
* Leave everything as-is (assuming that system user == db user == db
name): then the DB user will automatically become the DB owner and
everything else stays the same.
* Drop the `createDatabase = true;` declarations: nothing will change
because a removal of `ensure*` statements is ignored, so it doesn't
matter at all whether this option is kept after the first deploy (and
later on you'd usually restore from backups anyways).
The DB user isn't the owner of the DB then, but for an existing setup
this is irrelevant because CREATE on the public schema isn't revoked
from existing users (only not granted for new users).
[1] not really declarative though because removals of these statements
are simply ignored for instance: https://github.com/NixOS/nixpkgs/issues/206467
[2] `services.invidious`: I removed the `ensure*` part temporarily
because it IMHO falls into the category "manage the state on your
own" (see the commit message). See also
https://github.com/NixOS/nixpkgs/pull/265857
[3] e.g. roundcube had `"DATABASE ${cfg.database.username}" = "ALL PRIVILEGES";`
[4] As opposed to other changes that are considered a potential fix, but
also add more things like collation for DBs or passwords that are
_never_ touched again when changing those.
[5] As suggested in e.g. https://github.com/NixOS/nixpkgs/issues/206467
2023-11-08 11:50:09 +00:00
|
|
|
{ assertion = cfg.database.createLocally -> cfg.database.user == user && cfg.database.user == cfg.database.name;
|
2019-06-21 14:09:15 +00:00
|
|
|
message = "services.moodle.database.user must be set to ${user} if services.moodle.database.createLocally is set true";
|
|
|
|
}
|
|
|
|
{ assertion = cfg.database.createLocally -> cfg.database.passwordFile == null;
|
|
|
|
message = "a password cannot be specified if services.moodle.database.createLocally is set to true";
|
|
|
|
}
|
|
|
|
];
|
|
|
|
|
|
|
|
services.mysql = mkIf mysqlLocal {
|
|
|
|
enable = true;
|
|
|
|
package = mkDefault pkgs.mariadb;
|
|
|
|
ensureDatabases = [ cfg.database.name ];
|
|
|
|
ensureUsers = [
|
|
|
|
{ name = cfg.database.user;
|
|
|
|
ensurePermissions = {
|
|
|
|
"${cfg.database.name}.*" = "SELECT, INSERT, UPDATE, DELETE, CREATE, CREATE TEMPORARY TABLES, DROP, INDEX, ALTER";
|
|
|
|
};
|
|
|
|
}
|
|
|
|
];
|
|
|
|
};
|
|
|
|
|
|
|
|
services.postgresql = mkIf pgsqlLocal {
|
|
|
|
enable = true;
|
|
|
|
ensureDatabases = [ cfg.database.name ];
|
|
|
|
ensureUsers = [
|
|
|
|
{ name = cfg.database.user;
|
nixos/postgresql: drop ensurePermissions, fix ensureUsers for postgresql15
Closes #216989
First of all, a bit of context: in PostgreSQL, newly created users don't
have the CREATE privilege on the public schema of a database even with
`ALL PRIVILEGES` granted via `ensurePermissions` which is how most of
the DB users are currently set up "declaratively"[1]. This means e.g. a
freshly deployed Nextcloud service will break early because Nextcloud
itself cannot CREATE any tables in the public schema anymore.
The other issue here is that `ensurePermissions` is a mere hack. It's
effectively a mixture of SQL code (e.g. `DATABASE foo` is relying on how
a value is substituted in a query. You'd have to parse a subset of SQL
to actually know which object are permissions granted to for a user).
After analyzing the existing modules I realized that in every case with
a single exception[2] the UNIX system user is equal to the db user is
equal to the db name and I don't see a compelling reason why people
would change that in 99% of the cases. In fact, some modules would even
break if you'd change that because the declarations of the system user &
the db user are mixed up[3].
So I decided to go with something new which restricts the ways to use
`ensure*` options rather than expanding those[4]. Effectively this means
that
* The DB user _must_ be equal to the DB name.
* Permissions are granted via `ensureDBOwnerhip` for an attribute-set in
`ensureUsers`. That way, the user is actually the owner and can
perform `CREATE`.
* For such a postgres user, a database must be declared in
`ensureDatabases`.
For anything else, a custom state management should be implemented. This
can either be `initialScript`, doing it manual, outside of the module or
by implementing proper state management for postgresql[5], but the
current state of `ensure*` isn't even declarative, but a convergent tool
which is what Nix actually claims to _not_ do.
Regarding existing setups: there are effectively two options:
* Leave everything as-is (assuming that system user == db user == db
name): then the DB user will automatically become the DB owner and
everything else stays the same.
* Drop the `createDatabase = true;` declarations: nothing will change
because a removal of `ensure*` statements is ignored, so it doesn't
matter at all whether this option is kept after the first deploy (and
later on you'd usually restore from backups anyways).
The DB user isn't the owner of the DB then, but for an existing setup
this is irrelevant because CREATE on the public schema isn't revoked
from existing users (only not granted for new users).
[1] not really declarative though because removals of these statements
are simply ignored for instance: https://github.com/NixOS/nixpkgs/issues/206467
[2] `services.invidious`: I removed the `ensure*` part temporarily
because it IMHO falls into the category "manage the state on your
own" (see the commit message). See also
https://github.com/NixOS/nixpkgs/pull/265857
[3] e.g. roundcube had `"DATABASE ${cfg.database.username}" = "ALL PRIVILEGES";`
[4] As opposed to other changes that are considered a potential fix, but
also add more things like collation for DBs or passwords that are
_never_ touched again when changing those.
[5] As suggested in e.g. https://github.com/NixOS/nixpkgs/issues/206467
2023-11-08 11:50:09 +00:00
|
|
|
ensureDBOwnership = true;
|
2019-06-21 14:09:15 +00:00
|
|
|
}
|
|
|
|
];
|
|
|
|
};
|
|
|
|
|
|
|
|
services.phpfpm.pools.moodle = {
|
|
|
|
inherit user group;
|
2020-07-14 10:56:41 +00:00
|
|
|
phpPackage = phpExt;
|
2019-06-21 14:09:15 +00:00
|
|
|
phpEnv.MOODLE_CONFIG = "${moodleConfig}";
|
|
|
|
phpOptions = ''
|
|
|
|
zend_extension = opcache.so
|
|
|
|
opcache.enable = 1
|
2022-07-24 08:23:20 +00:00
|
|
|
max_input_vars = 5000
|
2019-06-21 14:09:15 +00:00
|
|
|
'';
|
|
|
|
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" ];
|
2019-11-04 21:24:55 +00:00
|
|
|
virtualHosts.${cfg.virtualHost.hostName} = mkMerge [ cfg.virtualHost {
|
|
|
|
documentRoot = mkForce "${cfg.package}/share/moodle";
|
|
|
|
extraConfig = ''
|
|
|
|
<Directory "${cfg.package}/share/moodle">
|
|
|
|
<FilesMatch "\.php$">
|
|
|
|
<If "-f %{REQUEST_FILENAME}">
|
|
|
|
SetHandler "proxy:unix:${fpm.socket}|fcgi://localhost/"
|
|
|
|
</If>
|
|
|
|
</FilesMatch>
|
|
|
|
Options -Indexes
|
|
|
|
DirectoryIndex index.php
|
|
|
|
</Directory>
|
|
|
|
'';
|
|
|
|
} ];
|
2019-06-21 14:09:15 +00:00
|
|
|
};
|
|
|
|
|
2024-01-11 21:10:18 +00:00
|
|
|
systemd.tmpfiles.settings."10-moodle".${stateDir}.d = {
|
|
|
|
inherit user group;
|
|
|
|
mode = "0750";
|
|
|
|
};
|
2019-06-21 14:09:15 +00:00
|
|
|
|
|
|
|
systemd.services.moodle-init = {
|
|
|
|
wantedBy = [ "multi-user.target" ];
|
|
|
|
before = [ "phpfpm-moodle.service" ];
|
|
|
|
after = optional mysqlLocal "mysql.service" ++ optional pgsqlLocal "postgresql.service";
|
|
|
|
environment.MOODLE_CONFIG = moodleConfig;
|
|
|
|
script = ''
|
2020-07-14 10:56:41 +00:00
|
|
|
${phpExt}/bin/php ${cfg.package}/share/moodle/admin/cli/check_database_schema.php && rc=$? || rc=$?
|
2019-06-21 14:09:15 +00:00
|
|
|
|
2020-07-14 10:56:41 +00:00
|
|
|
[ "$rc" == 1 ] && ${phpExt}/bin/php ${cfg.package}/share/moodle/admin/cli/upgrade.php \
|
2019-06-21 14:09:15 +00:00
|
|
|
--non-interactive \
|
|
|
|
--allow-unstable
|
|
|
|
|
2020-07-14 10:56:41 +00:00
|
|
|
[ "$rc" == 2 ] && ${phpExt}/bin/php ${cfg.package}/share/moodle/admin/cli/install_database.php \
|
2019-06-21 14:09:15 +00:00
|
|
|
--agree-license \
|
|
|
|
--adminpass=${cfg.initialPassword}
|
|
|
|
|
|
|
|
true
|
|
|
|
'';
|
|
|
|
serviceConfig = {
|
|
|
|
User = user;
|
|
|
|
Group = group;
|
|
|
|
Type = "oneshot";
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
systemd.services.moodle-cron = {
|
|
|
|
description = "Moodle cron service";
|
|
|
|
after = [ "moodle-init.service" ];
|
|
|
|
environment.MOODLE_CONFIG = moodleConfig;
|
|
|
|
serviceConfig = {
|
|
|
|
User = user;
|
|
|
|
Group = group;
|
2020-07-14 10:56:41 +00:00
|
|
|
ExecStart = "${phpExt}/bin/php ${cfg.package}/share/moodle/admin/cli/cron.php";
|
2019-06-21 14:09:15 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
systemd.timers.moodle-cron = {
|
|
|
|
description = "Moodle cron timer";
|
|
|
|
wantedBy = [ "timers.target" ];
|
|
|
|
timerConfig = {
|
|
|
|
OnCalendar = "minutely";
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
systemd.services.httpd.after = optional mysqlLocal "mysql.service" ++ optional pgsqlLocal "postgresql.service";
|
|
|
|
|
2019-10-12 20:25:28 +00:00
|
|
|
users.users.${user} = {
|
|
|
|
group = group;
|
|
|
|
isSystemUser = true;
|
|
|
|
};
|
2019-06-21 14:09:15 +00:00
|
|
|
};
|
|
|
|
}
|