move site to other file, organise builder

This commit is contained in:
Tristan 2024-01-04 05:07:45 +00:00
parent 63f1c014af
commit 669fccd57e
7 changed files with 124 additions and 97 deletions

View file

@ -11,49 +11,27 @@
system = "x86_64-linux";
pkgs = import nixpkgs {inherit system;};
nixite = import ./nixite/. {inherit pkgs;};
my-site = import ./testing/my-site.nix {inherit nixite;};
in
nixite
// {
formatter.${system} = pkgs.alejandra;
packages.${system} = {
raw = nixite.mkSite (let
readme = {
name = "readme";
content = nixite.md.mdToPage ./README.md;
};
markup = {
"index.html" = with nixite.elems; let
blue = nixite.style.component span "blue" {
style = {color = "blue";};
};
underblue = nixite.style.component blue "under" {
style = {text-decoration = "underline";};
};
in (
nixite.html.document {
head = [(title "Nixite")];
body = main [
(a {href = nixite.site.link readme;} "Readme")
(a "/blog" "blog")
(List {} ["item 1" "item 2" "item 3"])
(p [
"check out my"
(blue "blue span")
"isn't it"
(underblue {onclick = "alert(1)";} "great!")
])
];
}
);
blog = nixite.md.readDir ./testing/blog;
};
site =
nixite.site.prepare {favicon = ./testing/src/favicon.png;}
markup;
in
site);
raw = nixite.mkSite my-site;
default = nixite.serve self.packages.${system}.raw;
default = nixite.serve my-site {};
dev = tix.watch {
setup = ''
nix build .#raw
${pkgs.caddy}/bin/caddy file-server --root result &
'';
cmd = ''
nix build .#raw
'';
};
inherit (tix.packages.${system}) watch watchpipe results;
test = tix.run [
./testing/md.test.nix
@ -63,14 +41,6 @@
./testing/style.test.nix
];
watch = tix.packages.${system}.watch;
watchpipe = tix.packages.${system}.watchpipe;
results = tix.packages.${system}.results;
dev = tix.watch {
cmd = "nix run .# --show-trace";
stop = "pkill caddy";
};
};
};
}

View file

@ -1,8 +1,6 @@
{pkgs, ...}: {
mkSite = import ./make-site.nix pkgs;
serve = site: (pkgs.writeShellScriptBin "serve" ''
${pkgs.caddy}/bin/caddy file-server --root ${site}
'');
serve = import ./serve.nix pkgs;
md = import ./md.nix;
html = import ./html.nix;
elems = import ./elems.nix;

View file

@ -1,16 +1,39 @@
{stdenv, ...}: raw: let
site = import ./site.nix;
pkgs: raw: let
buildSiteTo = prefix: site:
builtins.toString (builtins.attrValues (builtins.mapAttrs (name: content:
if builtins.isString content
then ''
cp ${builtins.toFile name content} ${prefix}/${name}
''
else if builtins.isPath content
then ''
cp -r ${content} ${prefix}/${name}
''
else if builtins.isAttrs content && content ? "__toString"
then ''
cp ${builtins.toFile name (toString content)} ${prefix}/${name}
''
else if builtins.isAttrs content
then ''
mkdir -p ${prefix}/${name}
${buildSiteTo "${prefix}/${name}" content}
''
else
throw "Site page must be string, path or attrset, but got ${
builtins.typeOf content
}: [${toString content}]")
site));
in
stdenv.mkDerivation {
name = "site";
src = ./.;
buildPhase = ''
mkdir dist
${site.copyTo "dist" raw}
'';
installPhase = ''
cp -r dist $out
'';
derivation {
builder = "/bin/sh";
name = "my-nixite";
system = "x86_64-linux";
args = [
"-c"
''
PATH=$PATH:${pkgs.coreutils}/bin
mkdir $out
${buildSiteTo "$out" raw}
''
];
}

28
nixite/serve.nix Normal file
View file

@ -0,0 +1,28 @@
pkgs: raw: {host ? "localhost"}: let
mkSite = import ./make-site.nix pkgs;
my-site = mkSite raw;
Caddyfile = ''
${host}
root * ${my-site}
file_server
'';
run = ''
${pkgs.caddy}/bin/caddy run --config $out/Caddyfile
'';
in
derivation {
name = "nixite-server";
builder = "/bin/sh";
system = "x86_64-linux";
args = [
"-c"
''
PATH=$PATH:${pkgs.coreutils}/bin
mkdir -p $out/bin
echo '${Caddyfile}' > $out/Caddyfile
echo "${run}" > $out/bin/nixite-server
chmod +x $out/bin/nixite-server
''
];
inherit my-site;
}

View file

@ -142,29 +142,4 @@ in rec {
content,
} @ page:
page // {type = "link";};
copyTo = prefix: site:
builtins.toString (builtins.attrValues (builtins.mapAttrs (name: content:
if builtins.isString content
then ''
cp ${builtins.toFile name content} ${prefix}/${name}
''
else if builtins.isPath content
then ''
cp -r ${content} ${prefix}/${name}
''
else if builtins.isAttrs content && content ? "__toString"
then ''
cp ${builtins.toFile name (toString content)} ${prefix}/${name}
''
else if builtins.isAttrs content
then ''
mkdir -p ${prefix}/${name}
${copyTo "${prefix}/${name}" content}
''
else
throw "Site page must be string, path or attrset, but got ${
builtins.typeOf content
}: [${toString content}]")
site));
}

View file

@ -75,16 +75,19 @@ in
actual = processStr ''
this text **may** *or may not* contain **bold** words *inside* it.
'';
expected = [(elems.p [
expected = [
(elems.p [
"this text "
(elems.strong "may") " "
(elems.strong "may")
" "
(elems.em "or may not")
" contain "
(elems.strong "bold")
" words "
(elems.em "inside")
" it."
])];
])
];
asString = true;
})

30
testing/my-site.nix Normal file
View file

@ -0,0 +1,30 @@
{nixite}: let
markup = {
"index.html" = nixite.md.mdToPage ../README.md;
"my-page" = with nixite.elems; let
blue = nixite.style.component span "blue" {
style = {color = "blue";};
};
underblue = nixite.style.component blue "under" {
style = {text-decoration = "underline";};
};
in (
nixite.html.document {
head = [(title "Nixite")];
body = main [
(a "/" "Readme")
(a "/blog" "blog")
(List {} ["item 1" "item 2" "item 3"])
(p [
"check out my"
(blue "blue span")
"isn't it"
(underblue {onclick = "alert(1)";} "great!")
])
];
}
);
blog = nixite.md.readDir ./blog;
};
in
nixite.site.prepare {favicon = ./src/favicon.png;} markup