|
| 1 | +{ |
| 2 | + config, |
| 3 | + lib, |
| 4 | + pkgs, |
| 5 | + ... |
| 6 | +}: let |
| 7 | + bindings = builtins.fromJSON (builtins.readFile ../t3code-keybindings.json); |
| 8 | + bindingsJson = builtins.toJSON bindings; |
| 9 | + keybindingsPath = "${config.home.homeDirectory}/.t3/userdata/keybindings.json"; |
| 10 | + installBindings = pkgs.writeShellScript "install-t3code-keybindings" '' |
| 11 | + set -eu |
| 12 | +
|
| 13 | + keybindings_path=${lib.escapeShellArg keybindingsPath} |
| 14 | + if [ ! -f "$keybindings_path" ]; then |
| 15 | + exit 0 |
| 16 | + fi |
| 17 | +
|
| 18 | + keybindings_dir="$(${pkgs.coreutils}/bin/dirname "$keybindings_path")" |
| 19 | + temporary="$(${pkgs.coreutils}/bin/mktemp "$keybindings_dir/.keybindings.json.XXXXXX")" |
| 20 | + trap '${pkgs.coreutils}/bin/rm -f "$temporary"' EXIT |
| 21 | +
|
| 22 | + ${pkgs.jq}/bin/jq \ |
| 23 | + --argjson desired ${lib.escapeShellArg bindingsJson} \ |
| 24 | + '($desired | map(.key)) as $keys |
| 25 | + | ($desired | map(.command)) as $commands |
| 26 | + | [.[] as $binding |
| 27 | + | select(($keys | index($binding.key)) == null and ($commands | index($binding.command)) == null) |
| 28 | + | $binding] |
| 29 | + + $desired' \ |
| 30 | + "$keybindings_path" > "$temporary" |
| 31 | +
|
| 32 | + if ${pkgs.diffutils}/bin/cmp -s "$keybindings_path" "$temporary"; then |
| 33 | + exit 0 |
| 34 | + fi |
| 35 | + ${pkgs.coreutils}/bin/chmod 0600 "$temporary" |
| 36 | + ${pkgs.coreutils}/bin/mv "$temporary" "$keybindings_path" |
| 37 | + trap - EXIT |
| 38 | + ''; |
| 39 | +in { |
| 40 | + home.activation.installT3CodeKeybindings = lib.hm.dag.entryAfter ["writeBoundary"] '' |
| 41 | + ${installBindings} |
| 42 | + ''; |
| 43 | + |
| 44 | + systemd.user.services.t3code-keybindings = lib.mkIf pkgs.stdenv.isLinux { |
| 45 | + Unit.Description = "Install personal T3 Code keybindings"; |
| 46 | + Service = { |
| 47 | + Type = "oneshot"; |
| 48 | + ExecStart = "${installBindings}"; |
| 49 | + }; |
| 50 | + Install.WantedBy = ["default.target"]; |
| 51 | + }; |
| 52 | + |
| 53 | + systemd.user.paths.t3code-keybindings = lib.mkIf pkgs.stdenv.isLinux { |
| 54 | + Unit.Description = "Watch the T3 Code keybindings file"; |
| 55 | + Path = { |
| 56 | + PathChanged = keybindingsPath; |
| 57 | + Unit = "t3code-keybindings.service"; |
| 58 | + }; |
| 59 | + Install.WantedBy = ["default.target"]; |
| 60 | + }; |
| 61 | +} |
0 commit comments