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 = lib.mdDoc "Whether to enable Dysnomia";
91 };
92
93 enableAuthentication = mkOption {
94 type = types.bool;
95 default = false;
96 description = lib.mdDoc "Whether to publish privacy-sensitive authentication credentials";
97 };
98
99 package = mkOption {
100 type = types.path;
101 description = lib.mdDoc "The Dysnomia package";
102 };
103
104 properties = mkOption {
105 description = lib.mdDoc "An attribute set in which each attribute represents a machine property. Optionally, these values can be shell substitutions.";
106 default = {};
107 type = types.attrs;
108 };
109
110 containers = mkOption {
111 description = lib.mdDoc "An attribute set in which each key represents a container and each value an attribute set providing its configuration properties";
112 default = {};
113 type = types.attrsOf types.attrs;
114 };
115
116 components = mkOption {
117 description = lib.mdDoc "An attribute 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";
118 default = {};
119 type = types.attrsOf types.attrs;
120 };
121
122 extraContainerProperties = mkOption {
123 description = lib.mdDoc "An attribute set providing additional container settings in addition to the default properties";
124 default = {};
125 type = types.attrs;
126 };
127
128 extraContainerPaths = mkOption {
129 description = lib.mdDoc "A list of paths containing additional container configurations that are added to the search folders";
130 default = [];
131 type = types.listOf types.path;
132 };
133
134 extraModulePaths = mkOption {
135 description = lib.mdDoc "A list of paths containing additional modules that are added to the search folders";
136 default = [];
137 type = types.listOf types.path;
138 };
139
140 enableLegacyModules = mkOption {
141 type = types.bool;
142 default = true;
143 description = lib.mdDoc "Whether to enable Dysnomia legacy process and wrapper modules";
144 };
145 };
146 };
147
148 config = mkIf cfg.enable {
149
150 environment.etc = {
151 "dysnomia/containers" = {
152 source = containersDir;
153 };
154 "dysnomia/components" = {
155 source = componentsDir;
156 };
157 "dysnomia/properties" = {
158 source = properties;
159 };
160 };
161
162 environment.variables = {
163 DYSNOMIA_STATEDIR = "/var/state/dysnomia-nixos";
164 DYSNOMIA_CONTAINERS_PATH = "${lib.concatMapStrings (containerPath: "${containerPath}:") cfg.extraContainerPaths}/etc/dysnomia/containers";
165 DYSNOMIA_MODULES_PATH = "${lib.concatMapStrings (modulePath: "${modulePath}:") cfg.extraModulePaths}/etc/dysnomia/modules";
166 };
167
168 environment.systemPackages = [ cfg.package ];
169
170 dysnomia.package = pkgs.dysnomia.override (origArgs: dysnomiaFlags // lib.optionalAttrs (cfg.enableLegacyModules) {
171 enableLegacy = builtins.trace ''
172 WARNING: Dysnomia has been configured to use the legacy 'process' and 'wrapper'
173 modules for compatibility reasons! If you rely on these modules, consider
174 migrating to better alternatives.
175
176 More information: https://raw.githubusercontent.com/svanderburg/dysnomia/f65a9a84827bcc4024d6b16527098b33b02e4054/README-legacy.md
177
178 If you have migrated already or don't rely on these Dysnomia modules, you can
179 disable legacy mode with the following NixOS configuration option:
180
181 dysnomia.enableLegacyModules = false;
182
183 In a future version of Dysnomia (and NixOS) the legacy option will go away!
184 '' true;
185 });
186
187 dysnomia.properties = {
188 hostname = config.networking.hostName;
189 inherit (pkgs.stdenv.hostPlatform) system;
190
191 supportedTypes = [
192 "echo"
193 "fileset"
194 "process"
195 "wrapper"
196
197 # These are not base modules, but they are still enabled because they work with technology that are always enabled in NixOS
198 "systemd-unit"
199 "sysvinit-script"
200 "nixos-configuration"
201 ]
202 ++ optional (dysnomiaFlags.enableApacheWebApplication) "apache-webapplication"
203 ++ optional (dysnomiaFlags.enableAxis2WebService) "axis2-webservice"
204 ++ optional (dysnomiaFlags.enableDockerContainer) "docker-container"
205 ++ optional (dysnomiaFlags.enableEjabberdDump) "ejabberd-dump"
206 ++ optional (dysnomiaFlags.enableInfluxDatabase) "influx-database"
207 ++ optional (dysnomiaFlags.enableMySQLDatabase) "mysql-database"
208 ++ optional (dysnomiaFlags.enablePostgreSQLDatabase) "postgresql-database"
209 ++ optional (dysnomiaFlags.enableTomcatWebApplication) "tomcat-webapplication"
210 ++ optional (dysnomiaFlags.enableMongoDatabase) "mongo-database"
211 ++ optional (dysnomiaFlags.enableSubversionRepository) "subversion-repository";
212 };
213
214 dysnomia.containers = lib.recursiveUpdate ({
215 process = {};
216 wrapper = {};
217 }
218 // lib.optionalAttrs (config.services.httpd.enable) { apache-webapplication = {
219 documentRoot = config.services.httpd.virtualHosts.localhost.documentRoot;
220 }; }
221 // lib.optionalAttrs (config.services.tomcat.axis2.enable) { axis2-webservice = {}; }
222 // lib.optionalAttrs (config.services.ejabberd.enable) { ejabberd-dump = {
223 ejabberdUser = config.services.ejabberd.user;
224 }; }
225 // lib.optionalAttrs (config.services.mysql.enable) { mysql-database = {
226 mysqlPort = config.services.mysql.port;
227 mysqlSocket = "/run/mysqld/mysqld.sock";
228 } // lib.optionalAttrs cfg.enableAuthentication {
229 mysqlUsername = "root";
230 };
231 }
232 // lib.optionalAttrs (config.services.postgresql.enable) { postgresql-database = {
233 } // lib.optionalAttrs (cfg.enableAuthentication) {
234 postgresqlUsername = "postgres";
235 };
236 }
237 // lib.optionalAttrs (config.services.tomcat.enable) { tomcat-webapplication = {
238 tomcatPort = 8080;
239 }; }
240 // lib.optionalAttrs (config.services.mongodb.enable) { mongo-database = {}; }
241 // lib.optionalAttrs (config.services.influxdb.enable) {
242 influx-database = {
243 influxdbUsername = config.services.influxdb.user;
244 influxdbDataDir = "${config.services.influxdb.dataDir}/data";
245 influxdbMetaDir = "${config.services.influxdb.dataDir}/meta";
246 };
247 }
248 // lib.optionalAttrs (config.services.svnserve.enable) { subversion-repository = {
249 svnBaseDir = config.services.svnserve.svnBaseDir;
250 }; }) cfg.extraContainerProperties;
251
252 boot.extraSystemdUnitPaths = [ "/etc/systemd-mutable/system" ];
253
254 system.activationScripts.dysnomia = ''
255 mkdir -p /etc/systemd-mutable/system
256 if [ ! -f /etc/systemd-mutable/system/dysnomia.target ]
257 then
258 ( echo "[Unit]"
259 echo "Description=Services that are activated and deactivated by Dysnomia"
260 echo "After=final.target"
261 ) > /etc/systemd-mutable/system/dysnomia.target
262 fi
263 '';
264 };
265}