summary refs log tree commit diff
path: root/nixos/modules/services/misc/disnix.nix
blob: 0534c4fc942d9616613a04ab936dd529840e4ec9 (plain) (blame)
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
# Disnix server
{ config, lib, pkgs, ... }:

with lib;

let

  cfg = config.services.disnix;

  dysnomia = 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;
  });
in

{

  ###### interface

  options = {

    services.disnix = {

      enable = mkOption {
        default = false;
        description = "Whether to enable Disnix";
      };

      useWebServiceInterface = mkOption {
        default = false;
        description = "Whether to enable the DisnixWebService interface running on Apache Tomcat";
      };

      publishInfrastructure = {
        enable = mkOption {
          default = false;
          description = "Whether to publish capabilities/properties of this machine in as attributes in the infrastructure option";
        };

        enableAuthentication = mkOption {
          default = false;
          description = "Whether to publish authentication credentials through the infrastructure attribute (not recommended in combination with Avahi)";
        };
      };

      infrastructure = mkOption {
        default = {};
        description = "List of name value pairs containing properties for the infrastructure model";
      };

      publishAvahi = mkOption {
        default = false;
        description = "Whether to publish capabilities/properties as a Disnix service through Avahi";
      };

    };

  };


  ###### implementation

  config = mkIf cfg.enable {
    environment.systemPackages = [ pkgs.disnix pkgs.dysnomia ] ++ optional cfg.useWebServiceInterface pkgs.DisnixWebService;

    services.dbus.enable = true;
    services.dbus.packages = [ pkgs.disnix ];

    services.avahi.enable = cfg.publishAvahi;

    services.tomcat.enable = cfg.useWebServiceInterface;
    services.tomcat.extraGroups = [ "disnix" ];
    services.tomcat.javaOpts = "${optionalString cfg.useWebServiceInterface "-Djava.library.path=${pkgs.libmatthew_java}/lib/jni"} ";
    services.tomcat.sharedLibs = optional cfg.useWebServiceInterface "${pkgs.DisnixWebService}/share/java/DisnixConnection.jar"
                                 ++ optional cfg.useWebServiceInterface "${pkgs.dbus_java}/share/java/dbus.jar";
    services.tomcat.webapps = optional cfg.useWebServiceInterface pkgs.DisnixWebService;

    users.extraGroups = singleton
      { name = "disnix";
        gid = config.ids.gids.disnix;
      };

    services.disnix.infrastructure =
      optionalAttrs (cfg.publishInfrastructure.enable)
      ( { hostname = config.networking.hostName;
          #targetHost = config.deployment.targetHost;
          system = if config.nixpkgs.system == "" then builtins.currentSystem else config.nixpkgs.system;
          
          supportedTypes = (import "${pkgs.stdenv.mkDerivation {
            name = "supportedtypes";
            buildCommand = ''
              ( echo -n "[ "
                cd ${dysnomia}/libexec/dysnomia
                for i in *
                do
                    echo -n "\"$i\" "
                done
                echo -n " ]") > $out
            '';
          }}");
        }
        #// optionalAttrs (cfg.useWebServiceInterface) { targetEPR = "http://${config.deployment.targetHost}:8080/DisnixWebService/services/DisnixWebService"; }
        // optionalAttrs (config.services.httpd.enable) { documentRoot = config.services.httpd.documentRoot; }
        // optionalAttrs (config.services.mysql.enable) { mysqlPort = config.services.mysql.port; }
        // optionalAttrs (config.services.tomcat.enable) { tomcatPort = 8080; }
        // optionalAttrs (config.services.svnserve.enable) { svnBaseDir = config.services.svnserve.svnBaseDir; }
        // optionalAttrs (cfg.publishInfrastructure.enableAuthentication) (
          optionalAttrs (config.services.mysql.enable) { mysqlUsername = "root"; mysqlPassword = readFile config.services.mysql.rootPassword; })
        )
    ;

    services.disnix.publishInfrastructure.enable = cfg.publishAvahi;

    jobs = {
      disnix =
        { description = "Disnix server";
        
          wants = [ "dysnomia.target" ];
          wantedBy = [ "multi-user.target" ];
          after = [ "dbus.service" ]
            ++ optional config.services.httpd.enable "httpd.service"
            ++ optional config.services.mysql.enable "mysql.service"
            ++ optional config.services.postgresql.enable "postgresql.service"
            ++ optional config.services.tomcat.enable "tomcat.service"
            ++ optional config.services.svnserve.enable "svnserve.service"
            ++ optional config.services.mongodb.enable "mongodb.service";

          restartIfChanged = false;
          
          path = [ pkgs.nix pkgs.disnix dysnomia "/run/current-system/sw" ];
          
          environment = {
            HOME = "/root";
          };
          
          preStart = ''
            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
          '';

          exec = "disnix-service";
        };
    } // optionalAttrs cfg.publishAvahi {
      disnixAvahi =
        { description = "Disnix Avahi publisher";

          startOn = "started avahi-daemon";

          exec =
          ''
            ${pkgs.avahi}/bin/avahi-publish-service disnix-${config.networking.hostName} _disnix._tcp 22 \
              "mem=$(grep 'MemTotal:' /proc/meminfo | sed -e 's/kB//' -e 's/MemTotal://' -e 's/ //g')" \
              ${concatMapStrings (infrastructureAttrName:
                let infrastructureAttrValue = getAttr infrastructureAttrName (cfg.infrastructure);
                in
                if isInt infrastructureAttrValue then
                ''${infrastructureAttrName}=${toString infrastructureAttrValue} \
                ''
                else
                ''${infrastructureAttrName}=\"${infrastructureAttrValue}\" \
                ''
                ) (attrNames (cfg.infrastructure))}
          '';
        };
    };
  };
}