refactor: niri config

This commit is contained in:
2026-04-29 15:49:32 +02:00
parent efb0179344
commit 5bc37c7009
3 changed files with 376 additions and 307 deletions
+225
View File
@@ -0,0 +1,225 @@
{
config,
lib,
pkgs,
repo,
}:
let
nixosConfigDir = repo.account.nixosConfigurationPath;
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'
'';
};
vicinaeCommand = pkgs.writeShellApplication {
name = "niri-shortcut-vicinae-command";
runtimeInputs = [ config.programs.vicinae.package ];
text = ''
case "''${1:-}" in
files)
link="vicinae://extensions/sameoldlab/fuzzy-files/find"
;;
nix-options)
link="vicinae://extensions/knoopx/nix/options"
;;
home-manager-options)
link="vicinae://extensions/knoopx/nix/home-manager-options"
;;
nix-packages)
link="vicinae://extensions/knoopx/nix/packages"
;;
niri-windows)
link="vicinae://extensions/knoopx/niri/windows"
;;
*)
printf 'unknown Vicinae command target: %s\n' "''${1:-}" >&2
exit 64
;;
esac
exec vicinae deeplink "$link"
'';
};
clipboardHistory = pkgs.writeShellApplication {
name = "niri-shortcut-clipboard-history";
runtimeInputs = [
pkgs.cliphist
config.programs.vicinae.package
pkgs.wl-clipboard
];
text = ''
selection="$(cliphist list | vicinae dmenu --navigation-title Clipboard --placeholder 'Search clipboard' --no-metadata)"
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;
}