fix: centralize and nixifiy in 'zk' module
This commit is contained in:
45
home/modules/zk/default.nix
Normal file
45
home/modules/zk/default.nix
Normal file
@@ -0,0 +1,45 @@
|
||||
{
|
||||
config,
|
||||
lib,
|
||||
pkgs,
|
||||
...
|
||||
}:
|
||||
|
||||
let
|
||||
cfg = config.zk;
|
||||
in
|
||||
{
|
||||
options.zk = {
|
||||
enable = lib.mkEnableOption "zettelkasten";
|
||||
path = lib.mkOption {
|
||||
type = lib.types.str;
|
||||
default = config.home.homeDirectory + "/.zk";
|
||||
description = "Path to the zettelkasten directory";
|
||||
};
|
||||
};
|
||||
|
||||
config = lib.mkIf cfg.enable {
|
||||
home = {
|
||||
sessionVariables.ZK_PATH = cfg.path;
|
||||
packages = [
|
||||
(pkgs.writeShellApplication {
|
||||
name = "zk";
|
||||
runtimeInputs = with pkgs; [ tmux ];
|
||||
text = builtins.readFile ./scripts/zk.sh;
|
||||
})
|
||||
|
||||
(pkgs.writeShellApplication {
|
||||
name = "save-zk";
|
||||
runtimeInputs = with pkgs; [ git ];
|
||||
text = builtins.readFile ./scripts/save-zk.sh;
|
||||
})
|
||||
|
||||
(pkgs.writeShellApplication {
|
||||
name = "setup-zk";
|
||||
runtimeInputs = with pkgs; [ gh ];
|
||||
text = builtins.readFile ./scripts/setup-zk.sh;
|
||||
})
|
||||
];
|
||||
};
|
||||
};
|
||||
}
|
||||
2
home/modules/zk/scripts/save-zk.sh
Normal file
2
home/modules/zk/scripts/save-zk.sh
Normal file
@@ -0,0 +1,2 @@
|
||||
cd "$ZK_PATH" || { echo "No zettelkasten directory found"; exit 1; }
|
||||
git add . && git commit -m "Update" && git push
|
||||
13
home/modules/zk/scripts/setup-zk.sh
Normal file
13
home/modules/zk/scripts/setup-zk.sh
Normal file
@@ -0,0 +1,13 @@
|
||||
if [ ! -d "$ZK_PATH" ]; then
|
||||
echo "[zk] Setting up zettelkasten"
|
||||
gh repo clone zk "$ZK_PATH"
|
||||
else
|
||||
echo "[zk] Zettelkasten already set up."
|
||||
fi
|
||||
|
||||
read -p "Would you like open your zettelkasten? [y/N] " -n 1 -r
|
||||
echo
|
||||
|
||||
if [[ $REPLY =~ ^[Yy]$ ]]; then
|
||||
$EDITOR "$ZK_PATH"
|
||||
fi
|
||||
33
home/modules/zk/scripts/zk.sh
Normal file
33
home/modules/zk/scripts/zk.sh
Normal file
@@ -0,0 +1,33 @@
|
||||
current_zettel_path="$ZK_PATH/$(cat "$ZK_PATH/current-zettel.txt")"
|
||||
|
||||
if [ -n "$TMUX" ]; then
|
||||
cd "$ZK_PATH" && $EDITOR "$current_zettel_path"
|
||||
else
|
||||
echo 'Not in tmux'
|
||||
echo 'Choose an option:'
|
||||
echo '1. Open in tmux'
|
||||
echo '2. Open in current terminal'
|
||||
read -r -p 'Enter your choice: ' choice
|
||||
case $choice in
|
||||
1)
|
||||
# Check if a tmux session is running with a window named zk
|
||||
if tmux list-windows -F '#{window_name}' | grep -q zk; then
|
||||
# Attach to the session containing the 'zk' window
|
||||
session="$(tmux list-windows -F '#{window_name} #{session_name}' | grep zk | head -n 1 | awk '{ print $2 }')"
|
||||
tmux attach -t "$session"
|
||||
else
|
||||
# Create session with a window named 'zk' and start nvim
|
||||
tmux new-session -s zk -n zk -d
|
||||
tmux send-keys -t zk:zk "cd $ZK_PATH && $EDITOR $current_zettel_path" Enter
|
||||
tmux attach -t zk
|
||||
fi
|
||||
;;
|
||||
2)
|
||||
cd "$ZK_PATH" && $EDITOR "$current_zettel_path"
|
||||
;;
|
||||
*)
|
||||
echo 'Not opening Zettelkasten'
|
||||
exit 1
|
||||
;;
|
||||
esac
|
||||
fi
|
||||
Reference in New Issue
Block a user