about summary refs log tree commit diff stats
path: root/lib/default.nix
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--lib/default.nix63
1 files changed, 60 insertions, 3 deletions
diff --git a/lib/default.nix b/lib/default.nix
index 80227cf4..1cdbf936 100644
--- a/lib/default.nix
+++ b/lib/default.nix
@@ -1,4 +1,13 @@
-{lib}: let
+# nixos-config - My current NixOS configuration
+#
+# Copyright (C) 2025 Benedikt Peetz <benedikt.peetz@b-peetz.de>
+# SPDX-License-Identifier: GPL-3.0-or-later
+#
+# This file is part of my nixos-config.
+#
+# You should have received a copy of the License along with this program.
+# If not, see <https://www.gnu.org/licenses/gpl-3.0.txt>.
+{lib}: rec {
   /*
   Converts `number` from a binary number to a decimal one.
 
@@ -26,6 +35,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`.
@@ -64,6 +101,26 @@
     else if power > 0
     then (base * (pow base (power - 1)))
     else builtins.throw "Negative powers are not supported";
-in {
-  inherit binaryToDecimal pow;
+
+  options = {
+    /*
+    Generate an enable option definition, that defaults to true.
+
+    This conveniently allows the composition of a “common” module set.
+
+    # Type
+
+    mkEnable :: String -> AttrSet
+
+    # Arguments
+
+    name
+    : The name to use in the option description.
+    */
+    mkEnable = name:
+      (lib.mkEnableOption name)
+      // {
+        default = true;
+      };
+  };
 }