Unverified Commit a86a7daf authored by figsoda's avatar figsoda Committed by GitHub
Browse files

Merge pull request #226977 from mac-chaffee/sws-module

parents 4b8e99cb 61cb4170
Loading
Loading
Loading
Loading
+5 −0
Original line number Diff line number Diff line
@@ -9841,6 +9841,11 @@
      fingerprint = "1147 43F1 E707 6F3E 6F4B  2C96 B9A8 B592 F126 F8E8";
    }];
  };
  mac-chaffee = {
    name = "Mac Chaffee";
    github = "mac-chaffee";
    githubId = 7581860;
  };
  maddiethecafebabe = {
    email = "maddie@cafebabe.date";
    github = "maddiethecafebabe";
+1 −0
Original line number Diff line number Diff line
@@ -1282,6 +1282,7 @@
  ./services/web-servers/pomerium.nix
  ./services/web-servers/rustus.nix
  ./services/web-servers/stargazer.nix
  ./services/web-servers/static-web-server.nix
  ./services/web-servers/tomcat.nix
  ./services/web-servers/traefik.nix
  ./services/web-servers/trafficserver/default.nix
+68 −0
Original line number Diff line number Diff line
{ config, lib, pkgs, ... }:

let
  cfg = config.services.static-web-server;
  toml = pkgs.formats.toml {};
  configFilePath = toml.generate "config.toml" cfg.configuration;
in {
  options = {
    services.static-web-server = {
      enable = lib.mkEnableOption (lib.mdDoc ''Static Web Server'');
      listen = lib.mkOption {
        default = "[::]:8787";
        type = lib.types.str;
        description = lib.mdDoc ''
          The "ListenStream" used in static-web-server.socket.
          This is equivalent to SWS's "host" and "port" options.
          See here for specific syntax: <https://www.freedesktop.org/software/systemd/man/systemd.socket.html#ListenStream=>
        '';
      };
      root = lib.mkOption {
        type = lib.types.path;
        description = lib.mdDoc ''
          The location of files for SWS to serve. Equivalent to SWS's "root" config value.
          NOTE: This folder must exist before starting SWS.
        '';
      };
      configuration = lib.mkOption {
        default = { };
        type = toml.type;
        example = {
          general = { log-level = "error"; directory-listing = true; };
        };
        description = lib.mdDoc ''
          Configuration for Static Web Server. See
          <https://static-web-server.net/configuration/config-file/>.
          NOTE: Don't set "host", "port", or "root" here. They will be ignored.
          Use the top-level "listen" and "root" options instead.
        '';
      };
    };
  };

  config = lib.mkIf cfg.enable {
    environment.systemPackages = [ pkgs.static-web-server ];
    systemd.packages = [ pkgs.static-web-server ];
    # Have to set wantedBy since systemd.packages ignores the "Install" section
    systemd.sockets.static-web-server = {
      wantedBy = [ "sockets.target" ];
      # Start with empty string to reset upstream option
      listenStreams = [ "" cfg.listen ];
    };
    systemd.services.static-web-server = {
      wantedBy = [ "multi-user.target" ];
      serviceConfig = {
        # Remove upstream sample environment file; use config.toml exclusively
        EnvironmentFile = [ "" ];
        ExecStart = [ "" "${pkgs.static-web-server}/bin/static-web-server --fd 0 --config-file ${configFilePath} --root ${cfg.root}" ];
        # Supplementary groups doesn't work unless we create the group ourselves
        SupplementaryGroups = [ "" ];
        # If the user is serving files from their home dir, override ProtectHome to allow that
        ProtectHome = if lib.hasPrefix "/home" cfg.root then "tmpfs" else "true";
        BindReadOnlyPaths = cfg.root;
      };
    };
  };

  meta.maintainers = with lib.maintainers; [ mac-chaffee ];
}
+1 −0
Original line number Diff line number Diff line
@@ -709,6 +709,7 @@ in {
  sssd-ldap = handleTestOn ["x86_64-linux"] ./sssd-ldap.nix {};
  stargazer = runTest ./web-servers/stargazer.nix;
  starship = handleTest ./starship.nix {};
  static-web-server = handleTest ./web-servers/static-web-server.nix {};
  step-ca = handleTestOn ["x86_64-linux"] ./step-ca.nix {};
  stratis = handleTest ./stratis {};
  strongswan-swanctl = handleTest ./strongswan-swanctl.nix {};
+32 −0
Original line number Diff line number Diff line
import ../make-test-python.nix ({ pkgs, lib, ... } : {
  name = "static-web-server";
  meta = {
    maintainers = with lib.maintainers; [ mac-chaffee ];
  };

  nodes.machine = { pkgs, ... }: {
    services.static-web-server = {
      enable = true;
      listen = "[::]:8080";
      root = toString (pkgs.writeTextDir "nixos-test.html" ''
        <h1>Hello NixOS!</h1>
      '');
      configuration = {
        general = { directory-listing = true; };
      };
    };
  };

  testScript = ''
    machine.start()
    machine.wait_for_unit("static-web-server.socket")
    machine.wait_for_open_port(8080)
    # We don't use wait_until_succeeds() because we're testing socket
    # activation which better work on the first request
    response = machine.succeed("curl -fsS localhost:8080")
    assert "nixos-test.html" in response, "The directory listing page did not include a link to our nixos-test.html file"
    response = machine.succeed("curl -fsS localhost:8080/nixos-test.html")
    assert "Hello NixOS!" in response
    machine.wait_for_unit("static-web-server.service")
  '';
})
Loading