summaryrefslogtreecommitdiffstats
path: root/pkgs/simple/flameshot-once/default.nix
blob: 20a69c742ac0eb7fbd042c3db75cba3d439032ec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
{ name ? "flameshot-once", lib, mylib, pkgs, ... }@args:

let

  # config cannot be declared in the input attribute set because that would
  # cause callPackage to inject the wrong config.  Instead, get it from ...
  # via args.
  config = args.config or {};

  cfg = (lib.evalModules {
    specialArgs = {
      inherit mylib;
    };
    modules = lib.singleton {
      _file = toString ./default.nix;
      _module.args.pkgs = pkgs;
      imports = [
        config
        ./config.nix
      ];
    };
  }).config;
in

pkgs.symlinkJoin {
  inherit name;
  paths = [
    (pkgs.write "flameshot-once" {
      "/bin/flameshot-once" = {
        executable = true;
        text = /* sh */ ''
          #! ${pkgs.dash}/bin/dash
          export PATH=${lib.makeBinPath [
            pkgs.qt5.qtbase
          ]}:''${PATH+:$PATH}
          ${lib.optionalString (config != null) /* sh */ ''
            export XDG_CONFIG_HOME=${placeholder "out"}/etc
            ${lib.optionalString cfg.imgur.enable /* sh */ ''
              export IMGUR_CREATE_URL=${mylib.shell.escape cfg.imgur.createUrl}
              export IMGUR_DELETE_URL=${mylib.shell.escape cfg.imgur.deleteUrl}
              ${lib.optionalString cfg.imgur.xdg-open.enable /* sh */ ''
                export PATH=${placeholder "out"}/lib/imgur/bin''${PATH+:$PATH}
              ''}
            ''}
          ''}
          ${cfg.package}/bin/flameshot &
          exec ${cfg.package}/bin/flameshot gui
        '';
      };
      "/etc/flameshot/flameshot.ini".text =
        lib.generators.toINI {} (mylib.stripAttr cfg.settings);
      ${if cfg.imgur.enable then "/lib/imgur/bin/xdg-open" else null} = {
        executable = true;
        text = /* sh */ ''
          #! ${pkgs.dash}/bin/dash
          set -efu
          uri=$1
          prefix=$(${pkgs.coreutils}/bin/dirname "$uri")
          case $prefix in
            (${mylib.shell.escape cfg.imgur.xdg-open.createPrefix})
              echo "opening image in browser: $uri" >&2
              exec ${config.imgur.xdg-open.browser} "$uri"
              ;;
            (${mylib.shell.escape cfg.imgur.xdg-open.deletePrefix})
              echo "deleting image: $uri" >&2
              exec ${pkgs.curl}/bin/curl -fsS -X DELETE "$uri"
              ;;
            (*)
              echo "don't know how to open URI: $uri" >&2
              exit 1
          esac
        '';
      };
    })
  ];
}
// {
  dev = pkgs.write "flameshot-once-tools" {
    "/bin/get-recognizedGeneralOptions" = {
      executable = true;
      text = /* sh */ ''
        #! ${pkgs.dash}/bin/dash
        src=${cfg.package.src}/src/utils/confighandler.cpp
        ${pkgs.coreutils}/bin/cat "$src" |
        ${pkgs.gnused}/bin/sed -n '/\<recognizedGeneralOptions = {/,/^};/p' |
        ${pkgs.gcc}/bin/cpp |
        ${pkgs.gnugrep}/bin/grep -F OPTION |
        ${pkgs.coreutils}/bin/sort
      '';
    };
    "/bin/get-Shortcuts" = {
      executable = true;
      text = /* sh */ ''
        #! ${pkgs.dash}/bin/dash
        indent=$(${placeholder "out"}/bin/indent-of Shortcuts)
        src=${cfg.package.src}/src/utils/confighandler.cpp
        ${pkgs.coreutils}/bin/cat "$src" |
        ${pkgs.gnused}/bin/sed -n '/recognizedShortcuts = {/,/^};/p ' |
        ${pkgs.gcc}/bin/cpp |
        ${pkgs.gnused}/bin/sed -nr 's/^\s*SHORTCUT\("(TYPE_[^"]+).*/"\1"/p' |
        ${pkgs.coreutils}/bin/sort |
        ${placeholder "out"}/bin/prefix "  $indent"
      '';
    };
    "/bin/indent-of" = {
      executable = true;
      text = /* sh */ ''
        #! ${pkgs.dash}/bin/dash
        # usage: indent-of NAME NIX_FILE
        exec ${pkgs.gawk}/bin/awk -v name="$1" '
          $1 == name && $2 == "=" {
            sub("[^ ].*", "")
            print
          }
        ' ${./config.nix}
      '';
    };
    "/bin/prefix" = {
      executable = true;
      text = /* sh */ ''
        #! ${pkgs.dash}/bin/dash
        ${pkgs.gawk}/bin/awk -v prefix="$1" '{ print prefix $0 }'
      '';
    };
  };
}