allow exposeModules to be shared

This commit is contained in:
Jörg Thalheim 2024-06-12 19:29:14 +02:00
parent d6280e5c12
commit ea3d082265
2 changed files with 36 additions and 35 deletions

35
lib/exposeModules.nix Normal file
View File

@ -0,0 +1,35 @@
let
inherit (builtins)
listToAttrs
replaceStrings
stringLength
substring
;
removeSuffix =
# Suffix to remove if it matches
suffix:
# Input string
str:
let
sufLen = stringLength suffix;
sLen = stringLength str;
in
if sufLen <= sLen && suffix == substring (sLen - sufLen) sufLen str then
substring 0 (sLen - sufLen) str
else
str;
in
# Map 1:1 between paths and modules
baseDir: paths:
let
prefix = stringLength (toString baseDir) + 1;
toPair = path: {
name = replaceStrings [ "/" ] [ "-" ] (
removeSuffix ".nix" (substring prefix 1000000 (toString path))
);
value = path;
};
in
listToAttrs (map toPair paths)

View File

@ -1,39 +1,5 @@
let
inherit (builtins)
listToAttrs
replaceStrings
stringLength
substring
;
removeSuffix =
# Suffix to remove if it matches
suffix:
# Input string
str:
let
sufLen = stringLength suffix;
sLen = stringLength str;
in
if sufLen <= sLen && suffix == substring (sLen - sufLen) sufLen str then
substring 0 (sLen - sufLen) str
else
str;
# Map 1:1 between paths and modules
exposeModules = baseDir: paths:
let
prefix = stringLength (toString baseDir) + 1;
toPair = path: {
name = replaceStrings [ "/" ] [ "-" ] (removeSuffix ".nix" (substring prefix 1000000
(toString path)));
value = path;
};
in
listToAttrs (map toPair paths)
;
exposeModules = import ../lib/exposeModules.nix;
in
exposeModules ./. [
./common