nautilus-scripts: Factor out from configuration

This commit is contained in:
Andrew Kvalheim 2023-06-19 19:39:11 -07:00
parent 940d65b956
commit f91c83df36
2 changed files with 56 additions and 29 deletions

View file

@ -3,22 +3,13 @@
let
inherit (lib.generators) toKeyValue;
mkNautilusScript = content: {
executable = true;
text = with pkgs; ''
#!${bash}/bin/bash
set -Eeuxo pipefail
lines="$(echo -n "$NAUTILUS_SCRIPT_SELECTED_FILE_PATHS" | sed /^$/d)"
paths0="$(echo -n "$lines" | tr '\n' '\0')"
readarray -t paths < <(echo -n "$lines")
${content}
'';
};
toTOML = (pkgs.formats.toml { }).generate;
in
{
imports = [ ../../packages/organize-downloads.nix ];
imports = [
../../packages/nautilus-scripts.nix
../../packages/organize-downloads.nix
];
config = {
# Associations
@ -127,25 +118,19 @@ in
];
# Nautilus scripts
xdg.dataFile."nautilus/scripts/HEIF,PNG,TIFF JPEG" = with pkgs; mkNautilusScript ''
echo -n "$paths0" | xargs -0 -n 1 -P 8 nice ${mozjpeg-simple}/bin/mozjpeg
'';
xdg.dataFile."nautilus/scripts/JPEG: Strip geolocation" = with pkgs; mkNautilusScript ''
echo -n "$paths0" | xargs -0 nice ${exiftool}/bin/exiftool -overwrite_original -gps:all= -xmp:geotag=
'';
xdg.dataFile."nautilus/scripts/PNG: Optimize" = with pkgs; mkNautilusScript ''
echo -n "$paths0" | xargs -0 nice ${efficient-compression-tool}/bin/ect -8 -keep -quiet --mt-file \
nautilusScripts = with pkgs; {
"HEIF,PNG,TIFF JPEG".xargs = "-n 1 -P 8 nice ${mozjpeg-simple}/bin/mozjpeg";
"JPEG: Strip geolocation".xargs = "nice ${exiftool}/bin/exiftool -overwrite_original -gps:all= -xmp:geotag=";
"PNG: Optimize".xargs = ''
nice ${efficient-compression-tool}/bin/ect -8 -keep -quiet --mt-file \
2> >(${gnome.zenity}/bin/zenity --width 600 --progress --pulsate --auto-close --auto-kill)
'';
xdg.dataFile."nautilus/scripts/PNG: Quantize" = with pkgs; mkNautilusScript ''
for path in "''${paths[@]}"; do
'';
"PNG: Quantize".each = ''
${pngquant-interactive}/bin/pngquant-interactive --suffix '.qnt' "$path"
nice ${efficient-compression-tool}/bin/ect -8 -keep -quiet "''${path%.png}.qnt.png"
done
'';
xdg.dataFile."nautilus/scripts/PNG: Trim" = with pkgs; mkNautilusScript ''
echo -n "$paths0" | xargs -0 -n 1 -P 8 nice ${imagemagick}/bin/mogrify -trim
'';
'';
"PNG: Trim".xargs = "-n 1 -P 8 nice ${imagemagick}/bin/mogrify -trim";
};
# Configuration
home.sessionVariables.ANSIBLE_NOCOWS = "";

View file

@ -0,0 +1,42 @@
{ config, lib, pkgs, ... }:
let
inherit (lib) concatMapAttrs mkOption;
inherit (lib.types) attrsOf nullOr str submodule;
mkNautilusScript = content: {
executable = true;
text = ''
#!${pkgs.bash}/bin/bash
set -Eeuxo pipefail
paths_lines="$(echo -n "$NAUTILUS_SCRIPT_SELECTED_FILE_PATHS" | sed /^$/d)"
readarray -t paths < <(echo -n "$paths_lines")
${content}
'';
};
in
{
options.nautilusScripts = mkOption {
default = { };
type = attrsOf (submodule {
options = {
each = mkOption { default = null; type = nullOr str; };
xargs = mkOption { default = null; type = nullOr str; };
};
});
};
config = {
xdg.dataFile = concatMapAttrs
(name: { each ? null, xargs ? null }: {
"nautilus/scripts/${name}" = mkNautilusScript (if each != null then ''
for path in "''${paths[@]}"; do
${each}
done
'' else ''
echo -n "$paths_lines" | tr '\n' '\0' | xargs -0 ${xargs}
'');
})
config.nautilusScripts;
};
}