Execline: Difference between revisions
initial article with wrappers sections |
(No difference)
|
Revision as of 12:04, 19 July 2025
execline is an interpreter and a collection of utilities for composing Unix commands into scripts. The syntax of execline makes it better suited for code generation from Nix than interactive shell languages like Bash.
execlineb
execlineb is the interpreter for execline scripts. It reads a file, produces a command-line, and executes into that command line. execlineb manages the environment of the command that it executes and optionaly substitutes the arguments a script receives into the command-line. execlineb also handles the quoting of strings using "" and blocks using {}. Blocks are described later in this article.
Wrappers
Consider the scenario of wrapping a program to override or set a default value for the EDITOR enviroment variable.
The following example is a script that uses the export program to set EDITOR. Here the -s0 option is used to replace $1 and $@ within the command-line with the first and remaining arguments received by the script.
#! /usr/bin/env execlineb -s1
export EDITOR $1
$@If the script is executed as wrap-editor.el nano mutt then the command-line executed by execlineb would be export nano mutt.
To create a wrapper that would provide a default EDITOR the importas program would be used. In the following example the EDITOR value is imported from the calling enviroment, with a default value specified by -D, and any instance of $editor is replaced by that value. The export program exports the value of $editor after substitution.
#! /usr/bin/env execlineb -s1
importas -D $1 EDITOR $editor
export EDITOR $editor
$@Generating a wapper from Nix
To generate a wrapper with a fixed default for EDITOR the Nix function at execline.passthru.writeScript can be used:
{ pkgs ? import <nixpkgs> { } }:
pkgs.execline.passthru.writeScript "wrap-nano" "-s0" ''
importas -D ${pkgs.nano}/bin/nano EDITOR E
export EDITOR $E
$@
''