aboutsummaryrefslogtreecommitdiff
path: root/infra/libkookie/nixpkgs/nixos/modules/services/misc/dysnomia.nix
blob: 4b52963500d1b7e14087bb18a0f22e467b434c68 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
{pkgs, lib, config, ...}:

with lib;

let
  cfg = config.dysnomia;

  printProperties = properties:
    concatMapStrings (propertyName:
      let
        property = properties.${propertyName};
      in
      if isList property then "${propertyName}=(${lib.concatMapStrings (elem: "\"${toString elem}\" ") (properties.${propertyName})})\n"
      else "${propertyName}=\"${toString property}\"\n"
    ) (builtins.attrNames properties);

  properties = pkgs.stdenv.mkDerivation {
    name = "dysnomia-properties";
    buildCommand = ''
      cat > $out << "EOF"
      ${printProperties cfg.properties}
      EOF
    '';
  };

  containersDir = pkgs.stdenv.mkDerivation {
    name = "dysnomia-containers";
    buildCommand = ''
      mkdir -p $out
      cd $out

      ${concatMapStrings (containerName:
        let
          containerProperties = cfg.containers.${containerName};
        in
        ''
          cat > ${containerName} <<EOF
          ${printProperties containerProperties}
          type=${containerName}
          EOF
        ''
      ) (builtins.attrNames cfg.containers)}
    '';
  };

  linkMutableComponents = {containerName}:
    ''
      mkdir ${containerName}

      ${concatMapStrings (componentName:
        let
          component = cfg.components.${containerName}.${componentName};
        in
        "ln -s ${component} ${containerName}/${componentName}\n"
      ) (builtins.attrNames (cfg.components.${containerName} or {}))}
    '';

  componentsDir = pkgs.stdenv.mkDerivation {
    name = "dysnomia-components";
    buildCommand = ''
      mkdir -p $out
      cd $out

      ${concatMapStrings (containerName:
        linkMutableComponents { inherit containerName; }
      ) (builtins.attrNames cfg.components)}
    '';
  };
in
{
  options = {
    dysnomia = {

      enable = mkOption {
        type = types.bool;
        default = false;
        description = "Whether to enable Dysnomia";
      };

      enableAuthentication = mkOption {
        type = types.bool;
        default = false;
        description = "Whether to publish privacy-sensitive authentication credentials";
      };

      package = mkOption {
        type = types.path;
        description = "The Dysnomia package";
      };

      properties = mkOption {
        description = "An attribute set in which each attribute represents a machine property. Optionally, these values can be shell substitutions.";
        default = {};
      };

      containers = mkOption {
        description = "An attribute set in which each key represents a container and each value an attribute set providing its configuration properties";
        default = {};
      };

      components = mkOption {
        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";
        default = {};
      };

      extraContainerProperties = mkOption {
        description = "An attribute set providing additional container settings in addition to the default properties";
        default = {};
      };

      extraContainerPaths = mkOption {
        description = "A list of paths containing additional container configurations that are added to the search folders";
        default = [];
      };

      extraModulePaths = mkOption {
        description = "A list of paths containing additional modules that are added to the search folders";
        default = [];
      };
    };
  };

  config = mkIf cfg.enable {

    environment.etc = {
      "dysnomia/containers" = {
        source = containersDir;
      };
      "dysnomia/components" = {
        source = componentsDir;
      };
      "dysnomia/properties" = {
        source = properties;
      };
    };

    environment.variables = {
      DYSNOMIA_STATEDIR = "/var/state/dysnomia-nixos";
      DYSNOMIA_CONTAINERS_PATH = "${lib.concatMapStrings (containerPath: "${containerPath}:") cfg.extraContainerPaths}/etc/dysnomia/containers";
      DYSNOMIA_MODULES_PATH = "${lib.concatMapStrings (modulePath: "${modulePath}:") cfg.extraModulePaths}/etc/dysnomia/modules";
    };

    environment.systemPackages = [ cfg.package ];

    dysnomia.package = pkgs.dysnomia.override (origArgs: {
      enableApacheWebApplication = config.services.httpd.enable;
      enableAxis2WebService = config.services.tomcat.axis2.enable;
      enableEjabberdDump = config.services.ejabberd.enable;
      enableMySQLDatabase = config.services.mysql.enable;
      enablePostgreSQLDatabase = config.services.postgresql.enable;
      enableSubversionRepository = config.services.svnserve.enable;
      enableTomcatWebApplication = config.services.tomcat.enable;
      enableMongoDatabase = config.services.mongodb.enable;
      enableInfluxDatabase = config.services.influxdb.enable;
    });

    dysnomia.properties = {
      hostname = config.networking.hostName;
      inherit (config.nixpkgs.localSystem) system;

      supportedTypes = (import "${pkgs.stdenv.mkDerivation {
        name = "supportedtypes";
        buildCommand = ''
          ( echo -n "[ "
            cd ${cfg.package}/libexec/dysnomia
            for i in *
            do
                echo -n "\"$i\" "
            done
            echo -n " ]") > $out
        '';
      }}");
    };

    dysnomia.containers = lib.recursiveUpdate ({
      process = {};
      wrapper = {};
    }
    // lib.optionalAttrs (config.services.httpd.enable) { apache-webapplication = {
      documentRoot = config.services.httpd.virtualHosts.localhost.documentRoot;
    }; }
    // lib.optionalAttrs (config.services.tomcat.axis2.enable) { axis2-webservice = {}; }
    // lib.optionalAttrs (config.services.ejabberd.enable) { ejabberd-dump = {
      ejabberdUser = config.services.ejabberd.user;
    }; }
    // lib.optionalAttrs (config.services.mysql.enable) { mysql-database = {
        mysqlPort = config.services.mysql.port;
      } // lib.optionalAttrs cfg.enableAuthentication {
        mysqlUsername = "root";
        mysqlPassword = builtins.readFile (config.services.mysql.rootPassword);
      };
    }
    // lib.optionalAttrs (config.services.postgresql.enable) { postgresql-database = {
      } // lib.optionalAttrs (cfg.enableAuthentication) {
        postgresqlUsername = "postgres";
      };
    }
    // lib.optionalAttrs (config.services.tomcat.enable) { tomcat-webapplication = {
      tomcatPort = 8080;
    }; }
    // lib.optionalAttrs (config.services.mongodb.enable) { mongo-database = {}; }
    // lib.optionalAttrs (config.services.svnserve.enable) { subversion-repository = {
      svnBaseDir = config.services.svnserve.svnBaseDir;
    }; }) cfg.extraContainerProperties;

    system.activationScripts.dysnomia = ''
      mkdir -p /etc/systemd-mutable/system
      if [ ! -f /etc/systemd-mutable/system/dysnomia.target ]
      then
          ( echo "[Unit]"
            echo "Description=Services that are activated and deactivated by Dysnomia"
            echo "After=final.target"
          ) > /etc/systemd-mutable/system/dysnomia.target
      fi
    '';
  };
}