3
0
Fork 0
forked from mirrors/nixpkgs

writeShellApplication: init

This commit is contained in:
Bernardo Meurer 2021-10-12 12:31:02 -07:00
parent 2de888a972
commit 89979c9c5b
No known key found for this signature in database
GPG key ID: F4C0D53B8D14C246
2 changed files with 68 additions and 1 deletions

View file

@ -47,6 +47,28 @@ These functions write `text` to the Nix store. This is useful for creating scrip
Many more commands wrap `writeTextFile` including `writeText`, `writeTextDir`, `writeScript`, and `writeScriptBin`. These are convenience functions over `writeTextFile`.
## `writeShellApplication` {#trivial-builder-writeShellApplication}
This can be used to easily produce a shell script that has some dependencies (`buildInputs`). It automatically sets the `PATH` of the script to contain all of the listed inputs, sets some sanity shellopts (`errexit`, `nounset`, `pipefail`), and checks the resulting script with [`shellcheck`](https://github.com/koalaman/shellcheck).
For example, look at the following code:
```nix
writeShellApplication {
name = "show-nixos-org";
buildInputs = [ curl w3m ];
text = ''
curl -s 'https://nixos.org' | w3m -dump -T text/html
'';
}
```
Unlike with normal `writeShellScriptBin`, there is no need to manually write out `${curl}/bin/curl`, setting the PATH
was handled by `writeShellApplication`. Moreover, the script is being checked with `shellcheck` for more strict
validation.
## `symlinkJoin` {#trivial-builder-symlinkJoin}
This can be used to put many derivations into the same directory structure. It works by creating a new derivation and adding symlinks to each of the paths listed. It expects two arguments, `name`, and `paths`. `name` is the name used in the Nix store path for the created derivation. `paths` is a list of paths that will be symlinked. These paths can be to Nix store derivations or any other subdirectory contained within.

View file

@ -1,4 +1,4 @@
{ lib, stdenv, stdenvNoCC, lndir, runtimeShell }:
{ lib, stdenv, stdenvNoCC, lndir, runtimeShell, makeBinPath, shellcheck }:
rec {
@ -249,6 +249,51 @@ rec {
'';
};
/*
* Similar to writeShellScriptBin and writeScriptBin.
* Writes an executable Shell script to /nix/store/<store path>/bin/<name> and
* checks its syntax with shellcheck and the shell's -n option.
* Automatically includes sane set of shellopts (errexit, nounset, pipefail)
* and handles creation of PATH based on buildInputs
*
* Example:
* # Writes my-file to /nix/store/<store path>/bin/my-file and makes executable.
* writeShellApplication {
* name = "my-file";
* buildInputs = [ curl w3m ];
* text = ''
* curl -s 'https://nixos.org' | w3m -dump -T text/html
* '';
* }
*/
writeShellApplication =
{ name
, text
, buildInputs ? [ ]
, checkPhase ? null
}:
writeTextFile {
inherit name;
executable = true;
destination = "/bin/${name}";
text = ''
#!${runtimeShell}
set -o errexit
set -o nounset
set- o pipefail
export PATH="${makeBinPath buildInputs}:$PATH"
${text}
'';
checkPhase = if checkPhase == null then ''
${stdenv.shell} -n $out/bin/${name}
${shellcheck}/bin/shellcheck $out/bin/${name}
''
else checkPhase;
};
# Create a C binary
writeCBin = name: code:
runCommandCC name