2021-01-10 20:56:52 +01:00
|
|
|
{ depot, pkgs, ... }:
|
|
|
|
let
|
2021-08-07 19:37:54 +02:00
|
|
|
bins = depot.nix.getBins pkgs.coreutils [ "printf" "echo" "cat" "printenv" "tee" ]
|
|
|
|
// depot.nix.getBins pkgs.bash [ "bash" ]
|
2021-01-17 12:32:09 +01:00
|
|
|
// depot.nix.getBins pkgs.fdtools [ "multitee" ]
|
|
|
|
;
|
2021-01-10 20:56:52 +01:00
|
|
|
|
2021-08-07 19:37:54 +02:00
|
|
|
# Print `msg` and and argv to stderr, then execute into argv
|
2021-01-10 20:56:52 +01:00
|
|
|
debugExec = msg: depot.nix.writeExecline "debug-exec" { } [
|
|
|
|
"if"
|
|
|
|
[
|
|
|
|
"fdmove"
|
|
|
|
"-c"
|
|
|
|
"1"
|
|
|
|
"2"
|
|
|
|
"if"
|
|
|
|
[ bins.printf "%s: " msg ]
|
|
|
|
"if"
|
|
|
|
[ bins.echo "$@" ]
|
|
|
|
]
|
|
|
|
"$@"
|
|
|
|
];
|
|
|
|
|
2021-08-07 19:37:54 +02:00
|
|
|
# Print stdin to stderr and stdout
|
2021-01-17 12:32:09 +01:00
|
|
|
eprint-stdin = depot.nix.writeExecline "eprint-stdin" { } [
|
|
|
|
"pipeline"
|
|
|
|
[ bins.multitee "0-1,2" ]
|
|
|
|
"$@"
|
|
|
|
];
|
|
|
|
|
2021-08-07 19:37:54 +02:00
|
|
|
# Assume the input on stdin is netencode, pretty print it to stderr and forward it to stdout
|
2021-02-14 16:55:56 +01:00
|
|
|
eprint-stdin-netencode = depot.nix.writeExecline "eprint-stdin-netencode" { } [
|
|
|
|
"pipeline"
|
|
|
|
[
|
|
|
|
# move stdout to 3
|
|
|
|
"fdmove"
|
|
|
|
"3"
|
|
|
|
"1"
|
|
|
|
# the multitee copies stdin to 1 (the other pipeline end) and 3 (the stdout of the outer pipeline block)
|
|
|
|
"pipeline"
|
|
|
|
[ bins.multitee "0-1,3" ]
|
|
|
|
# make stderr the stdout of pretty, merging with the stderr of pretty
|
|
|
|
"fdmove"
|
|
|
|
"-c"
|
|
|
|
"1"
|
|
|
|
"2"
|
|
|
|
depot.users.Profpatsch.netencode.pretty
|
|
|
|
]
|
|
|
|
"$@"
|
|
|
|
];
|
|
|
|
|
2021-08-07 19:37:54 +02:00
|
|
|
# print the given environment variable in $1 to stderr, then execute into the rest of argv
|
2021-01-17 12:32:09 +01:00
|
|
|
eprintenv = depot.nix.writeExecline "eprintenv" { readNArgs = 1; } [
|
2021-02-13 20:53:41 +01:00
|
|
|
"ifelse"
|
|
|
|
[ "fdmove" "-c" "1" "2" bins.printenv "$1" ]
|
|
|
|
[ "$@" ]
|
2021-04-04 04:04:20 +02:00
|
|
|
"if"
|
|
|
|
[ depot.tools.eprintf "eprintenv: could not find \"\${1}\" in the environment\n" ]
|
2021-02-13 20:53:41 +01:00
|
|
|
"$@"
|
2021-01-17 12:32:09 +01:00
|
|
|
];
|
|
|
|
|
2021-08-07 19:37:54 +02:00
|
|
|
# Split stdin into two commands, given by a block and the rest of argv
|
|
|
|
#
|
|
|
|
# Example (execline):
|
|
|
|
#
|
|
|
|
# pipeline [ echo foo ]
|
|
|
|
# split-stdin [ fdmove 1 2 foreground [ cat ] echo "bar" ] cat
|
|
|
|
#
|
|
|
|
# stdout: foo\n
|
|
|
|
# stderr: foo\nbar\n
|
|
|
|
split-stdin = depot.nix.writeExecline "split-stdin" { argMode = "env"; } [
|
|
|
|
"pipeline"
|
|
|
|
[
|
|
|
|
# this is horrible yes but the quickest way I knew how to implement it
|
|
|
|
"runblock"
|
|
|
|
"1"
|
|
|
|
bins.bash
|
|
|
|
"-c"
|
|
|
|
''${bins.tee} >("$@")''
|
|
|
|
"bash-split-stdin"
|
|
|
|
]
|
|
|
|
"runblock"
|
|
|
|
"-r"
|
|
|
|
"1"
|
|
|
|
];
|
|
|
|
|
2021-02-09 21:50:21 +01:00
|
|
|
# remove everything but a few selected environment variables
|
|
|
|
runInEmptyEnv = keepVars:
|
|
|
|
let
|
|
|
|
importas = pkgs.lib.concatMap (var: [ "importas" "-i" var var ]) keepVars;
|
|
|
|
# we have to explicitely call export here, because PATH is probably empty
|
|
|
|
export = pkgs.lib.concatMap (var: [ "${pkgs.execline}/bin/export" var ''''${${var}}'' ]) keepVars;
|
|
|
|
in
|
|
|
|
depot.nix.writeExecline "empty-env" { }
|
|
|
|
(importas ++ [ "emptyenv" ] ++ export ++ [ "${pkgs.execline}/bin/exec" "$@" ]);
|
|
|
|
|
|
|
|
|
2021-01-10 20:56:52 +01:00
|
|
|
in
|
|
|
|
{
|
|
|
|
inherit
|
|
|
|
debugExec
|
2021-01-17 12:32:09 +01:00
|
|
|
eprint-stdin
|
2021-02-14 16:55:56 +01:00
|
|
|
eprint-stdin-netencode
|
2021-01-17 12:32:09 +01:00
|
|
|
eprintenv
|
2021-08-07 19:37:54 +02:00
|
|
|
split-stdin
|
2021-02-09 21:50:21 +01:00
|
|
|
runInEmptyEnv
|
2021-01-10 20:56:52 +01:00
|
|
|
;
|
|
|
|
}
|