This commit is contained in:
sadan 2025-03-17 17:46:15 -04:00
parent f5431cbae8
commit 3b9f424694
No known key found for this signature in database
7 changed files with 95 additions and 40 deletions

11
common/util/array.nix Normal file
View file

@ -0,0 +1,11 @@
{ lib, ... }:
{
array = rec {
popHead = list: lib.lists.ifilter0 (index: _element: index != 0) list;
popTail = list: lib.lists.ifilter0 (index: _element: index - 1 != length list) list;
head = list: at 0 list;
tail = list: at (length list - 1) list;
at = index: list: builtins.elemAt list index;
length = list: builtins.length list;
};
}

13
common/util/default.nix Normal file
View file

@ -0,0 +1,13 @@
{ lib, ... }:
let
util =
(import ./modules.nix { }) // (import ./array.nix { }) // (import ./paths.nix { inherit lib; });
in
{
_module.args = {
inherit util;
};
home-manager.extraSpecialArgs = {
inherit util;
};
}

4
common/util/modules.nix Normal file
View file

@ -0,0 +1,4 @@
{ ... }:
{
modulesFromPath = root: modules: builtins.map (mod: root + "/" + mod) modules;
}

28
common/util/paths.nix Normal file
View file

@ -0,0 +1,28 @@
{ lib, ... }:
let
array = import ./array.nix { };
in
{
path = rec {
split = { path }: builtins.split "/" "${path}";
removeSuffix = suffix: path: lib.removeSuffix suffix "${path}";
# ====
basename =
{
path,
suffix ? "",
}:
let
basename = if array.length (split path) >= 2 then array.tail (split path);
in
removeSuffix suffix basename;
# nix doesnt support windows
delimiter = ":";
dirname =
path:
let
ending = basename { inherit path; };
in
removeSuffix ending path;
};
}