Here's a .nix file that installs and configures nginx as a system service. Note that this version only supports HTTP and not HTTPS; see Nginx HTTPS for an example that includes HTTPS.
{ lib, pkgs,...}:{config={ nixpkgs.hostPlatform="x86_64-linux";# Enable and configure servicesservices={ nginx.enable=true;};environment={# Packages that should be installed on a systemsystemPackages=[ pkgs.hello
pkgs.mariadb
pkgs.nginx
];# Add directories and files to `/etc` and set their permissionsetc={"nginx/nginx.conf"={user="root";group="root";mode="0644";text=''# The user/group is often set to 'nginx' or 'www-data',# but for a simple root-only demo, we'll keep the default.# user nginx;worker_processes auto;# NGINX looks for modules relative to the install prefix,# but we explicitly point to the Nix store path to be safe.error_log /var/log/nginx/error.log;pid /run/nginx.pid;events { worker_connections 1024;}http { include ${pkgs.nginx}/conf/mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; # Basic default server block server { listen 80; server_name localhost; # Point the root directory to a standard location or a Nix store path root ${pkgs.nginx}/html; location / { index index.html; } # Example log files access_log /var/log/nginx/access.log; error_log /var/log/nginx/error.log; }} '';};};};# Enable and configure systemd services systemd.services={nginx={enable=true;description="A high performance web server and reverse proxy server";wantedBy=["system-manager.target"];preStart='' mkdir -p /var/log/nginx chown -R root:root /var/log/nginx # Ensure permissions are right for root user '';serviceConfig={Type="forking";PIDFile="/run/nginx.pid";# The main binary execution command, pointing to the Nix store pathExecStart="${pkgs.nginx}/bin/nginx -c /etc/nginx/nginx.conf";# The command to stop the service gracefullyExecStop="${pkgs.nginx}/bin/nginx -s stop";# NGINX needs to run as root to bind to port 80/443User="root";Group="root";# Restart policy for robustnessRestart="on-failure";};};};};}