mirror of
https://github.com/oddlama/nixos-extra-modules.git
synced 2025-10-11 06:10:39 +02:00
feat: add unified microvm & container definition; add net, misc, disko lib extension
This commit is contained in:
parent
6e0ea91254
commit
c55f465ba1
13 changed files with 1093 additions and 3 deletions
84
lib/misc.nix
Normal file
84
lib/misc.nix
Normal file
|
@ -0,0 +1,84 @@
|
|||
_inputs: _final: prev: let
|
||||
inherit
|
||||
(prev.lib)
|
||||
filter
|
||||
foldl'
|
||||
genAttrs
|
||||
genList
|
||||
mergeAttrs
|
||||
mkMerge
|
||||
stringToCharacters
|
||||
substring
|
||||
unique
|
||||
;
|
||||
|
||||
# Counts how often each element occurrs in xs.
|
||||
# Elements must be strings.
|
||||
countOccurrences =
|
||||
foldl'
|
||||
(acc: x: acc // {${x} = (acc.${x} or 0) + 1;})
|
||||
{};
|
||||
|
||||
# Returns all elements in xs that occur at least twice
|
||||
duplicates = xs: let
|
||||
occurrences = countOccurrences xs;
|
||||
in
|
||||
unique (filter (x: occurrences.${x} > 1) xs);
|
||||
|
||||
# Concatenates all given attrsets as if calling a // b in order.
|
||||
concatAttrs = foldl' mergeAttrs {};
|
||||
|
||||
# True if the path or string starts with /
|
||||
isAbsolutePath = x: substring 0 1 x == "/";
|
||||
|
||||
# Merges all given attributes from the given attrsets using mkMerge.
|
||||
# Useful to merge several top-level configs in a module.
|
||||
mergeToplevelConfigs = keys: attrs:
|
||||
genAttrs keys (attr: mkMerge (map (x: x.${attr} or {}) attrs));
|
||||
|
||||
# Calculates base^exp, but careful, this overflows for results > 2^62
|
||||
pow = base: exp: foldl' (a: x: x * a) 1 (genList (_: base) exp);
|
||||
|
||||
hexLiteralValues = {
|
||||
"0" = 0;
|
||||
"1" = 1;
|
||||
"2" = 2;
|
||||
"3" = 3;
|
||||
"4" = 4;
|
||||
"5" = 5;
|
||||
"6" = 6;
|
||||
"7" = 7;
|
||||
"8" = 8;
|
||||
"9" = 9;
|
||||
"a" = 10;
|
||||
"b" = 11;
|
||||
"c" = 12;
|
||||
"d" = 13;
|
||||
"e" = 14;
|
||||
"f" = 15;
|
||||
"A" = 10;
|
||||
"B" = 11;
|
||||
"C" = 12;
|
||||
"D" = 13;
|
||||
"E" = 14;
|
||||
"F" = 15;
|
||||
};
|
||||
|
||||
# Converts the given hex string to an integer. Only reliable for inputs in [0, 2^63),
|
||||
# after that the sign bit will overflow.
|
||||
hexToDec = v: foldl' (acc: x: acc * 16 + hexLiteralValues.${x}) 0 (stringToCharacters v);
|
||||
in {
|
||||
lib =
|
||||
prev.lib
|
||||
// {
|
||||
inherit
|
||||
concatAttrs
|
||||
countOccurrences
|
||||
duplicates
|
||||
hexToDec
|
||||
isAbsolutePath
|
||||
mergeToplevelConfigs
|
||||
pow
|
||||
;
|
||||
};
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue