diff options
author | Ismaël Bouya <ismael.bouya@normalesup.org> | 2019-01-10 23:29:52 +0100 |
---|---|---|
committer | Ismaël Bouya <ismael.bouya@normalesup.org> | 2019-01-10 23:29:52 +0100 |
commit | 54307da49e9975ca3cce0d45fd12ade016cf90d9 (patch) | |
tree | 5b9011f809e631f0a43bb21a1aa893c6279df8d6 /virtual/modules/websites | |
parent | 091ae73429b8853e6ab971bffaccd84bdd59f40d (diff) | |
download | Nix-54307da49e9975ca3cce0d45fd12ade016cf90d9.tar.gz Nix-54307da49e9975ca3cce0d45fd12ade016cf90d9.tar.zst Nix-54307da49e9975ca3cce0d45fd12ade016cf90d9.zip |
Move some more configuration for websites
Diffstat (limited to 'virtual/modules/websites')
-rw-r--r-- | virtual/modules/websites/default.nix | 192 |
1 files changed, 192 insertions, 0 deletions
diff --git a/virtual/modules/websites/default.nix b/virtual/modules/websites/default.nix new file mode 100644 index 0000000..14b9998 --- /dev/null +++ b/virtual/modules/websites/default.nix | |||
@@ -0,0 +1,192 @@ | |||
1 | { lib, pkgs, config, mylibs, myconfig, ... }: | ||
2 | let | ||
3 | cfg = config.services.myWebsites; | ||
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 | }; | ||
59 | in | ||
60 | { | ||
61 | imports = [ | ||
62 | ./chloe | ||
63 | ./ludivine | ||
64 | ./aten | ||
65 | ./piedsjaloux | ||
66 | ./connexionswing | ||
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 | ./apache/httpd_prod.nix | ||
71 | ./apache/httpd_inte.nix | ||
72 | ]; | ||
73 | |||
74 | options.services.myWebsites = { | ||
75 | production = makeServiceOptions "production" myconfig.ips.production; | ||
76 | integration = makeServiceOptions "integration" myconfig.ips.integration; | ||
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 | networking = { | ||
99 | firewall = { | ||
100 | enable = true; | ||
101 | allowedTCPPorts = [ 80 443 ]; | ||
102 | }; | ||
103 | interfaces."eth0".ipv4.addresses = [ | ||
104 | # 176.9.151.89 declared in nixops -> infra / tools | ||
105 | { address = myconfig.ips.production; prefixLength = 32; } | ||
106 | { address = myconfig.ips.integration; prefixLength = 32; } | ||
107 | ]; | ||
108 | }; | ||
109 | |||
110 | services.myWebsites.Chloe.production.enable = cfg.production.enable; | ||
111 | services.myWebsites.Ludivine.production.enable = cfg.production.enable; | ||
112 | services.myWebsites.Aten.production.enable = cfg.production.enable; | ||
113 | services.myWebsites.PiedsJaloux.production.enable = cfg.production.enable; | ||
114 | services.myWebsites.Connexionswing.production.enable = cfg.production.enable; | ||
115 | |||
116 | services.myWebsites.Chloe.integration.enable = cfg.integration.enable; | ||
117 | services.myWebsites.Ludivine.integration.enable = cfg.integration.enable; | ||
118 | services.myWebsites.Aten.integration.enable = cfg.integration.enable; | ||
119 | services.myWebsites.PiedsJaloux.integration.enable = cfg.integration.enable; | ||
120 | services.myWebsites.Connexionswing.integration.enable = cfg.integration.enable; | ||
121 | |||
122 | services.myWebsites.apacheConfig = { | ||
123 | gzip = { | ||
124 | modules = [ "deflate" "filter" ]; | ||
125 | extraConfig = '' | ||
126 | AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css text/javascript application/javascript | ||
127 | ''; | ||
128 | }; | ||
129 | macros = { | ||
130 | modules = [ "macro" ]; | ||
131 | }; | ||
132 | ldap = { | ||
133 | modules = [ "ldap" "authnz_ldap" ]; | ||
134 | # FIXME: starttls | ||
135 | extraConfig = assert mylibs.checkEnv "NIXOPS_HTTP_LDAP_PASSWORD"; '' | ||
136 | <IfModule ldap_module> | ||
137 | LDAPSharedCacheSize 500000 | ||
138 | LDAPCacheEntries 1024 | ||
139 | LDAPCacheTTL 600 | ||
140 | LDAPOpCacheEntries 1024 | ||
141 | LDAPOpCacheTTL 600 | ||
142 | </IfModule> | ||
143 | |||
144 | <Macro LDAPConnect> | ||
145 | <IfModule authnz_ldap_module> | ||
146 | AuthLDAPURL ldap://ldap.immae.eu:389/dc=immae,dc=eu STARTTLS | ||
147 | AuthLDAPBindDN cn=httpd,ou=services,dc=immae,dc=eu | ||
148 | AuthLDAPBindPassword "${builtins.getEnv "NIXOPS_HTTP_LDAP_PASSWORD"}" | ||
149 | AuthType Basic | ||
150 | AuthName "Authentification requise (Acces LDAP)" | ||
151 | AuthBasicProvider ldap | ||
152 | </IfModule> | ||
153 | </Macro> | ||
154 | |||
155 | <Macro Stats %{domain}> | ||
156 | Alias /awstats /var/lib/goaccess/%{domain} | ||
157 | <Directory /var/lib/goaccess/%{domain}> | ||
158 | DirectoryIndex index.html | ||
159 | AllowOverride None | ||
160 | Require all granted | ||
161 | </Directory> | ||
162 | <Location /awstats> | ||
163 | Use LDAPConnect | ||
164 | Require ldap-group cn=%{domain},ou=stats,cn=httpd,ou=services,dc=immae,dc=eu | ||
165 | </Location> | ||
166 | </Macro> | ||
167 | ''; | ||
168 | }; | ||
169 | http2 = { | ||
170 | modules = [ "http2" ]; | ||
171 | extraConfig = '' | ||
172 | Protocols h2 http/1.1 | ||
173 | ''; | ||
174 | }; | ||
175 | customLog = { | ||
176 | extraConfig = '' | ||
177 | LogFormat "%v:%p %h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combinedVhost | ||
178 | ''; | ||
179 | }; | ||
180 | }; | ||
181 | |||
182 | # FIXME: logrotate | ||
183 | # FIXME: ipv6 | ||
184 | services.httpdProd = makeService "production" config.services.myWebsites.production; | ||
185 | services.myWebsites.production.modules = pkgs.lib.lists.flatten (pkgs.lib.attrsets.mapAttrsToList (n: v: v.modules or []) cfg.apacheConfig); | ||
186 | services.myWebsites.production.extraConfig = (builtins.filter (x: x != null) (pkgs.lib.attrsets.mapAttrsToList (n: v: v.extraConfig or null) cfg.apacheConfig)); | ||
187 | |||
188 | services.httpdInte = makeService "integration" config.services.myWebsites.integration; | ||
189 | services.myWebsites.integration.modules = pkgs.lib.lists.flatten (pkgs.lib.attrsets.mapAttrsToList (n: v: v.modules or []) cfg.apacheConfig); | ||
190 | services.myWebsites.integration.extraConfig = (builtins.filter (x: x != null) (pkgs.lib.attrsets.mapAttrsToList (n: v: v.extraConfig or null) cfg.apacheConfig)); | ||
191 | }; | ||
192 | } | ||