Skip to content

Commit

Permalink
nginx: service init (#69)
Browse files Browse the repository at this point in the history
  • Loading branch information
shivaraj-bh authored Dec 22, 2023
1 parent e93e3ac commit 8cd80d7
Show file tree
Hide file tree
Showing 4 changed files with 142 additions and 0 deletions.
1 change: 1 addition & 0 deletions nix/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ in
./apache-kafka.nix
./elasticsearch.nix
./mysql.nix
./nginx.nix
./postgres.nix
./redis-cluster.nix
./redis.nix
Expand Down
122 changes: 122 additions & 0 deletions nix/nginx.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
# Based on: https://github.com/cachix/devenv/blob/main/src/modules/services/nginx.nix
{ pkgs, lib, name, config, ... }:
let
inherit (lib) types;
configFile = pkgs.writeText "nginx.conf" ''
pid ${config.dataDir}/nginx/nginx.pid;
error_log stderr debug;
daemon off;
events {
${config.eventsConfig}
}
http {
access_log off;
client_body_temp_path ${config.dataDir}/nginx/;
proxy_temp_path ${config.dataDir}/nginx/;
fastcgi_temp_path ${config.dataDir}/nginx/;
scgi_temp_path ${config.dataDir}/nginx/;
uwsgi_temp_path ${config.dataDir}/nginx/;
include ${config.defaultMimeTypes};
server {
listen ${builtins.toString config.port};
}
${config.httpConfig}
}
'';
in

{
options = {
enable = lib.mkEnableOption "nginx";

package = lib.mkOption {
type = lib.types.package;
default = pkgs.nginx;
defaultText = "pkgs.nginx";
description = "The nginx package to use.";
};

dataDir = lib.mkOption {
type = types.str;
default = "./data/${name}";
description = "The nginx data directory";
};

port = lib.mkOption {
type = types.port;
default = 8080;
description = ''
The TCP port to accept connections.
'';
};

defaultMimeTypes = lib.mkOption {
type = lib.types.path;
default = "${pkgs.mailcap}/etc/nginx/mime.types";
defaultText = lib.literalExpression "$''{pkgs.mailcap}/etc/nginx/mime.types";
example = lib.literalExpression "$''{pkgs.nginx}/conf/mime.types";
description = lib.mdDoc ''
Default MIME types for NGINX, as MIME types definitions from NGINX are very incomplete,
we use by default the ones bundled in the mailcap package, used by most of the other
Linux distributions.
'';
};

httpConfig = lib.mkOption {
type = lib.types.lines;
default = "";
description = "The nginx configuration.";
};

eventsConfig = lib.mkOption {
type = lib.types.lines;
default = "";
description = "The nginx events configuration.";
};

configFile = lib.mkOption {
type = lib.types.path;
default = configFile;
internal = true;
description = "The nginx configuration file.";
};

outputs.settings = lib.mkOption {
type = lib.types.deferredModule;
internal = true;
readOnly = true;
default =
let
startScript = pkgs.writeShellScriptBin "start-nginx" ''
set -euo pipefail
if [[ ! -d "${config.dataDir}" ]]; then
mkdir -p "${config.dataDir}"
fi
${config.package}/bin/nginx -p $(pwd) -c ${config.configFile} -e /dev/stderr
'';
in
{
processes."${name}" = {
command = "${startScript}/bin/start-nginx";
readiness_probe = {
# FIXME need a better health check
exec.command = "[ -e ${config.dataDir}/nginx/nginx.pid ]";
initial_delay_seconds = 2;
period_seconds = 10;
timeout_seconds = 4;
success_threshold = 1;
failure_threshold = 5;
};
namespace = name;
# https://github.com/F1bonacc1/process-compose#-auto-restart-if-not-healthy
availability.restart = "on_failure";
};
};
};
};
}
18 changes: 18 additions & 0 deletions nix/nginx_test.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{ pkgs, config, ... }: {
services.nginx."nginx1".enable = true;

settings.processes.test =
let
cfg = config.services.nginx."nginx1";
in
{
command = pkgs.writeShellApplication {
runtimeInputs = [ cfg.package pkgs.gnugrep pkgs.curl ];
text = ''
curl http://127.0.0.1:${builtins.toString cfg.port} | grep -q "nginx"
'';
name = "nginx-test";
};
depends_on."nginx1".condition = "process_healthy";
};
}
1 change: 1 addition & 0 deletions test/flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
../nix/apache-kafka_test.nix
../nix/elasticsearch_test.nix
../nix/mysql_test.nix
../nix/nginx_test.nix
../nix/postgres_test.nix
../nix/redis_test.nix
../nix/redis-cluster_test.nix
Expand Down

0 comments on commit 8cd80d7

Please sign in to comment.