Skip to content

Theme applications with your favourite base16 colorschemes in Nix

License

Notifications You must be signed in to change notification settings

SenchoPens/base16.nix

Repository files navigation

logo

demo

Introduction

tinted-theming (continuation of base16)is a framework with hundreds of colorschemes and application configuration templates.

base16.nixcombines the expressiveness of Nix with the abundance of tinted-theming to help you theme your setup.

Features

Withbase16.nix,you can:

  • load base16and base24schemes, override them or specify custom ones in YAML / nix, to use across the configuration;
  • theme applications with existing or custom templates.

Note thatbase16.nixis a simple theming interface,notan all-batteries-included ricing engine — but if you want one, check outStylix,which, among other things, complementsbase16.nixfunctionality with pre-defined configuration for common application.

👀 Tutorial by Example

In this tutorial, we will usebase16.nixas a NixOS module to theme zathura,neovimandalacrittyto use thenordscheme (home-manager module works the same way).

Import and set the scheme (step 1/2)

In your NixOS configuration directory:

flake.nix

{inputs={
# Add base16.nix, base16 schemes and
# zathura and vim templates to the flake inputs.
base16.url="github:SenchoPens/base16.nix";

tt-schemes={
url="github:tinted-theming/schemes";
flake=false;
};

base16-zathura={
url="github:haozeke/base16-zathura";
flake=false;
};

base16-vim={
url="github:tinted-theming/base16-vim";
flake=false;
};
...
};
outputs={self,...}@inputs{
...
nixosSystem{
modules=[
# import the base16.nix module
base16.nixosModule
# set system's scheme to nord by setting `config.scheme`
{scheme="${inputs.tt-schemes}/base16/nord.yaml";}
# import `theming.nix`, we will write it in the next, final, step
./theming.nix
...
];
# so you can use `inputs` in config files
specialArgs={
inheritinputs;
};
...
};
...
};
...}

Theme (step 2/2)

Now thatconfig.schemeis set, we can use it like a function to create themes from templates.

theming.nix

{config,pkgs,inputs,...}:
{
# Theme zathura
home-manager.users.sencho.programs.zathura.extraConfig=
builtins.readFile(config.schemeinputs.base16-zathura);

# Theme `neovim` — more complex, but the principle is the same.
home-manager.users.sencho.programs.neovim={
plugins=[(pkgs.vimPlugins.base16-vim.overrideAttrs(old:
letschemeFile=config.schemeinputs.base16-vim;
in{patchPhase=''cp${schemeFile}colors/base16-scheme.vim'';}
))];
extraConfig=''
set termguicolors background=dark
let base16colorspace=256
colorscheme base16-scheme
'';
};

# Theme `alacritty`. home-manager doesn't provide an `extraConfig`,
# but gives us `settings.colors` option of attrs type to set colors.
# As alacritty expects colors to begin with `#`, we use an attribute `withHashtag`.
# Notice that we now use `config.scheme` as an attrset, and that this attrset,
# besides from having attributes `base00`...`base0F`, has mnemonic attributes (`red`, etc.) -
# read more on that in the next section.
home-manager.users.sencho.programs.alacritty.settings.colors=
withconfig.scheme.withHashtag;letdefault={
black=base00;white=base07;
inheritredgreenyellowbluecyanmagenta;
};
in{
primary={background=base00;foreground=base07;};
cursor={text=base02;cursor=base07;};
normal=default;bright=default;dim=default;
};
}

That's all, we themed 3 applications!

The attentive reader will notice that after settingconfig.schemeto astring, we use it as afunction(to themezathuraandneovim) and as anattrset(to themealacritty) — that'sbase16.nix' magic! Read theDocumentationsection to see how it works.

🍳 How To

Import a scheme from a YAML file
config.scheme="${inputs.tt-schemes}/base16/nord.yaml ";
Override a scheme

We need to explicitly usemkSchemeAttrsfunction to use theoverridefield of the resultingscheme attrs:

config.scheme=(config.lib.base16.mkSchemeAttrs"${inputs.tt-schemes}/base16/nord.yaml ").override{
scheme="Now it's my scheme >:]";
base00="000000";# make background completely black
};
Declare a scheme in Nix
config.scheme={
slug="balsoftheme";scheme="Theme by balsoft";author="balsoft";
base00="000000";base01="333333";base02="666666";base03="999999";
base04="cccccc";base05="ffffff";base06="e6e6e6";base07="e6e6e6";
base08="bf4040";base09="bf8040";base0A="bfbf40";base0B="80bf40";
base0C="40bfbf";base0D="407fbf";base0E="7f40bf";base0F="bf40bf";
};

source

Use multiple schemes simultaneously

Achieve this by theming withoutconfig.scheme— by callingmkSchemeAttrs:

home-manager.users.sencho.programs.zathura.extraConfig=
builtins.readFile(config.lib.base16.mkSchemeAttrsinputs.tt-schemesinputs.base16-zathura);

Without importingbase16.nixas a module at all:

home-manager.users.sencho.programs.zathura.extraConfig=
builtins.readFile((pkgs.callPackageinputs.base16.lib{}).mkSchemeAttrsinputs.tt-schemesinputs.base16-zathura);
Use template variation

