From a9556bd812ba336f797ce1321e47550e99549552 Mon Sep 17 00:00:00 2001 From: Mohammad Rafiq Date: Sun, 6 Jul 2025 05:06:37 +0800 Subject: [PATCH] feat: init repo --- flake.nix | 3 ++ module.nix | 80 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 83 insertions(+) create mode 100644 flake.nix create mode 100644 module.nix diff --git a/flake.nix b/flake.nix new file mode 100644 index 0000000..3ad2932 --- /dev/null +++ b/flake.nix @@ -0,0 +1,3 @@ +{ + outputs = inputs: { flakeModules.default = ./module.nix; }; +} diff --git a/module.nix b/module.nix new file mode 100644 index 0000000..ec95ffe --- /dev/null +++ b/module.nix @@ -0,0 +1,80 @@ +{ lib, ... }: +let + inherit (lib) + mapAttrsToList + optional + concatStrings + flatten + mkOption + mapAttrs + isString + replicate + flip + getAttr + concatStringsSep + ; + inherit (lib.types) + lazyAttrsOf + oneOf + submodule + str + listOf + ; + textType = oneOf [ + str + (submodule { + options = { + heading = mkOption { + type = str; + default = ""; + }; + description = mkOption { + type = str; + default = ""; + }; + order = mkOption { + type = listOf str; + default = [ ]; + }; + parts = mkOption { type = lazyAttrsOf textType; }; + }; + }) + ]; + mkListFromAttrs = + prefix: + { name, value }: + let + sectionHeading = result: "${concatStrings (replicate prefix "#")} ${result}"; + in + if isString value then + [ + (sectionHeading name) + value + ] + else + flatten [ + [ + (sectionHeading (if value.heading == "" then name else value.heading)) + ] + (optional (value.description != "") value.description) + (map (mkListFromAttrs (prefix + 1)) ( + if value.order == [ ] then + mapAttrsToList (name: value: { inherit name value; }) value.parts + else + map (x: { + name = x; + value = flip getAttr value.parts x; + }) value.order + )) + ]; +in +{ + options.text = mkOption { + default = { }; + type = lazyAttrsOf textType; + apply = mapAttrs ( + name: value: concatStringsSep "\n" (flatten (mkListFromAttrs 1 { inherit name value; })) + ); + }; +} +