Initial commit

This commit is contained in:
niten 2022-06-21 11:56:49 -07:00
commit 57cad0cd85
4 changed files with 61 additions and 0 deletions

13
flake.nix Normal file
View File

@ -0,0 +1,13 @@
{
description = "Nix-to-XML converter.";
inputs = {
nixpkgs.url = "nixpkgs/nixos-22.05";
utils.url = "github:numtide/flake-utils";
};
outputs = { self, nixpkgs, utils }: {
lib.toXML = import ./to-xml.nix { inherit (nixpkgs) lib; };
overlay = final: prev: { lib = prev.lib // { toXML = self.lib.toXML; }; };
};
}

5
to-formatted-xml.nix Normal file
View File

@ -0,0 +1,5 @@
{ lib, toXMLFile, ... }:
content:
let xmlFile = toXMLFile "formatted-xml" content;
in lib.readFile xmlFile

10
to-xml-file.nix Normal file
View File

@ -0,0 +1,10 @@
{ lib, pkgs, toXML, ... }:
name: content:
let preformatXML = pkgs.writeText "${name}-preformat.xml" (toXML content);
in pkgs.stdenv.mkDerivation {
name = "${name}.xml";
phases = [ "installPhase" ];
buildInputs = with pkgs; [ xmlformat ];
installPhase = "xmlformat ${preformatXML} > $out";
}

33
to-xml.nix Normal file
View File

@ -0,0 +1,33 @@
{ config, lib, pkgs, ... }:
with lib;
let
isEmpty = lst: (length lst) == 0;
isEmptyAttrs = attrs: (length (attrNames attrs)) == 0;
renderAttr = name: value: ''${name}="${value}"'';
renderAttrs = attrs:
if (isEmptyAttrs attrs) then
""
else
" " + concatStringsSep " " (mapAttrsToList renderAttr attrs);
renderTag = tag: attrs: body: "<${tag}${renderAttrs attrs}>${body}</${tag}>";
# renderLoneTag = tag: attrs: "<${tag}${renderAttrs attrs} />";
toXML = { tag, attrs ? { }, children ? [ ] }:
let
renderElement = el: if isString el then el else toXML el;
body = concatStringsSep "" (map renderElement children);
in renderTag tag attrs body;
toXMLFile = name: content:
let preformatXML = pkgs.writeText "${name}-preformat.xml" (toXML content);
in pkgs.stdenv.mkDerivation {
name = "${name}.xml";
phases = [ "installPhase" ];
buildInputs = with pkgs; [ xmlformat ];
installPhase = "xmlformat ${preformatXML} > $out";
};
toFormattedXML = content:
let xmlFile = toXMLFile "formatted-xml" content;
in readFile xmlFile;
in { inherit toXML toXMLFile toFormattedXML; }