From 2a1df3a476d63075889a05cfe4769e82fe8158db Mon Sep 17 00:00:00 2001 From: Yusuf Bera Ertan Date: Sat, 15 May 2021 04:02:06 +0300 Subject: [PATCH] feat: mk site path, actual website example with serve script --- .gitignore | 1 + examples/serve.nix | 20 ++++++++++++++++++++ flake.nix | 32 +++++++++++++++++++++++++++----- pkgs-lib.nix | 16 ++++++++++++++-- 4 files changed, 62 insertions(+), 7 deletions(-) create mode 100644 .gitignore create mode 100644 examples/serve.nix diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..d6944e3 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/result* diff --git a/examples/serve.nix b/examples/serve.nix new file mode 100644 index 0000000..cdf364b --- /dev/null +++ b/examples/serve.nix @@ -0,0 +1,20 @@ +{ tags, pkgsLib, pkgs ? import { overlays = [ pkgsLib ]; } }: +with pkgs.htmlNix; +let + index = with tags; + html [ + (body [ + (p "Hello world!") + (link "./ex.html" "say bye") + ]) + ]; + + ex = with tags; + html [ + (body [ + (p "Bye world!") + (link "./index.html" "go back") + ]) + ]; +in +mkServePathScript (mkSitePath { "index.html" = index; "ex.html" = ex; }) diff --git a/flake.nix b/flake.nix index cb83bb8..8196133 100644 --- a/flake.nix +++ b/flake.nix @@ -5,8 +5,31 @@ outputs = { self, nixpkgsLib }: let - lib = nixpkgsLib.lib; - tags = import ./tags.nix { format = true; inherit lib; }; + lib = nixpkgsLib.lib // { + recursiveAttrPaths = set: + let + flattenIfHasList = x: + if (lib.isList x) && (lib.any lib.isList x) + then lib.concatMap flattenIfHasList x + else [ x ]; + + recurse = path: set: + let + g = + name: value: + if lib.isAttrs value + then recurse (path ++ [ name ]) value + else path ++ [ name ]; + in + lib.mapAttrsToList g set; + in + flattenIfHasList (recurse [ ] set); + }; + + tags = import ./tags.nix { format = false; inherit lib; }; + pkgsLib = (final: prev: { + htmlNix = import ./pkgs-lib.nix { inherit lib; pkgs = prev; }; + }); in { lib = { @@ -14,13 +37,12 @@ }; overlays = { - pkgsLib = (final: prev: { - htmlNix = import ./pkgs-lib.nix { inherit lib; pkgs = prev; }; - }); + inherit pkgsLib; }; examples = { tags = import ./examples/tags.nix tags; + serve = import ./examples/serve.nix { inherit tags pkgsLib; }; # needs --impure }; }; } diff --git a/pkgs-lib.nix b/pkgs-lib.nix index d5b9249..f4f345b 100644 --- a/pkgs-lib.nix +++ b/pkgs-lib.nix @@ -1,8 +1,20 @@ { lib, pkgs }: let pkgBin = name: "${pkgs.${name}}/bin/${name}"; in { - mkServePathScript = path: pkgs.writeScriptBin "serve" { } '' - #!${pkgBin "bash"} + mkServePathScript = path: pkgs.writeScriptBin "serve" '' + #!${pkgs.stdenv.shell} ${pkgBin "miniserve"} --index index.html ${path} ''; + + mkSitePath = site: + let + fileAttrPaths = lib.recursiveAttrPaths site; + texts = lib.mapAttrsRecursive (path: value: pkgs.writeText (lib.concatStringsSep "-" path) value) site; + mkCreateFileCmd = path: value: let p = lib.concatStringsSep "/" (lib.init path); in "mkdir -p $out/${p} && ln -s ${value} $out/${p}/${lib.last path}"; + createFileCmds = map (path: mkCreateFileCmd path (lib.getAttrFromPath path texts)) fileAttrPaths; + in + pkgs.runCommand "site-path" { } '' + mkdir -p $out + ${lib.concatStringsSep "\n" createFileCmds} + ''; }