bahaghari: init lib

Now it is pretty familiar to use as you can also extend it like nixpkgs'
library.
This commit is contained in:
Gabriel Arazas 2024-02-23 14:57:33 +08:00
parent 911476c37c
commit fdc8476751
No known key found for this signature in database
GPG Key ID: ADE0C41DAB221FCC
4 changed files with 61 additions and 13 deletions

View File

@ -0,0 +1,21 @@
# Bahaghari's set of library. This requires nixpkgs' package set which has its
# library anyways. This set is mostly copied over from nixpkgs' way of doing
# things.
{ pkgs }:
# Take note the `lib` attribute throughout all of the library files are
# referring to the Bahaghari library set. We mostly rely on `pkgs.lib` as an
# easy way to identify if we use nixpkgs' standard library.
pkgs.lib.makeExtensible
(self:
let
callLibs = file: import file { lib = self; inherit pkgs; };
in
{
trivial = callLibs ./trivial.nix;
hex = callLibs ./hex.nix;
tinted-theming = callLibs ./tinted-theming.nix;
inherit (self.trivial) importYAML;
inherit (self.hex) toHexString;
})

View File

@ -0,0 +1,11 @@
# A small utility library for manipulating hexadecimal numbers. It's made in 15
# minutes with a bunch of duct tape on it but it's working for its intended
# purpose.
{ pkgs, lib }:
rec {
inherit (pkgs.lib.trivial) toHexString;
# A variant of `lib.lists.range` function just with hexadecimal digits.
range = first: last: builtins.map (n: toHexString n) (lib.lists.range first last);
}

View File

@ -15,24 +15,17 @@
# Base16 scheme.
isBase16 = palette:
let
paletteNames = lib.attrNames palette;
schemeNames = builtins.map (number: "base${number}") [
"00" "01" "02" "03" "04" "05" "06" "07" "08" "09" "0A"
"0B" "0C" "0D" "0E" "0F"
];
paletteNames = pkgs.lib.attrNames palette;
schemeNames = builtins.map (number: "base${number}") (lib.hex.range 1 16);
in
(lib.count (name: lib.elem name schemeNames) paletteNames) == 16;
(pkgs.lib.count (name: pkgs.lib.elem name schemeNames) paletteNames) == 16;
# A very naive implementation of checking if a Tinted Theming scheme is a
# Base24 scheme.
isBase24 = palette:
let
paletteNames = lib.attrNames palette;
schemeNames = builtins.map (number: "base${number}") [
"00" "01" "02" "03" "04" "05" "06" "07" "08" "09" "0A"
"0B" "0C" "0D" "0E" "0F" "10" "11" "12" "13" "14" "15"
"16" "17"
];
paletteNames = pkgs.lib.attrNames palette;
schemeNames = builtins.map (number: "base${number}") (pkgs.lib.hex.range 1 24);
in
(lib.count (name: lib.elem name schemeNames) paletteNames) == 24;
(pkgs.lib.count (name: pkgs.lib.elem name schemeNames) paletteNames) == 24;
}

View File

@ -0,0 +1,23 @@
{ pkgs, lib }:
{
/* Read YAML files into a Nix expression similar to lib.importJSON and
lib.importTOML from nixpkgs standard library. Unlike both of them, this
unfortunately relies on an import-from-derivation (IFD) so it isn't exactly
perfect but it is very close.
This relies on yaml2json which uses the following YAML library which you
can view the following link for more details on YAML compatibility.
https://pkg.go.dev/gopkg.in/yaml.v3#readme-compatibility
Type: importYAML :: path -> any
*/
importYAML = path:
let
data = pkgs.runCommand "convert-yaml-to-json" { } ''
${pkgs.lib.getExe' pkgs.yaml2json "yaml2json"} < ${path} > $out
'';
in
pkgs.lib.importJSON data;
}