From 295f5daba92a3cf64b1be4ee6032cde5dfde6408 Mon Sep 17 00:00:00 2001 From: Mohammad Rafiq Date: Thu, 3 Jul 2025 02:08:54 +0800 Subject: [PATCH] feat: add files module, text helper, and update readme generation --- README.md | 5 +- modules/docs/readme.nix | 27 ++++++----- modules/flake-parts/files.nix | 11 +++++ modules/{meta => flake-parts}/make-shell.nix | 1 + modules/helpers/text.nix | 49 ++++++++++++++++++++ modules/meta/files.nix | 7 --- modules/systems.nix | 4 +- 7 files changed, 79 insertions(+), 25 deletions(-) create mode 100644 modules/flake-parts/files.nix rename modules/{meta => flake-parts}/make-shell.nix (77%) create mode 100644 modules/helpers/text.nix delete mode 100644 modules/meta/files.nix diff --git a/README.md b/README.md index c3bf1f5..52721cd 100644 --- a/README.md +++ b/README.md @@ -1 +1,4 @@ -test README +This flake uses the [files flake-parts module](https://flake.parts/options/files.html) to generate documentation. +The list of generated files are: + +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 diff --git a/modules/docs/readme.nix b/modules/docs/readme.nix index 78319e3..bc2f6f2 100644 --- a/modules/docs/readme.nix +++ b/modules/docs/readme.nix @@ -1,21 +1,20 @@ -{ config, ... }: +{ config, lib, ... }: +let + inherit (lib) singleton; +in { - # text.readme = { - # - # }; + text.readme.order = [ + "generated-files" + "helpers" + ]; + text.readme.parts.helpers.order = [ "text-helper" ]; perSystem = { pkgs, ... }: { - files.files = [ - { - path_ = "README.md"; - drv = - pkgs.writeText "README.md" # config.text.readme - '' - test README - ''; - } - ]; + files.files = singleton { + path_ = "README.md"; + drv = pkgs.writeText "README.md" config.text.readme; + }; }; } diff --git a/modules/flake-parts/files.nix b/modules/flake-parts/files.nix new file mode 100644 index 0000000..d4b944b --- /dev/null +++ b/modules/flake-parts/files.nix @@ -0,0 +1,11 @@ +{ inputs, ... }: +{ + imports = [ inputs.files.flakeModules.default ]; + perSystem = psArgs: { + make-shells.default.packages = [ psArgs.config.files.writer.drv ]; + }; + text.readme.parts.generated-files = '' + This flake uses the [files flake-parts module](https://flake.parts/options/files.html) to generate documentation. + The list of generated files are: + ''; +} diff --git a/modules/meta/make-shell.nix b/modules/flake-parts/make-shell.nix similarity index 77% rename from modules/meta/make-shell.nix rename to modules/flake-parts/make-shell.nix index 9206c92..66ca600 100644 --- a/modules/meta/make-shell.nix +++ b/modules/flake-parts/make-shell.nix @@ -1,4 +1,5 @@ { inputs, ... }: { + #TODO: add to readme imports = [ inputs.make-shell.flakeModules.default ]; } diff --git a/modules/helpers/text.nix b/modules/helpers/text.nix new file mode 100644 index 0000000..de43da0 --- /dev/null +++ b/modules/helpers/text.nix @@ -0,0 +1,49 @@ +{ lib, ... }: +let + inherit (lib) + mkOption + mapAttrs + isString + pipe + flip + getAttr + concatStringsSep + ; + inherit (lib.types) + lazyAttrsOf + oneOf + submodule + str + listOf + ; + textType = oneOf [ + str + (submodule { + options = { + order = mkOption { type = listOf str; }; + parts = mkOption { type = lazyAttrsOf textType; }; + }; + }) + ]; + recurseAttrs = + value: + if isString value then + value + else + # TODO: handle order being empty + # TODO: add headings for each part with possible option to disable + pipe value.order [ + (map (flip getAttr value.parts)) + (map recurseAttrs) + (concatStringsSep "\n") + ]; +in +{ + options.text = mkOption { + default = { }; + type = lazyAttrsOf textType; + apply = mapAttrs (_: recurseAttrs); + }; + config.text.readme.parts.helpers.parts.text-helper = + "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."; +} diff --git a/modules/meta/files.nix b/modules/meta/files.nix deleted file mode 100644 index 07da696..0000000 --- a/modules/meta/files.nix +++ /dev/null @@ -1,7 +0,0 @@ -{ inputs, ... }: -{ - imports = [ inputs.files.flakeModules.default ]; - perSystem = psArgs: { - make-shells.default.packages = [ psArgs.config.files.writer.drv ]; - }; -} diff --git a/modules/systems.nix b/modules/systems.nix index bed2497..ce708c6 100644 --- a/modules/systems.nix +++ b/modules/systems.nix @@ -1,5 +1,3 @@ { - systems = [ - "x86_64-linux" - ]; + systems = [ "x86_64-linux" ]; }