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"; system = "x86_64-linux";
pkgs = import nixpkgs {inherit system;}; pkgs = import nixpkgs {inherit system;};
nixite = import ./nixite/. {inherit pkgs;}; nixite = import ./nixite/. {inherit pkgs;};
my-site = import ./testing/my-site.nix {inherit nixite;};
in in
nixite nixite
// { // {
formatter.${system} = pkgs.alejandra; formatter.${system} = pkgs.alejandra;
packages.${system} = { packages.${system} = {
raw = nixite.mkSite (let raw = nixite.mkSite my-site;
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);
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 [ test = tix.run [
./testing/md.test.nix ./testing/md.test.nix
@ -63,14 +41,6 @@
./testing/style.test.nix ./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, ...}: { {pkgs, ...}: {
mkSite = import ./make-site.nix pkgs; mkSite = import ./make-site.nix pkgs;
serve = site: (pkgs.writeShellScriptBin "serve" '' serve = import ./serve.nix pkgs;
${pkgs.caddy}/bin/caddy file-server --root ${site}
'');
md = import ./md.nix; md = import ./md.nix;
html = import ./html.nix; html = import ./html.nix;
elems = import ./elems.nix; elems = import ./elems.nix;

View file

@ -1,16 +1,39 @@
{stdenv, ...}: raw: let pkgs: raw: let
site = import ./site.nix; 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 in
stdenv.mkDerivation { derivation {
name = "site"; builder = "/bin/sh";
src = ./.; name = "my-nixite";
buildPhase = '' system = "x86_64-linux";
mkdir dist args = [
"-c"
${site.copyTo "dist" raw} ''
PATH=$PATH:${pkgs.coreutils}/bin
''; mkdir $out
installPhase = '' ${buildSiteTo "$out" raw}
cp -r dist $out ''
''; ];
} }

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, content,
} @ page: } @ page:
page // {type = "link";}; 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 '' actual = processStr ''
this text **may** *or may not* contain **bold** words *inside* it. this text **may** *or may not* contain **bold** words *inside* it.
''; '';
expected = [(elems.p [ expected = [
"this text " (elems.p [
(elems.strong "may") " " "this text "
(elems.em "or may not") (elems.strong "may")
" contain " " "
(elems.strong "bold") (elems.em "or may not")
" words " " contain "
(elems.em "inside") (elems.strong "bold")
" it." " words "
])]; (elems.em "inside")
" it."
])
];
asString = true; 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