about summary refs log tree commit diff
path: root/pkgs/build-support/trivial-builders.nix
diff options
context:
space:
mode:
authorBernardo Meurer <bernardo@meurer.org>2021-10-12 12:31:02 -0700
committerBernardo Meurer <bernardo@meurer.org>2021-11-08 09:33:32 -0800
commit89979c9c5bef4cfc89e66662b1238a0bf7617702 (patch)
treed5594914e9cfc0346103f6120bbfd8579a1564d6 /pkgs/build-support/trivial-builders.nix
parent2de888a972be0747c29a23de73a092fd0e558677 (diff)
writeShellApplication: init
Diffstat (limited to 'pkgs/build-support/trivial-builders.nix')
-rw-r--r--pkgs/build-support/trivial-builders.nix47
1 files changed, 46 insertions, 1 deletions
diff --git a/pkgs/build-support/trivial-builders.nix b/pkgs/build-support/trivial-builders.nix
index 58cdeb269d58c..506f843101605 100644
--- a/pkgs/build-support/trivial-builders.nix
+++ b/pkgs/build-support/trivial-builders.nix
@@ -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