Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions pkgs/by-name/do/docopts/package.nix
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
pkgs,
lib,
buildGoModule,
fetchFromGitHub,
Expand All @@ -23,6 +24,39 @@ buildGoModule rec {
})
];

postPatch =
let
path = lib.makeBinPath (
with pkgs;
[
gawk
coreutils
gnugrep
]
);
in
''

# Make docopts.sh get its dependencies from Nix.
# First, next to the first non-comment line, inject code to add the
# dependencies to the PATH.
awk -i inplace '!inserted && !/^#/ {
print "__NIX_OLD_PATH=\"$PATH\""
print "PATH=\"${path}:$PATH\""
inserted = 1
}
{ print }' docopts.sh
# Now, since this will be sourced by the user and we don't want to clobber
# PATH in their scripts, add a line at the end to restore the old PATH.
echo 'PATH="$__NIX_OLD_PPATH"' >> docopts.sh
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm... it is a bit cursed. Does this really work? docopts.sh exports several functions like docopt_get_raw_value which calls awk. Resetting PATH at the end of the script probably breaks that, right?

I'm not sure I have a clean solution. I'd say maybe just append to the PATH only, and don't restore at the end?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah yes thank you you are right. I only thought about the wrapper mode but this will break the library mode.

This kinda just seems like an incompatiility between the "yolo everything is global" model of Bash and the principles of Nix.

I don't really like clobbering people's path when they source the script... In fact, since I first wrote this PR, I decided I also don't really like docopts all that much. So much so that maybe I'd rather not package docopts.sh at all, than package it with the "clobbers $PATH" issue... I will ponder it and get back to you!

(Google has an internal Bash library for arg parsing in the style of Abseil/Go that I like much more. It's much much simpler. Let me know if you know of anything like this for Bash! Otherwise maybe I'll try to make an open source version...)

'';

# Include docopts.sh, which can be sourced for a convenient interface, see:
# https://github.com/docopt/docopts/blob/master/examples/arguments_example.sh
postInstall = ''
cp ./docopts.sh $out/bin/docopts.sh
'';

vendorHash = "sha256-+pMgaHB69itbQ+BDM7/oaJg3HrT1UN+joJL7BO/2vxE=";

meta = {
Expand Down
Loading