summary refs log tree commit diff
path: root/nixos/modules/services/web-servers/apache-httpd/moodle.nix
blob: 84c8281ecd8d5425af1730b05ffea6ae6fc905b2 (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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
{ config, lib, pkgs, serverInfo, php, ... }:

with lib;

let

  httpd = serverInfo.serverConfig.package;

  version24 = !versionOlder httpd.version "2.4";

  allGranted = if version24 then ''
    Require all granted
  '' else ''
    Order allow,deny
    Allow from all
  '';

  moodleConfig = pkgs.writeText "config.php"
    ''
      <?php
      unset($CFG);
      global $CFG;
      $CFG = new stdClass();
      $CFG->dbtype    = '${config.dbType}';
      $CFG->dblibrary = 'native';
      $CFG->dbhost    = '${config.dbHost}';
      $CFG->dbname    = '${config.dbName}';
      $CFG->dbuser    = '${config.dbUser}';
      $CFG->dbpass    = '${config.dbPassword}';
      $CFG->prefix    = '${config.dbPrefix}';
      $CFG->dboptions = array(
          'dbpersist' => false,
          'dbsocket'  => false,
          'dbport'    => "${config.dbPort}",
      );
      $CFG->wwwroot   = '${config.wwwRoot}';
      $CFG->dataroot  = '${config.dataRoot}';
      $CFG->directorypermissions = 02777;
      $CFG->admin = 'admin';
      ${optionalString (config.debug.noEmailEver == true) ''
        $CFG->noemailever = true;
      ''}

      ${config.extraConfig}
      require_once(dirname(__FILE__) . '/lib/setup.php'); // Do not edit
    '';
  # Unpack Moodle and put the config file in its root directory.
  moodleRoot = pkgs.stdenv.mkDerivation rec {
    name= "moodle-2.8.5";

    src = pkgs.fetchurl {
      url = "https://download.moodle.org/stable28/${name}.tgz";
      sha256 = "1a159a193010cddedce10ee009184502e6f732e4d7c85167d8597fe5dff9e190";
    };

    buildPhase =
      ''
      ''; 

    installPhase =
      ''
        mkdir -p $out
        cp -r * $out
        cp ${moodleConfig} $out/config.php
      '';
  };

in

{

  extraConfig =
  ''
    # this should be config.urlPrefix instead of /
    Alias / ${moodleRoot}/
    <Directory ${moodleRoot}>
      DirectoryIndex index.php
    </Directory>
  '';

  documentRoot = moodleRoot; # TODO: fix this, should be config.urlPrefix

  enablePHP = true;

  options = {

    id = mkOption {
      default = "main";
      description = ''
        A unique identifier necessary to keep multiple Moodle server
        instances on the same machine apart.
      '';
    };

    dbType = mkOption {
      default = "postgres";
      example = "mysql";
      description = "Database type.";
    };

    dbName = mkOption {
      default = "moodle";
      description = "Name of the database that holds the Moodle data.";
    };

    dbHost = mkOption {
      default = "localhost";
      example = "10.0.2.2";
      description = ''
        The location of the database server.
      '';
    };

    dbPort = mkOption {
      default = ""; # use the default port
      example = "12345";
      description = ''
        The port that is used to connect to the database server.
      '';
    };

    dbUser = mkOption {
      default = "moodle";
      description = "The user name for accessing the database.";
    };

    dbPassword = mkOption {
      default = "";
      example = "password";
      description = ''
        The password of the database user.  Warning: this is stored in
        cleartext in the Nix store!
      '';
    };
    
    dbPrefix = mkOption {
      default = "mdl_";
      example = "my_other_mdl_";
      description = ''
        A prefix for each table, if multiple moodles should run in a single database.
      '';
    };

    wwwRoot = mkOption {
      type = types.string;
      example = "http://my.machine.com/my-moodle";
      description = ''
        The full web address where moodle has been installed.
      '';
    };

    dataRoot = mkOption {
      default = "/var/lib/moodledata";
      example = "/var/lib/moodledata";
      description = ''
        The data directory for moodle. Needs to be writable!
      '';
      type = types.path;
      };

    
    extraConfig = mkOption {
      default = "";
      example =
        ''
        '';
      description = ''
        Any additional text to be appended to Moodle's
        configuration file.  This is a PHP script.
      '';
    };

    debug = {
      noEmailEver = mkOption {
        default = false;
	example = "true";
	description = ''
	  Set this to true to prevent Moodle from ever sending any email.
	'';
	};
    };
  };

  startupScript = pkgs.writeScript "moodle_startup.sh" ''
  echo "Checking for existence of ${config.dataRoot}"
  if [ ! -e "${config.dataRoot}" ]
  then
    mkdir -p "${config.dataRoot}"
    chown ${serverInfo.serverConfig.user}.${serverInfo.serverConfig.group} "${config.dataRoot}"
  fi
  '';

}