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 let
66 components = cfg.components."${containerName}";
67 in
68 linkMutableComponents { inherit containerName; }
69 ) (builtins.attrNames cfg.components)}
70 '';
71 };
72in
73{
74 options = {
75 dysnomia = {
76
77 enable = mkOption {
78 type = types.bool;
79 default = false;
80 description = "Whether to enable Dysnomia";
81 };
82
83 enableAuthentication = mkOption {
84 type = types.bool;
85 default = false;
86 description = "Whether to publish privacy-sensitive authentication credentials";
87 };
88
89 package = mkOption {
90 type = types.path;
91 description = "The Dysnomia package";
92 };
93
94 properties = mkOption {
95 description = "An attribute set in which each attribute represents a machine property. Optionally, these values can be shell substitutions.";
96 default = {};
97 };
98
99 containers = mkOption {
100 description = "An attribute set in which each key represents a container and each value an attribute set providing its configuration properties";
101 default = {};
102 };
103
104 components = mkOption {
105 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";
106 default = {};
107 };
108
109 extraContainerProperties = mkOption {
110 description = "An attribute set providing additional container settings in addition to the default properties";
111 default = {};
112 };
113
114 extraContainerPaths = mkOption {
115 description = "A list of paths containing additional container configurations that are added to the search folders";
116 default = [];
117 };
118
119 extraModulePaths = mkOption {
120 description = "A list of paths containing additional modules that are added to the search folders";
121 default = [];
122 };
123 };
124 };
125
126 config = mkIf cfg.enable {
127
128 environment.etc = {
129 "dysnomia/containers" = {
130 source = containersDir;
131 };
132 "dysnomia/components" = {
133 source = componentsDir;
134 };
135 "dysnomia/properties" = {
136 source = properties;
137 };
138 };
139
140 environment.variables = {
141 DYSNOMIA_STATEDIR = "/var/state/dysnomia-nixos";
142 DYSNOMIA_CONTAINERS_PATH = "${lib.concatMapStrings (containerPath: "${containerPath}:") cfg.extraContainerPaths}/etc/dysnomia/containers";
143 DYSNOMIA_MODULES_PATH = "${lib.concatMapStrings (modulePath: "${modulePath}:") cfg.extraModulePaths}/etc/dysnomia/modules";
144 };
145
146 environment.systemPackages = [ cfg.package ];
147
148 dysnomia.package = pkgs.dysnomia.override (origArgs: {
149 enableApacheWebApplication = config.services.httpd.enable;
150 enableAxis2WebService = config.services.tomcat.axis2.enable;
151 enableEjabberdDump = config.services.ejabberd.enable;
152 enableMySQLDatabase = config.services.mysql.enable;
153 enablePostgreSQLDatabase = config.services.postgresql.enable;
154 enableSubversionRepository = config.services.svnserve.enable;
155 enableTomcatWebApplication = config.services.tomcat.enable;
156 enableMongoDatabase = config.services.mongodb.enable;
157 });
158
159 dysnomia.properties = {
160 hostname = config.networking.hostName;
161 system = if config.nixpkgs.system == "" then builtins.currentSystem else config.nixpkgs.system;
162
163 supportedTypes = (import "${pkgs.stdenv.mkDerivation {
164 name = "supportedtypes";
165 buildCommand = ''
166 ( echo -n "[ "
167 cd ${cfg.package}/libexec/dysnomia
168 for i in *
169 do
170 echo -n "\"$i\" "
171 done
172 echo -n " ]") > $out
173 '';
174 }}");
175 };
176
177 dysnomia.containers = lib.recursiveUpdate ({
178 process = {};
179 wrapper = {};
180 }
181 // lib.optionalAttrs (config.services.httpd.enable) { apache-webapplication = {
182 documentRoot = config.services.httpd.documentRoot;
183 }; }
184 // lib.optionalAttrs (config.services.tomcat.axis2.enable) { axis2-webservice = {}; }
185 // lib.optionalAttrs (config.services.ejabberd.enable) { ejabberd-dump = {
186 ejabberdUser = config.services.ejabberd.user;
187 }; }
188 // lib.optionalAttrs (config.services.mysql.enable) { mysql-database = {
189 mysqlPort = config.services.mysql.port;
190 } // lib.optionalAttrs cfg.enableAuthentication {
191 mysqlUsername = "root";
192 mysqlPassword = builtins.readFile (config.services.mysql.rootPassword);
193 };
194 }
195 // lib.optionalAttrs (config.services.postgresql.enable && cfg.enableAuthentication) { postgresql-database = {
196 postgresqlUsername = "root";
197 }; }
198 // lib.optionalAttrs (config.services.tomcat.enable) { tomcat-webapplication = {
199 tomcatPort = 8080;
200 }; }
201 // lib.optionalAttrs (config.services.mongodb.enable) { mongo-database = {}; }
202 // lib.optionalAttrs (config.services.svnserve.enable) { subversion-repository = {
203 svnBaseDir = config.services.svnserve.svnBaseDir;
204 }; }) cfg.extraContainerProperties;
205
206 system.activationScripts.dysnomia = ''
207 mkdir -p /etc/systemd-mutable/system
208 if [ ! -f /etc/systemd-mutable/system/dysnomia.target ]
209 then
210 ( echo "[Unit]"
211 echo "Description=Services that are activated and deactivated by Dysnomia"
212 echo "After=final.target"
213 ) > /etc/systemd-mutable/system/dysnomia.target
214 fi
215 '';
216 };
217}