From fdc84767517b130ac98b9315f3501169b9097129 Mon Sep 17 00:00:00 2001 From: Gabriel Arazas Date: Fri, 23 Feb 2024 14:57:33 +0800 Subject: [PATCH] bahaghari: init lib Now it is pretty familiar to use as you can also extend it like nixpkgs' library. --- subprojects/bahaghari/lib/default.nix | 21 ++++++++++++++++++ subprojects/bahaghari/lib/hex.nix | 11 ++++++++++ subprojects/bahaghari/lib/tinted-theming.nix | 19 +++++----------- subprojects/bahaghari/lib/trivial.nix | 23 ++++++++++++++++++++ 4 files changed, 61 insertions(+), 13 deletions(-) create mode 100644 subprojects/bahaghari/lib/default.nix create mode 100644 subprojects/bahaghari/lib/hex.nix create mode 100644 subprojects/bahaghari/lib/trivial.nix diff --git a/subprojects/bahaghari/lib/default.nix b/subprojects/bahaghari/lib/default.nix new file mode 100644 index 00000000..65cbee0e --- /dev/null +++ b/subprojects/bahaghari/lib/default.nix @@ -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; + }) diff --git a/subprojects/bahaghari/lib/hex.nix b/subprojects/bahaghari/lib/hex.nix new file mode 100644 index 00000000..3873866e --- /dev/null +++ b/subprojects/bahaghari/lib/hex.nix @@ -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); +} diff --git a/subprojects/bahaghari/lib/tinted-theming.nix b/subprojects/bahaghari/lib/tinted-theming.nix index c56810ea..b6d74ba5 100644 --- a/subprojects/bahaghari/lib/tinted-theming.nix +++ b/subprojects/bahaghari/lib/tinted-theming.nix @@ -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; } diff --git a/subprojects/bahaghari/lib/trivial.nix b/subprojects/bahaghari/lib/trivial.nix new file mode 100644 index 00000000..4f2a0d12 --- /dev/null +++ b/subprojects/bahaghari/lib/trivial.nix @@ -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; +}