### Initialize Project Directory Source: https://github.com/arnarg/nixtml/blob/main/docs/content/index.md Commands to create the base directory structure for a new nixtml website. ```bash mkdir my-website cd my-website ``` -------------------------------- ### Build and Serve nixtml Website Source: https://github.com/arnarg/nixtml/blob/main/docs/content/index.md Commands to build the static site using Nix and serve it locally for development. These commands interact with the project's flake configuration. ```bash nix build .# ls -la result/ nix run .#serve ``` -------------------------------- ### Configure flake.nix for nixtml Source: https://github.com/arnarg/nixtml/blob/main/docs/content/index.md The main configuration file defining site metadata, content directories, and a local development server app. ```nix { description = "My website generated using nixtml."; inputs = { nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable"; flake-utils.url = "github:numtide/flake-utils"; nixtml.url = "github:arnarg/nixtml"; }; outputs = { self, nixpkgs, flake-utils, nixtml, }: flake-utils.lib.eachDefaultSystem ( system: let pkgs = import nixpkgs { inherit system; }; in { packages.default = nixtml.lib.mkWebsite { inherit pkgs; name = "my-website"; baseURL = "https://my-website.com"; metadata = { lang = "en"; title = "My Website"; description = "Welcome to my website"; }; content.dir = ./content; imports = [ ./layouts.nix ]; }; apps.serve = { type = "app"; program = (pkgs.writeShellScript "serve" '' echo "Serving at http://localhost:8080" ${pkgs.python3}/bin/python -m http.server -d ${self.packages.${system}.default} 8080 '').outPath; }; } ); } ``` -------------------------------- ### Enable Nix Flakes Source: https://github.com/arnarg/nixtml/blob/main/docs/content/index.md Configuration to enable experimental Nix features required for nixtml projects. ```text experimental-features = nix-command flakes ``` -------------------------------- ### Nixtml Complete Blog Setup (flake.nix) Source: https://github.com/arnarg/nixtml/blob/main/docs/content/collections.md A comprehensive example of a `flake.nix` file for setting up a blog with Nixtml. It includes input definitions, system configuration, website metadata, content directory, collection settings, and layout imports. ```nix { description = "My blog"; inputs = { nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable"; flake-utils.url = "github:numtide/flake-utils"; nixtml.url = "github:arnarg/nixtml"; }; outputs = { self, nixpkgs, flake-utils, nixtml }: flake-utils.lib.eachDefaultSystem (system: let pkgs = import nixpkgs { inherit system; }; in { packages.default = nixtml.lib.mkWebsite { inherit pkgs; name = "my-blog"; baseURL = "https://my-blog.com"; metadata = { lang = "en"; title = "My Blog"; description = "Thoughts on Nix and programming"; }; content.dir = ./content; collections.blog = { path = "blog/posts"; pagination.perPage = 5; taxonomies = [ "tags" ]; rss.enable = true; }; imports = [ ./layouts.nix ]; }; } ); } ``` -------------------------------- ### Define Website Layouts Source: https://github.com/arnarg/nixtml/blob/main/docs/content/index.md A Nix-based layout configuration using HTML helper functions to define the structure of pages, collections, and taxonomies. ```nix { lib, config, ... }: let inherit (config.website) metadata; inherit (lib.tags) html head body div h1 p a nav ul li meta title link; inherit (lib) attrs; in { website.layouts = { base = { path, content, ... }: "\n" + (html [ (attrs.lang metadata.lang) ] [ (head [] [ (meta [ (attrs.charset "UTF-8") ]) (meta [ (attrs.name "viewport") (attrs.content "width=device-width, initial-scale=1.0") ]) (meta [ (attrs.name "description") (attrs.content metadata.description) ]) (title metadata.title) ]) (body [] [ (nav [] [ (ul [] [ (li [] [ (a [ (attrs.href "/") ] [ "Home" ]) ]) (li [] [ (a [ (attrs.href "/about") ] [ "About" ]) ]) ]) ]) (div [ (attrs.class "container") ] [ content ]) ]) ]); home = { content, ... }: content; page = { metadata, content, ... }: [ (h1 [] [ metadata.title ]) content ]; collection = { pageNumber, totalPages, items, ... }: (div [] [ (ul [] (map (item: li [] [ item.title ]) items)) "Page ${toString pageNumber} of ${toString totalPages}" ]); taxonomy = { title, pageNumber, totalPages, items, ... }: (div [] [ (h1 [] [ title ]) (ul [] (map (item: li [] [ item.title ]) items)) "Page ${toString pageNumber} of ${toString totalPages}" ]); }; } ``` -------------------------------- ### Configure Static Assets and Styling Source: https://github.com/arnarg/nixtml/blob/main/docs/content/index.md Instructions for adding CSS styles and configuring the static directory in the project's flake.nix file. This allows for custom visual themes. ```bash mkdir -p static/css ``` ```css * { box-sizing: border-box; } body { font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif; line-height: 1.6; color: #333; max-width: 800px; margin: 0 auto; padding: 20px; } nav ul { list-style: none; padding: 0; display: flex; gap: 20px; border-bottom: 1px solid #eee; padding-bottom: 10px; margin-bottom: 20px; } nav a { text-decoration: none; color: #0066cc; } nav a:hover { text-decoration: underline; } h1 { color: #111; } .container { padding: 20px 0; } ``` ```nix # Uncomment this line in mkWebsite: static.dir = ./static; ``` -------------------------------- ### Define Markdown Frontmatter Source: https://github.com/arnarg/nixtml/blob/main/docs/content/index.md Example of YAML frontmatter used in markdown files to define page metadata. This data is accessible within templates. ```markdown --- title: Page Title date: 2026-01-19T00:00:00 customField: any value --- Content here... ``` -------------------------------- ### Configure Nixtml via flake.nix Source: https://github.com/arnarg/nixtml/blob/main/docs/content/taxonomies.md A complete example of a flake.nix file configuring a Nixtml website, including metadata, content directory, and collection taxonomies. ```nix { inputs = { nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable"; flake-utils.url = "github:numtide/flake-utils"; nixtml.url = "github:arnarg/nixtml"; }; outputs = { self, nixpkgs, flake-utils, nixtml }: flake-utils.lib.eachDefaultSystem (system: let pkgs = import nixpkgs { inherit system; }; in { packages.default = nixtml.lib.mkWebsite { inherit pkgs; name = "my-blog"; baseURL = "https://my-blog.com"; metadata = { lang = "en"; title = "My Blog"; }; content.dir = ./content; collections.blog = { path = "blog/posts"; pagination.perPage = 5; taxonomies = [ "tags" "categories" ]; }; imports = [ ./layouts.nix ]; }; } ); } ``` -------------------------------- ### Complete Taxonomy Template Example Source: https://github.com/arnarg/nixtml/blob/main/docs/content/taxonomies.md A comprehensive nixtml taxonomy layout that includes a header, post count, a list of post previews (with title, date, and summary), and pagination controls. It utilizes various HTML-like elements provided by nixtml's library. ```nix { lib, config, ... }: let inherit (lib.tags) div h1 h2 ul li a p span article; inherit (lib) attrs; in { website.layouts.taxonomy = { title, pageNumber, totalPages, items, hasNext, hasPrev, nextPageURL, prevPageURL, ... }: [ # Header with term name (h1 [] [ "Posts tagged: #${title}" ]) # Post count (p [ (attrs.class "taxonomy-meta") ] [ "${toString (builtins.length items)} posts" ]) # List of posts (div [ (attrs.class "post-list") ] (map (item: article [ (attrs.class "post-preview") ] [ (h2 [] [ (a [ (attrs.href item.url) ] [ item.title ]) ]) (div [ (attrs.class "post-meta") ] [ (span [ (attrs.class "date") ] [ item.date ]) ]) (p [ (attrs.class "summary") ] [ item.summary ]) ] ) items) ) # Pagination (div [ (attrs.class "pagination") ] [ (if hasPrev then (a [ (attrs.href prevPageURL) (attrs.class "prev") ] [ "← Newer Posts" ]) else (span [ (attrs.class "prev disabled") ] [ "← Newer Posts" ])) (span [ (attrs.class "page-info") ] [ "Page ${toString pageNumber} of ${toString totalPages}" ]) (if hasNext then (a [ (attrs.href nextPageURL) (attrs.class "next") ] [ "Older Posts →" ]) else (span [ (attrs.class "next disabled") ] [ "Older Posts →" ])) ]) ]; } ``` -------------------------------- ### Structure Markdown Content with Frontmatter Source: https://context7.com/arnarg/nixtml/llms.txt Example of a Markdown file structure compatible with nixtml, featuring YAML frontmatter for metadata and the tag for content summarization. ```markdown --- title: Getting Started with Nix Flakes date: 2026-01-19T10:00:00 tags: - nix - flakes --- Introduction text. ## What are Flakes? Flakes provide hermetic evaluation. ``` -------------------------------- ### Nix Functional HTML Tag Usage Examples Source: https://github.com/arnarg/nixtml/blob/main/docs/content/templating.md Provides examples of how to use tag functions from nixtml's functional HTML library. Each tag function accepts a list of attributes and a list of children, allowing for nested structures and attribute assignment. ```nix # Examples (div [] [ "Hello" ]) #
Hello
(div [ (attrs.class "box") ] [ "Hi" ]) #
Hi
(a [ (attrs.href "/") ] [ "Home" ]) # Home ``` -------------------------------- ### Nixtml Taxonomy Setup Source: https://github.com/arnarg/nixtml/blob/main/docs/content/collections.md Configures Nixtml to generate taxonomy pages (like tags and categories) for content. This involves specifying the content path and the taxonomies to be used. ```nix website.collections.blog = { path = "blog/posts"; taxonomies = [ "tags" "categories" ]; }; ``` -------------------------------- ### Nix Custom Attribute Creation Example Source: https://github.com/arnarg/nixtml/blob/main/docs/content/templating.md Demonstrates how to create custom HTML attributes using `lib.mkAttr` in nixtml. This is useful for attributes not pre-defined in the `attrs` library, allowing for flexible attribute generation. ```nix let customAttr = lib.mkAttr "data-custom" "value"; in (div [ customAttr ] [ "Content" ]) #
Content
``` -------------------------------- ### Nix Functional HTML Template Example Source: https://github.com/arnarg/nixtml/blob/main/docs/content/templating.md Illustrates creating an HTML structure using nixtml's functional HTML library. This method provides type safety and a more programmatic way to build HTML. It leverages Nix functions to represent HTML tags and attributes. ```nix { lib, ... }: let inherit (lib.tags) html head body meta title div; inherit (lib) attrs; in { website.layouts.base = { path, content, ... }: "\n" + (html [ (attrs.lang "en") ] [ (head [] [ (meta [ (attrs.charset "UTF-8") ]) (title "My Website") ]) (body [] [ content ]) ]); } ``` -------------------------------- ### Nix String Template Example Source: https://github.com/arnarg/nixtml/blob/main/docs/content/templating.md Demonstrates using Nix string interpolation for a basic HTML template. This approach is suitable for simpler templating needs where type safety is not a primary concern. It directly embeds dynamic content within a Nix string. ```nix website.layouts.base = { path, content, ... }: '' My Website ${content} ''; ``` -------------------------------- ### Nix Functional HTML Void Tag Examples Source: https://github.com/arnarg/nixtml/blob/main/docs/content/templating.md Demonstrates the usage of void (self-closing) tags within nixtml's functional HTML library. These tags, such as meta, link, img, and br, only accept attributes and do not have closing tags. ```nix # Void tags only take attributes: (meta [ (attrs.charset "UTF-8") ]) # (link [ (attrs.rel "stylesheet") (attrs.href "/style.css") ]) (img [ (attrs.src "/logo.png") (attrs.alt "Logo") ]) (br []) #
``` -------------------------------- ### Nix Functional HTML Attribute Helper Examples Source: https://github.com/arnarg/nixtml/blob/main/docs/content/templating.md Showcases the use of attribute helper functions provided by nixtml's `attrs` module. These helpers simplify the creation of common HTML attributes, including common ones, width/height, multiple classes, data attributes, ARIA attributes, and meta attributes. ```nix inherit (lib) attrs; # Common attributes (attrs.id "main") # id="main" (attrs.class "container") # class="container" (attrs.href "/about") # href="/about" (attrs.src "/image.png") # src="/image.png" (attrs.alt "Description") # alt="Description" (attrs.type "text") # type="text" (attrs.name "email") # name="email" (attrs.content "value") # content="value" (attrs.rel "stylesheet") # rel="stylesheet" (attrs.lang "en") # lang="en" (attrs.charset "UTF-8") # charset="UTF-8" (attrs.target "_blank") # target="_blank" (attrs.title "Hover text") # title="Hover text" # Width/height (accepts int or string) (attrs.width 100) # width="100" (attrs.height "auto") # height="auto" # Multiple classes (attrs.classes [ "flex" "items-center" "gap-4" ]) # class="flex items-center gap-4" # Data attributes (attrs.data "theme" "dark") # data-theme="dark" (attrs.data "count" "5") # data-count="5" # ARIA attributes (attrs.aria "label" "Menu") # aria-label="Menu" (attrs.aria "hidden" "true") # aria-hidden="true" # Meta attributes (attrs.property "og:title") # property="og:title" (attrs.httpEquiv "refresh") # http-equiv="refresh" (attrs.itemprop "name") # itemprop="name" ``` -------------------------------- ### Configure Website Collections in Nix Source: https://github.com/arnarg/nixtml/blob/main/docs/content/collections.md Define a collection in your nixtml website configuration by specifying the content path and pagination settings. This setup enables automatic generation of list pages and RSS feeds. ```nix { website.collections.blog = { path = "posts"; pagination.perPage = 5; }; } ``` -------------------------------- ### Build and Serve Commands Source: https://context7.com/arnarg/nixtml/llms.txt Shell commands for building the static site, inspecting the output, and running the development server. ```bash nix build .# ls -la result/ nix run .#serve nix build .#examples.simple nix run .#serveDocs ``` -------------------------------- ### Create Nixtml Website with mkWebsite (Nix) Source: https://context7.com/arnarg/nixtml/llms.txt The `mkWebsite` function is the primary entry point for creating a Nixtml website. It takes configuration options such as website name, base URL, metadata, content and static directories, collections for pagination and RSS, and layout imports. It returns a derivation containing the built static site. ```nix { description = "My website generated using nixtml."; inputs = { nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable"; flake-utils.url = "github:numtide/flake-utils"; nixtml.url = "github:arnarg/nixtml"; }; outputs = { self, nixpkgs, flake-utils, nixtml }: flake-utils.lib.eachDefaultSystem (system: let pkgs = import nixpkgs { inherit system; }; in { packages.default = nixtml.lib.mkWebsite { inherit pkgs; # Required: Website name (used in derivation) name = "my-blog"; # Required: Full URL where site will be hosted baseURL = "https://my-blog.com"; # Arbitrary metadata available in all templates metadata = { lang = "en"; title = "My Blog"; description = "A blog about Nix"; }; # Directory containing markdown files content.dir = ./content; # Directory with static assets (CSS, images, etc.) static.dir = ./static; # Collections for pagination and RSS collections.blog = { path = "posts"; pagination.perPage = 5; taxonomies = [ "tags" ]; rss.enable = true; }; # Import layout modules imports = [ ./layouts.nix ]; }; # Development server apps.serve = { type = "app"; program = (pkgs.writeShellScript "serve" '' echo "Serving at http://localhost:8080" ${pkgs.python3}/bin/python -m http.server -d ${self.packages.${system}.default} 8080 '').outPath; }; } ); } ``` -------------------------------- ### Implement Reusable Partials in Nix Source: https://context7.com/arnarg/nixtml/llms.txt Shows how to define reusable UI components like navigation, meta tags, and footers within the website.layouts.partials namespace. These partials are then composed into a base layout template. ```nix { website.layouts = { partials = { navigation = { currentPath }: nav [ (attrs.class "main-nav") ] [ (ul [] [ (li [] [ (a [ (attrs.href "/") ] [ "Home" ]) ]) ]) ]; metaTags = { pageTitle, description ? metadata.description }: [ (meta [ (attrs.name "description") (attrs.content description) ]) ]; siteFooter = { }: footer [ (attrs.class "site-footer") ] [ (p [] [ "© 2026" ]) ]; }; base = { path, content, title, ... }: (html [] [ (head [] (partials.metaTags { inherit pageTitle; })) (body [] [ (partials.navigation {}) (div [] [ content ]) (partials.siteFooter {}) ]) ]); }; } ``` -------------------------------- ### Initialize flake.nix Source: https://context7.com/arnarg/nixtml/llms.txt The boilerplate structure for a new nixtml project, defining inputs and output placeholders. ```nix { description = "My first flake"; inputs.nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable"; outputs = { self, nixpkgs }: { # Your outputs here }; } ``` -------------------------------- ### Nix Flake Configuration for nixtml Website Source: https://github.com/arnarg/nixtml/blob/main/README.md This Nix flake configuration sets up a new website using nixtml. It defines inputs like nixpkgs and nixtml, and configures the website package with metadata, content directories, static assets, collections, and imports for themes. It also includes an app to serve the website locally using Python's http.server. ```nix { description = "My website generated using nixtml."; inputs = { nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable"; flake-utils.url = "github:numtide/flake-utils"; nixtml.url = "github:arnarg/nixtml"; }; outputs = { self, nixpkgs, flake-utils, nixtml, }: (flake-utils.lib.eachDefaultSystem ( system: let pkgs = import nixpkgs { inherit system; }; in { packages.blog = nixtml.lib.mkWebsite { inherit pkgs; name = "my-blog"; baseURL = "https://my-blog.com"; # Arbitrary metdata to be used in # templates. metadata = { lang = "en"; title = "My Blog"; description = "This is my blog"; }; # Walk a directory of markdown files # and create a page for each of them. content.dir = ./content; # Copy an entire directory and symlink # in the final website derivation. static.dir = ./static; # Collections are for paginating content # and generating RSS feeds. collections.blog = { path = "posts"; # Posts in the collection should be # grouped by optional tags in posts' # frontmatter. taxonomies = [ "tags" ]; }; # Import any nixtml modules (good for # "themes"). imports = [ ./theme.nix ]; }; # Quickly build and serve website with # `nix run .#serve`. apps.serve = { type = "app"; program = (pkgs.writeShellScript "serve-blog" "''${pkgs.python3}/bin/python -m http.server -d ${self.packages.${system}.blog} 8080") .outPath; }; } )); } ``` -------------------------------- ### Nixtml Content Front Matter Source: https://github.com/arnarg/nixtml/blob/main/docs/content/collections.md Example of front matter metadata within a content file, used by Nixtml to define post properties like title, date, tags, and categories. ```markdown --- title: Getting Started with Nix date: 2026-01-19 tags: - nix - beginner categories: - tutorials --- ``` -------------------------------- ### Define Website Collection Layout in Nix Source: https://context7.com/arnarg/nixtml/llms.txt Demonstrates how to map a collection of items into HTML article elements using Nix DSL. It utilizes frontmatter metadata such as title, date, and content to generate structured blog post listings. ```nix { lib, ... }: let inherit (lib.tags) article h2 p a div span time; inherit (lib) attrs; in { website.layouts.collection = { items, ... }: (map (item: article [] [ (h2 [] [ item.title ]) (p [] [ item.date ]) (time [ (attrs.data "datetime" item.dateW3C) ] [ item.date ]) (a [ (attrs.href item.url) ] [ "Read more" ]) (p [] [ item.summary ]) (div [] [ item.content ]) (if (item.metadata.tags or []) != [] then (div [ (attrs.class "tags") ] (map (tag: span [] [ "#${tag} " ]) item.metadata.tags)) else "") ] ) items); } ``` -------------------------------- ### Define website layouts using Nix functions Source: https://context7.com/arnarg/nixtml/llms.txt This module defines a set of layout functions for a static website. Each function accepts a context object and returns HTML elements or strings, utilizing a library of tag functions to build the document structure. ```nix { lib, config, ... }: let inherit (config.website) metadata; inherit (lib.tags) html head body div h1 h2 p a ul li meta link article nav; inherit (lib.tags) title; inherit (lib) attrs; in { website.layouts = { base = { path, content, title, ... }: let pageTitle = if title != null then "${title} - ${metadata.title}" else metadata.title; in "\n" + (html [ (attrs.lang metadata.lang) ] [ (head [] [ (meta [ (attrs.charset "UTF-8") ]) (meta [ (attrs.name "viewport") (attrs.content "width=device-width, initial-scale=1") ]) (lib.tags.title pageTitle) (link [ (attrs.rel "stylesheet") (attrs.href "/css/style.css") ]) ]) (body [] [ (nav [] [ (a [ (attrs.href "/") ] [ "Home" ]) " | " (a [ (attrs.href "/blog") ] [ "Blog" ]) ]) (div [ (attrs.class "content") ] [ content ]) ]) ]); home = { content, ... }: content; page = { metadata, content, ... }: [ (h1 [] [ metadata.title ]) (if metadata ? date then (p [ (attrs.class "date") ] [ metadata.date ]) else "") content ]; collection = { pageNumber, totalPages, items, hasNext, hasPrev, nextPageURL, prevPageURL, ... }: [ (h1 [] [ "Blog Posts" ]) (div [ (attrs.class "posts") ] (map (item: article [] [ (h2 [] [ (a [ (attrs.href item.url) ] [ item.title ]) ]) (p [] [ item.date ]) (p [] [ item.summary ]) ] ) items)) (nav [ (attrs.class "pagination") ] [ (if hasPrev then (a [ (attrs.href prevPageURL) ] [ "← Newer" ]) else "") " Page ${toString pageNumber} of ${toString totalPages} " (if hasNext then (a [ (attrs.href nextPageURL) ] [ "Older →" ]) else "") ]) ]; taxonomy = { title, pageNumber, totalPages, items, hasNext, hasPrev, nextPageURL, prevPageURL, ... }: [ (h1 [] [ "Tagged: #${title}" ]) (ul [] (map (item: li [] [ (a [ (attrs.href item.url) ] [ item.title ]) ]) items)) (div [] [ (if hasPrev then (a [ (attrs.href prevPageURL) ] [ "← Prev" ]) else "") " Page ${toString pageNumber}/${toString totalPages} " (if hasNext then (a [ (attrs.href nextPageURL) ] [ "Next →" ]) else "") ]) ]; }; } ``` -------------------------------- ### Nix Functional HTML Library Usage Source: https://context7.com/arnarg/nixtml/llms.txt Demonstrates how to use the lib.tags module to construct HTML elements. It covers regular tags, void tags, nested structures, and attribute application. Dependencies include the 'lib' and 'lib.tags' modules. ```nix { lib, ... }: let inherit (lib.tags) # Document structure html head body # Sections header footer main nav section article aside # Headings h1 h2 h3 h4 h5 h6 # Text content p div span pre blockquote # Lists ul ol li # Inline elements a em strong code # Tables table thead tbody tr th td # Forms form fieldset label button select option textarea input # Media figure figcaption img # Void tags (self-closing) meta link br hr ; inherit (lib.tags) title; inherit (lib) attrs; in { # Tag signature: tagName [ attributes ] [ children ] # Regular tags with closing element example1 = div [] [ "Hello" ]; # Output:
Hello
example2 = div [ (attrs.class "container") ] [ "Content" ]; # Output:
Content
example3 = a [ (attrs.href "/about") (attrs.class "link") ] [ "About Us" ]; # Output: About Us # Nested tags example4 = ul [ (attrs.class "nav") ] [ (li [] [ (a [ (attrs.href "/") ] [ "Home" ]) ]) (li [] [ (a [ (attrs.href "/blog") ] [ "Blog" ]) ]) (li [] [ (a [ (attrs.href "/about") ] [ "About" ]) ]) ]; # Output: # Void tags (only attributes, no children) example5 = meta [ (attrs.charset "UTF-8") ]; # Output: example6 = link [ (attrs.rel "stylesheet") (attrs.href "/style.css") ]; # Output: example7 = img [ (attrs.src "/logo.png") (attrs.alt "Logo") ]; # Output: Logo example8 = br []; # Output:
# Title convenience function (string instead of list) example9 = title "My Page Title"; # Output: My Page Title } ``` -------------------------------- ### Configure Content Collections in Nix Source: https://github.com/arnarg/nixtml/blob/main/README.md Defines a content collection by specifying the source directory, pagination settings, and RSS generation. This configuration enables automatic generation of paginated listing pages. ```nix website.collections.blog = { path = "blog/posts"; # ./content/blog/posts/ pagination.perPage = 5; # Number of items each listing page shows rss.enable = true; # Generate /blog/index.xml }; ``` -------------------------------- ### Access Site Configuration Source: https://github.com/arnarg/nixtml/blob/main/docs/content/templating.md Demonstrates how to access site metadata and base URI settings from the configuration object within a Nixtml template. ```nix { lib, config, ... }: let # Access website configuration inherit (config.website) metadata baseURL baseURI; # metadata contains your custom fields siteTitle = metadata.title; siteLang = metadata.lang; # URLs fullUrl = baseURL; # "https://example.com" basePath = baseURI; # "/" or "/blog/" if baseURL has path in { website.layouts.base = { path, content, ... }: ...; } ``` -------------------------------- ### Enable Taxonomies for Collections Source: https://github.com/arnarg/nixtml/blob/main/README.md Extends a collection configuration to include taxonomies like tags or series. This allows nixtml to generate index pages for specific terms. ```nix website.collections.blog = { path = "blog/posts"; taxonomies = [ "tags" "series" ]; }; ``` -------------------------------- ### Access Template Context Variables Source: https://github.com/arnarg/nixtml/blob/main/README.md Lists the available attributes provided to collection and taxonomy layout templates. These variables facilitate pagination and navigation within the generated site. ```nix { # --- collection & taxonomy ------------- pageNumber, # Current page number totalPages, # Total amount of pages items, # List of posts in this page hasNext, # A next page exists (bool) hasPrev, # A previous page exists (bool) nextPageURL, # URL to next page prevPageURL, # URL to previous page # --- only taxonomy -------------------- title, # The tag or term being shown } ``` -------------------------------- ### Implement String-based Templates Source: https://context7.com/arnarg/nixtml/llms.txt Uses Nix string interpolation to define website layouts, providing a simpler alternative to the functional HTML library. ```nix { lib, config, ... }: let inherit (config.website) metadata; in { website.layouts = { base = { path, content, ... }: '' ${metadata.title}
${content}
''; collection = { pageNumber, totalPages, items, ... }: ''

