diff options
| -rw-r--r-- | by-name.nix (renamed from by-name-overlay.nix) | 0 | ||||
| -rw-r--r-- | default.nix | 29 | ||||
| -rw-r--r-- | flake.nix | 4 | ||||
| -rw-r--r-- | math.nix | 95 | ||||
| -rw-r--r-- | options.nix | 21 |
5 files changed, 130 insertions, 19 deletions
diff --git a/by-name-overlay.nix b/by-name.nix index 2d03e75..2d03e75 100644 --- a/by-name-overlay.nix +++ b/by-name.nix diff --git a/default.nix b/default.nix index 9098bce..26e93c3 100644 --- a/default.nix +++ b/default.nix @@ -7,27 +7,18 @@ # # 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>. - -{}: let +{lib}: let # string -> string -> string colorize = color: string: "[${color}m${string}[0m"; - - # Taken from `nixpkgs/lib`. - # Is here to avoid a dependency on `lib` (making this file easy to test with `nix eval`) - warn = - # Since Nix 2.23, https://github.com/NixOS/nix/pull/10592 - builtins.warn - or ( - # Do not eta reduce v, so that we have the same strictness as `builtins.warn`. - msg: v: - # `builtins.warn` requires a string message, so we enforce that in our implementation, so that callers aren't accidentally incompatible with newer Nix versions. - assert builtins.isString msg; - builtins.trace "${colorize "1;35" "evaluation warning:"} ${msg}" v - ); in { - mkByName = import ./by-name-overlay.nix {inherit warn;}; + /** + Converts a `by-name` structured directory into. + * + */ + mkByName = import ./by-name.nix; # Warn while merging two attrsets if the RHS shares names with the LHS. + # # # Example: # ```nix # let @@ -36,6 +27,7 @@ in { # in # warnMerge attr1 attr2 "attr1 core value set" # ``` + # # # Type: # {} -> {} -> string -> {} warnMerge = lhs: rhs: sourceSet: let @@ -46,7 +38,10 @@ in { in if overridden != {} then - warn "Attributes overridden while merging ${sourceSet}: ${mkWarning overridden}" + builtins.warn "Attributes overridden while merging ${sourceSet}: ${mkWarning overridden}" merged else merged; + + math = import ./math.nix {inherit lib;}; + options = import ./options.nix {inherit lib;}; } @@ -12,8 +12,8 @@ inputs = {}; outputs = _: let - lib = import ./default.nix {}; + extra_lib = import ./default.nix; in { - nixLib = lib; + extraLib = extra_lib; }; } diff --git a/math.nix b/math.nix new file mode 100644 index 0000000..9860449 --- /dev/null +++ b/math.nix @@ -0,0 +1,95 @@ +{lib}: rec { + /* + 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"; + + /* + 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); + + /* + 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; +} diff --git a/options.nix b/options.nix new file mode 100644 index 0000000..e01c10d --- /dev/null +++ b/options.nix @@ -0,0 +1,21 @@ +{lib}: { + /* + 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. + */ + mkDefaultEnabled = name: + (lib.mkEnableOption name) + // { + default = true; + }; +} |
