1# Tests in: ./test.nix
2# This module provides a basic web server based on the python built-in http.server package.
3{
4 config,
5 lib,
6 ...
7}:
8let
9 inherit (lib) mkOption types;
10in
11{
12 _class = "service";
13
14 options = {
15 python-http-server = {
16 package = mkOption {
17 type = types.package;
18 description = "Python package to use for the web server";
19 };
20
21 port = mkOption {
22 type = types.port;
23 default = 8000;
24 description = "Port to listen on";
25 };
26
27 directory = mkOption {
28 type = types.str;
29 default = config.configData."webroot".path;
30 defaultText = lib.literalExpression ''config.configData."webroot".path'';
31 description = "Directory to serve files from";
32 };
33 };
34 };
35
36 config = {
37 process.argv = [
38 "${lib.getExe config.python-http-server.package}"
39 "-m"
40 "http.server"
41 "${toString config.python-http-server.port}"
42 "--directory"
43 config.python-http-server.directory
44 ];
45
46 configData = {
47 "webroot" = {
48 # Enable only if directory is set to use this path
49 enable = lib.mkDefault (config.python-http-server.directory == config.configData."webroot".path);
50 };
51 };
52 };
53}