Execline: Difference between revisions

Ehmry (talk | contribs)
initial article with wrappers sections
 
Ehmry (talk | contribs)
blocks section
Line 44: Line 44:
</syntaxHighlight>
</syntaxHighlight>


== Blocks ==
Some execline programs take code blocks as their arguments. Blocks are delimited by <code>{}</code> in execlineb scripts. In the following example the <code>pipeline</code> program executes a block of commands in the background with a pipe connecting the stdin of that block to the stdout of the command-line following the block.
<syntaxHighlight lang="nix">
{ pkgs ? import <nixpkgs> { } }:
pkgs.execline.passthru.writeScript "wrap-cowsay" "-s0" ''
  pipeline -w { ${pkgs.lib.getExe pkgs.cowsay} }
  $@
''
</syntaxHighlight>
=== Block quoting ===
execlineb parses <code>{}</code> blocks and constructs a command-line where each block item is prepended with a whitespace (\x20) and terminated with an empty string. This quoting can be done in pure Nix.
Given the function <code>quoteExecline</code> as
<syntaxHighlight lang="nix">
builtins.foldl' (acc: arg: acc ++ (
  if builtins.isList arg
    then map (_: " ${_}") (quoteExecline arg) ++ [ "" ]
    else [ arg ]
  )) [ ];
</syntaxHighlight>
then the expression
<syntaxHighlight lang="nix">
args: lib.escapeShellArgs (quoteExecline [ "pipeline" "-w" [ (lib.getExe pkgs.cowsay) ] ] ++ args)
</syntaxHighlight>
would produce the command-line executed by <code>execlineb</code> in the previous "wrap-cowsay" example when given the same arguments.


[[Category:Cookbook]]
[[Category:Cookbook]]