1# Disnix server 2{ config, lib, pkgs, ... }: 3 4with lib; 5 6let 7 8 cfg = config.services.disnix; 9 10 dysnomia = pkgs.dysnomia.override (origArgs: { 11 enableApacheWebApplication = config.services.httpd.enable; 12 enableAxis2WebService = config.services.tomcat.axis2.enable; 13 enableEjabberdDump = config.services.ejabberd.enable; 14 enableMySQLDatabase = config.services.mysql.enable; 15 enablePostgreSQLDatabase = config.services.postgresql.enable; 16 enableSubversionRepository = config.services.svnserve.enable; 17 enableTomcatWebApplication = config.services.tomcat.enable; 18 enableMongoDatabase = config.services.mongodb.enable; 19 }); 20in 21 22{ 23 24 ###### interface 25 26 options = { 27 28 services.disnix = { 29 30 enable = mkOption { 31 default = false; 32 description = "Whether to enable Disnix"; 33 }; 34 35 useWebServiceInterface = mkOption { 36 default = false; 37 description = "Whether to enable the DisnixWebService interface running on Apache Tomcat"; 38 }; 39 40 publishInfrastructure = { 41 enable = mkOption { 42 default = false; 43 description = "Whether to publish capabilities/properties of this machine in as attributes in the infrastructure option"; 44 }; 45 46 enableAuthentication = mkOption { 47 default = false; 48 description = "Whether to publish authentication credentials through the infrastructure attribute (not recommended in combination with Avahi)"; 49 }; 50 }; 51 52 infrastructure = mkOption { 53 default = {}; 54 description = "List of name value pairs containing properties for the infrastructure model"; 55 }; 56 57 publishAvahi = mkOption { 58 default = false; 59 description = "Whether to publish capabilities/properties as a Disnix service through Avahi"; 60 }; 61 62 }; 63 64 }; 65 66 67 ###### implementation 68 69 config = mkIf cfg.enable { 70 environment.systemPackages = [ pkgs.disnix pkgs.dysnomia ] ++ optional cfg.useWebServiceInterface pkgs.DisnixWebService; 71 72 services.dbus.enable = true; 73 services.dbus.packages = [ pkgs.disnix ]; 74 75 services.avahi.enable = cfg.publishAvahi; 76 77 services.tomcat.enable = cfg.useWebServiceInterface; 78 services.tomcat.extraGroups = [ "disnix" ]; 79 services.tomcat.javaOpts = "${optionalString cfg.useWebServiceInterface "-Djava.library.path=${pkgs.libmatthew_java}/lib/jni"} "; 80 services.tomcat.sharedLibs = optional cfg.useWebServiceInterface "${pkgs.DisnixWebService}/share/java/DisnixConnection.jar" 81 ++ optional cfg.useWebServiceInterface "${pkgs.dbus_java}/share/java/dbus.jar"; 82 services.tomcat.webapps = optional cfg.useWebServiceInterface pkgs.DisnixWebService; 83 84 users.extraGroups = singleton 85 { name = "disnix"; 86 gid = config.ids.gids.disnix; 87 }; 88 89 services.disnix.infrastructure = 90 optionalAttrs (cfg.publishInfrastructure.enable) 91 ( { hostname = config.networking.hostName; 92 #targetHost = config.deployment.targetHost; 93 system = if config.nixpkgs.system == "" then builtins.currentSystem else config.nixpkgs.system; 94 95 supportedTypes = (import "${pkgs.stdenv.mkDerivation { 96 name = "supportedtypes"; 97 buildCommand = '' 98 ( echo -n "[ " 99 cd ${dysnomia}/libexec/dysnomia 100 for i in * 101 do 102 echo -n "\"$i\" " 103 done 104 echo -n " ]") > $out 105 ''; 106 }}"); 107 } 108 #// optionalAttrs (cfg.useWebServiceInterface) { targetEPR = "http://${config.deployment.targetHost}:8080/DisnixWebService/services/DisnixWebService"; } 109 // optionalAttrs (config.services.httpd.enable) { documentRoot = config.services.httpd.documentRoot; } 110 // optionalAttrs (config.services.mysql.enable) { mysqlPort = config.services.mysql.port; } 111 // optionalAttrs (config.services.tomcat.enable) { tomcatPort = 8080; } 112 // optionalAttrs (config.services.svnserve.enable) { svnBaseDir = config.services.svnserve.svnBaseDir; } 113 // optionalAttrs (config.services.ejabberd.enable) { ejabberdUser = config.services.ejabberd.user; } 114 // optionalAttrs (cfg.publishInfrastructure.enableAuthentication) ( 115 optionalAttrs (config.services.mysql.enable) { mysqlUsername = "root"; mysqlPassword = readFile config.services.mysql.rootPassword; }) 116 ) 117 ; 118 119 services.disnix.publishInfrastructure.enable = cfg.publishAvahi; 120 121 systemd.services = { 122 disnix = { 123 description = "Disnix server"; 124 wants = [ "dysnomia.target" ]; 125 wantedBy = [ "multi-user.target" ]; 126 after = [ "dbus.service" ] 127 ++ optional config.services.httpd.enable "httpd.service" 128 ++ optional config.services.mysql.enable "mysql.service" 129 ++ optional config.services.postgresql.enable "postgresql.service" 130 ++ optional config.services.tomcat.enable "tomcat.service" 131 ++ optional config.services.svnserve.enable "svnserve.service" 132 ++ optional config.services.mongodb.enable "mongodb.service"; 133 134 restartIfChanged = false; 135 136 path = [ pkgs.nix pkgs.disnix dysnomia "/run/current-system/sw" ]; 137 138 environment = { 139 HOME = "/root"; 140 }; 141 142 preStart = '' 143 mkdir -p /etc/systemd-mutable/system 144 if [ ! -f /etc/systemd-mutable/system/dysnomia.target ] 145 then 146 ( echo "[Unit]" 147 echo "Description=Services that are activated and deactivated by Dysnomia" 148 echo "After=final.target" 149 ) > /etc/systemd-mutable/system/dysnomia.target 150 fi 151 ''; 152 153 script = "disnix-service"; 154 }; 155 } // optionalAttrs cfg.publishAvahi { 156 disnixAvahi = { 157 description = "Disnix Avahi publisher"; 158 wants = [ "avahi-daemon.service" ]; 159 wantedBy = [ "multi-user.target" ]; 160 161 script = '' 162 ${pkgs.avahi}/bin/avahi-publish-service disnix-${config.networking.hostName} _disnix._tcp 22 \ 163 "mem=$(grep 'MemTotal:' /proc/meminfo | sed -e 's/kB//' -e 's/MemTotal://' -e 's/ //g')" \ 164 ${concatMapStrings (infrastructureAttrName: 165 let infrastructureAttrValue = getAttr infrastructureAttrName (cfg.infrastructure); 166 in 167 if isInt infrastructureAttrValue then 168 ''${infrastructureAttrName}=${toString infrastructureAttrValue} \ 169 '' 170 else 171 ''${infrastructureAttrName}=\"${infrastructureAttrValue}\" \ 172 '' 173 ) (attrNames (cfg.infrastructure))} 174 ''; 175 }; 176 }; 177 }; 178}