~zinigor/cardano-node/trunk

« back to all changes in this revision

Viewing changes to nix/workbench/default.nix

  • Committer: Igor Zinovyev
  • Date: 2021-08-13 19:12:27 UTC
  • Revision ID: zinigor@gmail.com-20210813191227-stlnsj3mc5ypwn0c
Tags: upstream-1.27.0
ImportĀ upstreamĀ versionĀ 1.27.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
{ lib
 
2
, stdenv
 
3
 
 
4
, cabal-install
 
5
, graphviz
 
6
, jq
 
7
, moreutils
 
8
, makeWrapper
 
9
, runCommand
 
10
 
 
11
## Default to pure Nix-iness:
 
12
, useCabalRun ? false
 
13
, workbenchDevMode ? false
 
14
 
 
15
, cardano-cli
 
16
, cardano-topology
 
17
}:
 
18
 
 
19
with lib;
 
20
 
 
21
let
 
22
  nixWbMode =
 
23
    if useCabalRun
 
24
    then "cabal-exes+nix-wb"
 
25
    else "nix-exes+nix-wb";
 
26
 
 
27
  workbench =
 
28
    stdenv.mkDerivation {
 
29
      pname = "workbench";
 
30
 
 
31
      version = "0.1";
 
32
 
 
33
      src = ./.;
 
34
 
 
35
      buildInputs = [ jq makeWrapper ];
 
36
 
 
37
      buildPhase = ''
 
38
        patchShebangs .
 
39
      '';
 
40
 
 
41
      postFixup = ''
 
42
        wrapProgram "$out/bin/wb" --argv0 wb --add-flags "--set-mode ${nixWbMode}" --prefix PATH ":" ${stdenv.lib.makeBinPath
 
43
          [ graphviz
 
44
            jq
 
45
            moreutils
 
46
 
 
47
            cardano-cli
 
48
            cardano-topology
 
49
          ]}
 
50
      '';
 
51
 
 
52
      installPhase = ''
 
53
        mkdir -p         $out/bin
 
54
        cp -a wb profiles *.sh *.jq $out/bin
 
55
      '';
 
56
 
 
57
      dontStrip = true;
 
58
    };
 
59
 
 
60
  runWorkbench =
 
61
    name: command:
 
62
    runCommand name {} ''
 
63
      ${workbench}/bin/wb ${command} > $out
 
64
    '';
 
65
 
 
66
  runJq =
 
67
    name: args: query:
 
68
    runCommand name {} ''
 
69
      args=(${args})
 
70
      ${jq}/bin/jq '${query}' "''${args[@]}" > $out
 
71
    '';
 
72
 
 
73
  exeCabalOp = op: exe:
 
74
    toString [ "${cabal-install}/bin/cabal" op "${exe}" "--"];
 
75
 
 
76
  checkoutWbMode =
 
77
    if useCabalRun
 
78
    then "cabal-exes+checkout-wb"
 
79
    else "nix-exes+checkout-wb";
 
80
 
 
81
  shellHook = ''
 
82
    ${optionalString workbenchDevMode
 
83
    ''
 
84
    echo 'workbench:  dev mode enabled, calling wb directly from checkout (instead of using Nix store)' >&2
 
85
 
 
86
    workbench_cardano_node_repo_root=$(git rev-parse --show-toplevel)
 
87
    workbench_extra_flags=
 
88
 
 
89
    function wb() {
 
90
      $workbench_cardano_node_repo_root/nix/workbench/wb --set-mode ${checkoutWbMode} $workbench_extra_flags "$@"
 
91
    }
 
92
 
 
93
    export workbench_cardano_node_repo_root workbench_extra_flags
 
94
    export -f wb
 
95
 
 
96
    ''}
 
97
 
 
98
    ${optionalString useCabalRun
 
99
    ''
 
100
    ./scripts/cabal-inside-nix-shell.sh
 
101
 
 
102
    echo 'workbench:  cabal-inside-nix-shell mode enabled, calling cardano-* via 'cabal run' (instead of using Nix store)' >&2
 
103
 
 
104
    function cardano-cli() {
 
105
      ${exeCabalOp "exec" "cardano-cli"} "$@"
 
106
    }
 
107
 
 
108
    function cardano-node() {
 
109
      ${exeCabalOp "exec" "cardano-node"} "$@"
 
110
    }
 
111
 
 
112
    function cardano-topology() {
 
113
      ${exeCabalOp "exec" "cardano-topology"} "$@"
 
114
    }
 
115
 
 
116
    export -f cardano-cli cardano-node cardano-topology
 
117
 
 
118
    ''}
 
119
 
 
120
    function workbench-prebuild-executables() {
 
121
      ${optionalString useCabalRun
 
122
        ''
 
123
      echo -n "workbench:  prebuilding executables (because of useCabalRun):"
 
124
      for exe in cardano-cli cardano-node cardano-topology
 
125
      do echo -n " $exe"
 
126
         cabal -v0 build exe:$exe >/dev/null || return 1
 
127
      done
 
128
      echo
 
129
        ''}
 
130
      true
 
131
    }
 
132
    export -f workbench-prebuild-executables
 
133
 
 
134
    '';
 
135
 
 
136
  generateProfiles =
 
137
    { pkgs
 
138
 
 
139
    ## The backend is an attrset of AWS/supervisord-specific methods and parameters.
 
140
    , backend
 
141
 
 
142
    ## Environmental settings:
 
143
    ##   - either affect semantics on all backends equally,
 
144
    ##   - or have no semantic effect
 
145
    , environment
 
146
    }:
 
147
    rec {
 
148
      profile-names-json =
 
149
        runWorkbench "profile-names.json" "profiles list";
 
150
 
 
151
      profile-names =
 
152
        __fromJSON (__readFile profile-names-json);
 
153
 
 
154
      mkProfile =
 
155
        profileName:
 
156
        pkgs.callPackage ./profiles
 
157
          { inherit
 
158
              pkgs
 
159
              runWorkbench runJq workbench
 
160
              backend
 
161
              environment
 
162
              profileName;
 
163
          };
 
164
 
 
165
      profiles = genAttrs profile-names mkProfile;
 
166
 
 
167
      profilesJSON =
 
168
        runWorkbench "all-profiles.json" "profiles generate-all";
 
169
    };
 
170
in
 
171
{
 
172
  inherit workbench runWorkbench runJq;
 
173
 
 
174
  inherit generateProfiles shellHook;
 
175
}