refactor(web): Move nginx setup to //web/homepage

The homepage is going to be the landing page for all content, whether
it be blog posts or other stuff.
This commit is contained in:
Vincent Ambo 2020-02-08 14:06:57 +00:00
parent 8e9fb73958
commit 7935957938
3 changed files with 41 additions and 19 deletions

View file

@ -31,7 +31,7 @@ let
posts = list post (import ./posts.nix); posts = list post (import ./posts.nix);
fragments = import ./fragments.nix args; fragments = import ./fragments.nix args;
renderedBlog = pkgs.third_party.runCommandNoCC "tazjins-blog" {} '' rendered = pkgs.third_party.runCommandNoCC "tazjins-blog" {} ''
mkdir -p $out mkdir -p $out
cp ${fragments.blogIndex posts} $out/index.html cp ${fragments.blogIndex posts} $out/index.html
@ -40,6 +40,7 @@ let
"cp ${fragments.renderPost post} $out/${post.key}.html" "cp ${fragments.renderPost post} $out/${post.key}.html"
) posts)} ) posts)}
''; '';
in import ./nginx.nix (args // { in {
inherit posts renderedBlog; inherit post posts rendered;
}) static = ./static;
}

15
web/homepage/default.nix Normal file
View file

@ -0,0 +1,15 @@
# Assembles the website index and configures an nginx instance to
# serve it.
#
# The website is made up of a simple header&footer and content
# elements for things such as blog posts and projects.
#
# Content for the blog is in //web/blog instead of here.
{ pkgs, lib, ... }:
with pkgs;
with nix.yants;
third_party.callPackage ./nginx.nix {
blog = web.blog;
}

View file

@ -1,24 +1,30 @@
# This file creates an nginx server that serves the blog on port 8080. # This file creates an nginx server that serves the blog on port 8080.
# #
# It's not intended to be the user-facing nginx. # It's not intended to be the user-facing nginx.
{ pkgs, lib, posts, renderedBlog, ... }: {
# third_party attributes supplied by callPackage
writeText, writeShellScriptBin, nginx, lib,
# website content
blog
}:
let let
inherit (builtins) hasAttr filter map; inherit (builtins) hasAttr filter map;
inherit (pkgs.third_party) writeText writeShellScriptBin nginx; inherit (pkgs.third_party) ;
oldRedirects = lib.concatStringsSep "\n" (map (post: '' oldRedirects = lib.concatStringsSep "\n" (map (post: ''
location ~* ^(en)?/${post.oldKey} { location ~* ^(en)?/${post.oldKey} {
# TODO(tazjin): 301 once this works # TODO(tazjin): 301 once this works
return 302 /${post.key}; return 302 /${post.key};
} }
'') (filter (hasAttr "oldKey") posts)); '') (filter (hasAttr "oldKey") blog.posts));
config = writeText "blog-nginx.conf" '' config = writeText "homepage-nginx.conf" ''
daemon off; daemon off;
worker_processes 1; worker_processes 1;
error_log stderr; error_log stderr;
pid /tmp/nginx-tazblog.pid; pid /tmp/nginx-homepage.pid;
events { events {
worker_connections 1024; worker_connections 1024;
@ -26,11 +32,11 @@ let
http { http {
include ${nginx}/conf/mime.types; include ${nginx}/conf/mime.types;
fastcgi_temp_path /tmp/nginx-tazblog; fastcgi_temp_path /tmp/nginx-homepage;
uwsgi_temp_path /tmp/nginx-tazblog; uwsgi_temp_path /tmp/nginx-homepage;
scgi_temp_path /tmp/nginx-tazblog; scgi_temp_path /tmp/nginx-homepage;
client_body_temp_path /tmp/nginx-tazblog; client_body_temp_path /tmp/nginx-homepage;
proxy_temp_path /tmp/nginx-tazblog; proxy_temp_path /tmp/nginx-homepage;
sendfile on; sendfile on;
# Logging is handled by the primary nginx server # Logging is handled by the primary nginx server
@ -38,10 +44,10 @@ let
server { server {
listen 8080 default_server; listen 8080 default_server;
root ${renderedBlog}; root ${blog.rendered};
location /static { location /static {
alias ${./static}/; alias ${blog.static}/;
} }
${oldRedirects} ${oldRedirects}
@ -56,13 +62,13 @@ let
} }
} }
''; '';
in writeShellScriptBin "tazblog" '' in writeShellScriptBin "homepage" ''
if [[ -v CONTAINER_SETUP ]]; then if [[ -v CONTAINER_SETUP ]]; then
cd /run cd /run
echo 'nogroup:x:30000:nobody' >> /etc/group echo 'nogroup:x:30000:nobody' >> /etc/group
echo 'nobody:x:30000:30000:nobody:/tmp:/bin/bash' >> /etc/passwd echo 'nobody:x:30000:30000:nobody:/tmp:/bin/bash' >> /etc/passwd
fi fi
mkdir -p /tmp/nginx-tazblog mkdir -p /tmp/nginx-homepage
exec ${pkgs.third_party.nginx}/bin/nginx -c ${config} exec ${nginx}/bin/nginx -c ${config}
'' ''