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.
|
|
|
|
#
|
2020-06-26 21:28:06 +02:00
|
|
|
# Content for the blog is in //users/tazjin/blog instead of here.
|
2021-04-10 18:05:16 +02:00
|
|
|
{ depot, lib, pkgs, ... }@args:
|
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;
|
2022-09-26 19:33:05 +02:00
|
|
|
inherit (pkgs) writeFile runCommand;
|
2020-02-08 23:21:06 +01:00
|
|
|
|
|
|
|
# The different types of entries on the homepage.
|
2023-06-16 01:18:18 +02:00
|
|
|
entryClass = enum "entryClass" [
|
|
|
|
"blog"
|
|
|
|
"project"
|
|
|
|
"note"
|
|
|
|
"misc"
|
|
|
|
];
|
2020-02-08 23:21:06 +01:00
|
|
|
|
|
|
|
# The definition of a single entry.
|
|
|
|
entry = struct "entry" {
|
|
|
|
class = entryClass;
|
2023-06-16 01:18:18 +02:00
|
|
|
title = option string;
|
|
|
|
url = option string;
|
2020-02-08 23:21:06 +01:00
|
|
|
date = int; # epoch
|
|
|
|
description = option string;
|
|
|
|
};
|
|
|
|
|
|
|
|
escape = replaceStrings [ "<" ">" "&" "'" ] [ "<" ">" "&" "'" ];
|
|
|
|
|
2021-10-19 14:00:51 +02:00
|
|
|
postToEntry = defun [ web.blog.post entry ] (post: {
|
2020-02-08 23:21:06 +01:00
|
|
|
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;
|
2023-06-16 01:18:18 +02:00
|
|
|
description = post.description or "Blog post from ${formatDate post.date}";
|
2020-02-08 23:21:06 +01:00
|
|
|
});
|
|
|
|
|
2022-09-26 19:33:05 +02:00
|
|
|
formatDate = defun [ int string ] (date: readFile (runCommand "date" { } ''
|
2023-06-16 01:18:18 +02:00
|
|
|
date --date='@${toString date}' '+%Y-%m-%d' | tr -d '\n' > $out
|
2020-02-09 01:02:10 +01:00
|
|
|
''));
|
|
|
|
|
2023-06-16 01:18:18 +02:00
|
|
|
entryUrl = defun [ entry string ] (entry:
|
|
|
|
if entry.class == "note"
|
|
|
|
then "#${toString entry.date}"
|
|
|
|
else entry.url
|
|
|
|
);
|
|
|
|
|
|
|
|
hasDescription = defun [ entry bool ] (entry:
|
|
|
|
((entry ? description) && (entry.description != null))
|
|
|
|
);
|
|
|
|
|
|
|
|
entryTitle = defun [ entry string ] (entry:
|
|
|
|
let
|
|
|
|
optionalColon = lib.optionalString (hasDescription entry) ":";
|
|
|
|
titleText =
|
|
|
|
if (!(entry ? title) && (entry.class == "note"))
|
|
|
|
then "[${formatDate entry.date}]"
|
|
|
|
else lib.optionalString (entry ? title) ((escape entry.title) + optionalColon);
|
|
|
|
in
|
|
|
|
lib.optionalString (titleText != "")
|
|
|
|
''<span class="entry-title ${entry.class}">${titleText}</span>''
|
|
|
|
);
|
2020-02-09 01:02:10 +01:00
|
|
|
|
2020-02-08 23:21:06 +01:00
|
|
|
entryToDiv = defun [ entry string ] (entry: ''
|
2023-06-16 01:18:18 +02:00
|
|
|
<a href="${entryUrl entry}" id="${toString entry.date}" class="entry">
|
|
|
|
${entryTitle entry}
|
|
|
|
${
|
|
|
|
lib.optionalString (hasDescription entry)
|
|
|
|
"<span class=\"entry-description\">${escape entry.description}</span>"
|
|
|
|
}
|
2020-02-09 03:20:41 +01:00
|
|
|
</a>
|
2020-02-08 23:21:06 +01:00
|
|
|
'');
|
|
|
|
|
2021-04-10 18:05:16 +02:00
|
|
|
index = entries: pkgs.writeText "index.html" (lib.concatStrings (
|
2020-02-08 23:21:06 +01:00
|
|
|
[ (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) ]
|
|
|
|
));
|
|
|
|
|
2020-08-11 01:16:29 +02:00
|
|
|
pageEntries = import ./entries.nix;
|
|
|
|
homepage = index ((map postToEntry users.tazjin.blog.posts) ++ pageEntries);
|
|
|
|
atomFeed = import ./feed.nix (args // { inherit entry pageEntries; });
|
2022-01-30 17:06:58 +01:00
|
|
|
in
|
2022-09-26 19:33:05 +02:00
|
|
|
runCommand "website" { } ''
|
2020-02-11 20:31:20 +01:00
|
|
|
mkdir $out
|
|
|
|
cp ${homepage} $out/index.html
|
2020-08-11 00:38:07 +02:00
|
|
|
cp ${atomFeed} $out/feed.atom
|
2021-08-26 19:28:06 +02:00
|
|
|
mkdir $out/static
|
|
|
|
cp -r ${depot.web.static}/* $out/static
|
|
|
|
cp -rf ${./static}/* $out/static
|
2020-02-11 20:31:20 +01:00
|
|
|
''
|