{ config, lib, pkgs, repo, }: let nixosConfigDir = repo.account.nixosConfigurationPath; launcherDmenuCommand = lib.concatMapStringsSep " " lib.escapeShellArg config.meta.desktop.launcher.commands.dmenu; mkTerminalScript = { name, title, appId ? "niri-shortcut-terminal", workdir ? nixosConfigDir, command ? null, runtimeInputs ? [ ], }: let args = ( if config.meta.desktop.terminal.desktopEntryName == "kitty" then [ "--class" appId "--title" title "--directory" workdir ] else if config.meta.desktop.terminal.desktopEntryName == "foot" then [ "--app-id" appId "--title" title "--working-directory" workdir ] else [ ] ) ++ lib.optionals (command != null) [ "--" "${pkgs.bash}/bin/bash" "-lc" command ]; in pkgs.writeShellApplication { inherit name runtimeInputs; checkPhase = ""; text = '' # shellcheck disable=SC2016 cd ${lib.escapeShellArg workdir} exec ${lib.escapeShellArg config.meta.desktop.terminal.command} ${ lib.concatMapStringsSep " " lib.escapeShellArg args } ''; }; in rec { scripts = { nixosTerminal = mkTerminalScript { name = "niri-shortcut-nixos-terminal"; title = "NixOS Config"; }; nixosSwitch = mkTerminalScript { name = "niri-shortcut-nixos-switch"; title = "NixOS Switch"; appId = "niri-shortcut-float"; runtimeInputs = [ pkgs.coreutils pkgs.nh ]; command = '' set -o pipefail log_dir="''${XDG_STATE_HOME:-$HOME/.local/state}/nixos-switch" mkdir -p "$log_dir" log="$log_dir/$(date +%Y%m%d-%H%M%S).log" status_file="$(mktemp)" printf 'Running nh os switch in %s\n\n' ${lib.escapeShellArg nixosConfigDir} ( cd ${lib.escapeShellArg nixosConfigDir} nh os switch printf '%s' "$?" > "$status_file" ) 2>&1 | tee "$log" status="$(cat "$status_file")" rm -f "$status_file" printf '\nLog: %s\n' "$log" if [ "$status" -eq 0 ]; then printf 'NixOS switch completed successfully.\n' else printf 'NixOS switch failed with exit code %s.\n' "$status" fi printf 'Press Enter to close...' read -r _ exit "$status" ''; }; editSecrets = mkTerminalScript { name = "niri-shortcut-edit-secrets"; title = "Edit Secrets"; appId = "niri-shortcut-float"; runtimeInputs = [ pkgs.sops ]; command = '' sops edit ${lib.escapeShellArg "${nixosConfigDir}/modules/secrets/secrets.yaml"} ''; }; neovimProjects = mkTerminalScript { name = "niri-shortcut-neovim-projects"; title = "Neovim Projects"; command = '' nvim -c 'Telescope projects' ''; }; clipboardHistory = pkgs.writeShellApplication { name = "niri-shortcut-clipboard-history"; runtimeInputs = [ pkgs.cliphist config.meta.desktop.launcher.package pkgs.wl-clipboard ]; text = '' selection="$(cliphist list | ${launcherDmenuCommand})" if [ -z "$selection" ]; then exit 0 fi printf '%s' "$selection" | cliphist decode | wl-copy ''; }; pickColor = pkgs.writeShellApplication { name = "niri-shortcut-pick-color"; runtimeInputs = [ pkgs.libnotify pkgs.niri pkgs.wl-clipboard ]; text = '' color="$(niri msg pick-color)" printf '%s' "$color" | wl-copy notify-send 'Color picked' "$color" ''; }; swapMonitorWorkspaces = pkgs.writeShellApplication { name = "niri-shortcut-swap-monitor-workspaces"; runtimeInputs = [ pkgs.jq pkgs.niri ]; text = '' focused_output="$(niri msg --json focused-output | jq -r '.name // empty')" if [ -z "$focused_output" ]; then exit 0 fi niri msg action focus-monitor-next target_output="$(niri msg --json focused-output | jq -r '.name // empty')" if [ -z "$target_output" ] || [ "$target_output" = "$focused_output" ]; then exit 0 fi target_workspace_idx="$( niri msg --json workspaces \ | jq -r --arg output "$target_output" ' first(.[] | select(.output == $output and .is_active) | .idx) // empty ' )" if [ -z "$target_workspace_idx" ]; then niri msg action focus-monitor "$focused_output" exit 0 fi niri msg action focus-monitor "$focused_output" niri msg action move-workspace-to-monitor "$target_output" niri msg action focus-workspace "$target_workspace_idx" niri msg action move-workspace-to-monitor "$focused_output" ''; }; }; commands = lib.mapAttrs (_: lib.getExe) scripts; }