1 { config, lib, pkgs, ... }:
4 cfg = config.services.static-web-server;
5 toml = pkgs.formats.toml {};
6 configFilePath = toml.generate "config.toml" cfg.configuration;
9 services.static-web-server = {
10 enable = lib.mkEnableOption ''Static Web Server'';
11 listen = lib.mkOption {
12 default = "[::]:8787";
15 The "ListenStream" used in static-web-server.socket.
16 This is equivalent to SWS's "host" and "port" options.
17 See here for specific syntax: <https://www.freedesktop.org/software/systemd/man/systemd.socket.html#ListenStream=>
21 type = lib.types.path;
23 The location of files for SWS to serve. Equivalent to SWS's "root" config value.
24 NOTE: This folder must exist before starting SWS.
27 configuration = lib.mkOption {
31 general = { log-level = "error"; directory-listing = true; };
34 Configuration for Static Web Server. See
35 <https://static-web-server.net/configuration/config-file/>.
36 NOTE: Don't set "host", "port", or "root" here. They will be ignored.
37 Use the top-level "listen" and "root" options instead.
43 config = lib.mkIf cfg.enable {
44 environment.systemPackages = [ pkgs.static-web-server ];
45 systemd.packages = [ pkgs.static-web-server ];
46 # Have to set wantedBy since systemd.packages ignores the "Install" section
47 systemd.sockets.static-web-server = {
48 wantedBy = [ "sockets.target" ];
49 # Start with empty string to reset upstream option
50 listenStreams = [ "" cfg.listen ];
52 systemd.services.static-web-server = {
53 wantedBy = [ "multi-user.target" ];
55 # Remove upstream sample environment file; use config.toml exclusively
56 EnvironmentFile = [ "" ];
57 ExecStart = [ "" "${pkgs.static-web-server}/bin/static-web-server --fd 0 --config-file ${configFilePath} --root ${cfg.root}" ];
58 # Supplementary groups doesn't work unless we create the group ourselves
59 SupplementaryGroups = [ "" ];
60 # If the user is serving files from their home dir, override ProtectHome to allow that
61 ProtectHome = if lib.hasPrefix "/home" cfg.root then "tmpfs" else "true";
62 BindReadOnlyPaths = cfg.root;
67 meta.maintainers = with lib.maintainers; [ mac-chaffee ];