Writing my own local httpd subservice

I’d like to locally install moodle, which is written in PHP and, by its installation guide, is supposed to be served via httpd.

It would be awesome to have a Nix derivation to download and use it, possibly as a httpd subservice, similar to mediawiki in nixpkgs. However, I’m not sure how define that and just use it locally (i.e. instead of adding it “globally” and using my own version of nixpkgs).

Does anyone have experience with that? Is there any more documentation than that in the nixpkgs manual and the nixos manual?

The httpd.extraSubservices code hasn’t been maintained in a long time. I’m in the process of rewriting all of the httpd subservices as full NixOS modules. After I finish rewriting them all as modules I’m going to create a PR to deprecate httpd subservices entirely.

If you want to manage moodle the NixOS way I suggest a module and package. That being said… a coworker had mentioned to me recently a clients possible need for a moodle instance and I was asked how much work it would be to create such a module. I’ll see what I can whip together in the next couple weeks.

@pimiddy I have created a PR to add moodle to NixOS: https://github.com/NixOS/nixpkgs/pull/63634

I’m assuming you are familiar enough with moodle to adequately test this. I have never used moodle before so my testing was rather limited. You can see a minimal example in the test (nixos/tests/moodle.nix), but I’ll also include a full example here as well.

services.moodle = {
  hostName = "moodle.example.org";
  enableSSL = true;
  adminAddr = "webmaster@example.org";
  sslServerCert = "/var/lib/acme/moodle.example.org/full.pem";
  sslServerKey = "/var/lib/acme/moodle.example.org/key.pem";
};
# used for both ssl cert and https redirect
services.httpd.virtualHosts = [
  { hostName = "moodle.example.org";
    servedDirs = [
      { dir = "/var/run/acme-challenges/.well-known/acme-challenge";
        urlPath = "/.well-known/acme-challenge";
      }
    ];
    extraConfig = ''
      RewriteEngine On
      RewriteCond %{REQUEST_URI} !^/\.well\-known/
      RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
    '';
  }
];
security.acme.certs = {
  "moodle.example.org" =
    { email = "webmaster@example.org";
      postRun = "systemctl reload httpd.service";
      webroot = "/var/run/acme-challenges";
    };
};

@pimiddy If you have an account on github please comment on the PR I created with your results for testing, otherwise feel free to comment here.

On a slightly unrelated note this example demonstrates how far behind nginx the httpd service has fallen. PRs to follow, I guess.

1 Like

@aanderse Thanks for your work! I’d love to test this. However, I’m kind of a NixOS n00b still, so could you explain how I could easily test this new service?

What version of NixOS are you running?

I’m using nixos-unstable.

One quick and dirty way would be to git clone my branch and include the relevant files. To do so you can run the following:

git clone https://github.com/aanderse/nixpkgs.git
git checkout moodle

And then add the following to your configuration.nix:

imports = [
  /home/pimiddy/nixpkgs/nixos/modules/services/web-apps/moodle.nix
];
nixpkgs.overlays = [
  (self: super: {
    moodle = super.callPackage /home/pimiddy/nixpkgs/pkgs/servers/web-apps/moodle {};
  })
];

As well as the configuration listed above. Please let me know if any part of this isn’t clear or there is any further information I can provide.