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.
This commit is contained in:
parent
e385783de2
commit
03a43150a1
6 changed files with 24 additions and 95 deletions
|
@ -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.
|
This flake uses the [files flake-parts module](https://flake.parts/options/files.html) to generate documentation.
|
||||||
The list of generated files are:
|
The list of generated files are:
|
||||||
- [docs/cheatsheet.md](docs/cheatsheet.md)
|
- [docs/cheatsheet.md](docs/cheatsheet.md)
|
||||||
- [README.md](README.md)
|
- [README.md](README.md)
|
||||||
## Helpers
|
|
||||||
The following are some helpers for the repo as a whole.
|
|
||||||
### Generating Text
|
|
||||||
The option `text.<name>` 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.<name>.order`, or can itself have the attributes order and parts, in which case it will be evaluated recursively.
|
|
18
flake.lock
generated
18
flake.lock
generated
|
@ -156,7 +156,23 @@
|
||||||
"git-hooks": "git-hooks",
|
"git-hooks": "git-hooks",
|
||||||
"import-tree": "import-tree",
|
"import-tree": "import-tree",
|
||||||
"make-shell": "make-shell",
|
"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"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
|
@ -11,6 +11,8 @@
|
||||||
import-tree.url = "github:vic/import-tree";
|
import-tree.url = "github:vic/import-tree";
|
||||||
# files lets us write text files and automatically add checks for them
|
# files lets us write text files and automatically add checks for them
|
||||||
files.url = "github:mightyiam/files";
|
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.<name> creates devShells and checks
|
# make-shells.<name> creates devShells and checks
|
||||||
make-shell = {
|
make-shell = {
|
||||||
url = "github:nicknovitski/make-shell";
|
url = "github:nicknovitski/make-shell";
|
||||||
|
|
|
@ -8,10 +8,6 @@ in
|
||||||
description = ''
|
description = ''
|
||||||
This flake serves as a monorepo for my systems (using IaC), dotfiles, and scripts.
|
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 =
|
perSystem =
|
||||||
|
|
4
modules/flake-parts/text.nix
Normal file
4
modules/flake-parts/text.nix
Normal file
|
@ -0,0 +1,4 @@
|
||||||
|
{ inputs, ... }:
|
||||||
|
{
|
||||||
|
imports = [ inputs.text.flakeModules.default ];
|
||||||
|
}
|
|
@ -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.<name>` 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.<name>.order`, or can itself have the attributes order and parts, in which case it will be evaluated recursively.'';
|
|
||||||
}
|
|
Loading…
Add table
Add a link
Reference in a new issue