From 03a43150a1e2a14794649048470651b94ebbf435 Mon Sep 17 00:00:00 2001 From: Mohammad Rafiq Date: Sun, 6 Jul 2025 05:14:55 +0800 Subject: [PATCH] refactor(text): extract text helper to rrvsh/text.nix This commit adds the rrvsh/text.nix flake to manage text generation. It also removes the old text generation helpers module and updates the README and flake.nix files to use the new flake. --- README.md | 7 +-- flake.lock | 18 +++++++- flake.nix | 2 + modules/docs/readme.nix | 4 -- modules/flake-parts/text.nix | 4 ++ modules/helpers/text.nix | 84 ------------------------------------ 6 files changed, 24 insertions(+), 95 deletions(-) create mode 100644 modules/flake-parts/text.nix delete mode 100644 modules/helpers/text.nix diff --git a/README.md b/README.md index 3b89d3b..899b255 100644 --- a/README.md +++ b/README.md @@ -5,9 +5,4 @@ This flake serves as a monorepo for my systems (using IaC), dotfiles, and script This flake uses the [files flake-parts module](https://flake.parts/options/files.html) to generate documentation. The list of generated files are: - [docs/cheatsheet.md](docs/cheatsheet.md) -- [README.md](README.md) -## Helpers -The following are some helpers for the repo as a whole. -### Generating Text -The option `text.` supports either a string or a submodule with attributes order and parts. -The parts attribute can either be a string, which will get concatenated in the order laid out in `text..order`, or can itself have the attributes order and parts, in which case it will be evaluated recursively. \ No newline at end of file +- [README.md](README.md) \ No newline at end of file diff --git a/flake.lock b/flake.lock index 68b6df3..3abdd18 100644 --- a/flake.lock +++ b/flake.lock @@ -156,7 +156,23 @@ "git-hooks": "git-hooks", "import-tree": "import-tree", "make-shell": "make-shell", - "nixpkgs": "nixpkgs" + "nixpkgs": "nixpkgs", + "text": "text" + } + }, + "text": { + "locked": { + "lastModified": 1751749699, + "narHash": "sha256-eSeb0ERcdldtV1YLzVqi9NxITr4OLiuiGDS6T0t3Yh4=", + "owner": "rrvsh", + "repo": "text.nix", + "rev": "929c8863a1c323e1264887501e9a7914571654db", + "type": "github" + }, + "original": { + "owner": "rrvsh", + "repo": "text.nix", + "type": "github" } } }, diff --git a/flake.nix b/flake.nix index 4b85473..fc48b8a 100644 --- a/flake.nix +++ b/flake.nix @@ -11,6 +11,8 @@ import-tree.url = "github:vic/import-tree"; # files lets us write text files and automatically add checks for them files.url = "github:mightyiam/files"; + # text.nix lets us easily define markdown text to pass to files + text.url = "github:rrvsh/text.nix"; # make-shells. creates devShells and checks make-shell = { url = "github:nicknovitski/make-shell"; diff --git a/modules/docs/readme.nix b/modules/docs/readme.nix index aa7bb54..d17959a 100644 --- a/modules/docs/readme.nix +++ b/modules/docs/readme.nix @@ -8,10 +8,6 @@ in description = '' This flake serves as a monorepo for my systems (using IaC), dotfiles, and scripts. ''; - parts.helpers = { - heading = "Helpers"; - description = "The following are some helpers for the repo as a whole."; - }; }; perSystem = diff --git a/modules/flake-parts/text.nix b/modules/flake-parts/text.nix new file mode 100644 index 0000000..81b2f51 --- /dev/null +++ b/modules/flake-parts/text.nix @@ -0,0 +1,4 @@ +{ inputs, ... }: +{ + imports = [ inputs.text.flakeModules.default ]; +} diff --git a/modules/helpers/text.nix b/modules/helpers/text.nix deleted file mode 100644 index 4773d2c..0000000 --- a/modules/helpers/text.nix +++ /dev/null @@ -1,84 +0,0 @@ -# TODO: extract into separate repo -{ 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; })) - ); - }; - config.text.readme.parts.helpers.parts."Generating Text" = - '' - The option `text.` supports either a string or a submodule with attributes order and parts. - The parts attribute can either be a string, which will get concatenated in the order laid out in `text..order`, or can itself have the attributes order and parts, in which case it will be evaluated recursively.''; -}