aboutsummaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--lib/default.nix30
1 files changed, 29 insertions, 1 deletions
diff --git a/lib/default.nix b/lib/default.nix
index 80227cf4..f18e4b33 100644
--- a/lib/default.nix
+++ b/lib/default.nix
@@ -26,6 +26,34 @@
(lib.lists.imap0 (index: elem: elem * (pow 2 index)) binaryList);
/*
+ Converts `string` to a (probably) unique numerical id.
+
+ This is achieved by hashing the string and returning the first six hex-digits
+ converted to a base 10 number.
+
+ # Type
+
+ idFromString :: String -> Int
+
+ # Arguments
+
+ string
+ : The string to use as seed for the id generation.
+
+ # Examples
+
+ idFromString "3d-printer"
+ => 10365713
+ */
+ idFromString = string: let
+ inputSeed =
+ builtins.concatStringsSep ""
+ (lib.lists.take 6
+ (lib.strings.stringToCharacters (builtins.hashString "sha256" string)));
+ in
+ lib.trivial.fromHexString inputSeed;
+
+ /*
source: https://github.com/NixOS/nix/issues/10387#issuecomment-2494597690
Raises the `base` to the power of `power`.
@@ -65,5 +93,5 @@
then (base * (pow base (power - 1)))
else builtins.throw "Negative powers are not supported";
in {
- inherit binaryToDecimal pow;
+ inherit binaryToDecimal idFromString pow;
}