Template repositories often define more than one template variation. For example,zathura template repository definesdefault.mustache(colors only the interface) andrecolor.mustache (colors the interface and pdfs).

By defaultbase16.nixusesdefault.mustache. To use another template, e.g.recolor.mustache:

home-manager.users.sencho.programs.zathura.extraConfig=
builtins.readFile(config.scheme{
templateRepo=inputs.base16-zathura;target="recolor";
});
Override a template

Sample use-case: suppose you likezathura'sdefault.mustachetemplate, but want to change the background (default-bg) frombase00tobase01.

  1. Override the scheme only forzathura:
home-manager.users.sencho.programs.zathura.extraConfig=
builtins.readFile((config.scheme.override{
base00=config.scheme.base01;
})inputs.base16-zathura);

Keep in mind that by doing so you'll change not only default-bgcolor, but alsoinputbar-bg,notification-bg,etc.

  1. Copy-paste the template and modify it:
home-manager.users.sencho.programs.zathura.extraConfig=
builtins.readFile(config.scheme{template=''
...
set default-bg "#{{base01-hex}}" # <-- we changed this
set default-fg "#{{base01-hex}}"

set statusbar-fg "#{{base04-hex}}"
set statusbar-bg "#{{base02-hex}}"
...
'';});

📚 Documentation

Consult theDOCUMENTATION.mdto learn about every feature in detail and see howbase16.nixworks underhood.

☎️ Troubleshooting

Error / incorrect behavior after updating base16.nix or adding a new source / template

The most probable reason of such an error is incorrectly parsed YAML file of either a scheme or a template.

Fix incorrectly parsed YAML file

  • Enable IFD (but beware of a possible error described below): If the problem is in the scheme YAML file, set the scheme as such:
    config.scheme={
    yaml="${inputs.tt-schemes}/base16/nord.yaml ";
    use-ifd="auto";# to suppress errors, set to "always"
    };
    If the problem is in the templatetemplates/config.yamlfile, turn onuse-ifd:
    home-manager.users.sencho.programs.zathura.extraConfig=
    builtins.readFile(config.scheme{
    use-ifd="always";
    templateRepo=inputs.base16-zathura;target="recolor";
    });
  • If you think that it's safe to ignore the error on template instantiation, you can turn off the check:
    home-manager.users.sencho.programs.zathura.extraConfig=
    builtins.readFile(config.scheme{
    check-parsed-config-yaml=false;
    templateRepo=inputs.base16-zathura;target="recolor";
    });
  • If the problem is with a scheme YAML file and the nix evaluates, add theconfig.scheme.checkderivation to your NixOS / home-manager package list, this will indicate which part of the YAML is being parsed incorrectly.
  • Submit an issue.
  • Fix the YAML upstream. Probable causes: trailing spaces, file structure differs from typicalconfig.yaml/ scheme YAML files.
  • Fix the Nix parser 😈.

Context: since version v2.0.0base16.nixparses the YAML file in pure Nix to bypass IFD issues. The parser works for mostbase16-<scheme-name>.yamland templates'config.yamlfiles, but, as YAML can be quite complicated, sometimes they can be parsed incorrectly.

The exact error depends on the point of failure. It will probably be cryptic if incorrect parsing caused an issue during nix evaluation. Otherwise, if your flake evaluates (nix flake checksucceeds), the error may look something like this:

error: builder for '/nix/store/snbbfb43qphzfl6xr1mjs0mr8jny66x9-base16-nix-parse-check.drv' failed with exit code 1;
last 7 log lines:
> running tests
> Output of "jd /nix/store/9jvxabhfx9acrysknblg0r2hzvcwv6ab-fromYAML /nix/store/qwmj9cbg7fpi5fvyd2x3kywfbw7hlm8f-parsed-yaml-as-json":
> @ [ "gotcha" ]
> - [ "1 2" ]
> + "[ 1 2 ]"
> Error: /nix/store/qhdqwj0mfp8qn0gq5s95pgd2i57lb09c-source/base16-kandinsky.yaml was parsed incorrectly during nix evaluation.
> Please consult https://github /SenchoPens/base16.nix/tree/main#%EF%B8%8F-troubleshooting

The check that produces this error happens by default for templates by installing a special derivation. You can do it for scheme too by adding theconfig.scheme.checkderivation to your NixOS / home-manager package list, though you might need to set the scheme to{ yaml =...; use-ifd = "auto"; }.

Error on `nix flake check` or `nix flake show`

First, check that you have the most recent version ofbase16.nix. If the error persists, check that you don't setuse-ifdanywhere to"auto"or"always".

Relevant issue: #3.

If neither of the above listed solutions do not work for you, please open an issue.

Anyhow, feel free to open an issue!

💙 Acknowledgments

Thanks to:

👩‍💻 Contributing

Contributions are highly welcome, but please keep in mind I want to keep the code compact.

Testing

To test the module, you can do the following:

  1. Set the flake url to the fork's absolute path:base16.url = "/home/sencho/github /SenchoPens/base16.nix";.
  2. Build the configuration:
nix flake lock --update-input base16
nixos-rebuild build --flake.--fast#NixOS
home-manager build --flake.--no-out-link#home-manager

Note that you don't have to commit the changes to test them.