yazi: open from other apps

This commit is contained in:
2026-05-31 14:54:06 +03:00
parent 7ea803d7cd
commit 5b2fbe0db4
+120 -4
View File
@@ -1,16 +1,132 @@
{ pkgs, lib, inputs, ... }: { { pkgs, lib, inputs, ... }: let
hostSystem = pkgs.stdenv.hostPlatform.system;
yaziPkg = inputs.yazi.packages.${hostSystem}.default.override {
_7zz = pkgs._7zz-rar;
};
yaziOpen = pkgs.writeShellScriptBin "yazi-open" ''
set -efu
target="''${1:-$HOME}"
case "$target" in
file://*)
target="$(${lib.getExe pkgs.python3} -c 'import sys, urllib.parse; print(urllib.parse.unquote(urllib.parse.urlparse(sys.argv[1]).path))' "$target")"
;;
esac
if [ -f "$target" ]; then
target="$(dirname "$target")"
fi
if [ ! -e "$target" ]; then
target="$HOME"
fi
exec ${lib.getExe pkgs.ghostty} --title="Yazi" -e ${lib.getExe yaziPkg} "$target"
'';
fileManager1 = pkgs.writeShellScriptBin "yazi-filemanager1" ''
exec ${pkgs.python3.withPackages (ps: [ ps.dbus-next ])}/bin/python ${pkgs.writeText "yazi-filemanager1.py" ''
import asyncio
import os
import subprocess
import urllib.parse
from dbus_next.aio import MessageBus
from dbus_next.constants import BusType
from dbus_next.service import ServiceInterface, method
YAZI_OPEN = ${builtins.toJSON (lib.getExe yaziOpen)}
def uri_to_path(uri: str) -> str | None:
parsed = urllib.parse.urlparse(uri)
if parsed.scheme != "file":
return None
return urllib.parse.unquote(parsed.path)
def open_paths(uris: list[str], reveal_items: bool = False) -> None:
for uri in uris:
path = uri_to_path(uri)
if not path:
continue
if reveal_items and os.path.isfile(path):
path = os.path.dirname(path)
subprocess.Popen(
[YAZI_OPEN, path],
start_new_session=True,
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
)
class FileManager1(ServiceInterface):
def __init__(self) -> None:
super().__init__("org.freedesktop.FileManager1")
@method()
def ShowFolders(self, uris: "as", startup_id: "s") -> "":
open_paths(uris)
@method()
def ShowItems(self, uris: "as", startup_id: "s") -> "":
open_paths(uris, reveal_items=True)
@method()
def ShowItemProperties(self, uris: "as", startup_id: "s") -> "":
open_paths(uris, reveal_items=True)
async def main() -> None:
bus = await MessageBus(bus_type=BusType.SESSION).connect()
bus.export("/org/freedesktop/FileManager1", FileManager1())
await bus.request_name("org.freedesktop.FileManager1")
await asyncio.Event().wait()
asyncio.run(main())
''}
'';
in {
home.packages = with pkgs; [ home.packages = with pkgs; [
( ouch.override { enableUnfree = true; } ) ( ouch.override { enableUnfree = true; } )
yaziOpen
]; ];
xdg.desktopEntries.yazi = {
name = "Yazi";
genericName = "File Manager";
exec = "${lib.getExe yaziOpen} %U";
terminal = false;
mimeType = [ "inode/directory" ];
categories = [ "System" "FileTools" "FileManager" ];
startupNotify = false;
};
xdg.mimeApps = {
enable = true;
defaultApplications = {
"inode/directory" = "yazi.desktop";
"application/x-gnome-saved-search" = "yazi.desktop";
};
};
xdg.dataFile."dbus-1/services/org.freedesktop.FileManager1.service".text = ''
[D-BUS Service]
Name=org.freedesktop.FileManager1
Exec=${lib.getExe fileManager1}
'';
wayland.windowManager.hyprland.settings.windowrule = [ wayland.windowManager.hyprland.settings.windowrule = [
"match:class dragon-drop, move cursor_x-window_w/2 cursor_y-window_h/2" "match:class dragon-drop, move cursor_x-window_w/2 cursor_y-window_h/2"
]; ];
programs.yazi = { programs.yazi = {
package = inputs.yazi.packages package = yaziPkg;
.${pkgs.stdenv.hostPlatform.system}.default
.override { _7zz = pkgs._7zz-rar; };
enable = true; enable = true;
enableZshIntegration = true; enableZshIntegration = true;
shellWrapperName = "y"; shellWrapperName = "y";