diff --git a/configs/hledger.nix b/configs/hledger.nix index 92c4644..d86b5c0 100644 --- a/configs/hledger.nix +++ b/configs/hledger.nix @@ -1,33 +1,15 @@ { - config, pkgs, ... }: let - ledgerDirectory = "$HOME/projects/ledger"; - timeLedger = "${ledgerDirectory}/time.timeclock"; - hora = pkgs.writers.writeDashBin "hora" '' - ${pkgs.hledger}/bin/hledger -f "${timeLedger}" "$@" - ''; + ledgerDirectory = "/home/kfm/projects/ledger"; + hora = pkgs.callPackage ../packages/hora.nix { timeLedger = "${ledgerDirectory}/time.timeclock"; }; in { environment.systemPackages = let git = "${pkgs.git}/bin/git -C ${ledgerDirectory}"; in [ - pkgs.hledger - (pkgs.writers.writeDashBin "hora-edit" '' - $EDITOR + "${timeLedger}" && ${pkgs.git}/bin/git -C "$(${pkgs.coreutils}/bin/dirname ${timeLedger})" commit --all --message "$(${pkgs.coreutils}/bin/date -Im)" - '') hora - (pkgs.writers.writeDashBin "hora-year" '' - ${hora}/bin/hora bal --tree --monthly --begin $(date +%Y) --depth 1 - '') - (pkgs.writers.writeDashBin "hora-filli" '' - ${pkgs.hledger}/bin/hledger -f "${timeLedger}" register fillidefilla -O csv \ - -b "$(date -d "$(date +%Y-%m)-01 last month" +%Y-%m-%d)" \ - -e "$(date -d "$(date +%Y-%m)-01" +%Y-%m-%d)" \ - | sed 's/(fillidefilla:\(.*\))/\1/g' \ - | xsv select date,amount,total,account,description - '') - + pkgs.hledger (pkgs.writers.writeDashBin "hledger-git" '' if [ "$1" = entry ]; then ${pkgs.hledger}/bin/hledger balance -V > "${ledgerDirectory}/balance.txt" diff --git a/packages/hora.nix b/packages/hora.nix new file mode 100644 index 0000000..94e641c --- /dev/null +++ b/packages/hora.nix @@ -0,0 +1,80 @@ +{ symlinkJoin +, hledger +, writers +, lib +, git +, coreutils +, gnugrep +, timeLedger +, ... +}: +let + date = "${coreutils}/bin/date +'%Y-%m-%d %H:%M:%S'"; + + hora = writers.writeDashBin "hora" "${hledger}/bin/hledger -f ${lib.escapeShellArg timeLedger} \"$@\""; + hora-edit = writers.writeDashBin "hora-edit" "$EDITOR ${lib.escapeShellArg timeLedger}"; + hora-status = writers.writeDashBin "hora-status" "${coreutils}/bin/tac ${lib.escapeShellArg timeLedger} | ${gnugrep}/bin/grep -m 1 ."; + + hora-start = writers.writeDashBin "hora-start" '' + last_nonempty_line=$(${hora-status}/bin/hora-status) + (echo $last_nonempty_line | ${gnugrep}/bin/grep -q "^o") || { + echo "Last activity must be closed: $last_nonempty_line" >/dev/stderr + exit 1 + } + + account=$1 + (${hora}/bin/hora accounts | ${gnugrep}/bin/grep -q "^$account\$") || { + echo "The account '$account' is not known. Please add manually." + exit 1 + } + + message=$2 + date=$(${date}) + echo "i $date $account $message\n" >> "${timeLedger}" + echo "Started $account at $date" >/dev/stderr + ''; + + hora-stop = writers.writeDashBin "hora-stop" '' + last_nonempty_line=$(${hora-status}/bin/hora-status) + + (echo $last_nonempty_line | ${gnugrep}/bin/grep "^i") || { + echo "Last activity cannot be closed: $last_nonempty_line" >/dev/stderr + exit 1 + } + + last_activity=$(echo "$last_nonempty_line" | ${coreutils}/bin/cut -d' ' -f 4) + date=$(${date}) + + echo "o $date\n" >> ${timeLedger} + echo "Stopped $last_activity at $date" >/dev/stderr + ''; + + hora-year = writers.writeDashBin "hora-year" '' + ${hora}/bin/hora balance --tree --monthly --begin $(${coreutils}/bin/date +%Y) --depth 1 + ''; + hora-git = writers.writeDashBin "hora-git" '' + directory=$(${coreutils}/bin/dirname ${lib.escapeShellArg timeLedger}) + if [ $# -gt 0 ] + then + ${git}/bin/git -C "$directory" --all --message=$(${date}) + else + ${git}/bin/git -C "$directory" "$@" + fi + ''; + hora-weekly = writers.writeDashBin "hora-weekly" '' + ${hora}/bin/hora register -p weekly --depth 1 --empty + ''; +in +symlinkJoin { + name = "hora"; + paths = [ + hora + hora-edit + hora-start + hora-status + hora-stop + hora-year + hora-git + hora-weekly + ]; +} diff --git a/systems/maakaron/home.nix b/systems/maakaron/home.nix index f284f53..bad73e1 100644 --- a/systems/maakaron/home.nix +++ b/systems/maakaron/home.nix @@ -9,7 +9,10 @@ system = "x86_64-darwin"; nextcloud = "${config.home.homeDirectory}/Nextcloud/ZODIAC"; - timeLedger = "${nextcloud}/hora.timeclock"; + + hora = pkgs.callPackage ../../packages/hora.nix { + timeLedger = "${nextcloud}/hora.timeclock"; + }; adminEssentials = import ../../configs/admin-essentials.nix { inherit pkgs niveumPackages lib system; @@ -25,53 +28,7 @@ in { home.packages = [ - (pkgs.writers.writeDashBin "hora" '' - ${pkgs.hledger}/bin/hledger -f "${timeLedger}" "$@" - '') - (pkgs.writers.writeDashBin "hora-edit" '' - nvim + "${timeLedger}" - '') - (pkgs.writers.writeDashBin "hora-status" '' - ${pkgs.coreutils}/bin/tac "${timeLedger}" | ${pkgs.gnugrep}/bin/grep -m 1 . - '') - (pkgs.writers.writeDashBin "hora-start" '' - [ -w "${timeLedger}" ] || { - echo "${timeLedger}" is not a writable file >/dev/stderr - exit 1 - } - last_nonempty_line=$(hora-status) - (echo $last_nonempty_line | ${pkgs.gnugrep}/bin/grep -q "^o") || { - echo "Last activity must be closed: $last_nonempty_line" >/dev/stderr - exit 1 - } - account=$1 - (hora accounts | ${pkgs.gnugrep}/bin/grep -q "^$account\$") || { - echo "The account '$account' is not known. Please add manually." - exit 1 - } - - message=$2 - date=$(${pkgs.coreutils}/bin/date +"%Y-%m-%d %H:%M:%S") - echo "i $date $account $message\n" >> "${timeLedger}" - echo Started $account at $date >/dev/stderr - '') - (pkgs.writers.writeDashBin "hora-stop" '' - [ -w "${timeLedger}" ] || { - echo "${timeLedger}" is not a writable file >/dev/stderr - exit 1 - } - last_nonempty_line=$(hora-status) - (echo $last_nonempty_line | ${pkgs.gnugrep}/bin/grep "^i") || { - echo "Last activity cannot be closed: $last_nonempty_line" >/dev/stderr - exit 1 - } - - last_activity=$(${pkgs.coreutils}/bin/tail -n 1 ${timeLedger} | ${pkgs.coreutils}/bin/cut -d' ' -f 4) - date=$(${pkgs.coreutils}/bin/date +"%Y-%m-%d %H:%M:%S") - - echo "o $date\n" >> ${timeLedger} - echo Stopped $last_activity at $date >/dev/stderr - '') + hora niveumPackages.vim pkgs.ghc pkgs.python3