]> git.immae.eu Git - perso/Immae/Config/Nix.git/blame - virtual/modules/websites.nix
Move packages fils to module
[perso/Immae/Config/Nix.git] / virtual / modules / websites.nix
CommitLineData
f8bde3d6 1{ lib, pkgs, config, mylibs, myconfig, ... }:
42429ef0
IB
2let
3 cfg = config.services.myWebsites;
f8bde3d6
IB
4 makeService = name: cfg: let
5 toVhost = vhostConf: {
6 enableSSL = true;
7 sslServerCert = "/var/lib/acme/${vhostConf.certName}/cert.pem";
8 sslServerKey = "/var/lib/acme/${vhostConf.certName}/key.pem";
9 sslServerChain = "/var/lib/acme/${vhostConf.certName}/fullchain.pem";
10 logFormat = "combinedVhost";
11 listen = [
12 { ip = cfg.ip; port = 443; }
13 ];
14 hostName = builtins.head vhostConf.hosts;
15 serverAliases = builtins.tail vhostConf.hosts or [];
16 documentRoot = vhostConf.root;
17 extraConfig = builtins.concatStringsSep "\n" vhostConf.extraConfig;
18 };
19 in rec {
20 enable = true;
21 listen = [
22 { ip = cfg.ip; port = 443; }
23 ];
24 stateDir = "/run/httpd_${name}";
25 logPerVirtualHost = true;
26 multiProcessingModule = "worker";
27 adminAddr = "httpd@immae.eu";
28 logFormat = "combinedVhost";
29 extraModules = pkgs.lib.lists.unique (pkgs.lib.lists.flatten cfg.modules);
30 extraConfig = builtins.concatStringsSep "\n" cfg.extraConfig;
31 virtualHosts = pkgs.lib.attrsets.mapAttrsToList (n: v: toVhost v) cfg.vhostConfs;
32 };
33 makeServiceOptions = name: ip: {
34 enable = lib.mkEnableOption "enable websites in ${name}";
35 ip = lib.mkOption {
36 type = lib.types.string;
37 default = ip;
38 description = "${name} ip to listen to";
39 };
40 modules = lib.mkOption {
41 type = lib.types.listOf (lib.types.str);
42 default = [];
43 };
44 extraConfig = lib.mkOption {
45 type = lib.types.listOf (lib.types.lines);
46 default = [];
47 };
48 vhostConfs = lib.mkOption {
49 type = lib.types.attrsOf (lib.types.submodule {
50 options = {
51 certName = lib.mkOption { type = lib.types.string; };
52 hosts = lib.mkOption { type = lib.types.listOf lib.types.string; };
53 root = lib.mkOption { type = lib.types.nullOr lib.types.path; };
54 extraConfig = lib.mkOption { type = lib.types.listOf lib.types.lines; default = []; };
55 };
56 });
57 };
58 };
42429ef0
IB
59in
60{
61 imports = [
091ae734
IB
62 ./websites/chloe
63 ./websites/ludivine
64 ./websites/aten
65 ./websites/piedsjaloux
66 ./websites/connexionswing
f8bde3d6
IB
67 # built using:
68 # sed -e "s/services\.httpd/services\.httpdProd/g" .nix-defexpr/channels/nixpkgs/nixos/modules/services/web-servers/apache-httpd/default.nix
69 # And removed users / groups
70 ./websites/apache/httpd_prod.nix
71 ./websites/apache/httpd_inte.nix
42429ef0
IB
72 ];
73
74 options.services.myWebsites = {
f8bde3d6
IB
75 production = makeServiceOptions "production" myconfig.ips.production;
76 integration = makeServiceOptions "integration" myconfig.ips.integration;
42429ef0
IB
77
78 apacheConfig = lib.mkOption {
79 type = lib.types.attrsOf (lib.types.submodule {
80 options = {
81 modules = lib.mkOption {
82 type = lib.types.listOf (lib.types.str);
83 default = [];
84 };
85 extraConfig = lib.mkOption {
86 type = lib.types.nullOr lib.types.lines;
87 default = null;
88 };
89 };
90 });
91 default = {};
92 description = "Extra global config";
93 };
94
95 };
96
97 config = {
98 services.myWebsites.Chloe.production.enable = cfg.production.enable;
99 services.myWebsites.Ludivine.production.enable = cfg.production.enable;
100 services.myWebsites.Aten.production.enable = cfg.production.enable;
101 services.myWebsites.PiedsJaloux.production.enable = cfg.production.enable;
102 services.myWebsites.Connexionswing.production.enable = cfg.production.enable;
103
104 services.myWebsites.Chloe.integration.enable = cfg.integration.enable;
105 services.myWebsites.Ludivine.integration.enable = cfg.integration.enable;
106 services.myWebsites.Aten.integration.enable = cfg.integration.enable;
107 services.myWebsites.PiedsJaloux.integration.enable = cfg.integration.enable;
108 services.myWebsites.Connexionswing.integration.enable = cfg.integration.enable;
109
110 services.myWebsites.apacheConfig = {
111 gzip = {
112 modules = [ "deflate" "filter" ];
113 extraConfig = ''
114 AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css text/javascript application/javascript
115 '';
116 };
117 macros = {
118 modules = [ "macro" ];
119 };
120 ldap = {
121 modules = [ "ldap" "authnz_ldap" ];
122 # FIXME: starttls
123 extraConfig = assert mylibs.checkEnv "NIXOPS_HTTP_LDAP_PASSWORD"; ''
124 <IfModule ldap_module>
125 LDAPSharedCacheSize 500000
126 LDAPCacheEntries 1024
127 LDAPCacheTTL 600
128 LDAPOpCacheEntries 1024
129 LDAPOpCacheTTL 600
130 </IfModule>
131
132 <Macro LDAPConnect>
133 <IfModule authnz_ldap_module>
134 AuthLDAPURL ldap://ldap.immae.eu:389/dc=immae,dc=eu STARTTLS
135 AuthLDAPBindDN cn=httpd,ou=services,dc=immae,dc=eu
136 AuthLDAPBindPassword "${builtins.getEnv "NIXOPS_HTTP_LDAP_PASSWORD"}"
137 AuthType Basic
138 AuthName "Authentification requise (Acces LDAP)"
139 AuthBasicProvider ldap
140 </IfModule>
141 </Macro>
142
143 <Macro Stats %{domain}>
144 Alias /awstats /var/lib/goaccess/%{domain}
145 <Directory /var/lib/goaccess/%{domain}>
146 DirectoryIndex index.html
147 AllowOverride None
148 Require all granted
149 </Directory>
150 <Location /awstats>
151 Use LDAPConnect
152 Require ldap-group cn=%{domain},ou=stats,cn=httpd,ou=services,dc=immae,dc=eu
153 </Location>
154 </Macro>
155 '';
156 };
157 http2 = {
158 modules = [ "http2" ];
159 extraConfig = ''
160 Protocols h2 http/1.1
161 '';
162 };
163 customLog = {
164 extraConfig = ''
165 LogFormat "%v:%p %h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combinedVhost
166 '';
167 };
168 };
f8bde3d6
IB
169
170 # FIXME: logrotate
171 # FIXME: ipv6
172 services.httpdProd = makeService "production" config.services.myWebsites.production;
173 services.myWebsites.production.modules = pkgs.lib.lists.flatten (pkgs.lib.attrsets.mapAttrsToList (n: v: v.modules or []) cfg.apacheConfig);
174 services.myWebsites.production.extraConfig = (builtins.filter (x: x != null) (pkgs.lib.attrsets.mapAttrsToList (n: v: v.extraConfig or null) cfg.apacheConfig));
175
176 services.httpdInte = makeService "integration" config.services.myWebsites.integration;
177 services.myWebsites.integration.modules = pkgs.lib.lists.flatten (pkgs.lib.attrsets.mapAttrsToList (n: v: v.modules or []) cfg.apacheConfig);
178 services.myWebsites.integration.extraConfig = (builtins.filter (x: x != null) (pkgs.lib.attrsets.mapAttrsToList (n: v: v.extraConfig or null) cfg.apacheConfig));
42429ef0
IB
179 };
180}