1{pkgs, lib, config, ...}:
2
3with lib;
4
5let
6 cfg = config.dysnomia;
7
8 printProperties = properties:
9 concatMapStrings (propertyName:
10 let
11 property = properties.${propertyName};
12 in
13 if isList property then "${propertyName}=(${lib.concatMapStrings (elem: "\"${toString elem}\" ") (properties.${propertyName})})\n"
14 else "${propertyName}=\"${toString property}\"\n"
15 ) (builtins.attrNames properties);
16
17 properties = pkgs.stdenv.mkDerivation {
18 name = "dysnomia-properties";
19 buildCommand = ''
20 cat > $out << "EOF"
21 ${printProperties cfg.properties}
22 EOF
23 '';
24 };
25
26 containersDir = pkgs.stdenv.mkDerivation {
27 name = "dysnomia-containers";
28 buildCommand = ''
29 mkdir -p $out
30 cd $out
31
32 ${concatMapStrings (containerName:
33 let
34 containerProperties = cfg.containers.${containerName};
35 in
36 ''
37 cat > ${containerName} <<EOF
38 ${printProperties containerProperties}
39 type=${containerName}
40 EOF
41 ''
42 ) (builtins.attrNames cfg.containers)}
43 '';
44 };
45
46 linkMutableComponents = {containerName}:
47 ''
48 mkdir ${containerName}
49
50 ${concatMapStrings (componentName:
51 let
52 component = cfg.components.${containerName}.${componentName};
53 in
54 "ln -s ${component} ${containerName}/${componentName}\n"
55 ) (builtins.attrNames (cfg.components.${containerName} or {}))}
56 '';
57
58 componentsDir = pkgs.stdenv.mkDerivation {
59 name = "dysnomia-components";
60 buildCommand = ''
61 mkdir -p $out
62 cd $out
63
64 ${concatMapStrings (containerName:
65 linkMutableComponents { inherit containerName; }
66 ) (builtins.attrNames cfg.components)}
67 '';
68 };
69
70 dysnomiaFlags = {
71 enableApacheWebApplication = config.services.httpd.enable;
72 enableAxis2WebService = config.services.tomcat.axis2.enable;
73 enableDockerContainer = config.virtualisation.docker.enable;
74 enableEjabberdDump = config.services.ejabberd.enable;
75 enableMySQLDatabase = config.services.mysql.enable;
76 enablePostgreSQLDatabase = config.services.postgresql.enable;
77 enableTomcatWebApplication = config.services.tomcat.enable;
78 enableMongoDatabase = config.services.mongodb.enable;
79 enableSubversionRepository = config.services.svnserve.enable;
80 enableInfluxDatabase = config.services.influxdb.enable;
81 };
82in
83{
84 options = {
85 dysnomia = {
86
87 enable = mkOption {
88 type = types.bool;
89 default = false;
90 description = "Whether to enable Dysnomia";
91 };
92
93 enableAuthentication = mkOption {
94 type = types.bool;
95 default = false;
96 description = "Whether to publish privacy-sensitive authentication credentials";
97 };
98
99 package = mkOption {
100 type = types.path;
101 description = "The Dysnomia package";
102 };
103
104 properties = mkOption {
105 description = "An attribute set in which each attribute represents a machine property. Optionally, these values can be shell substitutions.";
106 default = {};
107 };
108
109 containers = mkOption {
110 description = "An attribute set in which each key represents a container and each value an attribute set providing its configuration properties";
111 default = {};
112 };
113
114 components = mkOption {
115 description = "An atttribute set in which each key represents a container and each value an attribute set in which each key represents a component and each value a derivation constructing its initial state";
116 default = {};
117 };
118
119 extraContainerProperties = mkOption {
120 description = "An attribute set providing additional container settings in addition to the default properties";
121 default = {};
122 };
123
124 extraContainerPaths = mkOption {
125 description = "A list of paths containing additional container configurations that are added to the search folders";
126 default = [];
127 };
128
129 extraModulePaths = mkOption {
130 description = "A list of paths containing additional modules that are added to the search folders";
131 default = [];
132 };
133
134 enableLegacyModules = mkOption {
135 type = types.bool;
136 default = true;
137 description = "Whether to enable Dysnomia legacy process and wrapper modules";
138 };
139 };
140 };
141
142 config = mkIf cfg.enable {
143
144 environment.etc = {
145 "dysnomia/containers" = {
146 source = containersDir;
147 };
148 "dysnomia/components" = {
149 source = componentsDir;
150 };
151 "dysnomia/properties" = {
152 source = properties;
153 };
154 };
155
156 environment.variables = {
157 DYSNOMIA_STATEDIR = "/var/state/dysnomia-nixos";
158 DYSNOMIA_CONTAINERS_PATH = "${lib.concatMapStrings (containerPath: "${containerPath}:") cfg.extraContainerPaths}/etc/dysnomia/containers";
159 DYSNOMIA_MODULES_PATH = "${lib.concatMapStrings (modulePath: "${modulePath}:") cfg.extraModulePaths}/etc/dysnomia/modules";
160 };
161
162 environment.systemPackages = [ cfg.package ];
163
164 dysnomia.package = pkgs.dysnomia.override (origArgs: dysnomiaFlags // lib.optionalAttrs (cfg.enableLegacyModules) {
165 enableLegacy = builtins.trace ''
166 WARNING: Dysnomia has been configured to use the legacy 'process' and 'wrapper'
167 modules for compatibility reasons! If you rely on these modules, consider
168 migrating to better alternatives.
169
170 More information: https://raw.githubusercontent.com/svanderburg/dysnomia/f65a9a84827bcc4024d6b16527098b33b02e4054/README-legacy.md
171
172 If you have migrated already or don't rely on these Dysnomia modules, you can
173 disable legacy mode with the following NixOS configuration option:
174
175 dysnomia.enableLegacyModules = false;
176
177 In a future version of Dysnomia (and NixOS) the legacy option will go away!
178 '' true;
179 });
180
181 dysnomia.properties = {
182 hostname = config.networking.hostName;
183 inherit (config.nixpkgs.localSystem) system;
184
185 supportedTypes = [
186 "echo"
187 "fileset"
188 "process"
189 "wrapper"
190
191 # These are not base modules, but they are still enabled because they work with technology that are always enabled in NixOS
192 "systemd-unit"
193 "sysvinit-script"
194 "nixos-configuration"
195 ]
196 ++ optional (dysnomiaFlags.enableApacheWebApplication) "apache-webapplication"
197 ++ optional (dysnomiaFlags.enableAxis2WebService) "axis2-webservice"
198 ++ optional (dysnomiaFlags.enableDockerContainer) "docker-container"
199 ++ optional (dysnomiaFlags.enableEjabberdDump) "ejabberd-dump"
200 ++ optional (dysnomiaFlags.enableInfluxDatabase) "influx-database"
201 ++ optional (dysnomiaFlags.enableMySQLDatabase) "mysql-database"
202 ++ optional (dysnomiaFlags.enablePostgreSQLDatabase) "postgresql-database"
203 ++ optional (dysnomiaFlags.enableTomcatWebApplication) "tomcat-webapplication"
204 ++ optional (dysnomiaFlags.enableMongoDatabase) "mongo-database"
205 ++ optional (dysnomiaFlags.enableSubversionRepository) "subversion-repository";
206 };
207
208 dysnomia.containers = lib.recursiveUpdate ({
209 process = {};
210 wrapper = {};
211 }
212 // lib.optionalAttrs (config.services.httpd.enable) { apache-webapplication = {
213 documentRoot = config.services.httpd.virtualHosts.localhost.documentRoot;
214 }; }
215 // lib.optionalAttrs (config.services.tomcat.axis2.enable) { axis2-webservice = {}; }
216 // lib.optionalAttrs (config.services.ejabberd.enable) { ejabberd-dump = {
217 ejabberdUser = config.services.ejabberd.user;
218 }; }
219 // lib.optionalAttrs (config.services.mysql.enable) { mysql-database = {
220 mysqlPort = config.services.mysql.port;
221 mysqlSocket = "/run/mysqld/mysqld.sock";
222 } // lib.optionalAttrs cfg.enableAuthentication {
223 mysqlUsername = "root";
224 };
225 }
226 // lib.optionalAttrs (config.services.postgresql.enable) { postgresql-database = {
227 } // lib.optionalAttrs (cfg.enableAuthentication) {
228 postgresqlUsername = "postgres";
229 };
230 }
231 // lib.optionalAttrs (config.services.tomcat.enable) { tomcat-webapplication = {
232 tomcatPort = 8080;
233 }; }
234 // lib.optionalAttrs (config.services.mongodb.enable) { mongo-database = {}; }
235 // lib.optionalAttrs (config.services.influxdb.enable) {
236 influx-database = {
237 influxdbUsername = config.services.influxdb.user;
238 influxdbDataDir = "${config.services.influxdb.dataDir}/data";
239 influxdbMetaDir = "${config.services.influxdb.dataDir}/meta";
240 };
241 }
242 // lib.optionalAttrs (config.services.svnserve.enable) { subversion-repository = {
243 svnBaseDir = config.services.svnserve.svnBaseDir;
244 }; }) cfg.extraContainerProperties;
245
246 boot.extraSystemdUnitPaths = [ "/etc/systemd-mutable/system" ];
247
248 system.activationScripts.dysnomia = ''
249 mkdir -p /etc/systemd-mutable/system
250 if [ ! -f /etc/systemd-mutable/system/dysnomia.target ]
251 then
252 ( echo "[Unit]"
253 echo "Description=Services that are activated and deactivated by Dysnomia"
254 echo "After=final.target"
255 ) > /etc/systemd-mutable/system/dysnomia.target
256 fi
257 '';
258 };
259}