From 65a9e7cd89ab452bb3c75cfa496d0dcd90b3707d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Karel=20Ko=C4=8D=C3=AD?= Date: Wed, 2 Oct 2024 09:32:54 +0200 Subject: lib: implement as overlay This uses fully the ability to overlay the nixpkgs library instead of doing that hackilly. --- flake.nix | 2 +- lib/default.nix | 51 +++++++++++++++++++++++---------------------------- 2 files changed, 24 insertions(+), 29 deletions(-) diff --git a/flake.nix b/flake.nix index c9a68bd..4518a92 100644 --- a/flake.nix +++ b/flake.nix @@ -35,7 +35,7 @@ in { overlays = { - lib = _: prev: import ./lib prev; + lib = final: prev: import ./lib final prev; pkgs = final: prev: import ./pkgs final prev; default = nixpkgs.lib.composeManyExtensions [ agenix.overlays.default diff --git a/lib/default.nix b/lib/default.nix index 3f14de0..76eb693 100644 --- a/lib/default.nix +++ b/lib/default.nix @@ -1,30 +1,25 @@ -nixpkgslib: let - lib = nixpkgslib // nlib; +final: _: +with final; { + # Generic power of number + pow = base: e: + if e == 0 + then 1 + else foldl (x: _: x * base) 1 (genList id (e - 1)); + # Power of 2 + pow2 = pow 2; - nlib = with builtins; - with lib; { - # Generic power of number - pow = base: e: - if e == 0 - then 1 - else foldl (x: _: x * base) 1 (genList id (e - 1)); - # Power of 2 - pow2 = pow 2; + # Convert integer to list of bits + int2bits = len: e: reverseList (genList (x: bitAnd e (pow2 (x + 1)) > 0) len); + # Reverse operation for int2bits + bits2int = l: let + len = length l; + zf = a: b: + if a + then pow2 b + else 0; + in + foldl add 0 (zipListsWith zf l (genList (i: len - i) len)); - # Convert integer to list of bits - int2bits = len: e: reverseList (genList (x: bitAnd e (pow2 (x + 1)) > 0) len); - # Reverse operation for int2bits - bits2int = l: let - len = length l; - zf = a: b: - if a - then pow2 b - else 0; - in - foldl add 0 (zipListsWith zf l (genList (i: len - i) len)); - - # IPv4 utilities - ipv4 = import ./ipv4.nix lib; - }; -in - nlib + # IPv4 utilities + ipv4 = import ./ipv4.nix final; +} -- cgit v1.2.3