{
  pkgs,
  lib,
  config,
  ...
}:
let
  inherit (pkgs.writers) writeFishBin writeBashBin;
  inherit (lib.my) getExe;
  runner = config.modules.system.roles.desktop.runner;
  nixos-rebuild =
    name: word:
    writeFishBin name ''
      env --chdir ~/.nixconf ${pkgs.nh}/bin/nh os ${word} . $argv \
        && ${lib.my.getExe' pkgs.libnotify "notify-send"} nixos-rebuild "Rebuild complete" \
            -a nixos-rebuild \
            -i ${pkgs.nixos-icons}/share/icons/hicolor/scalable/apps/nix-snowflake.svg
    '';
in
{
  screenshot =
    with pkgs;
    writeShellApplication {
      name = "screenshot";
      runtimeInputs = [
        slurp
        wl-clipboard
        libnotify
      ];
      text = ''
        location="$HOME/Pictures/Screenshots/$(date +%Y-%m-%d-%H%M%S)-screenshot.png";

        if zone=$(slurp); then
        grim -t png -g "$zone" - | wl-copy --type image/png && wl-paste > "$location" \
        && notify-send --app-name Screenshot -i "$location" --urgency=low "Screenshot copied to clipboard" "Screenshot created at $location";
        fi
      '';
    };

  set-background = writeFishBin "set-background" ''
    argparse 'f/file=!test -e "$_flag_value"' -- $argv; or return

    set pids $(pidof swaybg)

    if set -q _flag_file
      ${pkgs.swaybg}/bin/swaybg -i "$(find $_flag_file | shuf -n 1)" > /dev/null 2>&1 &
    else
      ${pkgs.swaybg}/bin/swaybg -i "$(find ~/Pictures/backgrounds | shuf -n 1)" > /dev/null 2>&1 &
    end

    sleep 0.5;

    for i in $(string split " " $pids)
      echo "killing process $i";
      kill -9 "$i";
    end
  '';

  nixedit = writeFishBin "nixedit" "env --chdir ~/.nixconf $EDITOR .";

  getext = pkgs.writeScriptBin "getext" "ls | grep -E \"\.[a-zA-Z0-9]+$\" --only-matching  | sort | uniq";

  rpaste = writeFishBin "rpaste" ''
    ${getExe pkgs.rustypaste-cli} -a "$(cat ${config.age.secrets.rustypaste.path})" -s "https://rusty.ccnlc.eu/" $argv
  '';

  gentest = nixos-rebuild "gentest" "test";

  genswitch = nixos-rebuild "genswitch" "switch";

  fishl = writeFishBin "fishl" ./logo.fish;

  nrun = writeFishBin "nrun" ''
    if echo "$argv[1]" | grep -Eq '^[a-z]+:.+/.+$'
        nix run $argv[1] -- $argv[2..]
    else
        nix run nixpkgs#$argv[1] -- $argv[2..]
    end
  '';

  nruni = writeFishBin "nruni" ''
    export NIXPKGS_ALLOW_UNFREE=1;
    export NIXPKGS_ALLOW_INSECURE=1

    if echo "$argv[1]" | grep -Eq '^[a-z]+:.+/.+$'
        nix run --impure $argv[1] -- $argv[2..]
    else
        nix run --impure nixpkgs#$argv[1] -- $argv[2..]
    end
  '';

  editsym = writeFishBin "editsym" ''
    for file in $argv
        cp "$file" "$file.tmp"
        unlink "$file"
        mv "$file.tmp" "$file"
        chmod -R 777 "$file"
    end
  '';

  deploytest = writeFishBin "deploytest" ''
    nixos-rebuild test \
        --flake ~/.nixconf#"$argv[1]" \
        --target-host "$argv[1]" \
        --use-remote-sudo  &| ${getExe pkgs.nix-output-monitor}
  '';

  deployswitch = writeFishBin "deployswitch" ''
    nixos-rebuild switch \
        --flake ~/.nixconf#"$argv[1]" \
        --target-host "$argv[1]" \
        --use-remote-sudo  &| ${getExe pkgs.nix-output-monitor}
  '';

  powerMenu = writeBashBin "power-menu" ''
    case "$(echo -e "Shutdown\nRestart\nHibernate\nLogout\nSuspend\nLock" | ${runner.dmenu} )" in
        Shutdown) exec systemctl poweroff ;;
        Restart) exec systemctl reboot ;;
        Logout) exec loginctl terminate-session "$${XDG_SESSION_ID-}" ;;
        Hibernate) exec systemctl hibernate ;;
        Suspend) exec systemctl suspend ;;
        Lock) exec loginctl lock-session "$${XDG_SESSION_ID-}" ;;
    esac
  '';

}
