···11+{ self, pkgs, config, lib, ... }:
22+33+{
44+ services.postgresql = {
55+ ensureDatabases = [ "ulogger" ];
66+ ensureUsers = [{
77+ name = "ulogger";
88+ ensurePermissions = {
99+ "DATABASE ulogger" = "ALL PRIVILEGES";
1010+ };
1111+ }];
1212+ };
1313+1414+ services.ulogger = {
1515+ enable = true;
1616+ hostName = "tracks.mossnet.lan";
1717+ conf = ''
1818+ <?php
1919+ /* μlogger
2020+ *
2121+ * Copyright(C) 2017 Bartek Fabiszewski (www.fabiszewski.net)
2222+ *
2323+ * This is free software; you can redistribute it and/or modify it under
2424+ * the terms of the GNU General Public License as published by
2525+ * the Free Software Foundation; either version 3 of the License, or
2626+ * (at your option) any later version.
2727+ *
2828+ * This program is distributed in the hope that it will be useful, but
2929+ * WITHOUT ANY WARRANTY; without even the implied warranty of
3030+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
3131+ * General Public License for more details.
3232+ *
3333+ * You should have received a copy of the GNU General Public License
3434+ * along with this program; if not, see <http://www.gnu.org/licenses/>.
3535+ */
3636+3737+ // This is default configuration file.
3838+ // Copy it to config.php and customize
3939+4040+ // default map drawing framework
4141+ // (gmaps = google maps, openlayers = openlayers/osm)
4242+ //$mapapi = "gmaps";
4343+ $mapapi = "openlayers";
4444+4545+ // openlayers additional map layers
4646+ // OpenCycleMap (0 = no, 1 = yes)
4747+ $layer_ocm = 1;
4848+ // MapQuest-OSM (0 = no, 1 = yes)
4949+ $layer_mq = 1;
5050+ // osmapa.pl (0 = no, 1 = yes)
5151+ $layer_osmapa = 1;
5252+ // UMP (0 = no, 1 = yes)
5353+ $layer_ump = 1;
5454+5555+ // default coordinates for initial map
5656+ $init_latitude = 52.23;
5757+ $init_longitude = 21.01;
5858+5959+ // you may set your google maps api key
6060+ // this is not obligatory by now
6161+ //$gkey = "";
6262+6363+ // MySQL config
6464+ $dbhost = "localhost"; // mysql host, eg. localhost
6565+ $dbuser = "ulogger"; // database user
6666+ $dbpass = ""; // database pass
6767+ $dbname = "ulogger"; // database name
6868+ $dbprefix = ""; // optional table names prefix, eg. "ulogger_"
6969+7070+ // other
7171+ // require login/password authentication
7272+ // (0 = no, 1 = yes)
7373+ $require_authentication = 0;
7474+7575+ // all users tracks are visible to authenticated user
7676+ // (0 = no, 1 = yes)
7777+ $public_tracks = 0;
7878+7979+ // admin user, who
8080+ // - can add new users
8181+ // - can edit all tracks, users
8282+ // - has access to all users locations
8383+ // none if empty
8484+ $admin_user = "admin";
8585+8686+ // miniumum required length of user password
8787+ $pass_lenmin = 12;
8888+8989+ // required strength of user password
9090+ // 0 = no requirements,
9191+ // 1 = require mixed case letters (lower and upper),
9292+ // 2 = require mixed case and numbers,
9393+ // 3 = require mixed case, numbers and non-alphanumeric characters
9494+ $pass_strength = 0;
9595+9696+ // Default interval in seconds for live auto reload
9797+ $interval = 10;
9898+9999+ // Default language
100100+ // (en, pl, de, hu)
101101+ $lang = "en";
102102+ //$lang = "pl";
103103+ //$lang = "de";
104104+ //$lang = "hu";
105105+ //$lang = "fr";
106106+ //$lang = "it";
107107+108108+ // units
109109+ // (metric, imperial)
110110+ $units = "metric";
111111+ //$units = "imperial";
112112+113113+ ?>
114114+115115+ '';
116116+ };
117117+}
+282
modules/nixos/ulogger.nix
···11+{ config, options, lib, pkgs, ... }:
22+33+with lib;
44+let
55+ cfg = config.services.ulogger;
66+77+ poolName = "ulogger";
88+99+ configFile = pkgs.writeTextFile {
1010+ name = "ulogger-config";
1111+ text = cfg.conf;
1212+ destination = "config.php";
1313+ };
1414+1515+ appDir = pkgs.buildEnv {
1616+ name = "ulogger-app-dir";
1717+ ignoreCollisions = true;
1818+ checkCollisionContents = false;
1919+ paths = [ configFile "${cfg.package}/app" ];
2020+ };
2121+2222+in
2323+{
2424+ options = {
2525+ services.ulogger = {
2626+ enable = mkEnableOption "ulogger";
2727+ user = mkOption {
2828+ type = types.str;
2929+ default = "nginx";
3030+ description = ''
3131+ User account under which both the update daemon and the web-application run.
3232+ '';
3333+ };
3434+ dataDir = mkOption {
3535+ type = types.path;
3636+ default = "/var/lib/ulogger";
3737+ description = ''
3838+ Data directory.
3939+ '';
4040+ };
4141+4242+ package = mkOption {
4343+ type = types.package;
4444+ default = pkgs.ulogger-server;
4545+ description = ''
4646+ ulogger-server package to use.
4747+ '';
4848+ };
4949+5050+ hostName = mkOption {
5151+ type = types.str;
5252+ description = ''
5353+ Name of the nginx virtualhost to use and setup.
5454+ '';
5555+ };
5656+5757+ poolConfig = mkOption {
5858+ type = types.lines;
5959+ default = ''
6060+ pm = dynamic
6161+ pm.max_children = 75
6262+ pm.start_servers = 1
6363+ pm.min_spare_servers = 1
6464+ pm.max_spare_servers = 20
6565+ pm.max_requests = 500
6666+ catch_workers_output = 1
6767+ '';
6868+ description = ''
6969+ Options for uloggers's PHP pool. See the documentation on <literal>php-fpm.conf</literal> for details on configuration directives.
7070+ '';
7171+ };
7272+7373+ conf = mkOption {
7474+ type = types.str;
7575+ default = ''
7676+ <?php
7777+ /* μlogger
7878+ *
7979+ * Copyright(C) 2017 Bartek Fabiszewski (www.fabiszewski.net)
8080+ *
8181+ * This is free software; you can redistribute it and/or modify it under
8282+ * the terms of the GNU General Public License as published by
8383+ * the Free Software Foundation; either version 3 of the License, or
8484+ * (at your option) any later version.
8585+ *
8686+ * This program is distributed in the hope that it will be useful, but
8787+ * WITHOUT ANY WARRANTY; without even the implied warranty of
8888+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
8989+ * General Public License for more details.
9090+ *
9191+ * You should have received a copy of the GNU General Public License
9292+ * along with this program; if not, see <http://www.gnu.org/licenses/>.
9393+ */
9494+9595+ // This is default configuration file.
9696+ // Copy it to config.php and customize
9797+9898+ // default map drawing framework
9999+ // (gmaps = google maps, openlayers = openlayers/osm)
100100+ //$mapapi = "gmaps";
101101+ $mapapi = "openlayers";
102102+103103+ // openlayers additional map layers
104104+ // OpenCycleMap (0 = no, 1 = yes)
105105+ $layer_ocm = 1;
106106+ // MapQuest-OSM (0 = no, 1 = yes)
107107+ $layer_mq = 1;
108108+ // osmapa.pl (0 = no, 1 = yes)
109109+ $layer_osmapa = 1;
110110+ // UMP (0 = no, 1 = yes)
111111+ $layer_ump = 1;
112112+113113+ // default coordinates for initial map
114114+ $init_latitude = 52.23;
115115+ $init_longitude = 21.01;
116116+117117+ // you may set your google maps api key
118118+ // this is not obligatory by now
119119+ //$gkey = "";
120120+121121+ // MySQL config
122122+ $dbhost = ""; // mysql host, eg. localhost
123123+ $dbuser = ""; // database user
124124+ $dbpass = ""; // database pass
125125+ $dbname = ""; // database name
126126+ $dbprefix = ""; // optional table names prefix, eg. "ulogger_"
127127+128128+ // other
129129+ // require login/password authentication
130130+ // (0 = no, 1 = yes)
131131+ $require_authentication = 1;
132132+133133+ // all users tracks are visible to authenticated user
134134+ // (0 = no, 1 = yes)
135135+ $public_tracks = 0;
136136+137137+ // admin user, who
138138+ // - can add new users
139139+ // - can edit all tracks, users
140140+ // - has access to all users locations
141141+ // none if empty
142142+ $admin_user = "";
143143+144144+ // miniumum required length of user password
145145+ $pass_lenmin = 12;
146146+147147+ // required strength of user password
148148+ // 0 = no requirements,
149149+ // 1 = require mixed case letters (lower and upper),
150150+ // 2 = require mixed case and numbers,
151151+ // 3 = require mixed case, numbers and non-alphanumeric characters
152152+ $pass_strength = 2;
153153+154154+ // Default interval in seconds for live auto reload
155155+ $interval = 10;
156156+157157+ // Default language
158158+ // (en, pl, de, hu)
159159+ $lang = "en";
160160+ //$lang = "pl";
161161+ //$lang = "de";
162162+ //$lang = "hu";
163163+ //$lang = "fr";
164164+ //$lang = "it";
165165+166166+ // units
167167+ // (metric, imperial)
168168+ $units = "metric";
169169+ //$units = "imperial";
170170+171171+ ?>
172172+ '';
173173+ description = ''
174174+ Contents of the ulogger configuration file (config.php)
175175+ '';
176176+ };
177177+ };
178178+ };
179179+180180+181181+ config = mkIf cfg.enable {
182182+ services.phpfpm.pools."${poolName}" = {
183183+ user = "${cfg.user}";
184184+ group = "nginx";
185185+ phpPackage = pkgs.php;
186186+ settings = {
187187+ "listen.owner" = "nginx";
188188+ "listen.group" = "nginx";
189189+ "listen.mode" = "0600";
190190+ "user" = "${cfg.user}";
191191+ "group" = "nginx";
192192+ "pm" = "dynamic";
193193+ "pm.max_children" = "75";
194194+ "pm.min_spare_servers" = "5";
195195+ "pm.max_spare_servers" = "20";
196196+ "pm.max_requests" = "10";
197197+ "catch_workers_output" = "1";
198198+ "php_admin_value[error_log]" = "/var/log/nginx/${poolName}-phpfpm-error.log";
199199+ };
200200+ };
201201+ services.phpfpm.phpOptions = ''
202202+ max_execution_time = 120
203203+ '';
204204+205205+ services.nginx.enable = mkDefault true;
206206+207207+ services.nginx.virtualHosts."${cfg.hostName}" = {
208208+ enableACME = false;
209209+ forceSSL = false;
210210+ root = "${cfg.package}";
211211+212212+ extraConfig = ''
213213+ add_header X-Frame-Options SAMEORIGIN;
214214+ add_header X-Content-Type-Options nosniff;
215215+ add_header X-XSS-Protection "1; mode=block";
216216+ '';
217217+218218+ locations."/" = {
219219+ extraConfig = ''
220220+ '';
221221+ };
222222+223223+ locations."~ ^/app\\.php(/|$)" = {
224224+ extraConfig = ''
225225+ fastcgi_pass unix:${config.services.phpfpm.pools."${poolName}".socket};
226226+ include ${pkgs.nginx}/conf/fastcgi_params;
227227+ include ${pkgs.nginx}/conf/fastcgi.conf;
228228+ fastcgi_index ${conf.pkg}/index.php;
229229+ '';
230230+ };
231231+232232+ systemd.services.ulogger-install = {
233233+ description = "ulogger install service";
234234+ wantedBy = [ "multi-user.target" ];
235235+ before = [ "phpfpm-ulogger.service" ];
236236+ after = [ "mysql.service" "postgresql.service" ];
237237+ path = with pkgs; [ coreutils php phpPackages.composer ];
238238+239239+ serviceConfig = {
240240+ User = cfg.user;
241241+ Type = "oneshot";
242242+ RemainAfterExit = "yes";
243243+ PermissionsStartOnly = true;
244244+ };
245245+246246+ preStart = ''
247247+ mkdir -p "${cfg.dataDir}"
248248+ mkdir -p "${cfg.dataDir}/uploads"
249249+ chown ${cfg.user}:nginx "${cfg.dataDir}"
250250+ '';
251251+252252+ environment = {
253253+ ULOGGER_ADMIN_USER = "admin";
254254+ ULOGGER_ADMIN_PASSWORD = "admin";
255255+ };
256256+257257+ script = ''
258258+ echo "Setting up wallabag files in ${cfg.dataDir} ..."
259259+ cd "${cfg.dataDir}"
260260+261261+ rm -rf var/cache/*
262262+ rm -f app
263263+ ln -sf ${appDir} app
264264+ ln -sf ${cfg.package}/composer.{json,lock} .
265265+266266+ if [ ! -f installed ]; then
267267+ echo "Install file not found, installing ..."
268268+ # TODO, taken from https://github.com/bfabiszewski/ulogger-server/blob/master/.docker/init.sh
269269+ su postgres -c "psql -U ulogger < ${appDir}/scripts/ulogger.pgsql"
270270+ su postgres -c "psql -d ulogger -c \"INSERT INTO users (login, password, admin) VALUES ('$ULOGGER_ADMIN_USER', '$ULOGGER_ADMIN_PASSWORD', TRUE)\""
271271+ touch installed
272272+ fi
273273+ '';
274274+ };
275275+ };
276276+277277+ meta = with stdenv.lib; {
278278+ maintainers = with maintainers; [ nadrieril ];
279279+ };
280280+281281+ };
282282+}