summaryrefslogtreecommitdiffstats
path: root/nixos/tests/modular-service-etc/python-http-server.nix
blob: 72315831ba944891e1be38ed57e208cf31614621 (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
# Tests in: ./test.nix
# This module provides a basic web server based on the python built-in http.server package.
{
  config,
  lib,
  ...
}:
let
  inherit (lib) mkOption types;
in
{
  _class = "service";

  options = {
    python-http-server = {
      package = mkOption {
        type = types.package;
        description = "Python package to use for the web server";
      };

      port = mkOption {
        type = types.port;
        default = 8000;
        description = "Port to listen on";
      };

      directory = mkOption {
        type = types.str;
        default = config.configData."webroot".path;
        defaultText = lib.literalExpression ''config.configData."webroot".path'';
        description = "Directory to serve files from";
      };
    };
  };

  config = {
    process.argv = [
      "${lib.getExe config.python-http-server.package}"
      "-m"
      "http.server"
      "${toString config.python-http-server.port}"
      "--directory"
      config.python-http-server.directory
    ];

    configData = {
      "webroot" = {
        # Enable only if directory is set to use this path
        enable = lib.mkDefault (config.python-http-server.directory == config.configData."webroot".path);
      };
    };
  };
}