This repository has no description
2.3 kB
76 lines
1{ self, config, lib, pkgs, ... }: with lib; let
2 cfg = config.lass.klem;
3 slib = self.inputs.stockholm.lib;
4in {
5 options.lass.klem = mkOption {
6 default = {};
7 type = types.attrsOf (types.submodule ({ config, ...}: {
8 options = {
9 target = mkOption {
10 default = ".*";
11 description = ''
12 regex of valid targets
13 can be shown with xclip -selection clipboard -t TARGETS
14 the first hit is taken as target argument
15 '';
16 type = types.str;
17 };
18 script = mkOption {
19 description = ''
20 file to run if entry is selected
21 '';
22 type = types.path;
23 };
24 label = mkOption {
25 default = config._module.args.name;
26 description = ''
27 label to show in dmenu for this script
28 '';
29 type = types.str;
30 };
31 };
32 }));
33 };
34 config = let
35 klem = pkgs.writers.writeDashBin "klem" ''
36 set -x
37
38 labels=""
39 # match filetype against patterns
40 ${concatMapStringsSep "\n" (script: ''
41 ${pkgs.xclip}/bin/xclip -selection clipboard -target TARGETS -out \
42 | ${pkgs.gnugrep}/bin/grep -q '${script.target}'
43 if [ $? -eq 0 ]; then
44 labels="$labels:${script.label}"
45 fi
46 '') (attrValues cfg)}
47
48 #remove empty line, feed into dmenu
49 script=$(echo "$labels" \
50 | ${pkgs.gnused}/bin/sed 's/^://;s/:/\n/g' \
51 | ${pkgs.dmenu}/bin/dmenu)
52
53 #run the chosen script
54 case $script in
55 ${concatMapStringsSep "\n" (script: slib.indent ''
56 ${script.label})
57 target=$(${pkgs.xclip}/bin/xclip -selection clipboard -target TARGETS -out \
58 | ${pkgs.gnugrep}/bin/grep '${script.target}' \
59 | ${pkgs.gnugrep}/bin/grep -v TARGETS \
60 | ${pkgs.coreutils}/bin/head -1)
61 ${pkgs.xclip}/bin/xclip -selection clipboard -target "$target" -out \
62 | ${script.script} \
63 | ${pkgs.xclip}/bin/xclip -selection clipboard -in
64 ;;
65 '') (attrValues cfg)}
66 esac
67 '';
68 in mkIf (cfg != {}) {
69 environment.systemPackages = [ klem ];
70 nixpkgs.overlays = [
71 (self: super: {
72 klem = klem;
73 })
74 ];
75 };
76}