From ff9ac6367687482736bc9d9fe397b83c8e5fdeea Mon Sep 17 00:00:00 2001 From: Ingolf Wagner Date: Wed, 26 Apr 2023 09:23:56 +0200 Subject: [PATCH] add treefmt and format everything --- nixos/assets/jack.sh | 45 +++++++------- nixos/assets/music-making.sh | 58 +++++++++---------- ...nx-show-config.sh => nginx-show-config.py} | 7 ++- nixos/pkgs/gitlog2json/gitlog2json.py | 54 +++++++++-------- nixos/pkgs/gitlog2json/log.py | 10 +++- nixos/system/all/packages.nix | 5 ++ nixos/system/server/packages.nix | 2 +- ...etzner-dedicated-wipe-and-install-nixos.sh | 2 +- treefmt.toml | 25 ++++++++ 9 files changed, 123 insertions(+), 85 deletions(-) rename nixos/assets/{nginx-show-config.sh => nginx-show-config.py} (86%) create mode 100644 treefmt.toml diff --git a/nixos/assets/jack.sh b/nixos/assets/jack.sh index b9ca596..2f87842 100755 --- a/nixos/assets/jack.sh +++ b/nixos/assets/jack.sh @@ -1,8 +1,10 @@ +#!/usr/bin/env bash + set -e defaultDevice=PCH -start_jack(){ +start_jack() { internal_device_number=-1 komplete_device_number=$(aplay -l | grep Vestax | cut -d":" -f1 | cut -d" " -f2) @@ -39,30 +41,27 @@ start_jack(){ # # to find configuration options do # jack_control dp - if [[ $device_number -eq -1 ]] - then + if [[ $device_number -eq -1 ]]; then # we use alsa in reality, but pulse opens up all the pulse # sink and source stuff # jack_control ds pulse # not working for some reason jack_control ds alsa jack_control dps device hw:$defaultDevice else - jack_control ds alsa - jack_control dps device hw:$device_number # use usb card + jack_control ds alsa + jack_control dps device "hw:$device_number" # use usb card fi - jack_control dps duplex True # record and playback ports - jack_control dps hwmon False # no hardware monitoring - jack_control dps rate 48000 # use cd sample rate - + jack_control dps duplex True # record and playback ports + jack_control dps hwmon False # no hardware monitoring + jack_control dps rate 48000 # use cd sample rate # nperiods are the splitup of the # sound-ring-buffer. 2 are ok for internal cards # but for usb you should use 3 because # you can have to write in junks to the card # so there is one backup slice in the middle - if [[ $internal_device_number -ne -1 ]] - then + if [[ $internal_device_number -ne -1 ]]; then jack_control dps nperiods 3 fi @@ -82,7 +81,7 @@ start_jack(){ jack_control start } -stop_jack(){ +stop_jack() { jack_control exit } @@ -92,14 +91,18 @@ status_jack() { jack_control status } - case $1 in - start) start_jack - ;; - stop) stop_jack - ;; - restart) stop_jack ; start_jack - ;; - *) status_jack - ;; +start) + start_jack + ;; +stop) + stop_jack + ;; +restart) + stop_jack + start_jack + ;; +*) + status_jack + ;; esac diff --git a/nixos/assets/music-making.sh b/nixos/assets/music-making.sh index 1175001..ecb9c8b 100644 --- a/nixos/assets/music-making.sh +++ b/nixos/assets/music-making.sh @@ -1,50 +1,48 @@ +#!/usr/bin/env bash -function stop_program(){ - echo "stop $1" - sudo systemctl stop $1 +function stop_program() { + echo "stop $1" + sudo systemctl stop "$1" } -function start_program(){ - echo "start $1" - sudo systemctl stop $1 +function start_program() { + echo "start $1" + sudo systemctl stop "$1" } - -function start(){ - echo "starting programs again" - echo "-----------------------" - echo - start_program backup.on-porani.insecure.timer - start_program backup.on-workhorse.insecure.timer - start_program backup.on-workout.insecure.timer - start_program syncthing.service - start_program tlp.service - start_program tor.service +function start() { + echo "starting programs again" + echo "-----------------------" + echo + start_program backup.on-porani.insecure.timer + start_program backup.on-workhorse.insecure.timer + start_program backup.on-workout.insecure.timer + start_program syncthing.service + start_program tlp.service + start_program tor.service } -function stop(){ - echo "stopping programs" - echo "-----------------" - echo - stop_program backup.on-porani.insecure.timer - stop_program backup.on-workhorse.insecure.timer - stop_program backup.on-workout.insecure.timer - stop_program syncthing.service - stop_program tlp.service - stop_program tor.service +function stop() { + echo "stopping programs" + echo "-----------------" + echo + stop_program backup.on-porani.insecure.timer + stop_program backup.on-workhorse.insecure.timer + stop_program backup.on-workout.insecure.timer + stop_program syncthing.service + stop_program tlp.service + stop_program tor.service } - # ---- # main # ---- - stop echo echo -n "wait to start again -> " -read +read -r echo start diff --git a/nixos/assets/nginx-show-config.sh b/nixos/assets/nginx-show-config.py similarity index 86% rename from nixos/assets/nginx-show-config.sh rename to nixos/assets/nginx-show-config.py index 3ac4699..06cd646 100644 --- a/nixos/assets/nginx-show-config.sh +++ b/nixos/assets/nginx-show-config.py @@ -10,7 +10,7 @@ from tempfile import TemporaryDirectory def nginx_config() -> str: - reload_config ="/etc/nginx/nginx.conf" + reload_config = "/etc/nginx/nginx.conf" if os.path.exists(reload_config): return reload_config out = subprocess.check_output(["systemctl", "cat", "nginx"]) @@ -26,8 +26,9 @@ def main(): config_path = nginx_config() with TemporaryDirectory() as temp_dir: temp_path = os.path.join(temp_dir, "nginx.conf") - with open(temp_path, "wb+") as temp_file, \ - open(config_path, "rb") as config_file: + with open(temp_path, "wb+") as temp_file, open( + config_path, "rb" + ) as config_file: shutil.copyfileobj(config_file, temp_file) temp_file.flush() subprocess.check_call(["nginxfmt", temp_file.name]) diff --git a/nixos/pkgs/gitlog2json/gitlog2json.py b/nixos/pkgs/gitlog2json/gitlog2json.py index 3e67948..095e72a 100644 --- a/nixos/pkgs/gitlog2json/gitlog2json.py +++ b/nixos/pkgs/gitlog2json/gitlog2json.py @@ -19,29 +19,33 @@ class GitLogger: commits = (self.repo.commit(logEntry) for logEntry in self.repo.iter_commits()) return (self.to_dict(x) for x in commits) - def to_dict(self,commit): + def to_dict(self, commit): """create a dict out of a commit that is easy to json serialize""" time_difference = commit.authored_datetime - commit.committed_datetime return { - "author_email" : commit.author.email, - "author_name" : commit.author.name, - "authored_date" : commit.authored_datetime.isoformat(), - "files": [{"file":file,"changes":changes} for file,changes in commit.stats.files.items()], - "committed_date" : commit.committed_datetime.isoformat(), - "committer_email" : commit.committer.email, - "committer_name" : commit.committer.name, - "date_difference_in_seconds" : abs( time_difference.total_seconds() ), - "encoding" : commit.encoding, - "hash" : commit.hexsha, - "message" : commit.message , - "summary" : commit.summary, - "size" : commit.size, - "stats_total" : commit.stats.total, - "parents" : [parent.hexsha for parent in commit.parents], + "author_email": commit.author.email, + "author_name": commit.author.name, + "authored_date": commit.authored_datetime.isoformat(), + "files": [ + {"file": file, "changes": changes} + for file, changes in commit.stats.files.items() + ], + "committed_date": commit.committed_datetime.isoformat(), + "committer_email": commit.committer.email, + "committer_name": commit.committer.name, + "date_difference_in_seconds": abs(time_difference.total_seconds()), + "encoding": commit.encoding, + "hash": commit.hexsha, + "message": commit.message, + "summary": commit.summary, + "size": commit.size, + "stats_total": commit.stats.total, + "parents": [parent.hexsha for parent in commit.parents], } + @click.command() -@click.argument("path", type=click.Path(exists=True), envvar='PWD') +@click.argument("path", type=click.Path(exists=True), envvar="PWD") @click.option("--host") @click.option("--index") @click.option("--port", default=9200) @@ -52,24 +56,18 @@ def main(path, host, index, port): exit(1) print("Sending commits from %s to %s on index %s" % (path, host, index)) - es = Elasticsearch([ - {'host': host, 'port': port} - ]) + es = Elasticsearch([{"host": host, "port": port}]) for entry in GitLogger(path).log(): try: - es.index( - index=index, - doc_type="commit", - id=entry["hash"], - body=entry - ) + es.index(index=index, doc_type="commit", id=entry["hash"], body=entry) except Exception as e: print(json.dumps(entry)) print(e.info) exit(1) else: for entry in GitLogger(path).log(): - print(json.dumps( entry )) + print(json.dumps(entry)) -if __name__ == '__main__': + +if __name__ == "__main__": main() diff --git a/nixos/pkgs/gitlog2json/log.py b/nixos/pkgs/gitlog2json/log.py index 86419a3..8523999 100644 --- a/nixos/pkgs/gitlog2json/log.py +++ b/nixos/pkgs/gitlog2json/log.py @@ -1,5 +1,6 @@ from git import Repo import os + repo = Repo("/home/palo/dev/nixpkgs") a = repo.head.log()[0] current_hash = a.newhexsha @@ -7,7 +8,14 @@ commit = repo.commit(current_hash) commit.author commit.committed_date import json -json.dumps( { "commit_name" : commit.author.name, "message" : commit.message , "changes": commit.stats.files } ) + +json.dumps( + { + "commit_name": commit.author.name, + "message": commit.message, + "changes": commit.stats.files, + } +) commit.committed_date commit.committer.email commit.parents diff --git a/nixos/system/all/packages.nix b/nixos/system/all/packages.nix index dd8a429..0b109be 100644 --- a/nixos/system/all/packages.nix +++ b/nixos/system/all/packages.nix @@ -93,6 +93,11 @@ in memoryUsage + treefmt + shellcheck + shfmt + black + #gitlog2json pciutils diff --git a/nixos/system/server/packages.nix b/nixos/system/server/packages.nix index e10333b..877f428 100644 --- a/nixos/system/server/packages.nix +++ b/nixos/system/server/packages.nix @@ -8,7 +8,7 @@ { flakeIgnore = [ "E265" "E225" "W292" ]; } - (lib.fileContents ../../assets/nginx-show-config.sh); + (lib.fileContents ../../assets/nginx-show-config.py); in [ pkgs.mosh diff --git a/scripts/hetzner-dedicated-wipe-and-install-nixos.sh b/scripts/hetzner-dedicated-wipe-and-install-nixos.sh index b47c2c7..f5ecd6f 100644 --- a/scripts/hetzner-dedicated-wipe-and-install-nixos.sh +++ b/scripts/hetzner-dedicated-wipe-and-install-nixos.sh @@ -368,7 +368,7 @@ ssh-keygen -t ed25519 -N "" -f /mnt/etc/secrets/initrd/ssh_host_ed25519_key # Install NixOS #PATH="$PATH" NIX_PATH="$NIX_PATH" `which nixos-install` --no-root-passwd --root /mnt --max-jobs 40 -PATH="$PATH" `which nixos-install` --no-root-passwd --root /mnt --max-jobs 40 +PATH="$PATH" $( which nixos-install` --no-root-passwd --root /mnt --max-jobs 40 ) umount /mnt/boot-{1,2} umount /mnt diff --git a/treefmt.toml b/treefmt.toml new file mode 100644 index 0000000..409607d --- /dev/null +++ b/treefmt.toml @@ -0,0 +1,25 @@ +# One CLI to format the code tree - https://github.com/numtide/treefmt + +[formatter.nix] +command = "nixpkgs-fmt" +includes = [ "*.nix"] + +[formatter.shell] +command = "shfmt" +options = [ + "-i", + "2", # indent 2 + "-s", # simplify the code + "-w", # write back to the file +] +includes = ["*.sh"] +excludes = ["./scripts/hetzner-dedicated-wipe-and-install-nixos.sh"] + +[formatter.shellcheck] +command = "shellcheck" +includes = ["*.sh"] +excludes = ["./scripts/hetzner-dedicated-wipe-and-install-nixos.sh"] + +[formatter.python] +command = "black" +includes = ["*.py"] \ No newline at end of file