about summary refs log tree commit diff
path: root/tools/eaglemode/default.nix
blob: eade62c028989d85dac70b78c1f52fdf6fe7698b (plain) (blame)
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
# Helper functions for extending Eagle Mode with useful stuff.
#
# Eagle Mode's customisation usually expects people to copy the entire
# configuration into their user folder, which we can automate fairly easily
# using Nix, letting users choose whether to keep upstream config or not.
{ depot, lib, pkgs, ... }:

let
  mkDesc = d: lib.concatMapStringsSep "\n"
    (x: "# Descr =${x}")
    (builtins.filter (s: s != "") (lib.splitString "\n" d));

  configWrapper = pkgs.runCommand "eaglemode-config-wrapper" { } ''
    cp ${./wrapper.go} wrapper.go
    export HOME=$PWD
    ${pkgs.go}/bin/go build wrapper.go
    install -Dm755 wrapper $out/bin/wrapper
  '';
in
rec {
  # mkCommand creates an Eagle Mode command for the file browser.
  #
  # Commands are basically little Perl scripts with a command standard library
  # available. They receive the user's selected target from Eagle Mode.
  mkCommand = lib.makeOverridable (
    {
      # Name of the command.
      name
    , # User-facing description, displayed in Eagle Mode UI. Can be multi-line.
      description
    , # Verbatim Perl code of the command. Command library is already available.
      code
    , # Caption for the UI button (defaults to name).
      caption ? name
    , icon ? "terminal.tga"
    , # TODO: what's a good default?
      hotkey ? ""
    , order ? 1.0
    }: pkgs.writeTextDir "emFileMan/Commands/${name}.pl" (''
      #!${pkgs.perl}/bin/perl
      #[[BEGIN PROPERTIES]]
      # Type = Command
      # Interpreter = perl
      # DefaultFor = directory
      # Caption = ${caption}
      # Order = ${toString order}
      # Icon = ${icon}
    ''
    + (lib.optionalString (description != "") "${mkDesc description}\n")
    + (lib.optionalString (hotkey != "") "# Hotkey = ${hotkey}\n")
    + ''
      #[[END PROPERTIES]]

      use strict;
      use warnings;
      BEGIN { require "$ENV{'EM_DIR'}/res/emFileMan/scripts/cmd-util.pl"; }

      ${if builtins.isString code
        then code
        else (if builtins.isPath code
             then builtins.readFile code
             else throw "code must be a string (literal code) or path to file")}
    '')
  );

  # etcDir creates a directory layout suitable for use in the EM_USER_CONFIG_DIR
  # environment variable.
  #
  # Note that Eagle Mode requires the value of that variable to be mutable at
  # runtime (it is the same place where it persists all of its user-controlled
  # state), so the results of this function can not be used directly.
  etcDir =
    { eaglemode ? pkgs.eaglemode
    , extraPaths ? [ ]
    }: pkgs.runCommand "eaglemode-config" { } ''
      mkdir $out

      ${
        lib.concatMapStringsSep "\n" (s: "cp -rT ${s} $out/\nchmod -R u+rw $out/\n") ([ "${eaglemode}/etc"] ++ extraPaths)
      }
    '';

  # withConfig creates an Eagle Mode wrapper that runs it with the given
  # configuration.
  withConfig = { eaglemode ? pkgs.eaglemode, config }: pkgs.writeShellScriptBin "eaglemode" ''
    set -ue
    ${configWrapper}/bin/wrapper --em-config "${config}"
    exec ${eaglemode}/bin/eaglemode "$@"
  '';
}