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