From 0caacbdf420e1da5da01353958f5bd082f77b884 Mon Sep 17 00:00:00 2001 From: lassulus Date: Sat, 4 Jul 2020 06:15:53 +0200 Subject: l: add klem service --- lass/2configs/baseX.nix | 32 +++++++++++++++++++++ lass/3modules/klem.nix | 75 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 107 insertions(+) create mode 100644 lass/3modules/klem.nix diff --git a/lass/2configs/baseX.nix b/lass/2configs/baseX.nix index 1185be9d4..3376f5ea2 100644 --- a/lass/2configs/baseX.nix +++ b/lass/2configs/baseX.nix @@ -147,4 +147,36 @@ in { krebs.xresources.enable = true; lass.screenlock.enable = true; + + lass.klem = { + kpaste.script = pkgs.writeDash "kpaste-wrapper" '' + ${pkgs.kpaste}/bin/kpaste \ + | ${pkgs.coreutils}/bin/tail -1 \ + | ${pkgs.coreutils}/bin/tr -d '\r\n' + ''; + go = { + target = "STRING"; + script = "${pkgs.goify}/bin/goify"; + }; + "go.lassul.us" = { + target = "STRING"; + script = pkgs.writeDash "go.lassul.us" '' + export GO_HOST='go.lassul.us' + ${pkgs.goify}/bin/goify + ''; + }; + qrcode = { + target = "image"; + script = pkgs.writeDash "zbar" '' + ${pkgs.zbar}/bin/zbarimg -q - + ''; + }; + ocr = { + target = "image"; + script = pkgs.writeDash "gocr" '' + ${pkgs.netpbm}/bin/pngtopnm - \ + | ${pkgs.gocr}/bin/gocr - + ''; + }; + }; } diff --git a/lass/3modules/klem.nix b/lass/3modules/klem.nix new file mode 100644 index 000000000..a297adcd1 --- /dev/null +++ b/lass/3modules/klem.nix @@ -0,0 +1,75 @@ +{ config, pkgs, ... }: with import ; let + cfg = config.lass.klem; +in { + options.lass.klem = mkOption { + default = {}; + type = types.attrsOf (types.submodule ({ config, ...}: { + options = { + target = mkOption { + default = ".*"; + description = '' + regex of valid targets + can be shown with xclip -selection clipboard -t TARGETS + the first hit is taken as target argument + ''; + type = types.str; + }; + script = mkOption { + description = '' + file to run if entry is selected + ''; + type = types.path; + }; + label = mkOption { + default = config._module.args.name; + description = '' + label to show in dmenu for this script + ''; + type = types.str; + }; + }; + })); + }; + config = let + klem = pkgs.writers.writeDashBin "klem" '' + set -x + + labels="" + # match filetype against patterns + ${concatMapStringsSep "\n" (script: '' + ${pkgs.xclip}/bin/xclip -selection clipboard -target TARGETS -out \ + | grep -q '${script.target}' + if [ $? -eq 0 ]; then + labels="$labels:${script.label}" + fi + '') (attrValues cfg)} + + #remove empty line, feed into dmenu + script=$(echo "$labels" \ + | ${pkgs.gnused}/bin/sed 's/^://;s/:/\n/g' \ + | ${pkgs.dmenu}/bin/dmenu) + + #run the chosen script + case $script in + ${concatMapStringsSep "\n" (script: indent '' + ${script.label}) + target=$(${pkgs.xclip}/bin/xclip -selection clipboard -target TARGETS -out \ + | ${pkgs.gnugrep}/bin/grep '${script.target}' \ + | ${pkgs.gnugrep}/bin/grep -v TARGETS \ + | ${pkgs.coreutils}/bin/head -1) + ${pkgs.xclip}/bin/xclip -selection clipboard -target "$target" -out \ + | ${script.script} \ + | ${pkgs.xclip}/bin/xclip -selection clipboard -in + ;; + '') (attrValues cfg)} + esac + ''; + in mkIf (cfg != {}) { + environment.systemPackages = [ klem ]; + nixpkgs.overlays = [ + (self: super: { + klem = klem; + }) + ]; + }; +} -- cgit v1.2.3