nixos-config/subprojects/bahaghari
2025-01-12 21:33:15 +08:00
..
lib subprojects/bahaghari/lib: update nixdoc docstring format 2025-01-12 21:33:15 +08:00
modules pkgs/firefox-addons: add RSSHub Radar 2025-01-05 18:53:54 +08:00
npins bahaghari: use npins for pinning nixpkgs branches 2024-06-05 21:16:01 +08:00
tests pkgs/firefox-addons: add RSSHub Radar 2025-01-05 18:53:54 +08:00
utils bahaghari: fix utils as extra module arg 2024-07-31 22:01:02 +08:00
CHANGELOG.adoc pkgs/firefox-addons: add RSSHub Radar 2025-01-05 18:53:54 +08:00
default.nix bahaghari: update comments and project documentation 2024-08-15 17:21:27 +08:00
flake.nix bahaghari: update comments and project documentation 2024-08-15 17:21:27 +08:00
LICENSE bahaghari/lib: add trigonometric functions to math subset 2024-07-07 20:14:41 +08:00
README.adoc bahaghari: update comments and project documentation 2024-08-15 17:21:27 +08:00
shell.nix bahaghari/shell: add treefmt config and replace nixpkgs-fmt with the official formatter 2024-08-01 09:02:40 +08:00
treefmt.toml bahaghari/shell: add treefmt config and replace nixpkgs-fmt with the official formatter 2024-08-01 09:02:40 +08:00

Bahaghari [1] is a specialized set of Nix modules for generating and applying themes. Mainly useful for making your ricing process with NixOS and home-manager environments easier. This module set should allow for some flexibility for your use case.

At its current state, Bahaghari is considered unstable and might have breaking changes more than what youd expect.

Getting started

To get started using Bahaghari, you have to set some things up first. The way how Bahaghari expects you to use it is by choosing one (or more if you want) of the module sets which you can view more details at its dedicated section. For now, lets assume that you are using Tinted Theming module set (which is basically the most developed out of all of them) in your NixOS configuration which you can set it up with the following code.

First, youll have to install Bahaghari as part of your Nix project or whatever. There are multiple ways to do this.

  • You can import the dependencies with a pinning tool (that is not flakes) like niv or npins.

    # I would recommend to rename this into more specific package name.
    niv add foo-dogsquared/nixos-config --name foo-dogsquared-nixos-config

    Then somewhere in your Nix code, you can add it like in the following listing.

    let
      sources = import ./nix/sources.nix;
      bahaghari = import "${sources.foo-dogsquared-nixos-config}/subprojects/bahaghari" { };
    in
    {
      imports = [ bahaghari.nixosModules.bahaghari ];
    }
  • You could install it with Nix flakes, an experimental feature featuring a built-in way of pinning them dependencies.

    {
      inputs.bahaghari.url = "github:foo-dogsquared/nixos-config?dir=subprojects/bahaghari";
    
      # ...
    
      outputs = { nixpkgs, ... }@inputs: {
        nixosConfigurations = nixpkgs.lib.nixosSystem {
          modules = [
            inputs.bahaghari.nixosModules.bahaghari
          ];
        };
      };
    }
  • Last but not least, you can import it through channels.

    # You'll likely to run this as root since you'll be dealing with your NixOS
    # system after all.
    nix-channel --add "https://github.com/foo-dogsquared/nixos-config/archive/master.tar.gz" foo-dogsquared-nixos-config

    Similarly to importing it through niv, you can now use it somewhere in your Nix code.

    { config, lib, ... }:
    
    let
      bahaghari = import <foo-dogsquared-nixos-config/subprojects/bahaghari> { };
    in
    {
      imports = [ bahaghari.nixosModules.bahaghari ];
    }

Importing the module will also import Bahagharis library set (bahaghariLib), making it available as bahaghariLib module argument.

If for whatever reason you need Bahaghari library outside of using it with Bahaghari (for example, you want to use the math subset), you can also import it yourself. Heres one way to set it to be available in the module of your environment.

{ pkgs, ... }:

let
  # This is assuming you have Bahaghari as part of NIX_PATH.
  bahaghari = import <foo-dogsquared-nixos-config/subprojects/bahaghari> { };
in
{
  _module.args.bahaghariLib =
    import bahaghari.bahaghariLib { inherit pkgs };
}

Module sets

As a whole, Bahaghari is more like a set of sets, a metaset, if you will. Specifically, it is composed of module sets of which has different ways to use them. Here is the exhaustive list of them and its details.

Tinted Theming

Bahaghari has a module set for interacting with Tinted Theming standard schemes and templates.

This module is designed to closely follow Tinted Themings standard while offering some convenience with the library set. For example, you can use importYAML from Bahagharis standard library set to easily import standard Base16 schemes into Nix-representable data which you can press onto your Nix configuration.

However, Bahaghari has a dedicated set of library functions in bahaghariLib.tinted-theming to make interacting with this module set a bit easier. For example, instead of using importYAML, you can use tinted-theming.importScheme instead which the function will also take care of modernizing legacy Base16 schemes if it detected as one.

{ config, lib, bahaghariLib, ... }:

