aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--flake.nix3
-rw-r--r--flake/default.nix4
-rw-r--r--flake/nixosConfigurations/default.nix2
-rw-r--r--lib/default.nix69
4 files changed, 78 insertions, 0 deletions
diff --git a/flake.nix b/flake.nix
index d8e38f34..6dd46d91 100644
--- a/flake.nix
+++ b/flake.nix
@@ -297,6 +297,8 @@
system = "x86_64-linux";
sysLib = shell_library.lib.${system};
+ baseLib = import ./lib {inherit (pkgs) lib;};
+
inherit (library) nixLib;
pkgsStable = nixpkgs-stable.legacyPackages.${system};
@@ -323,6 +325,7 @@
myPkgs
system
sysLib
+ baseLib
nixpkgs_as_input
nixpkgs_open_prs
# modules
diff --git a/flake/default.nix b/flake/default.nix
index be6d2b70..c942c678 100644
--- a/flake/default.nix
+++ b/flake/default.nix
@@ -4,6 +4,7 @@
pkgs,
pkgsStable,
nixLib,
+ baseLib,
myPkgs,
system,
sysLib,
@@ -43,6 +44,7 @@
shell_library
sysLib
nixLib
+ baseLib
templates
# modules
home-manager
@@ -111,6 +113,8 @@ in {
formatter."${system}" = treefmtEval.config.build.wrapper;
+ baseLib."${system}" = baseLib;
+
apps."${system}" = import ./apps {inherit self system;};
devShells."${system}" = {
diff --git a/flake/nixosConfigurations/default.nix b/flake/nixosConfigurations/default.nix
index b0a02580..59720c76 100644
--- a/flake/nixosConfigurations/default.nix
+++ b/flake/nixosConfigurations/default.nix
@@ -9,6 +9,7 @@
shell_library,
sysLib,
nixLib,
+ baseLib,
templates,
# modules
home-manager,
@@ -50,6 +51,7 @@
shell_library
sysLib
nixLib
+ baseLib
# extra information
system
# modules
diff --git a/lib/default.nix b/lib/default.nix
new file mode 100644
index 00000000..80227cf4
--- /dev/null
+++ b/lib/default.nix
@@ -0,0 +1,69 @@
+{lib}: let
+ /*
+ Converts `number` from a binary number to a decimal one.
+
+ The input is indented to be a number composed only of `1`s and `0` (e.g., 10010.)
+
+ # Type
+
+ binaryToDecimal :: Int -> Int
+
+ # Arguments
+
+ number
+ : The sequence of `1`s and `0`s defining a binary number, in little endian.
+
+ # Examples
+
+ binaryToDecimal 1001
+ => 9
+ */
+ binaryToDecimal = number: let
+ binaryList =
+ builtins.map lib.strings.toInt (lib.lists.reverseList (lib.strings.stringToCharacters (builtins.toString number)));
+ in
+ builtins.foldl' (acc: num: acc + num) 0
+ (lib.lists.imap0 (index: elem: elem * (pow 2 index)) binaryList);
+
+ /*
+ source: https://github.com/NixOS/nix/issues/10387#issuecomment-2494597690
+
+ Raises the `base` to the power of `power`.
+
+ Considering the small input size and the lack of a built-in power function [2],
+ this naive power implementation should satisfy reasonable performance
+ requirements.
+
+ Due to the lack of a modulo and bitwise AND operator, it is questionable whether
+ the recursive exponentiation by squaring [1] implementation would even be
+ faster.
+
+ [1]: https://en.wikipedia.org/wiki/Exponentiation_by_squaring
+ [2]: https://github.com/NixOS/nix/issues/10387
+
+ # Type
+
+ pow :: Number -> Number -> Number
+
+ # Arguments
+
+ base
+ : The base number to be raised.
+
+ power
+ : The exponent to raise `base` to.
+
+ # Examples
+
+ pow 2 5
+ => 32
+ */
+ pow = base: power:
+ if power == 0
+ then 1
+ else if power > 0
+ then (base * (pow base (power - 1)))
+ else builtins.throw "Negative powers are not supported";
+in {
+ inherit binaryToDecimal pow;
+}