Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/RGBCube/HTMNIX
HTML for Nix to rule them all.
https://github.com/RGBCube/HTMNIX
html nix
Last synced: 2 days ago
JSON representation
HTML for Nix to rule them all.
- Host: GitHub
- URL: https://github.com/RGBCube/HTMNIX
- Owner: RGBCube
- License: mit
- Created: 2024-02-28T08:55:36.000Z (9 months ago)
- Default Branch: master
- Last Pushed: 2024-03-06T10:22:38.000Z (8 months ago)
- Last Synced: 2024-08-01T22:47:57.143Z (3 months ago)
- Topics: html, nix
- Language: Nix
- Homepage: https://rgbcu.be/blog/htmnix
- Size: 60.5 KB
- Stars: 63
- Watchers: 5
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# HTMNIX
Write composeable HTML with Nix!
Here is an example snippet:
```nix
"Hello, Internet!"<.title>
<.head>
"Yep, this is 100% Nix!"<.p>
{src="/foo.png"; alt="Attributes also work!";}
<.body>
<.html>
```[Here is an example site created using HTMNIX and the `build.sh` in
the current directory that you are supposed to vendor](https://github.com/RGBCube/NixSite)You might be wondering, _How?_
If you are, go read my [blog post](https://rgbcu.be/blog/htmnix)!
But before that you may want to try it for yourself!
You can! Just enter the REPL by running this command:```sh
nix repl github:RGBCube/HTMNIX
```## Provided Functions
These are the functions and variables provided by HTMNIX
which will be available in every HTMNIX file and the HTMNIX REPL
(You can enter it by running `nix repl github:RGBCube/HTMNIX`!).- `lib`: Just the nixpkgs lib. Pretty useful to have.
- `raw`: Used for a string to be included in the HTML without escaping.
Just pass the string into this and it will not be escaped.- `call`: Calls another HTMNIX file and brings this list of provided
variables into its scope before evaulation. Basically the same as `import`
if you disregard the bringing-into-scope it does.- `DOCTYPE`: Equivalent to a `` tag, this exists because you can't
express it in Nix syntax and have to resort to calling `__findFile` with the
string you want instead.- `__findFile`: Where the magic happens. This overrides the default `__findFile`
allowing for us to return magic functor sets for `` expressions.
The `` expression however is propagated into the builtin one so it does
not interfere with your workflow.## More Examples
> All of the examples here can be rendered with the following
> command (assuming `html.nix` has the example content):
>
> ```sh
> TARGET_FILE=$(realpath html.nix) nix eval github:RGBCube/HTMNIX#result --raw --impure
> ```> Also keep in mind that everything is passed as an argument to the
> first HTML tag's functor. So you will need to surrond some things with
> parens for it to evaulate properly.
>
> Some notable things that require parens include:
> - Function calls.
> - `let in`'s.
> - Expressions that have spaces in them.> Adding onto the previous note, if you want to see how your
> HTMNIX file is **actually** evaluated, you can run the following command:
>
> ```sh
> nix-instantiate --parse html.nix
> ```
>
> This will insert a ton of parens everywhere, thus un-ambiguating the parsing.
> Here is an example:
>
> ```nix
> nix-instantiate --parse - << :end
>
- "That's all!"<.li>
> <.ul>
> :end
> ```
>
> This produces the following output:
>
> ```nix
> (__findFile __nixPath "ul" (__findFile __nixPath "li") "That's all!" (__findFile __nixPath ".li") (__findFile __nixPath ".ul"))
> ```Create a directory listing:
```nix
- "${name} (${type})"<.li>)
(builtins.readDir ./.))
<.ul>
```List metadata about a derivation:
```nix
let
pkg = (import {}).youtube-dl;
in{class="package";}
"Name: ${pkg.pname}"<.p>
"See metadata"<.summary>
- "Full name: ${pkg.name}"
- "Version: ${pkg.version}"
- (let
license = if lib.isList pkg.meta.license then
lib.elemAt pkg.meta.license 0
else
pkg.meta.license;
in "License: ${license.fullName}")
<.ul>
<.details>
<.div>
```Insert a raw unescaped string into your HTML:
```nix
"Look ma! So unsafe!"<.title>
<.head>
(raw ''
Please don't do this at home...
'')<.body>
```Call another Nix file as a HTMNIX file, with all the magic:
```nix
# -- inside comment.nix --
{ name, comment }:{class="review";}
{src="/assets/${lib.replaceStrings [ " " ] [ "-" ] name}-headshot.webp";}
name<.h2>
<.figcaption>comment<.p>
<.div># -- inside html.nix --
let
comments = [
{ name = "John Doe"; comment = "Very nice service, reversed my hair loss!"; }
{ name = "Joe"; comment = "Didn't work for me. 0/10."; }
{ name = "Skid"; comment = "alert('Got you!')"; } # Does not work as all strings are escaped by default.
];
in- (call ./comment.nix comment)<.li>)
comments)
<.ul>
```## License
```
MIT LicenseCopyright (c) 2024-present RGBCube
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
```
(map
(comment: - (call ./comment.nix comment)<.li>)
- "Full name: ${pkg.name}"
(lib.mapAttrsToList
(name: type: - "${name} (${type})"<.li>)
>