html.nix/pkgs-lib.nix

169 lines
5.0 KiB
Nix
Raw Normal View History

2022-07-30 12:42:09 +03:00
{
2023-04-06 01:43:12 +03:00
lib,
flake-parts-lib,
...
2022-07-30 12:42:09 +03:00
}: let
2023-04-06 01:43:12 +03:00
l = lib // builtins;
2023-04-06 05:43:25 +03:00
recursiveAttrPaths = set: let
flattenIfHasList = x:
if (l.isList x) && (l.any l.isList x)
then l.concatMap flattenIfHasList x
else [x];
recurse = path: set: let
g = name: value:
if l.isAttrs value
then recurse (path ++ [name]) value
else path ++ [name];
in
l.mapAttrsToList g set;
in
flattenIfHasList (recurse [] set);
2022-07-30 12:42:09 +03:00
in {
2023-04-06 01:43:12 +03:00
options = {
perSystem =
flake-parts-lib.mkPerSystemOption
({...}: {
2023-04-06 05:43:25 +03:00
options = {
html-nix.lib = {
mkServeFromSite = l.mkOption {
type = with l.types; functionTo package;
};
mkSiteFrom = l.mkOption {
type = with l.types; functionTo attrs;
};
mkSitePathFrom = l.mkOption {
type = l.types.raw;
};
2023-04-06 06:56:45 +03:00
parseMarkdown = l.mkOption {
type = l.types.raw;
};
2023-04-06 01:43:12 +03:00
};
};
});
};
config = {
perSystem = {pkgs, ...}: let
pkgBin = name: "${pkgs.${name}}/bin/${name}";
mkServePathScript = path:
pkgs.writeScriptBin "serve" ''
${pkgs.nodePackages.http-server}/bin/http-server -c-1 ${path}
'';
2021-05-15 04:15:00 +03:00
2023-04-06 01:43:12 +03:00
mkSitePath = site: let
convertToPath = path: value:
if builtins.isPath value
then value
else pkgs.writeText (l.concatStringsSep "-" path) value;
2023-04-06 05:43:25 +03:00
fileAttrPaths = recursiveAttrPaths site;
2023-04-06 01:43:12 +03:00
texts = l.mapAttrsRecursive convertToPath site;
mkCreateFileCmd = path: value: let
p = l.concatStringsSep "/" (l.init path);
in "mkdir -p \"$out/${p}\" && ln -s \"${value}\" \"$out/${p}/${l.last path}\"";
createFileCmds =
l.map
(path: mkCreateFileCmd path (l.getAttrFromPath path texts))
fileAttrPaths;
in
pkgs.runCommandLocal "site-path" {} ''
mkdir -p $out
${l.concatStringsSep "\n" createFileCmds}
'';
2021-05-15 19:42:16 +03:00
parseMarkdown = name: path:
2023-04-06 01:43:12 +03:00
pkgs.runCommandLocal name {} ''
${pkgBin "pandoc"} ${path} -f gfm -o $out
2023-04-06 01:43:12 +03:00
'';
in {
html-nix.lib = {
2023-04-06 06:56:45 +03:00
inherit parseMarkdown;
2023-04-06 05:43:25 +03:00
mkSitePathFrom = mkSitePath;
2023-04-06 01:43:12 +03:00
mkServeFromSite = site: mkServePathScript (mkSitePath site);
mkSiteFrom = {
src,
templater,
local ? false,
config ? {},
} @ args: let
2023-04-06 06:45:41 +03:00
getPath = from: name:
l.path {
name = l.strings.sanitizeDerivationName name;
path = "${toString from}/${name}";
};
2023-04-06 01:43:12 +03:00
postsRendered = let
2023-04-06 06:45:41 +03:00
path = "${toString src}/posts";
2022-07-30 12:42:09 +03:00
in
2023-04-06 07:00:37 +03:00
if l.pathExists path
2023-04-06 06:58:20 +03:00
then
l.pipe (l.readDir path) [
(l.mapAttrsToList (
name: _: let
__displayName = l.head (l.splitString "." name);
_displayName = l.splitString "_" __displayName;
id = l.replaceStrings [" "] ["_"] __displayName;
date = l.head _displayName;
in {
inherit id;
displayName = l.last _displayName;
date =
if date == ""
then null
else date;
content = l.readFile (parseMarkdown id (getPath path name));
}
))
(l.sort (
p: op: let
extractDate = date: l.splitString "-" date;
getPart = date: el: l.removeSuffix "0" (l.elemAt (extractDate date) el);
d = getPart p.date;
od = getPart op.date;
in
if p.date == null
then false
else if op.date == null
then true
else !(d 0 > od 0 && d 1 > od 1 && d 2 > od 2)
))
]
else [];
2023-04-06 01:43:12 +03:00
pagesRendered = let
2023-04-06 06:45:41 +03:00
path = "${toString src}/pages";
2023-04-06 01:43:12 +03:00
in
2023-04-06 06:58:20 +03:00
if l.pathExists path
then
l.mapAttrsToList
(
name: _: rec {
displayName = l.head (l.splitString "." name);
id = l.replaceStrings [" "] ["_"] displayName;
content = l.readFile (parseMarkdown id (getPath path name));
}
)
(l.readDir path)
else [];
2023-04-06 01:43:12 +03:00
baseurl =
if local
then "http://localhost:8080"
else args.config.baseurl or (throw "Need baseurl");
2021-05-15 19:42:16 +03:00
2023-04-06 01:43:12 +03:00
context = {
inherit lib baseurl;
inherit (args) config;
2023-04-06 01:43:12 +03:00
posts = postsRendered;
pages = pagesRendered;
site = {
"robots.txt" = ''
User-agent: *
Allow: /
'';
};
};
in
(templater context).site;
2021-05-15 19:42:16 +03:00
};
2022-07-30 12:42:09 +03:00
};
2023-04-06 01:43:12 +03:00
};
}