1
1
Fork 1
mirror of https://github.com/oddlama/nix-config.git synced 2025-10-10 23:00:39 +02:00

refactor(screenshots): use separate scripts package

This commit is contained in:
oddlama 2023-10-16 00:46:30 +02:00
parent 3e0e03fc31
commit 6e8aae7d8d
No known key found for this signature in database
GPG key ID: 14EFE510775FE39A
15 changed files with 216 additions and 109 deletions

36
flake.lock generated
View file

@ -664,11 +664,11 @@
]
},
"locked": {
"lastModified": 1697323135,
"narHash": "sha256-tlAv11c0NIRTk2IzpFxYknHrveeFXojVyCTAMg749Zg=",
"lastModified": 1697371398,
"narHash": "sha256-Tn5feZ5SoYHQM9BTjw5e06DuNu8wc21gC9+bq/kXA8Y=",
"owner": "nix-community",
"repo": "home-manager",
"rev": "d4a5076ea8c2c063c45e0165f9f75f69ef583e20",
"rev": "3b67ae3f665379c06999641f99d94dba75b53876",
"type": "github"
},
"original": {
@ -698,11 +698,11 @@
"nixpkgs-lib": "nixpkgs-lib"
},
"locked": {
"lastModified": 1696766909,
"narHash": "sha256-lU1BmCWpQ9cx64YnJKc89lMg9cx4pCokXIbh5J//2t0=",
"lastModified": 1697371715,
"narHash": "sha256-1ZV4qoL1B35DsciS/inC+z6vAwjxjWHL3+4G4QQhT0A=",
"owner": "nix-community",
"repo": "lib-aggregate",
"rev": "9f495e4feea66426589cbb59ac8b972993b5d872",
"rev": "af42578368ca0c97d5836ba55b146745911aaecc",
"type": "github"
},
"original": {
@ -773,11 +773,11 @@
]
},
"locked": {
"lastModified": 1696736548,
"narHash": "sha256-Dg0gJ9xVXud55sAbXspMapFYZOpVAldQQo7MFp91Vb0=",
"lastModified": 1697340827,
"narHash": "sha256-XlrR68N7jyaZ0bs8TPrhqcWG0IPG3pbjrKzJMpYOsos=",
"owner": "Mic92",
"repo": "nix-index-database",
"rev": "2902dc66f64f733bfb45754e984e958e9fe7faf9",
"rev": "29977d0796c058bbcfb2df5b18eb5badf1711007",
"type": "github"
},
"original": {
@ -877,11 +877,11 @@
},
"nixpkgs-lib": {
"locked": {
"lastModified": 1696726172,
"narHash": "sha256-89yxFXzTA7JRyWo6hg7SD4DlS/ejYt8Y8IvGZHbSWsg=",
"lastModified": 1697331025,
"narHash": "sha256-a5LJWWHfEvnq9tBd9UyNVdtzLXc2ehu5MCp//Bex/0E=",
"owner": "nix-community",
"repo": "nixpkgs.lib",
"rev": "59da6ac0c02c48aa92dee37057f978412797db2a",
"rev": "05c07c73de74725ec7efa6609011687035a92c0f",
"type": "github"
},
"original": {
@ -948,11 +948,11 @@
]
},
"locked": {
"lastModified": 1697300259,
"narHash": "sha256-gmLtTy9kuLnw5MekDHY1Roc36kkEsLNTnKq8lSQkD3U=",
"lastModified": 1697372844,
"narHash": "sha256-Ut7Bojh7SqNf2pKRIoGPWWRU9ZjvSxSJS+GByC8AHD0=",
"owner": "nix-community",
"repo": "nixpkgs-wayland",
"rev": "6b428305043afaf77119adb44dfea246c809e07f",
"rev": "3bd7c4e831bc3dc82828e179397b06652adf2878",
"type": "github"
},
"original": {
@ -1258,11 +1258,11 @@
},
"templates": {
"locked": {
"lastModified": 1691421369,
"narHash": "sha256-Agfum0ykpUxUUS4AKYPVSWF+NeHPWay2o1TTNH0BFXA=",
"lastModified": 1697364028,
"narHash": "sha256-t7IGwY/nvopK9n9tgCVrHlLimhU2MuoP9VbVy07AR2A=",
"owner": "NixOS",
"repo": "templates",
"rev": "0fb94bf87144b18e42765693c3e15ac5f17eeab0",
"rev": "98bc26d94008617aac7cd0244fb09ff04d6c8cf6",
"type": "github"
},
"original": {

View file

@ -1,6 +1,6 @@
_inputs: _self: super: {
_inputs: _final: prev: {
lib =
super.lib
prev.lib
// {
disko = {
gpt = {

View file

@ -1,6 +1,6 @@
inputs: self: super: let
inputs: _final: prev: let
inherit
(super.lib)
(prev.lib)
concatMapStrings
escapeShellArg
filter
@ -71,7 +71,7 @@ inputs: self: super: let
hexToDec = v: foldl' (acc: x: acc * 16 + hexLiteralValues.${x}) 0 (stringToCharacters v);
in {
lib =
super.lib
prev.lib
// {
inherit
concatAttrs

View file

@ -1,4 +1,4 @@
inputs: self: super: let
inputs: final: prev: let
inherit
(inputs.nixpkgs.lib)
all
@ -22,7 +22,7 @@ inputs: self: super: let
;
inherit
(self.lib)
(final.lib)
hexToDec
pow
;
@ -37,7 +37,7 @@ inputs: self: super: let
.lib
.net;
in {
lib = recursiveUpdate super.lib {
lib = recursiveUpdate prev.lib {
net = recursiveUpdate (removeAttrs libNet ["types"]) {
cidr = rec {
# host :: (ip | mac | integer) -> cidr -> ip

View file

@ -1,4 +1,4 @@
inputs: _self: super: let
inputs: _final: prev: let
inherit
(inputs.nixpkgs.lib)
all
@ -39,7 +39,7 @@ inputs: _self: super: let
# automatically be coerced to the given type when merged.
lazyOf = type: types.coercedTo (lazyValueOf type) (x: x._lazyValue) type;
in {
lib = recursiveUpdate super.lib {
lib = recursiveUpdate prev.lib {
types = {
inherit
isLazyValue

View file

@ -1,4 +1,4 @@
inputs: self: super: let
inputs: final: prev: let
inherit
(inputs.nixpkgs.lib)
assertMsg
@ -19,21 +19,21 @@ inputs: self: super: let
;
inherit
(self.lib)
(final.lib)
net
concatAttrs
types
;
inherit
(self.lib.secrets)
(final.lib.secrets)
rageDecryptArgs
;
inherit (inputs.self) nodes;
in {
lib =
super.lib
prev.lib
// {
wireguard = wgName: let
# Returns the given node's wireguard configuration of this network

View file

@ -102,6 +102,8 @@
imports = cfg.commonImports ++ vmCfg.modules;
node.name = vmCfg.nodeName;
# TODO needed because of https://github.com/NixOS/nixpkgs/issues/102137
environment.noXlibs = mkForce false;
lib.microvm.mac = mac;
microvm = {

View file

@ -1,6 +1,6 @@
self: super: let
_final: prev: let
inherit
(self.lib)
(prev.lib)
escapeShellArg
concatMapStrings
flip
@ -20,9 +20,9 @@ self: super: let
version,
}: "go get ${escapeShellArg name}@${escapeShellArg version}\n");
in
super.caddy.override {
prev.caddy.override {
buildGoModule = args:
super.buildGoModule (args
prev.buildGoModule (args
// {
inherit vendorHash;
passthru.plugins = plugins;
@ -44,5 +44,5 @@ in {
# ];
# vendorHash = "sha256-AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=";
# }
caddy = super.caddy.overrideAttrs (_: {passthru.withPackages = make-custom-caddy;});
caddy = prev.caddy.overrideAttrs (_: {passthru.withPackages = make-custom-caddy;});
}

View file

@ -1,21 +1,22 @@
[
(import ./caddy.nix)
(import ./oauth2-proxy)
(_self: super: {
deploy = super.callPackage ./deploy.nix {};
git-fuzzy = super.callPackage ./git-fuzzy {};
kanidm-secret-manipulator = super.callPackage ./kanidm-secret-manipulator.nix {};
segoe-ui-ttf = super.callPackage ./segoe-ui-ttf.nix {};
zsh-histdb-skim = super.callPackage ./zsh-skim-histdb.nix {};
(import ./scripts)
(_final: prev: {
deploy = prev.callPackage ./deploy.nix {};
git-fuzzy = prev.callPackage ./git-fuzzy {};
kanidm-secret-manipulator = prev.callPackage ./kanidm-secret-manipulator.nix {};
segoe-ui-ttf = prev.callPackage ./segoe-ui-ttf.nix {};
zsh-histdb-skim = prev.callPackage ./zsh-skim-histdb.nix {};
kanidm = super.kanidm.overrideAttrs (_finalAttrs: _previousAttrs: {
kanidm = prev.kanidm.overrideAttrs (_finalAttrs: _previousAttrs: {
patches = [
(super.fetchpatch {
(prev.fetchpatch {
name = "group-list-json-output.patch";
url = "https://patch-diff.githubusercontent.com/raw/kanidm/kanidm/pull/2016.patch";
hash = "sha256-gc75KBzhth4fZvuvRa3Rjg1J7DIGy25mzUPCf2aha80=";
})
(super.fetchpatch {
(prev.fetchpatch {
name = "person-and-oauth-json-output.patch";
url = "https://patch-diff.githubusercontent.com/raw/kanidm/kanidm/pull/2017.patch";
hash = "sha256-fZgJ7dY2LHvBi64A/6o7kfArUAsLqjWRRpH2q1GL5ic=";
@ -26,9 +27,9 @@
});
formats =
super.formats
prev.formats
// {
ron = import ./ron.nix {inherit (super) lib pkgs;};
ron = import ./ron.nix {inherit (prev) lib pkgs;};
};
})
]

7
pkgs/scripts/default.nix Normal file
View file

@ -0,0 +1,7 @@
_final: prev: {
scripts = {
screenshot-area = prev.callPackage ./screenshot-area.nix {};
screenshot-area-scan-qr = prev.callPackage ./screenshot-area-scan-qr.nix {};
screenshot-screen = prev.callPackage ./screenshot-screen.nix {};
};
}

View file

@ -0,0 +1,58 @@
{
writeShellApplication,
libnotify,
xclip,
maim,
zbar,
yq,
}:
writeShellApplication {
name = "screenshot-area-scan-qr";
text = ''
set -euo pipefail
umask 077
# Create in-memory tmpfile
TMPFILE=$(mktemp)
exec 3<>"$TMPFILE"
rm "$TMPFILE" # still open in-memory as /dev/fd/3
TMPFILE=/dev/fd/3
date=$(date +"%Y-%m-%dT%H:%M:%S%:z")
out="''${XDG_PICTURES_DIR-$HOME/Pictures}/screenshots/$date-selection.png"
mkdir -p "$(dirname "$out")"
if ${maim}/bin/maim --color=.4,.7,1 --bordersize=1.0 --nodecorations=1 --hidecursor --format=png --quality=10 --noopengl --select \
| ${zbar}/bin/zbarimg --xml - > "$TMPFILE"; then
N=$(${yq}/bin/xq -r '.barcodes.source.index.symbol | if type == "array" then length else 1 end' < "$TMPFILE")
# Append codes Copy data separated by ---
DATA=$(${yq}/bin/xq -r '.barcodes.source.index.symbol | if type == "array" then .[0].data else .data end' < "$TMPFILE")
for ((i=1;i<N;++i)); do
DATA="$DATA"$'\n'"---"$'\n'"$(${yq}/bin/xq -r ".barcodes.source.index.symbol[$i].data" < "$TMPFILE")"
done
${xclip}/bin/xclip -selection clipboard <<< "$DATA"
${libnotify}/bin/notify-send \
"🔍 QR Code scan" " $N codes detected\n📋 copied ''${#DATA} bytes" \
--hint="string:image-path:$out" \
--hint="string:wired-tag:screenshot-$date" \
|| true
else
case "$?" in
"4")
${libnotify}/bin/notify-send \
"🔍 QR Code scan" " 0 codes detected" \
--hint="string:image-path:$out" \
--hint="string:wired-tag:screenshot-$date" \
|| true
;;
*)
${libnotify}/bin/notify-send \
"🔍 QR Code scan" " Error while processing image: zbarimg exited with code $?" \
--hint="string:image-path:$out" \
--hint="string:wired-tag:screenshot-$date" \
|| true
;;
esac
fi
'';
}

View file

@ -0,0 +1,46 @@
{
writeShellApplication,
libnotify,
xclip,
tesseract,
maim,
}:
writeShellApplication {
name = "screenshot-area";
text = ''
set -euo pipefail
umask 077
date=$(date +"%Y-%m-%dT%H:%M:%S%:z")
out="''${XDG_PICTURES_DIR-$HOME/Pictures}/screenshots/$date-selection.png"
mkdir -p "$(dirname "$out")"
${maim}/bin/maim --color=.4,.7,1,0.2 --bordersize=1.0 --nodecorations=1 \
--hidecursor --format=png --quality=10 --noopengl --select "$out"
${xclip}/bin/xclip -selection clipboard -t image/png < "$out"
action=$(${libnotify}/bin/notify-send \
"📷 Screenshot captured" "📋 copied to clipboard" \
--hint="string:wired-tag:screenshot-$date" \
--action=ocr=OCR) \
|| true
if [[ "$action" == "ocr" ]]; then
${libnotify}/bin/notify-send \
"📷 Screenshot captured" " Running OCR ..." \
--hint="string:wired-tag:screenshot-$date" \
|| true
if ${tesseract}/bin/tesseract "$out" - -l eng+deu | ${xclip}/bin/xclip -selection clipboard; then
${libnotify}/bin/notify-send \
"📷 Screenshot captured" "🔠 OCR copied to clipboard" \
--hint="string:wired-tag:screenshot-$date" \
|| true
else
${libnotify}/bin/notify-send \
"📷 Screenshot captured" " Error while running OCR" \
--hint="string:wired-tag:screenshot-$date" \
|| true
fi
fi
'';
}

View file

@ -0,0 +1,20 @@
{
writeShellApplication,
libnotify,
maim,
}:
writeShellApplication {
name = "screenshot-screen";
text = ''
set -euo pipefail
umask 077
out="''${XDG_PICTURES_DIR-$HOME/Pictures}/screenshots/$(date +"%Y-%m-%dT%H:%M:%S%:z")-fullscreen.png"
mkdir -p "$(dirname "$out")"
${maim}/bin/maim --hidecursor --format=png --quality=10 --noopengl "$out"
${libnotify}/bin/notify-send \
"📷 Screenshot captured" "💾 Saved to $out" \
|| true
'';
}

View file

@ -88,67 +88,9 @@ in {
# TODO only open if not already open
# TODO shortcut to open these from eww bar with 1 click
"b" = "exec firefox"; # TODO ; exec signal-desktop; exec discord
"Shift+s" =
"exec --no-startup-id "
+ toString (pkgs.writeShellScript "screenshot-area" ''
set -euo pipefail
umask 077
out="/tmp/screenshots.$UID/$(date +"%Y-%m-%dT%H:%M:%S%:z")-selection.png"
mkdir -p "$(dirname "$out")"
${pkgs.maim}/bin/maim --color=.4,.7,1,0.2 --bordersize=1.0 --nodecorations=1 --hidecursor --format=png --quality=10 --noopengl --select "$out"
notification_id=$(${pkgs.libnotify}/bin/notify-send --icon="$out" --print-id --app-name "screenshot-area" "Screenshot Captured" "📋 copied to clipboard\n Running OCR...")
${pkgs.xclip}/bin/xclip -selection clipboard -t image/png < "$out"
if ${pkgs.tesseract}/bin/tesseract "$out" - -l eng+deu | ${pkgs.xclip}/bin/xclip -selection primary; then
${pkgs.libnotify}/bin/notify-send --icon="$out" --replace-id="$notification_id" --app-name "screenshot-area" "Screenshot Captured" "📋 copied to clipboard\n OCR (copied to primary)."
else
${pkgs.libnotify}/bin/notify-send --icon="$out" --replace-id="$notification_id" --app-name "screenshot-area" "Screenshot Captured" "📋 copied to clipboard\n Error while running OCR."
fi
'');
"F11" =
"exec --no-startup-id "
# TODO use writeShellApplication for shellcheck
# TODO --icon=some-qr-image
+ toString (pkgs.writeShellScript "screenshot-area-scan-qr" ''
set -euo pipefail
umask 077
# Create in-memory tmpfile
TMPFILE=$(mktemp)
exec 3<>"$TMPFILE"
rm "$TMPFILE" # still open in-memory as /dev/fd/3
TMPFILE=/dev/fd/3
if ${pkgs.maim}/bin/maim --color=.4,.7,1 --bordersize=1.0 --nodecorations=1 --hidecursor --format=png --quality=10 --noopengl --select \
| ${pkgs.zbar}/bin/zbarimg --xml - > "$TMPFILE"; then
N=$(${pkgs.yq}/bin/xq -r '.barcodes.source.index.symbol | if type == "array" then length else 1 end' < "$TMPFILE")
# Append codes Copy data separated by ---
DATA=$(${pkgs.yq}/bin/xq -r '.barcodes.source.index.symbol | if type == "array" then .[0].data else .data end' < "$TMPFILE")
for ((i=1;i<N;++i)); do
DATA="$DATA"$'\n'"---"$'\n'"$(${pkgs.yq}/bin/xq -r ".barcodes.source.index.symbol[$i].data" < "$TMPFILE")"
done
${pkgs.xclip}/bin/xclip -selection clipboard <<< "$DATA"
${pkgs.libnotify}/bin/notify-send --app-name "screenshot-area-scan-qr" "QR Scan" " $N codes detected\\n📋 copied ''${#DATA} bytes"
else
case "$?" in
"4") ${pkgs.libnotify}/bin/notify-send --app-name "screenshot-area-scan-qr" "QR Scan" " 0 codes detected" ;;
*) ${pkgs.libnotify}/bin/notify-send --app-name "screenshot-area-scan-qr" "QR Scan" " Error while processing image: zbarimg exited with code $?" ;;
esac
fi
'');
"F12" =
"exec --no-startup-id "
+ toString (pkgs.writeShellScript "screenshot-screen" ''
set -euo pipefail
umask 077
out="${config.xdg.userDirs.pictures}/screenshots/$(date +"%Y-%m-%dT%H:%M:%S%:z")-fullscreen.png"
mkdir -p "$(dirname "$out")"
${pkgs.maim}/bin/maim --hidecursor --format=png --quality=10 --noopengl "$out"
notification_id=$(${pkgs.libnotify}/bin/notify-send --icon="$out" --print-id --app-name "screenshot-screen" "Screenshot Captured" "💾 saved to $out")
'');
"Shift+s" = "exec --no-startup-id ${getExe pkgs.scripts.screenshot-area}";
"F11" = "exec --no-startup-id ${getExe pkgs.scripts.screenshot-area-scan-qr}";
"F12" = "exec --no-startup-id ${getExe pkgs.scripts.screenshot-screen}";
"Shift+r" = "reload";
"q" = "kill";
@ -261,9 +203,39 @@ in {
};
in ''
exec_always --no-startup-id ${getExe i3-per-workspace-layout} --config ${configLayouts}
for_window [class="^flameshot$"] floating enable, border none
'';
};
systemd.user = {
targets.i3-session = {
Unit = {
Description = "i3 session";
Documentation = ["man:systemd.special(7)"];
BindsTo = ["graphical-session.target"];
Wants = ["graphical-session-pre.target"];
After = ["graphical-session-pre.target"];
};
};
services = {
#feh = {
# Unit = {
# Description = "feh background";
# PartOf = [ "i3-session.target" ];
# After = [ "xrandr.service" "picom.service" ];
# };
# Service = {
# ExecStart = "${pkgs.feh}/bin/feh --bg-fill ${config.xdg.dataHome}/wall.png";
# RemainAfterExit = true;
# Type = "oneshot";
# };
# Install.WantedBy = [ "i3-session.target" ];
#};
wired.Install.WantedBy = lib.mkForce ["i3-session.target"];
flameshot.Install.WantedBy = lib.mkForce ["i3-session.target"];
};
};
programs.autorandr.enable = true;
programs.autorandr.profiles =
{

View file

@ -165,6 +165,7 @@ in {
.${nixosConfig.node.name}
or {};
};
# TODO for_window [app_id="flameshot"] border pixel 0, floating enable, fullscreen disable, move absolute position 0 0
home.sessionVariables = {
# Let nixos electron wrappers enable wayland