servers/lib/utils.nix

55 lines
1.3 KiB
Nix
Raw Normal View History

2025-01-26 18:49:45 -06:00
rec {
2026-02-24 00:00:35 -08:00
findNixFiles =
dir:
let
inherit (builtins)
attrNames
readDir
pathExists
concatMap
;
2025-01-26 18:49:45 -06:00
2026-02-24 00:00:35 -08:00
# Helper function to build full paths
fullPath = name: dir + "/${name}";
2025-01-26 18:49:45 -06:00
2026-02-24 00:00:35 -08:00
# Get directory contents
contents = readDir dir;
2025-01-26 18:49:45 -06:00
2026-02-24 00:00:35 -08:00
# Convert contents attrset to list of names
names = attrNames contents;
2025-01-26 18:49:45 -06:00
2026-02-24 00:00:35 -08:00
# Filter and process each item
processItem =
name:
let
path = fullPath name;
type = contents.${name};
in
if type == "regular" && hasSuffix "nix" name then
[ path ]
else if type == "directory" && pathExists path then
findNixFiles path
else
[ ];
2025-01-26 18:49:45 -06:00
in
concatMap processItem names;
2025-02-03 13:00:31 -06:00
2026-02-24 00:00:35 -08:00
getName =
filename:
let
parts = builtins.split "\\." filename;
base = builtins.head (builtins.split "\\." filename);
in
if builtins.length parts == 1 then filename else base;
2025-02-03 13:00:31 -06:00
2026-02-24 00:00:35 -08:00
getSuffix =
filename:
let
parts = builtins.split "\\." filename;
end = builtins.tail (builtins.split "\\." filename);
in
if builtins.length parts == 1 then filename else builtins.elemAt end (builtins.length end - 1);
2025-02-03 13:00:31 -06:00
2026-02-24 00:00:35 -08:00
hasSuffix = suffix: filename: if (getSuffix filename) == suffix then true else false;
2025-01-26 18:49:45 -06:00
}