feat: mk site path, actual website example with serve script

This commit is contained in:
dusk 2021-05-15 04:02:06 +03:00
parent 13d7fc0759
commit 2a1df3a476
Signed by: dusk
GPG Key ID: 1D8F8FAF2294D6EA
4 changed files with 62 additions and 7 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
/result*

20
examples/serve.nix Normal file
View File

@ -0,0 +1,20 @@
{ tags, pkgsLib, pkgs ? import <nixpkgs> { 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; })

View File

@ -5,8 +5,31 @@
outputs = { self, nixpkgsLib }: outputs = { self, nixpkgsLib }:
let let
lib = nixpkgsLib.lib; lib = nixpkgsLib.lib // {
tags = import ./tags.nix { format = true; inherit 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 in
{ {
lib = { lib = {
@ -14,13 +37,12 @@
}; };
overlays = { overlays = {
pkgsLib = (final: prev: { inherit pkgsLib;
htmlNix = import ./pkgs-lib.nix { inherit lib; pkgs = prev; };
});
}; };
examples = { examples = {
tags = import ./examples/tags.nix tags; tags = import ./examples/tags.nix tags;
serve = import ./examples/serve.nix { inherit tags pkgsLib; }; # needs --impure
}; };
}; };
} }

View File

@ -1,8 +1,20 @@
{ lib, pkgs }: { lib, pkgs }:
let pkgBin = name: "${pkgs.${name}}/bin/${name}"; in let pkgBin = name: "${pkgs.${name}}/bin/${name}"; in
{ {
mkServePathScript = path: pkgs.writeScriptBin "serve" { } '' mkServePathScript = path: pkgs.writeScriptBin "serve" ''
#!${pkgBin "bash"} #!${pkgs.stdenv.shell}
${pkgBin "miniserve"} --index index.html ${path} ${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}
'';
} }