Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Integrate keymap-drawer into flake #8

Closed
wants to merge 9 commits into from
20 changes: 10 additions & 10 deletions config/keymap_drawer.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -327,16 +327,16 @@ parse_config:
KP_MULTIPLY: '*'
KP_EQUAL: '='
KP_DOT: '.'
KP_N1: 1
KP_N2: 2
KP_N3: 3
KP_N4: 4
KP_N5: 5
KP_N6: 6
KP_N7: 7
KP_N8: 8
KP_N9: 9
KP_N0: 0
KP_N1: '1'
KP_N2: '2'
KP_N3: '3'
KP_N4: '4'
KP_N5: '5'
KP_N6: '6'
KP_N7: '7'
KP_N8: '8'
KP_N9: '9'
KP_N0: '0'

'LC(LEFT)': $$mdi:chevron-double-left$$
'LC(RIGHT)': $$mdi:chevron-double-right$$
Expand Down
115 changes: 115 additions & 0 deletions drawer/default.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
{
inputs,
lib,
...
}:
with builtins;
with lib; {
perSystem = {
config,
pkgs,
system,
...
}: let
inherit (inputs.poetry2nix.lib.mkPoetry2Nix {inherit pkgs;}) mkPoetryApplication;

getKeymapFiles = dir:
mapAttrsToList (name: _: dir + "/${name}") (filterAttrs (name: type: hasSuffix ".keymap" name && type != "directory") (readDir dir));

# Nix can't import yaml, so use `yj` to convert to JSON 😢
importYaml = file: let
jsonFile = pkgs.runCommandNoCC "converted-yaml.json" {} ''
${getExe pkgs.yj} < "${file}" > "$out"
'';
in
importJSON jsonFile;

pkg = config.packages.keymap-drawer;
exe = getExe pkg;

parsedPkg = config.packages.keymap-drawer-parsed;

# List of parsed keyboard configs, complete with various metadata
parsedCfgs = mapAttrsToList (fname: type:
assert hasSuffix ".yaml" fname;
assert type != "directory"; rec {
file = parsedPkg + "/${fname}";
name = removeSuffix ".yaml" fname;
data = importYaml file;
layers = attrNames data.layers;
}) (readDir parsedPkg);

keymapFiles = getKeymapFiles ../config;
configFile = ../config/keymap_drawer.yaml;
in {
packages = {
keymap-drawer = mkPoetryApplication {
projectDir = inputs.keymap-drawer;
preferWheels = true;
meta = {
mainProgram = "keymap";
homepage = "https://github.com/caksoylar/keymap-drawer";
};
};

keymap-drawer-parsed = pkgs.symlinkJoin {
name = "configs";
paths = map (file: let
name = removeSuffix ".keymap" (baseNameOf file);
in
pkgs.runCommandNoCC "${name}-parsed" {} ''
echo "Parsing keymap for ${name}"
mkdir -p "$out"

# Create a temp link because keymap-drawer uses the file's stem
# to guess the keyboard name...
# Using `file` directly won't work because it has a nar hash prefix.
# FIXME find workaround upstream
LINK="$out/${baseNameOf file}"
ln -s "${file}" "$LINK"

${exe} --config "${configFile}" \
parse --zmk-keymap "$LINK" \
> "$out/${name}.yaml"

# Cleanup temp link
rm "$LINK"
'')
keymapFiles;
};

keymap-drawer-svgs = pkgs.symlinkJoin {
name = "keymap-drawer-svgs";
paths =
concatMap (
{
file,
name,
layers,
...
}:
[
(pkgs.runCommandNoCC "${name}-all" {} ''
echo "Drawing all layers for ${name}"
mkdir -p "$out"
${exe} --config "${configFile}" \
draw "${file}" \
> "$out/${name}.svg"
'')
]
++ (map (layer:
pkgs.runCommandNoCC "${name}-${layer}" {} ''
echo "Drawing ${layer} layer for ${name}"
mkdir -p "$out"
${exe} --config "${configFile}" \
draw "${file}" \
--select-layers "${layer}" \
> "$out/${name}-${layer}.svg"
'')
layers)
)
parsedCfgs;
};
};
};
}
133 changes: 132 additions & 1 deletion flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@
url = "github:moergo-sc/zmk";
flake = false;
};
keymap-drawer = {
url = "github:caksoylar/keymap-drawer";
flake = false;
};
poetry2nix = {
url = "github:nix-community/poetry2nix";
inputs.nixpkgs.follows = "nixpkgs";
};
flake-parts = {
url = "github:hercules-ci/flake-parts";
inputs.nixpkgs-lib.follows = "nixpkgs";
Expand All @@ -19,13 +27,16 @@
self,
nixpkgs,
glove80-zmk,
keymap-drawer,
poetry2nix,
flake-parts,
devshell,
} @ inputs:
flake-parts.lib.mkFlake {inherit inputs;} {
systems = nixpkgs.lib.systems.flakeExposed;
imports = [
inputs.devshell.flakeModule
./drawer
];

perSystem = {
Expand Down
Loading
Loading