Blog

''; }; } ``` -------------------------------- ### Nix HTML Attributes Helper Functions Source: https://context7.com/arnarg/nixtml/llms.txt Shows how to use the lib.attrs module for generating HTML attributes. It includes common attributes, builders for data and ARIA attributes, and handling for multiple classes. Requires the 'lib' and 'lib.attrs' modules. ```nix { lib, ... }: let inherit (lib) attrs; in { # Common attributes id_attr = attrs.id "main"; # id="main" class_attr = attrs.class "container"; # class="container" href_attr = attrs.href "/about"; # href="/about" src_attr = attrs.src "/image.png"; # src="/image.png" alt_attr = attrs.alt "Description"; # alt="Description" type_attr = attrs.type "text"; # type="text" name_attr = attrs.name "email"; # name="email" content_attr = attrs.content "value"; # content="value" rel_attr = attrs.rel "stylesheet"; # rel="stylesheet" lang_attr = attrs.lang "en"; # lang="en" charset_attr = attrs.charset "UTF-8"; # charset="UTF-8" target_attr = attrs.target "_blank"; # target="_blank" title_attr = attrs.title "Tooltip"; # title="Tooltip" # Width/height (accepts int or string) width_attr = attrs.width 100; # width="100" height_attr = attrs.height "auto"; # height="auto" # Multiple classes classes_attr = attrs.classes [ "flex" "items-center" "gap-4" ]; # Output: class="flex items-center gap-4" # Data attributes data_attr = attrs.data "theme" "dark"; # data-theme="dark" data_attr2 = attrs.data "count" "5"; # data-count="5" # ARIA attributes aria_attr = attrs.aria "label" "Menu"; # aria-label="Menu" aria_attr2 = attrs.aria "hidden" "true"; # aria-hidden="true" # Meta attributes property_attr = attrs.property "og:title"; # property="og:title" httpEquiv_attr = attrs.httpEquiv "refresh"; # http-equiv="refresh" itemprop_attr = attrs.itemprop "name"; # itemprop="name" } ``` -------------------------------- ### Escape HTML Entities with lib.escapeHTML Source: https://context7.com/arnarg/nixtml/llms.txt Demonstrates how to use the functional HTML library to sanitize user input and safely render content in page layouts. ```nix { lib, ... }: let inherit (lib) escapeHTML; inherit (lib.tags) p div a; inherit (lib) attrs; in { userInput = ""; safeContent = escapeHTML userInput; website.layouts.page = { metadata, content, ... }: [ (p [] [ escapeHTML metadata.title ]) content ]; website.layouts.collection = { hasNext, hasPrev, nextPageURL, prevPageURL, ... }: (div [] [ (if hasPrev then (a [ (attrs.href prevPageURL) ] [ (escapeHTML "< Prev") ]) else "") (if hasNext then (a [ (attrs.href nextPageURL) ] [ (escapeHTML "Next >") ]) else "") ]); } ``` -------------------------------- ### Nixtml Website Layout Generation (Nix) Source: https://github.com/arnarg/nixtml/blob/main/docs/content/templating.md This Nix code defines a set of functions for generating website layouts using the Nixtml library. It includes reusable partials for common HTML elements like head, header, and footer, as well as templates for different page types such as home, articles, collections, and taxonomies. The code leverages Nix's attribute sets and functions for dynamic content generation and relies on `lib.tags` for HTML element creation. ```nix { lib, config, ... }: let inherit (config.website) metadata baseURL; inherit (config.website.layouts) partials; inherit (lib.tags) html head body header main footer nav div section article h1 h2 h3 p span a ul ol li meta link time img ; inherit (lib.tags) title; inherit (lib) attrs escapeHTML; in { website.layouts = { # Reusable partials partials = { head = { pageTitle, description ? metadata.description }: [ (meta [ (attrs.charset "UTF-8") ]) (meta [ (attrs.name "viewport") (attrs.content "width=device-width, initial-scale=1") ]) (meta [ (attrs.name "description") (attrs.content description) ]) (meta [ (attrs.property "og:title") (attrs.content pageTitle) ]) (meta [ (attrs.property "og:type") (attrs.content "website") ]) (title pageTitle) (link [ (attrs.rel "stylesheet") (attrs.href "/css/style.css") ]) (link [ (attrs.rel "alternate") (attrs.type "application/rss+xml") (attrs.href "/blog/index.xml") ]) ]; header = { }: header [ (attrs.class "site-header") ] [ (div [ (attrs.class "container") ] [ (a [ (attrs.href "/") (attrs.class "logo") ] [ metadata.title ]) (nav [ (attrs.class "main-nav") ] [ (ul [] [ (li [] [ (a [ (attrs.href "/") ] [ "Home" ]) ]) (li [] [ (a [ (attrs.href "/blog") ] [ "Blog" ]) ]) (li [] [ (a [ (attrs.href "/about") ] [ "About" ]) ]) ]) ]) ]) ]; footer = { }: footer [ (attrs.class "site-footer") ] [ (div [ (attrs.class "container") ] [ (p [] [ "© 2026 ${metadata.title}. All rights reserved." ]) (p [] [ "Built with " (a [ (attrs.href "https://github.com/arnarg/nixtml") ] [ "nixtml" ]) ]) ]) ]; postMeta = { item }: div [ (attrs.class "post-meta") ] [ (time [ (attrs.data "datetime" item.dateW3C) ] [ item.date ]) (if (item.metadata.tags or []) != [] then (span [ (attrs.class "tags") ] (map (tag: a [ (attrs.href "/blog/tags/${tag}") (attrs.class "tag") ] [ "#${tag}" ] ) item.metadata.tags)) else "") ]; }; # Base template base = { path, content, title, ... }: let pageTitle = if title != null then "${title} - ${metadata.title}" else metadata.title; in "\n" + (html [ (attrs.lang metadata.lang) ] [ (head [] (partials.head { inherit pageTitle; })) (body [ (attrs.class "page") ] [ (partials.header {}) (main [ (attrs.class "main-content") ] [ (div [ (attrs.class "container") ] [ content ]) ]) (partials.footer {}) ]) ]); # Homepage home = { content, ... }: section [ (attrs.class "homepage") ] [ content ]; # Regular pages page = { metadata, content, ... }: article [ (attrs.class "page-content") ] [ (header [] [ (h1 [] [ metadata.title ]) (if metadata ? date then (time [] [ metadata.date ]) else "") ]) (div [ (attrs.class "content") ] [ content ]) ]; # Collection listing collection = { pageNumber, totalPages, items, hasNext, hasPrev, nextPageURL, prevPageURL, ... }: [ (h1 [] [ "Blog" ]) (div [ (attrs.class "posts") ] (map (item: article [ (attrs.class "post-preview") ] [ (h2 [] [ (a [ (attrs.href item.url) ] [ item.title ]) ]) (partials.postMeta { inherit item; }) (p [ (attrs.class "summary") ] [ item.summary ]) (a [ (attrs.href item.url) (attrs.class "read-more") ] [ "Read more →" ]) ] ) items)) (nav [ (attrs.class "pagination") ] [ (if hasPrev then (a [ (attrs.href prevPageURL) (attrs.class "prev") ] [ "← Newer Posts" ]) else (span [ (attrs.class "prev disabled") ] [ "← Newer Posts" ])) (span [ (attrs.class "page-info") ] [ "Page ${toString pageNumber} of ${toString totalPages}" ]) (if hasNext then (a [ (attrs.href nextPageURL) (attrs.class "next") ] [ "Older Posts →" ]) else (span [ (attrs.class "next disabled") ] [ "Older Posts →" ])) ]) ]; # Taxonomy pages taxonomy = { title, pageNumber, totalPages, items, hasNext, hasPrev, nextPageURL, prevPageURL, ... }: [ (h1 [] [ "Posts tagged #${title}" ]) (p [ (attrs.class "taxonomy-meta") ] [ "${toString (builtins.length items)} posts" ]) ``` -------------------------------- ### Render List Items with Links and Dates (Nixtml) Source: https://github.com/arnarg/nixtml/blob/main/docs/content/templating.md This snippet demonstrates how to render a list of items, where each item includes a title linked to its URL and a date. It utilizes Nixtml's functional HTML syntax for creating list elements and attributes. ```nixtml (ul [ (attrs.class "post-list") ] (map (item: li [] [ (a [ (attrs.href item.url) ] [ item.title ]) (span [ (attrs.class "date") ] [ " — ${item.date}" ]) ] ) items)) ``` -------------------------------- ### Implement Pagination Controls (Nixtml) Source: https://github.com/arnarg/nixtml/blob/main/docs/content/templating.md This code illustrates how to create navigation for pagination using Nixtml. It conditionally renders 'Newer' and 'Older' links based on the presence of previous and next pages, and displays the current page number within the total number of pages. ```nixtml (nav [ (attrs.class "pagination") ] [ (if hasPrev then (a [ (attrs.href prevPageURL) ] [ "← Newer" ]) else "") (span [] [ " Page ${toString pageNumber}/${toString totalPages} " ]) (if hasNext then (a [ (attrs.href nextPageURL) ] [ "Older →" ]) else "") ]) ``` -------------------------------- ### Create and Use Reusable Partials Source: https://github.com/arnarg/nixtml/blob/main/docs/content/templating.md Partials are reusable template functions defined within the website.layouts.partials scope. They allow for modular UI components like navigation bars, footers, and meta tags that can be injected into base templates. ```nix website.layouts.partials = { navigation = { currentPath }: nav [ (attrs.class "main-nav") ] [ ... ]; siteFooter = { }: footer [ (attrs.class "site-footer") ] [ ... ]; metaTags = { title, description ? metadata.description }: [ ... ]; }; # Usage in base template base = { path, content, title, ... }: (partials.navigation { inherit currentPath; }) ``` -------------------------------- ### Process and Map Lists Source: https://github.com/arnarg/nixtml/blob/main/docs/content/templating.md Lists of items, such as blog posts or tags, are processed using the 'map' function. This allows for dynamic generation of HTML structures like lists or link collections based on input data. ```nix (ul [] (map (item: li [] [ (a [ (attrs.href item.url) ] [ item.title ]) ] ) items)) ```