base16-nix/base16.nix

123 lines
3.6 KiB
Nix
Raw Permalink Normal View History

2020-07-20 20:41:02 +01:00
{ config, lib, pkgs, ... }:
2019-02-17 10:29:11 +00:00
with lib;
2020-07-20 20:41:02 +01:00
2019-02-17 10:29:11 +00:00
let
cfg = config.themes.base16;
2020-07-20 20:41:02 +01:00
inherit (builtins) pathExists;
2020-07-23 12:28:12 +01:00
2020-07-20 20:41:02 +01:00
schemes = importJSON ./schemes.json;
2019-02-17 10:29:11 +00:00
templates = importJSON ./templates.json;
2020-07-20 20:41:02 +01:00
# Data file for a given base16 scheme and variant. Returns the nix store
# path of the file.
mkTheme = scheme: variant:
2021-06-16 04:20:41 +03:00
"${builtins.fetchGit {
url = schemes.${scheme}.url;
rev = schemes.${scheme}.rev;
}}/${variant}.yaml";
2020-07-20 20:41:02 +01:00
2020-08-10 00:51:23 +04:00
# Source file for a given base16 template.
2020-07-20 20:41:02 +01:00
# Returns the nix store path of the file.
2020-08-10 00:51:23 +04:00
mkTemplate = name: type:
2020-08-09 21:30:32 +04:00
let
2021-06-16 04:20:41 +03:00
templateDir = "${builtins.fetchGit {
url = templates.${name}.url;
rev = templates.${name}.rev;
}}/templates";
2020-07-20 20:41:02 +01:00
in
2020-08-10 00:51:23 +04:00
if pathExists (templateDir + "/${type}.mustache")
then templateDir + "/${type}.mustache"
2020-08-09 21:30:32 +04:00
else templateDir + "/default.mustache";
2019-02-17 10:29:11 +00:00
2020-07-20 20:41:02 +01:00
# The theme yaml files only supply 16 hex values, but the templates take
# a transformation of this data such as rgb. The hacky python script pre-
# processes the theme file in this way for consumption by the mustache
# engine below.
2022-10-11 19:45:09 +03:00
python = pkgs.python3.withPackages (ps: [ ps.pyyaml ]);
2020-07-20 20:41:02 +01:00
preprocess = src:
pkgs.stdenv.mkDerivation {
2020-08-09 21:30:32 +04:00
name = "yaml";
2020-07-20 20:41:02 +01:00
inherit src;
builder = pkgs.writeText "builder.sh" ''
2020-08-09 21:30:32 +04:00
slug_all=$(${pkgs.coreutils}/bin/basename $src)
slug=''${slug_all%.*}
${python}/bin/python ${./base16writer.py} $slug < $src > $out
'';
2020-07-20 20:41:02 +01:00
allowSubstitutes = false; # will never be in cache
};
2019-02-17 10:29:11 +00:00
2020-07-20 20:41:02 +01:00
# Mustache engine. Applies any theme to any template, providing they are
# included in the local json source files.
2020-08-10 00:51:23 +04:00
mustache = scheme: variant: name: type:
2020-07-20 20:41:02 +01:00
pkgs.stdenv.mkDerivation {
name = "${name}-base16-${variant}";
data = preprocess (mkTheme scheme variant);
2020-08-10 00:51:23 +04:00
src = mkTemplate name type;
2020-07-20 20:41:02 +01:00
phases = [ "buildPhase" ];
buildPhase ="${pkgs.mustache-go}/bin/mustache $data $src > $out";
allowSubstitutes = false; # will never be in cache
2019-02-17 10:29:11 +00:00
};
2020-08-10 18:58:49 +04:00
mustacheCustom = schemePath: name: type:
pkgs.stdenv.mkDerivation {
name = "${name}-base16-scheme";
data = preprocess schemePath;
src = mkTemplate name type;
phases = [ "buildPhase" ];
buildPhase ="${pkgs.mustache-go}/bin/mustache $data $src > $out";
allowSubstitutes = false; # will never be in cache
};
2020-08-09 21:30:32 +04:00
schemeJSON = scheme: variant:
importJSON (preprocess (mkTheme scheme variant));
2020-08-10 18:58:49 +04:00
schemeJSONCustom = schemePath:
importJSON (preprocess schemePath);
2019-02-17 10:29:11 +00:00
in
{
2020-08-10 18:58:49 +04:00
options = with types; {
themes.base16 = {
enable = mkEnableOption "Base 16 Color Schemes";
customScheme = {
enable = mkEnableOption "Use custom scheme instead of remote repository";
path = mkOption {
type = nullOr path;
default = null;
};
};
scheme = mkOption {
type = str;
default = "solarized";
};
variant = mkOption {
type = str;
default = "solarized-dark";
};
extraParams = mkOption {
type = attrsOf anything;
2020-08-10 18:58:49 +04:00
default = {};
};
defaultTemplateType = mkOption {
type = str;
default = "default";
example = "colors";
};
2020-08-10 00:51:23 +04:00
};
2019-02-17 10:29:11 +00:00
};
config = {
2021-06-16 04:20:41 +03:00
lib.base16.theme =
2020-08-10 18:58:49 +04:00
if cfg.customScheme.enable then
schemeJSONCustom cfg.customScheme.path // cfg.extraParams
else
schemeJSON cfg.scheme cfg.variant // cfg.extraParams;
2021-06-16 04:20:41 +03:00
lib.base16.templateFile = { name, type ? cfg.defaultTemplateType, ... }:
2020-08-10 18:58:49 +04:00
if cfg.customScheme.enable then
mustacheCustom cfg.customScheme.path name type
else
2020-08-10 00:51:23 +04:00
mustache cfg.scheme cfg.variant name type;
2019-02-17 10:29:11 +00:00
};
}