From 6b0bb6a806859337b338d9a63a9659e375870362 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kier=C3=A1n=20Meinhardt?= Date: Tue, 27 Sep 2022 22:07:03 +0200 Subject: [PATCH] feat(cloud): add script for browsing mega --- .bin/toposort.nix | 50 +++++++++++++++++++++++++++++++++++++++++++++++ configs/cloud.nix | 32 ++++++++++++++++++++++++++---- 2 files changed, 78 insertions(+), 4 deletions(-) create mode 100644 .bin/toposort.nix diff --git a/.bin/toposort.nix b/.bin/toposort.nix new file mode 100644 index 0000000..c8bbf43 --- /dev/null +++ b/.bin/toposort.nix @@ -0,0 +1,50 @@ +let + lib = import ; +in rec { + inherit lib; + + input = [ + { + x = ["pool" "zfs"]; + y = ["mdadm" "raid1"]; + } + { + x = ["pool" "zfs"]; + y = ["disk" "sda"]; + } + { + x = ["mdadm" "raid1"]; + y = ["disk" "sdb"]; + } + { + x = ["mdadm" "raid1"]; + y = ["disk" "sdc"]; + } + ]; + + outNodes = node: graph: + lib.unique + (builtins.map (e: e.y) + (builtins.filter (v: v.x == node) graph)); + + vertices = graph: + lib.unique + (builtins.map (x: x.y) graph ++ builtins.map (x: x.x) graph); + + deleteVertex = node: graph: (builtins.filter (v: v.x != node && v.y != node) graph); + + findSink = graph: + lib.findFirst + (v: outNodes v graph == []) + (lib.trace graph (builtins.abort "No sink found")) + (vertices graph); + + topSort = graph: + if graph == [] + then [] + else if builtins.length graph == 1 + then let only = builtins.head graph; in [only.y only.x] + else let sink = findSink graph; in [sink] ++ topSort (deleteVertex sink graph); + + output = topSort input; +} diff --git a/configs/cloud.nix b/configs/cloud.nix index dfd7dfc..7037298 100644 --- a/configs/cloud.nix +++ b/configs/cloud.nix @@ -57,11 +57,11 @@ in { user = "kieran"; password = lib.fileContents ; endpoint = "https://cloud.xn--kiern-0qa.de"; + target = "${config.users.users.me.home}/notes"; }; in '' - KIERAN_TARGET=${config.users.users.me.home}/notes - mkdir -p "$KIERAN_TARGET" - ${pkgs.nextcloud-client}/bin/nextcloudcmd --user ${kieran.user} --password ${lib.escapeShellArg kieran.password} --path /Notes "$KIERAN_TARGET" ${kieran.endpoint} + mkdir -p ${lib.escapeShellArg kieran.target} + ${pkgs.nextcloud-client}/bin/nextcloudcmd --user ${kieran.user} --password ${lib.escapeShellArg kieran.password} --path /Notes ${lib.escapeShellArg kieran.target} ${kieran.endpoint} ''; serviceConfig = { Type = "oneshot"; @@ -76,8 +76,32 @@ in { ${pkgs.findutils}/bin/find ${config.users.users.me.home}/cloud/syncthing/library -type f ${pkgs.findutils}/bin/find ${config.users.users.me.home}/cloud/Seafile/Books -type f } | ${pkgs.fzf}/bin/fzf)" - ${pkgs.zathura}/bin/zathura "$book" + exec ${pkgs.zathura}/bin/zathura "$book" '') + (let + kieran = { + user = "kieran.meinhardt@gmail.com"; + password = lib.fileContents ; + }; + megatools = command: "${pkgs.megatools}/bin/megatools ${command} --username ${lib.escapeShellArg kieran.user} --password ${lib.escapeShellArg kieran.password}"; + in + pkgs.writers.writeDashBin "book-mega" '' + set -efu + selection="$(${megatools "ls"} | ${pkgs.fzf}/bin/fzf)" + test -n "$selection" || exit 1 + + tmpdir="$(mktemp -d)" + trap clean EXIT + clean() { + rm -rf "$tmpdir" + } + + ( + cd "$tmpdir" + ${megatools "get"} "$selection" + exec ${pkgs.zathura}/bin/zathura "$(basename "$selection")" + ) + '') ]; fileSystems."/media/moodle" = {