about summary refs log tree commit diff
path: root/web/blog/nginx.nix
diff options
context:
space:
mode:
authorVincent Ambo <tazjin@google.com>2020-02-08T13·33+0000
committerVincent Ambo <tazjin@google.com>2020-02-08T13·33+0000
commit15b871806b5ceb0a1c6f563e02c1ef79ee761412 (patch)
tree30677d52497736f71858b7d722b74f662c2dfff3 /web/blog/nginx.nix
parent1d7b1334fd4c2a9aff678891ce5f305be21e5c95 (diff)
feat(web/blog): Add Nix-based static blog generator r/484
This introduces a derivation which builds an instance of nginx
statically serving my blog posts, though as of now no indexes are
being generated and no XML feed is available.

This is just the initial draft of this setup and not yet what shall be
yielded in the end.
Diffstat (limited to 'web/blog/nginx.nix')
-rw-r--r--web/blog/nginx.nix68
1 files changed, 68 insertions, 0 deletions
diff --git a/web/blog/nginx.nix b/web/blog/nginx.nix
new file mode 100644
index 000000000000..7c600c38c02e
--- /dev/null
+++ b/web/blog/nginx.nix
@@ -0,0 +1,68 @@
+# This file creates an nginx server that serves the blog on port 8080.
+#
+# It's not intended to be the user-facing nginx.
+{ pkgs, lib, posts, renderedBlog, ... }:
+
+let
+  inherit (builtins) hasAttr filter map;
+  inherit (pkgs.third_party) writeText writeShellScriptBin nginx;
+
+  oldRedirects = lib.concatStringsSep "\n" (map (post: ''
+    location ~* ^(en)?/${post.oldKey} {
+      # TODO(tazjin): 301 once this works
+      return 302 /${post.key};
+    }
+  '') (filter (hasAttr "oldKey") posts));
+
+  config = writeText "blog-nginx.conf" ''
+    daemon off;
+    worker_processes 1;
+    error_log stderr;
+    pid /tmp/nginx-tazblog.pid;
+
+    events {
+      worker_connections  1024;
+    }
+
+    http {
+      include ${nginx}/conf/mime.types;
+      fastcgi_temp_path /tmp/nginx-tazblog;
+      uwsgi_temp_path /tmp/nginx-tazblog;
+      scgi_temp_path /tmp/nginx-tazblog;
+      client_body_temp_path /tmp/nginx-tazblog;
+      proxy_temp_path /tmp/nginx-tazblog;
+      sendfile on;
+
+      # Logging is handled by the primary nginx server
+      access_log off;
+
+      server {
+        listen 8080 default_server;
+        root ${renderedBlog};
+
+        location /static {
+          alias ${./static}/;
+        }
+
+        ${oldRedirects}
+
+        location / {
+          if ($request_uri ~ ^/(.*)\.html$) {
+            return 302 /$1;
+          }
+
+          try_files $uri $uri.html $uri/ =404;
+        }
+      }
+    }
+  '';
+in writeShellScriptBin "tazblog" ''
+  if [[ -v CONTAINER_SETUP ]]; then
+    cd /run
+    echo 'nogroup:x:30000:nobody' >> /etc/group
+    echo 'nobody:x:30000:30000:nobody:/tmp:/bin/bash' >> /etc/passwd
+  fi
+
+  mkdir -p /tmp/nginx-tazblog
+  exec ${pkgs.third_party.nginx}/bin/nginx -c ${config}
+''