diff options
author | Benedikt Peetz <benedikt.peetz@b-peetz.de> | 2025-03-29 12:19:17 +0100 |
---|---|---|
committer | Benedikt Peetz <benedikt.peetz@b-peetz.de> | 2025-03-29 12:20:52 +0100 |
commit | 663ada07b652d8be40f4f0496640adb796d8930f (patch) | |
tree | c926f1b769a64d5ecb9dd675cd196d4ebbfda262 /modules/by-name | |
parent | refactor(pkgs/update-vim-plugins): Remove (diff) | |
download | nixos-config-663ada07b652d8be40f4f0496640adb796d8930f.zip |
fix(modules/xdg): Migrate to the merged xdg-desktop-terminal-filechooser
Diffstat (limited to '')
-rwxr-xr-x | modules/by-name/xd/xdg/lf-wrapper.sh | 39 | ||||
-rw-r--r-- | modules/by-name/xd/xdg/module.nix | 52 | ||||
-rwxr-xr-x | modules/by-name/xd/xdg/scripts/lf_wrapper.sh | 79 | ||||
-rwxr-xr-x | modules/by-name/xd/xdg/scripts/ranger_wrapper.sh | 68 |
4 files changed, 66 insertions, 172 deletions
diff --git a/modules/by-name/xd/xdg/lf-wrapper.sh b/modules/by-name/xd/xdg/lf-wrapper.sh new file mode 100755 index 00000000..f85f7bac --- /dev/null +++ b/modules/by-name/xd/xdg/lf-wrapper.sh @@ -0,0 +1,39 @@ +#!/usr/bin/env sh +# This wrapper script is invoked by xdg-desktop-portal-termfilechooser. +# +# For more information about input/output arguments read `xdg-desktop-portal-termfilechooser(5)` + +set -ex + +multiple="$1" +directory="$2" +save="$3" +path="$4" +out="$5" + +# echo > /tmp/stdout +# echo > /tmp/stderr +# +# exec 1>> /tmp/stdout +# exec 2>> /tmp/stderr + +if [ "$save" = "1" ]; then + # save a file + set -- -selection-path="$out" -command='set promptfmt "Select the file to write to %S \033[32;1m%u@%h\033[0m:\033[34;1m%d\033[0m\033[1m%f\033[0m"' "$path" +elif [ "$directory" = "1" ]; then + # upload files from a directory + set -- -last-dir-path="$out" -command='set dironly' -command='set promptfmt "Select directory (quit in dir to select it) %S \033[32;1m%u@%h\033[0m:\033[34;1m%d\033[0m\033[1m%f\033[0m"' "$path" +elif [ "$multiple" = "1" ]; then + # upload multiple files + set -- -selection-path="$out" -command='set promptfmt "Select file(s) (open file to select it; <Space> to select multiple) %S \033[32;1m%u@%h\033[0m:\033[34;1m%d\033[0m\033[1m%f\033[0m"' "$path" +else + # upload only 1 file + set -- -selection-path="$out" -command='set promptfmt "Select file (open file to select it) %S \033[32;1m%u@%h\033[0m:\033[34;1m%d\033[0m\033[1m%f\033[0m"' "$path" +fi + +alacritty --title 'floating please' -e lf "$@" + +# Delete the left recommended file, if we did not actually save anything. +if [ "$save" = "1" ] && ! [ -s "$out" ]; then + rm "$path" +fi diff --git a/modules/by-name/xd/xdg/module.nix b/modules/by-name/xd/xdg/module.nix index 5140a832..1202cd96 100644 --- a/modules/by-name/xd/xdg/module.nix +++ b/modules/by-name/xd/xdg/module.nix @@ -1,36 +1,28 @@ { pkgs, - nixpkgs_open_prs, - sysLib, - system, + lib, ... }: let - pkgs_tfc = nixpkgs_open_prs.nixpkgs-tfc.legacyPackages."${system}"; + cmd = pkgs.writeShellApplication { + name = "lf_wrapper"; + + runtimeInputs = [pkgs.lf pkgs.alacritty]; + inheritPath = true; + + text = builtins.readFile ./lf-wrapper.sh; + }; + + tfcConfigFile = (pkgs.formats.ini {}).generate "xdg-desktop-portal-termfilechooser.ini" { + filechooser = { + default_dir = "/tmp"; + cmd = "${lib.getExe cmd}"; + }; + }; in { services.dbus.enable = true; xdg = { portal = { enable = true; - termfilechooser = { - enable = true; - logLevel = "TRACE"; - package = pkgs_tfc.xdg-desktop-portal-termfilechooser; - settings = { - filechooser = { - default_dir = "/tmp"; - cmd = "${sysLib.writeShellScript { - src = ./scripts/lf_wrapper.sh; - name = "lf_wrapper"; - keepPath = true; - dependencies = with pkgs; [ - lf - alacritty - bash - ]; - }}/bin/lf_wrapper"; - }; - }; - }; wlr = { enable = true; }; @@ -47,12 +39,22 @@ in { "org.freedesktop.impl.portal.FileChooser" = ["termfilechooser"]; }; }; + extraPortals = [ pkgs.xdg-desktop-portal-gtk pkgs.xdg-desktop-portal-wlr - pkgs_tfc.xdg-desktop-portal-termfilechooser + pkgs.xdg-desktop-portal-termfilechooser ]; }; }; + + environment.etc."xdg/xdg-desktop-portal-termfilechooser/config".source = tfcConfigFile; + + systemd.user.services.xdg-desktop-portal-termfilechooser = { + serviceConfig.ExecStart = [ + "" + "${pkgs.xdg-desktop-portal-termfilechooser}/libexec/xdg-desktop-portal-termfilechooser --loglevel=TRACE" + ]; + }; # TODO: mime = {}; } diff --git a/modules/by-name/xd/xdg/scripts/lf_wrapper.sh b/modules/by-name/xd/xdg/scripts/lf_wrapper.sh deleted file mode 100755 index 16603fe4..00000000 --- a/modules/by-name/xd/xdg/scripts/lf_wrapper.sh +++ /dev/null @@ -1,79 +0,0 @@ -#!/usr/bin/env dash - -# shellcheck source=/dev/null -SHELL_LIBRARY_VERSION="2.1.2" . %SHELL_LIBRARY_PATH - -# This wrapper script is invoked by xdg-desktop-portal-termfilechooser. -# -# Inputs: -# 1. "1" if multiple files can be chosen, "0" otherwise. -# 2. "1" if a directory should be chosen, "0" otherwise. -# 3. "0" if opening files was requested, "1" if writing to a file was -# requested. For example, when uploading files in Firefox, this will be "0". -# When saving a web page in Firefox, this will be "1". -# 4. If writing to a file, this is recommended path provided by the caller. For -# example, when saving a web page in Firefox, this will be the recommended -# path Firefox provided, such as "~/Downloads/webpage_title.html". -# Note that if the path already exists, we keep appending "_" to it until we -# get a path that does not exist. -# 5. The output path, to which results should be written. -# -# Output: -# The script should print the selected paths to the output path (argument #5), -# one path per line. -# If nothing is printed, then the operation is assumed to have been canceled. - -multiple="$1" -directory="$2" -save="$3" -recommended_path="$4" -out="$5" - -# echo > /tmp/stdout -# echo > /tmp/stderr -# -# exec 1>> /tmp/stdout -# exec 2>> /tmp/stderr - -cmd="$(command -v lf)" -termcmd="${TERMINAL:-$(command -v alacritty)}" - -if [ "$save" = "1" ]; then - set -- -selection-path="$out" -command='set promptfmt "Select the file to write to %S \033[32;1m%u@%h\033[0m:\033[34;1m%d\033[0m\033[1m%f\033[0m"' "$recommended_path" - cat <<EOF >"$recommended_path" -xdg-desktop-portal-termfilechooser saving files tutorial - -!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! -!!! === WARNING! === !!! -!!! The contents of *whatever* file you open last in !!! -!!! lf will be *overwritten*! !!! -!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! - -Instructions: -1) Move this file wherever you want. -2) Rename the file if needed. -3) Confirm your selection by opening the file, for - example by pressing <Enter>. - -Notes: -1) This file is provided for your convenience. You - could delete it and choose another file to overwrite - that, for example. -2) If you quit lf without opening a file, this file - will be removed and the save operation aborted. -EOF - -elif [ "$directory" = "1" ]; then - set -- -selection-path="$out" -command='set dironly' -command='set promptfmt "Select directory (quit in dir to select it) %S \033[32;1m%u@%h\033[0m:\033[34;1m%d\033[0m\033[1m%f\033[0m"' -elif [ "$multiple" = "1" ]; then - set -- -selection-path="$out" -command='set promptfmt "Select file(s) (open file to select it; <Space> to select multiple) %S \033[32;1m%u@%h\033[0m:\033[34;1m%d\033[0m\033[1m%f\033[0m"' -else - set -- -selection-path="$out" -command='set promptfmt "Select file (open file to select it) %S \033[32;1m%u@%h\033[0m:\033[34;1m%d\033[0m\033[1m%f\033[0m"' -fi - -"$termcmd" --title 'floating please' -e "$cmd" "$@" - -if [ "$save" = "1" ] && [ ! -s "$out" ]; then - rm "$recommended_path" -fi -# vim: ft=sh diff --git a/modules/by-name/xd/xdg/scripts/ranger_wrapper.sh b/modules/by-name/xd/xdg/scripts/ranger_wrapper.sh deleted file mode 100755 index e148bf19..00000000 --- a/modules/by-name/xd/xdg/scripts/ranger_wrapper.sh +++ /dev/null @@ -1,68 +0,0 @@ -#!/usr/bin/env dash - -# shellcheck source=/dev/null -SHELL_LIBRARY_VERSION="2.1.2" . %SHELL_LIBRARY_PATH -# This wrapper script is invoked by xdg-desktop-portal-termfilechooser. -# -# Inputs: -# 1. "1" if multiple files can be chosen, "0" otherwise. -# 2. "1" if a directory should be chosen, "0" otherwise. -# 3. "0" if opening files was requested, "1" if writing to a file was -# requested. For example, when uploading files in Firefox, this will be "0". -# When saving a web page in Firefox, this will be "1". -# 4. If writing to a file, this is recommended path provided by the caller. For -# example, when saving a web page in Firefox, this will be the recommended -# path Firefox provided, such as "~/Downloads/webpage_title.html". -# Note that if the path already exists, we keep appending "_" to it until we -# get a path that does not exist. -# 5. The output path, to which results should be written. -# -# Output: -# The script should print the selected paths to the output path (argument #5), -# one path per line. -# If nothing is printed, then the operation is assumed to have been canceled. - -multiple="$1" -directory="$2" -save="$3" -path="$4" -out="$5" - -cmd="$(command -v ranger)" -termcmd="${TERMCMD:-$(command -v kitty)}" - -if [ "$save" = "1" ]; then - set -- --choosefile="$out" --cmd='echo Select save path (see tutorial in preview pane; try pressing zv or zp if no preview)' "$path" - printf '%s' 'xdg-desktop-portal-termfilechooser saving files tutorial - -!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! -!!! === WARNING! === !!! -!!! The contents of *whatever* file you open last in !!! -!!! ranger will be *overwritten*! !!! -!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! - -Instructions: -1) Move this file wherever you want. -2) Rename the file if needed. -3) Confirm your selection by opening the file, for - example by pressing <Enter>. - -Notes: -1) This file is provided for your convenience. You - could delete it and choose another file to overwrite - that, for example. -2) If you quit ranger without opening a file, this file - will be removed and the save operation aborted. -' >"$path" -elif [ "$directory" = "1" ]; then - set -- --choosedir="$out" --show-only-dirs --cmd="echo Select directory (quit in dir to select it)" -elif [ "$multiple" = "1" ]; then - set -- --choosefiles="$out" --cmd="echo Select file(s) (open file to select it; <Space> to select multiple)" -else - set -- --choosefile="$out" --cmd="echo Select file (open file to select it)" -fi - -"$termcmd" -- "$cmd" "$@" -if [ "$save" = "1" ] && [ ! -s "$out" ]; then - rm "$path" -fi |