From 669fccd57e04519b5e40142754f9f68a7b5caf38 Mon Sep 17 00:00:00 2001 From: Tristan Date: Thu, 4 Jan 2024 05:07:45 +0000 Subject: [PATCH] move site to other file, organise builder --- flake.nix | 60 +++++++++++--------------------------------- nixite/default.nix | 4 +-- nixite/make-site.nix | 51 ++++++++++++++++++++++++++----------- nixite/serve.nix | 28 +++++++++++++++++++++ nixite/site.nix | 25 ------------------ testing/md.test.nix | 23 +++++++++-------- testing/my-site.nix | 30 ++++++++++++++++++++++ 7 files changed, 124 insertions(+), 97 deletions(-) create mode 100644 nixite/serve.nix create mode 100644 testing/my-site.nix diff --git a/flake.nix b/flake.nix index 01c501c..3823004 100644 --- a/flake.nix +++ b/flake.nix @@ -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"; - }; }; }; } diff --git a/nixite/default.nix b/nixite/default.nix index 3d83062..4433310 100644 --- a/nixite/default.nix +++ b/nixite/default.nix @@ -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; diff --git a/nixite/make-site.nix b/nixite/make-site.nix index 9f5177d..3730a78 100644 --- a/nixite/make-site.nix +++ b/nixite/make-site.nix @@ -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} + '' + ]; } diff --git a/nixite/serve.nix b/nixite/serve.nix new file mode 100644 index 0000000..5b425bd --- /dev/null +++ b/nixite/serve.nix @@ -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; + } diff --git a/nixite/site.nix b/nixite/site.nix index 50e95fb..cd06f0a 100644 --- a/nixite/site.nix +++ b/nixite/site.nix @@ -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)); } diff --git a/testing/md.test.nix b/testing/md.test.nix index 6373adb..e7a8e04 100644 --- a/testing/md.test.nix +++ b/testing/md.test.nix @@ -75,16 +75,19 @@ in actual = processStr '' this text **may** *or may not* contain **bold** words *inside* it. ''; - expected = [(elems.p [ - "this text " - (elems.strong "may") " " - (elems.em "or may not") - " contain " - (elems.strong "bold") - " words " - (elems.em "inside") - " it." - ])]; + expected = [ + (elems.p [ + "this text " + (elems.strong "may") + " " + (elems.em "or may not") + " contain " + (elems.strong "bold") + " words " + (elems.em "inside") + " it." + ]) + ]; asString = true; }) diff --git a/testing/my-site.nix b/testing/my-site.nix new file mode 100644 index 0000000..3af57ea --- /dev/null +++ b/testing/my-site.nix @@ -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