1 { pkgs, lib, config, ... }:
6 cfg = config.services.flarum;
8 flarumInstallConfig = pkgs.writeText "config.json" (builtins.toJSON {
12 baseUrl = cfg.baseUrl;
13 databaseConfiguration = cfg.database;
15 username = cfg.adminUser;
16 password = cfg.initialAdminPassword;
17 email = cfg.adminEmail;
20 forum_title = cfg.forumTitle;
24 options.services.flarum = {
25 enable = mkEnableOption "Flarum discussion platform";
27 package = mkPackageOption pkgs "flarum" { };
29 forumTitle = mkOption {
31 default = "A Flarum Forum on NixOS";
32 description = "Title of the forum.";
37 default = "localhost";
38 example = "forum.example.com";
39 description = "Domain to serve on.";
44 default = "http://localhost";
45 example = "https://forum.example.com";
46 description = "Change `domain` instead.";
49 adminUser = mkOption {
52 description = "Username for first web application administrator";
55 adminEmail = mkOption {
57 default = "admin@example.com";
58 description = "Email for first web application administrator";
61 initialAdminPassword = mkOption {
64 description = "Initial password for the adminUser";
70 description = "System user to run Flarum";
76 description = "System group to run Flarum";
81 default = "/var/lib/flarum";
82 description = "Home directory for writable storage";
85 database = mkOption rec {
86 type = with types; attrsOf (oneOf [str bool int]);
87 description = "MySQL database parameters";
89 # the database driver; i.e. MySQL; MariaDB...
91 # the host of the connection; localhost in most cases unless using an external service
93 # the name of the database in the instance
99 # the prefix for the tables; useful if you are sharing the same database with another service
101 # the port of the connection; defaults to 3306 with MySQL
107 createDatabaseLocally = mkOption {
111 Create the database and database user locally, and run installation.
113 WARNING: Due to https://github.com/flarum/framework/issues/4018, this option is set
114 to false by default. The 'flarum install' command may delete existing database tables.
115 Only set this to true if you are certain you are working with a fresh, empty database.
120 config = mkIf cfg.enable {
121 users.users.${cfg.user} = {
128 users.groups.${cfg.group} = {};
130 services.phpfpm.pools.flarum = {
133 "listen.owner" = config.services.nginx.user;
134 "listen.group" = config.services.nginx.group;
135 "listen.mode" = "0600";
136 "pm" = mkDefault "dynamic";
137 "pm.max_children" = mkDefault 10;
138 "pm.max_requests" = mkDefault 500;
139 "pm.start_servers" = mkDefault 2;
140 "pm.min_spare_servers" = mkDefault 1;
141 "pm.max_spare_servers" = mkDefault 3;
151 virtualHosts."${cfg.domain}" = {
152 root = "${cfg.stateDir}/public";
153 locations."~ \.php$".extraConfig = ''
154 fastcgi_pass unix:${config.services.phpfpm.pools.flarum.socket};
155 fastcgi_index site.php;
159 include ${cfg.package}/share/php/flarum/.nginx.conf;
164 services.mysql = mkIf cfg.enable {
166 package = pkgs.mysql;
167 ensureDatabases = [cfg.database.database];
170 name = cfg.database.username;
171 ensurePermissions = {
172 "${cfg.database.database}.*" = "ALL PRIVILEGES";
180 assertion = !cfg.createDatabaseLocally || cfg.database.driver == "mysql";
181 message = "Flarum can only be automatically installed in MySQL/MariaDB.";
185 systemd.services.flarum-install = {
186 description = "Flarum installation";
187 requiredBy = ["phpfpm-flarum.service"];
188 before = ["phpfpm-flarum.service"];
189 requires = ["mysql.service"];
190 after = ["mysql.service"];
196 path = [config.services.phpfpm.phpPackage];
198 mkdir -p ${cfg.stateDir}/{extensions,public/assets/avatars}
199 mkdir -p ${cfg.stateDir}/storage/{cache,formatter,sessions,views}
201 cp -f ${cfg.package}/share/php/flarum/{extend.php,site.php,flarum} .
202 ln -sf ${cfg.package}/share/php/flarum/vendor .
203 ln -sf ${cfg.package}/share/php/flarum/public/index.php public/
204 '' + optionalString (cfg.createDatabaseLocally && cfg.database.driver == "mysql") ''
205 if [ ! -f config.php ]; then
206 php flarum install --file=${flarumInstallConfig}
209 if [ -f config.php ]; then
211 php flarum cache:clear
217 meta.maintainers = with lib.maintainers; [ fsagbuya jasonodoom ];