Created
July 8, 2023 00:07
-
-
Save ldub/bf3c107c5b88bbf444aef319c9f3e5e3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/pkgs/build-support/trivial-builders/default.nix b/pkgs/build-support/trivial-builders/default.nix | |
index 871f9722a4a..be329a6881a 100644 | |
--- a/pkgs/build-support/trivial-builders/default.nix | |
+++ b/pkgs/build-support/trivial-builders/default.nix | |
@@ -1,4 +1,4 @@ | |
-{ lib, stdenv, stdenvNoCC, lndir, runtimeShell, shellcheck, haskell }: | |
+{ lib, stdenv, stdenvNoCC, lndir, runtimeShell, shellcheck, haskell, argbash }: | |
let | |
inherit (lib) | |
@@ -138,6 +138,7 @@ rec { | |
, meta ? { } | |
, allowSubstitutes ? false | |
, preferLocalBuild ? true | |
+ , postInstall ? "" | |
}: | |
let | |
matches = builtins.match "/bin/([^/]+)" destination; | |
@@ -163,6 +164,7 @@ rec { | |
chmod +x "$target" | |
fi | |
+ ${postInstall} | |
eval "$checkPhase" | |
''; | |
@@ -330,6 +332,7 @@ rec { | |
, text | |
, runtimeInputs ? [ ] | |
, checkPhase ? null | |
+ , postInstall ? null | |
}: | |
writeTextFile { | |
inherit name; | |
@@ -349,7 +352,7 @@ rec { | |
${text} | |
''; | |
- | |
+ postInstall = postInstall; | |
checkPhase = | |
if checkPhase == null then '' | |
runHook preCheck | |
@@ -362,6 +365,50 @@ rec { | |
else checkPhase; | |
}; | |
+ /* | |
+ Similar to writeShellApplication but runs the provided text through argbash | |
+ to generate an options parser before using writeShellApplication under the hood. | |
+ | |
+ Example: | |
+ | |
+ Writes my-file to /nix/store/<store path>/bin/my-file and makes executable. | |
+ | |
+ > my-file --help | |
+ A cow greets you from an argbash-generated, nix-packaged script. | |
+ Usage: my-file [-n|--name <arg>] [-h|--help] | |
+ -n, --name: The name to greet (default: 'Lev') | |
+ -h, --help: Prints help | |
+ | |
+ writeArgbashShellApplication { | |
+ name = "my-file"; | |
+ runtimeInputs = [ cowsay ]; | |
+ text = '' | |
+ # ARG_OPTIONAL_SINGLE([name], [n], [The name to greet], [Lev]) | |
+ # ARG_HELP([A cow greets you from an argbash-generated, nix-packaged script.]) | |
+ # ARGBASH_GO | |
+ # [ <-- needed because of Argbash | |
+ | |
+ cowsay "Hello, $_arg_name!" | |
+ | |
+ # ] <-- needed because of Argbash | |
+ ''; | |
+ } | |
+ */ | |
+ writeArgbashShellApplication = | |
+ { name | |
+ , text | |
+ , runtimeInputs ? [ ] | |
+ , checkPhase ? null | |
+ }: | |
+ writeShellApplication { | |
+ inherit name runtimeInputs checkPhase; | |
+ text = text; | |
+ postInstall = '' | |
+ argbash -o $out $out | |
+ chmod +x $out | |
+ ''; | |
+ }; | |
+ | |
# Create a C binary | |
writeCBin = name: code: | |
runCommandCC name | |
diff --git a/pkgs/top-level/stage.nix b/pkgs/top-level/stage.nix | |
index 3886ae04e49..3dafadeb38a 100644 | |
--- a/pkgs/top-level/stage.nix | |
+++ b/pkgs/top-level/stage.nix | |
@@ -104,7 +104,7 @@ let | |
import ../build-support/trivial-builders { | |
inherit lib; | |
inherit (self) runtimeShell stdenv stdenvNoCC haskell; | |
- inherit (self.pkgsBuildHost) shellcheck; | |
+ inherit (self.pkgsBuildHost) shellcheck argbash; | |
inherit (self.pkgsBuildHost.xorg) lndir; | |
}; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment