1 { lib, pkgs, config, mylibs, myconfig, ... }:
3 mypkgs = pkgs.callPackage ../../packages.nix {
4 inherit (mylibs) checkEnv fetchedGit fetchedGithub;
6 cfg = config.services.myWebsites;
7 makeService = name: cfg: let
10 sslServerCert = "/var/lib/acme/${vhostConf.certName}/cert.pem";
11 sslServerKey = "/var/lib/acme/${vhostConf.certName}/key.pem";
12 sslServerChain = "/var/lib/acme/${vhostConf.certName}/fullchain.pem";
13 logFormat = "combinedVhost";
15 { ip = cfg.ip; port = 443; }
17 hostName = builtins.head vhostConf.hosts;
18 serverAliases = builtins.tail vhostConf.hosts or [];
19 documentRoot = vhostConf.root;
20 extraConfig = builtins.concatStringsSep "\n" vhostConf.extraConfig;
22 redirectVhost = { # Should go last, catchall http -> https redirect
23 listen = [ { ip = cfg.ip; port = 80; } ];
24 hostName = "redirectSSL";
25 serverAliases = [ "*" ];
27 logFormat = "combinedVhost";
28 documentRoot = "/var/lib/acme/acme-challenge";
31 RewriteCond "%{REQUEST_URI}" "!^/\.well-known"
32 RewriteRule ^(.+) https://%{HTTP_HOST}$1 [R=301]
33 # To redirect in specific "VirtualHost *:80", do
34 # RedirectMatch 301 ^/((?!\.well-known.*$).*)$ https://host/$1
38 fallbackVhost = toVhost { # Should go first, default choice
40 hosts = ["eldiron.immae.eu" ];
42 extraConfig = [ "DirectoryIndex index.htm" ];
47 { ip = cfg.ip; port = 443; }
49 stateDir = "/run/httpd_${name}";
50 logPerVirtualHost = true;
51 multiProcessingModule = "worker";
52 adminAddr = "httpd@immae.eu";
53 logFormat = "combinedVhost";
54 extraModules = pkgs.lib.lists.unique (pkgs.lib.lists.flatten cfg.modules);
55 extraConfig = builtins.concatStringsSep "\n" cfg.extraConfig;
56 virtualHosts = [ fallbackVhost ]
57 ++ (pkgs.lib.attrsets.mapAttrsToList (n: v: toVhost v) cfg.vhostConfs)
60 makeServiceOptions = name: ip: {
61 enable = lib.mkEnableOption "enable websites in ${name}";
63 type = lib.types.string;
65 description = "${name} ip to listen to";
67 modules = lib.mkOption {
68 type = lib.types.listOf (lib.types.str);
71 extraConfig = lib.mkOption {
72 type = lib.types.listOf (lib.types.lines);
75 vhostConfs = lib.mkOption {
76 type = lib.types.attrsOf (lib.types.submodule {
78 certName = lib.mkOption { type = lib.types.string; };
79 hosts = lib.mkOption { type = lib.types.listOf lib.types.string; };
80 root = lib.mkOption { type = lib.types.nullOr lib.types.path; };
81 extraConfig = lib.mkOption { type = lib.types.listOf lib.types.lines; default = []; };
101 # sed -e "s/services\.httpd/services\.httpdProd/g" .nix-defexpr/channels/nixpkgs/nixos/modules/services/web-servers/apache-httpd/default.nix
102 # And removed users / groups
103 ./apache/httpd_prod.nix
104 ./apache/httpd_inte.nix
105 # Adapted from base phpfpm
109 options.services.myWebsites = {
110 production = makeServiceOptions "production" myconfig.ips.production;
111 integration = makeServiceOptions "integration" myconfig.ips.integration;
112 tools = makeServiceOptions "tools" myconfig.ips.main;
114 apacheConfig = lib.mkOption {
115 type = lib.types.attrsOf (lib.types.submodule {
117 modules = lib.mkOption {
118 type = lib.types.listOf (lib.types.str);
121 extraConfig = lib.mkOption {
122 type = lib.types.nullOr lib.types.lines;
128 description = "Extra global config";
137 allowedTCPPorts = [ 80 443 ];
139 interfaces."eth0".ipv4.addresses = [
140 # 176.9.151.89 declared in nixops -> infra / tools
141 { address = myconfig.ips.production; prefixLength = 32; }
142 { address = myconfig.ips.integration; prefixLength = 32; }
146 nixpkgs.config.packageOverrides = oldpkgs: rec {
148 php72 = (oldpkgs.php72.override {
149 mysql.connector-c = pkgs.mariadb;
150 config.php.mysqlnd = false;
151 config.php.mysqli = false;
152 }).overrideAttrs(old: rec {
153 # Didn't manage to build with mysqli + mysql_config connector
154 configureFlags = old.configureFlags ++ [
155 "--with-mysqli=shared,mysqlnd"
157 # preConfigure = (old.preConfigure or "") + ''
158 # export CPPFLAGS="$CPPFLAGS -I${pkgs.mariadb}/include/mysql/server";
159 # sed -i -e 's/#include "mysqli_priv.h"/#include "mysqli_priv.h"\n#include <mysql_version.h>/' \
160 # ext/mysqli/mysqli.c ext/mysqli/mysqli_prop.c
163 phpPackages = oldpkgs.php72Packages.override { inherit php; };
166 services.myWebsites.tools.databases.enable = true;
167 services.myWebsites.tools.tools.enable = true;
168 services.myWebsites.tools.dav.enable = true;
169 services.myWebsites.tools.cloud.enable = true;
170 services.myWebsites.tools.git.enable = true;
171 services.myWebsites.tools.mastodon.enable = true;
173 services.myWebsites.Chloe.production.enable = cfg.production.enable;
174 services.myWebsites.Ludivine.production.enable = cfg.production.enable;
175 services.myWebsites.Aten.production.enable = cfg.production.enable;
176 services.myWebsites.PiedsJaloux.production.enable = cfg.production.enable;
177 services.myWebsites.Connexionswing.production.enable = cfg.production.enable;
179 services.myWebsites.Chloe.integration.enable = cfg.integration.enable;
180 services.myWebsites.Ludivine.integration.enable = cfg.integration.enable;
181 services.myWebsites.Aten.integration.enable = cfg.integration.enable;
182 services.myWebsites.PiedsJaloux.integration.enable = cfg.integration.enable;
183 services.myWebsites.Connexionswing.integration.enable = cfg.integration.enable;
185 services.myWebsites.apacheConfig = {
187 modules = [ "deflate" "filter" ];
189 AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css text/javascript application/javascript
193 modules = [ "macro" ];
196 modules = [ "ldap" "authnz_ldap" ];
198 extraConfig = assert mylibs.checkEnv "NIXOPS_HTTP_LDAP_PASSWORD"; ''
199 <IfModule ldap_module>
200 LDAPSharedCacheSize 500000
201 LDAPCacheEntries 1024
203 LDAPOpCacheEntries 1024
208 <IfModule authnz_ldap_module>
209 AuthLDAPURL ldap://ldap.immae.eu:389/dc=immae,dc=eu STARTTLS
210 AuthLDAPBindDN cn=httpd,ou=services,dc=immae,dc=eu
211 AuthLDAPBindPassword "${builtins.getEnv "NIXOPS_HTTP_LDAP_PASSWORD"}"
213 AuthName "Authentification requise (Acces LDAP)"
214 AuthBasicProvider ldap
218 <Macro Stats %{domain}>
219 Alias /awstats /var/lib/goaccess/%{domain}
220 <Directory /var/lib/goaccess/%{domain}>
221 DirectoryIndex index.html
227 Require ldap-group cn=%{domain},ou=stats,cn=httpd,ou=services,dc=immae,dc=eu
233 modules = [ "http2" ];
235 Protocols h2 http/1.1
240 LogFormat "%v:%p %h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combinedVhost
245 system.activationScripts = {
247 install -d -m 0755 /var/lib/acme/acme-challenge
248 install -d -m 0750 -o wwwrun -g wwwrun /var/lib/php/sessions
249 install -d -m 0750 -o wwwrun -g wwwrun /var/lib/php/sessions/adminer
250 install -d -m 0750 -o wwwrun -g wwwrun /var/lib/php/sessions/mantisbt
251 install -d -m 0750 -o wwwrun -g wwwrun /var/lib/php/sessions/davical
255 services.myPhpfpm = {
256 phpPackage = pkgs.php;
258 session.save_path = "/var/lib/php/sessions"
259 session.gc_maxlifetime = 60*60*24*15
260 session.cache_expire = 60*24*30
269 services.httpdProd = makeService "production" config.services.myWebsites.production;
270 services.myWebsites.production.modules = pkgs.lib.lists.flatten (pkgs.lib.attrsets.mapAttrsToList (n: v: v.modules or []) cfg.apacheConfig);
271 services.myWebsites.production.extraConfig = (builtins.filter (x: x != null) (pkgs.lib.attrsets.mapAttrsToList (n: v: v.extraConfig or null) cfg.apacheConfig));
273 services.httpdInte = makeService "integration" config.services.myWebsites.integration;
274 services.myWebsites.integration.modules = pkgs.lib.lists.flatten (pkgs.lib.attrsets.mapAttrsToList (n: v: v.modules or []) cfg.apacheConfig);
275 services.myWebsites.integration.extraConfig = (builtins.filter (x: x != null) (pkgs.lib.attrsets.mapAttrsToList (n: v: v.extraConfig or null) cfg.apacheConfig));
277 services.httpd = makeService "tools" config.services.myWebsites.tools;
278 services.myWebsites.tools.modules = pkgs.lib.lists.flatten (pkgs.lib.attrsets.mapAttrsToList (n: v: v.modules or []) cfg.apacheConfig);
279 services.myWebsites.tools.extraConfig = (builtins.filter (x: x != null) (pkgs.lib.attrsets.mapAttrsToList (n: v: v.extraConfig or null) cfg.apacheConfig));