2020-02-08 15:06:57 +01:00
|
|
|
# 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.
|
2020-02-21 13:47:29 +01:00
|
|
|
{ depot, lib, ... }:
|
2020-02-08 15:06:57 +01:00
|
|
|
|
2020-02-21 13:47:29 +01:00
|
|
|
with depot;
|
2020-02-08 15:06:57 +01:00
|
|
|
with nix.yants;
|
|
|
|
|
2020-02-08 23:21:06 +01:00
|
|
|
let
|
|
|
|
inherit (builtins) readFile replaceStrings sort;
|
|
|
|
inherit (third_party) writeFile runCommandNoCC;
|
|
|
|
|
|
|
|
# The different types of entries on the homepage.
|
|
|
|
entryClass = enum "entryClass" [ "blog" "project" "misc" ];
|
|
|
|
|
|
|
|
# The definition of a single entry.
|
|
|
|
entry = struct "entry" {
|
|
|
|
class = entryClass;
|
|
|
|
title = string;
|
|
|
|
url = string;
|
|
|
|
date = int; # epoch
|
|
|
|
description = option string;
|
|
|
|
};
|
|
|
|
|
|
|
|
escape = replaceStrings [ "<" ">" "&" "'" ] [ "<" ">" "&" "'" ];
|
|
|
|
|
|
|
|
postToEntry = defun [ web.blog.post entry ] (post: {
|
|
|
|
class = "blog";
|
2020-02-09 01:02:10 +01:00
|
|
|
title = post.title;
|
2020-02-08 23:21:06 +01:00
|
|
|
url = "/blog/${post.key}";
|
|
|
|
date = post.date;
|
|
|
|
});
|
|
|
|
|
2020-02-09 01:02:10 +01:00
|
|
|
formatDate = defun [ int string ] (date: readFile (runCommandNoCC "date" {} ''
|
|
|
|
date --date='@${toString date}' '+%Y-%m-%d' > $out
|
|
|
|
''));
|
|
|
|
|
|
|
|
formatEntryDate = defun [ entry string ] (entry: entryClass.match entry.class {
|
|
|
|
blog = "Blog post from ${formatDate entry.date}";
|
|
|
|
project = "Project from ${formatDate entry.date}";
|
|
|
|
misc = "Posted on ${formatDate entry.date}";
|
|
|
|
});
|
|
|
|
|
2020-02-08 23:21:06 +01:00
|
|
|
entryToDiv = defun [ entry string ] (entry: ''
|
2020-02-09 03:20:41 +01:00
|
|
|
<a href="${entry.url}" class="entry ${entry.class}">
|
|
|
|
<div>
|
|
|
|
<p class="entry-title">${escape entry.title}</p>
|
|
|
|
${
|
|
|
|
lib.optionalString ((entry ? description) && (entry.description != null))
|
|
|
|
"<p class=\"entry-description\">${escape entry.description}</p>"
|
|
|
|
}
|
|
|
|
<p class="entry-date">${formatEntryDate entry}</p>
|
|
|
|
</div>
|
|
|
|
</a>
|
2020-02-08 23:21:06 +01:00
|
|
|
'');
|
|
|
|
|
|
|
|
index = entries: third_party.writeText "index.html" (lib.concatStrings (
|
|
|
|
[ (builtins.readFile ./header.html) ]
|
2020-02-09 01:02:10 +01:00
|
|
|
++ (map entryToDiv (sort (a: b: a.date > b.date) entries))
|
2020-02-08 23:21:06 +01:00
|
|
|
++ [ (builtins.readFile ./footer.html) ]
|
|
|
|
));
|
|
|
|
|
|
|
|
homepage = index ((map postToEntry web.blog.posts) ++ (import ./entries.nix));
|
2020-02-11 20:31:20 +01:00
|
|
|
in runCommandNoCC "website" {} ''
|
|
|
|
mkdir $out
|
|
|
|
cp ${homepage} $out/index.html
|
|
|
|
cp -r ${./static} $out/static
|
|
|
|
''
|