{
  bahaghari.tinted-theming.schemes = {
    # Instead of using `importYAML`...
    bark-on-a-tree = bahaghariLib.importYAML ./legacy-base16-scheme.yml;

    # ...you can use `tinted-theming.importScheme` instead.
    albino-bark-on-a-tree = bahaghariLib.tinted-theming.importScheme ./legacy-base16-scheme.yml;

    # This should work both on legacy and modern Tinted Theming schemes.
    ice-ice-baby = bahaghariLib.tinted-theming.importScheme ./modern-base16-scheme.yml;
  };
}

Templated configuration sub-modules

Bahaghari offers a pre-configured version of already existing modules for NixOS, home-manager, and NixVim quite similar to Stylixs stylix.targets.<name> submodules. To make use of this, youll have to import Bahaghari module sets

Comparison with other modules

Bahaghari is not the first nor the last Nix project to ever deal with setting up a foundation for generating them rainbowy rices. Heres some (unbiased ;p) insights comparing Bahaghari and some of the more established projects in this space.

nix-colors

Bahaghari initially started as a derivative to nix-colors as a single Nix module entirely dedicated for Tinted Theming standard "proper". It was created to address its limitation of allowing only one colorscheme at a time which limits a lot of possible applications. Most notably, the feature Im looking for is generating multiple colorscheme templates for different applications which is nice for hybrid deployments of home-manager-plus-mutable-configurations (AKA traditional dotfiles) and for mixed installations of NixOS and home-manager (or whatever else that can be combined).

While Bahaghari eventually diverged from nix-colors entirely, it can be used as an alternative specifically with Bahagharis Tinted Theming module set. You can replicate nix-colors' preference of allowing a default set of colorscheme by creating a module argument holding the name of the preferred colorscheme. Heres one way to implement it.

{ bahaghariLib, ... }:

{
  bahaghari.tinted-theming.schemes.bark-on-a-tree =
    bahaghariLib.importYAML ./base16-bark-on-a-tree.yml;

  # Just assume there's importing more schemes here.

  _module.args.defaultTintedThemingScheme = "bark-on-a-tree";
}

Stylix

While Bahaghari can be used similarly to Stylix, it isnt completely 1-to-1 to Stylix as the latter focuses on the holistic side of customization including for fonts and wallpaper. On the other hand, Bahaghari completely focuses on colorscheme generation. [2]

Still, if youre looking for a streamlined way of generating a theme configuration, Stylix is still the tool for the job.

Hacking this project

If you want to hack this hack, you can do so with Nix. This is mostly a Nix project so you only need Nix and youre good to go.

The folder structure of this project should be intuitive but well give a recap like a good manager. Heres the rundown.

  • ./default.nix is basically the entrypoint for our user and onlookers whore looking to extend Bahaghari. This is used both by the flake- and non-flake-based setups for consistency (and also easier to maintain).

  • ./lib/ is where the Bahaghari library set lives. It takes a lot of cues from nixpkgs how the library is maintained with individual files separated by purpose and the module sets (most of the time).

  • ./utils/ is another part of Bahaghari library. You can see more details in Library development.

  • ./modules/ is where the Bahaghari module sets reside. All of them are then separated by… module sets and are organized by the aforementioned default.nix.

  • ./tests/ is where the test suites reside. Well cover how to interact with the test suite right after this section.

An interesting thing to note: being a project where it exposes a Nix module and a library set, we actually dont use flakes for pinning our supported branches of nixpkgs and/or NixOS as much as we dont want to be fully locked into that feature and will also limit support for non-flake usage especially if a potential contributor doesnt use flakes. Instead, we use a different pinning tool with npins. Please see its homepage for more information on using it.

Library development

A large part of Bahaghari is its library. Theres actually two top-level directories that you have to keep in mind: lib and utils. Bahaghari separates its function set as either part of the library subset or utilities subset patterned after the NixOS environment of adding utils module argument. The main difference between these two is the utilities subset depends on the environment configuration (for example, using config.bahaghari.tinted-theming.schemes) while those in library subset does not. We just separate these for easier maintenance.

Testing

This project comes with a test suite for… testing things. You know, for the purpose of preventing as much regression and catching as many potential bugs before releasing this for the users.

For the Bahaghari library set, we have a testing infrastructure set all set in ./tests/. This relies on the lib.debug.runTests from nixpkgs which is enough for our simple needs of making sure the functions are correct. To run the test suite, heres one way to do it.[3]

nix eval -f ./tests lib

We also have a test suites for the Nix module sets. Similarly from the library set testing, it relies on nixpkgs' NixOS test integration.[4] You can test those puppies out with the following command.[3]

nix eval -f ./tests modules

1. Tagalog word for "rainbow" or a "kings loincloth" if you prefer.
2. While Bahaghari as a project can also focus beyond colorschemes similar to Stylix, it isnt a part of the vision for now.
3. Assuming you have the new Nix CLI and have nixpkgs as part of NIX_PATH.
4. For now, we only test these with NixOS since most of Bahaghari is environment-agnostic anyways. Were more concerned if it can used as intended.