Skip to content

Commit

Permalink
docs: modulesFromListExporter
Browse files Browse the repository at this point in the history
  • Loading branch information
David Arnold committed Apr 14, 2021
1 parent bcfc9d3 commit 33bcda7
Showing 1 changed file with 42 additions and 24 deletions.
66 changes: 42 additions & 24 deletions moduleFromListExporter.nix
Original file line number Diff line number Diff line change
@@ -1,29 +1,47 @@
{ flake-utils-plus }: let

modulesFromListExporter =

let

removeSuffix = suffix: str:
let
sufLen = builtins.stringLength suffix;
sLen = builtins.stringLength str;
in
if sufLen <= sLen && suffix == builtins.substring (sLen - sufLen) sufLen str then
builtins.substring 0 (sLen - sufLen) str
else
str;

genAttrs' = func: values: builtins.listToAttrs (map func values);

in

paths: genAttrs'
(path: {
name = removeSuffix ".nix" (baseNameOf path);
value = import path;
})
paths;
modulesFromListExporter = paths:
/**
Synopsis: modulesFromListExporter _paths_
paths: [ <path> <path> ]
Returns an attribute set of modules from a list of paths by converting
the path's basename into the attribute key.
Example:
paths: [ ./path/to/moduleA.nix ./path/to/moduleBfolder ]
{
moduleA = import ./path/to/moduleA.nix;
moduleBfolder = import ./path/to/moduleBfolder;
}
**/

let

removeSuffix = suffix: str:
let
sufLen = builtins.stringLength suffix;
sLen = builtins.stringLength str;
in
if sufLen <= sLen && suffix == builtins.substring (sLen - sufLen) sufLen str then
builtins.substring 0 (sLen - sufLen) str
else
str;

genAttrs' = func: values: builtins.listToAttrs (map func values);

in

genAttrs'
(path: {
name = removeSuffix ".nix" (baseNameOf path);
value = import path;
})
paths;

in
modulesFromListExporter

0 comments on commit 33bcda7

Please sign in to comment.