Commit Graph

44 Commits

Author SHA1 Message Date
emilylange
c47f245253
patch-shebangs: fix crash with shebang without trailing newline
This fixes a bug where `patchShebangs` crashes when trying to patch
files that contain only a shebang (e.g. `#!/bin/bash`) (and nothing
else) and do not end with a newline.

Such file can be produced using `printf "#!/bin/bash" > example` or
`echo -n "#!/bin/bash" > example`.

I don't understand why one would want to create such files, as they do
literally nothing, but the chromium tarball we are using started
shipping some 🫠

Full reproducer:

```nix
with import <nixpkgs> { };

stdenv.mkDerivation {
  dontUnpack = true;
  name = "patch-shebangs-no-trailing-newline-reproducer";
  postPatch = ''
    printf "#!/bin/bash" > reproducer
    chmod +x reproducer
    patchShebangs reproducer
  '';
}
```

```
❯ nix-build reproducer.nix
this derivation will be built:
  /nix/store/vmbshdkdk4a0bayw3wi21wvxyhzpcsy2-patch-shebangs-no-trailing-newline-reproducer.drv
building '/nix/store/vmbshdkdk4a0bayw3wi21wvxyhzpcsy2-patch-shebangs-no-trailing-newline-reproducer.drv'...
patching sources
patching script interpreter paths in reproducer
/nix/store/vr6wwdxkmyy44sg0gwxi10b8fc5zhwz0-stdenv-linux/setup: line 144: pop_var_context: head of shell_variables not a function context
error: builder for '/nix/store/vmbshdkdk4a0bayw3wi21wvxyhzpcsy2-patch-shebangs-no-trailing-newline-reproducer.drv' failed with exit code 1;
       last 3 log lines:
       > patching sources
       > patching script interpreter paths in reproducer
       > /nix/store/vr6wwdxkmyy44sg0gwxi10b8fc5zhwz0-stdenv-linux/setup: line 144: pop_var_context: head of shell_variables not a function context
       For full logs, run 'nix log /nix/store/vmbshdkdk4a0bayw3wi21wvxyhzpcsy2-patch-shebangs-no-trailing-newline-reproducer.drv'.
```
2023-09-16 14:20:56 +02:00
Artturi
d5139e3017
Merge pull request #245909 from Artturin/setupshchanges2 2023-09-04 20:41:16 +03:00
Artturin
1db1e3c467 stdenv: Fix possible issues discovered with
```
set -o errexit -o nounset -o pipefail
shopt -s inherit_errexit
```

in `stdenv/default-builder.sh`
2023-09-02 03:25:36 +03:00
Ivan Trubach
ebd4619053 patch-shebangs: add a flag to update shebangs with store paths
This change adds a flag to update shebang paths that point to the Nix
store. This is particularly useful when a cross-compiled package uses
same script at compile-time and run-time, but the interpreter must be
changed since hostPlatform != buildPlatform.
2023-06-19 09:01:21 +03:00
Timothy DeHerrera
792907e3bf patch-shebangs: handle env -S shebangs
`env -S` allows one to set more than one argument in a shebang. This
small patch allows the patch-shebangs hook to handle this case
appropriately.
2023-02-17 22:22:26 +02:00
Artturin
fe1c7a1945 treewide: remove usages of header and stopNest
they're obsolete
2023-01-16 00:08:12 +02:00
Andrew Childs
ca156a66b7 stdenv/patchShebangs: fix off by one reading old interpreter
This caused shebangs that were already store paths to be rewritten.

Introduced by ab4c359822 in #94642

Example difference:

    $ echo "hello world" | tail -c+3
    llo world

    $ str="hello world"; echo ${str:3}
    lo world
2021-02-09 13:05:38 +09:00
Andrew Childs
2bd3aa4bc2 Revert "Revert "stdenv/patchShebangs: avoid temporary time reference file""
This reverts commit df21fb8afa.
2021-02-09 12:57:21 +09:00
Martin Weinelt
df21fb8afa Revert "stdenv/patchShebangs: avoid temporary time reference file"
This reverts commit 6e3f4c9079.

This apparently breaks the x86_64 darwin stdenv. Details in #112417.
2021-02-09 01:33:33 +01:00
Jörg Thalheim
6e3f4c9079
stdenv/patchShebangs: avoid temporary time reference file
less commands -> faster
2021-01-14 20:00:58 +01:00
Jörg Thalheim
cb1654ff92
stdenv/patchShebangs: consistent conditional tests
according to shellcheck [[ foo == "bla" ]] && [[ ... ]] has better posix
semantics over [ foo = "bla" -a ... ]. It is also easier to read.
2021-01-14 20:00:57 +01:00
Jörg Thalheim
ab4c359822
stdenv/patchShebangs: use builtins where possible
builtins for small input sizes should be faster due to less forking.
2021-01-14 20:00:54 +01:00
Bryan Gardiner
bc21c247fc
patch-shebangs.sh: Fix error messages to go to stderr, not '0' file. 2020-03-20 17:08:10 -07:00
John Ericson
2811b032d6 treewide: Make still dont* Variables are optional in most cases
Go beyond the obvious setup hooks now, with a bit of sed, with a skipped case:

 - cc-wrapper's `dontlink`, because it already is handled.

Also, in nix files escaping was manually added.

EMP
2019-11-01 14:44:44 -04:00
Daniel Schaefer
7c3d7521f0 patchShebangs: Exit if no arguments were given
Commit "patchShebangs: Allow for multiple arguments" 4a1e51f957
removed the check. We don't want to break existing usages so this
introduces it again with a successful exit code.
2019-05-26 16:05:06 +02:00
Daniel Schaefer
4a1e51f957 patchShebangs: Allow for multiple arguments
It's tempting to think patchShebangs supports multiple arguments.
Without this patch it just silently ignores all but the first. Now it
patches the shebangs in all of its arguments.

Fixes: #57695
2019-05-25 16:15:54 +02:00
Daniel Schaefer
81d3bd60a4 patchShebangs: Explain that script must be executable 2019-05-25 16:15:54 +02:00
Matthew Bauer
87944c3125
Merge pull request #56744 from matthewbauer/macos-10-12
Update macOS to 10.12
2019-04-26 22:20:03 -04:00
Matthew Bauer
a1533dfc41 patch-shebangs: fix dev detection
$output = $outputDev is the right condition. ${!outputDev} is only
useful if you want a path.
2019-04-26 21:54:48 -04:00
Matthew Bauer
61bc03c017 Revert "Revert "patch-shebangs: respect cross compilation""
This reverts commit 9c4b11e9a0.
2019-04-26 21:54:48 -04:00
Graham Christensen
a682d720c7
Merge pull request #60215 from nmattia/nm-tmp-timestamp
patchShebangs: create timestamp in tmp dir
2019-04-25 12:50:16 -04:00
Nicolas Mattia
cc9b4029bc patchShebangs: create timestamp in tmp dir
Creating the timestamp in the patched script's directory has a few
drawbacks:

* if "foo.timestamp" already exists, it will be overwritten
* it requires the directory to be writable
2019-04-25 17:07:58 +02:00
Daiderd Jordan
bdec3ed049
Revert "Revert "Revert "patch-shebangs: respect cross compilation"""
Completely breaks darwin. Every package in the stdenv that has shebangs
in the output will end up with references to bootstrap-tools.

This reverts commit bde99096a8.
2018-11-14 23:37:32 +01:00
Daiderd Jordan
c9223a17bc
Revert "patch-shebangs: use --build for auto patch shebangs"
Completely breaks darwin. Every package in the stdenv that has shebangs
in the output will end up with references to bootstrap-tools.

This reverts commit eb7c50a993.
2018-11-14 23:37:31 +01:00
Matthew Bauer
eb7c50a993 patch-shebangs: use --build for auto patch shebangs
In strictDeps=false, autoPatchshebangs should use
--build (corresponding to PATH) to lookup commands. This restores the
previous behavior of patchshebangs so that we don’t break stuff that
isn’t careful in the buildInputs vs. nativeBuildInputs distinction.
Unfortunately this won’t work under cross compilation.
2018-11-02 00:27:14 -05:00
Matthew Bauer
bde99096a8 Revert "Revert "patch-shebangs: respect cross compilation""
This reverts commit 9c4b11e9a0.
2018-11-02 00:27:14 -05:00
Will Dietz
f7db287960 patch-shebangs.sh: use more robust 'for each file' loop, check for dir
The latter is to avoid warnings printed by find if it doesn't exist.
2018-09-28 11:21:51 -05:00
Will Dietz
286381f072 patch-shebangs: simplify a bit per reviewer suggestion 2018-09-28 11:17:33 -05:00
Will Dietz
830f9fabd4 patch-shebangs: use isScript to safely check for shebang start
Fixes commonly encountered errors about broken pipes or null-bytes in
command-substitution.
2018-09-28 11:15:36 -05:00
Daiderd Jordan
9c4b11e9a0
Revert "patch-shebangs: respect cross compilation"
This causes problems for packages built using a bootstrap stdenv,
resulting in references to /bin/sh or even bootstrap-tools. The darwin
stdenv is much stricter about what requisites/references are allowed but
using /bin/sh on linux is also undesirable.

eg. https://hydra.nixos.org/build/81754896

    $ nix-build -A xz
    $ head -n1 result-bin/bin/xzdiff
    #!/nix/store/yvc7kmw98kq547bnqn1afgyxm8mxdwhp-bootstrap-tools/bin/sh

This reverts commit f06942327a.
2018-09-25 21:11:54 +02:00
Matthew Bauer
e258c8d8dd
Merge pull request #45698 from kamilchm/filename-in-usnupported-shebang-error
Show the filename on unsupported shebang error
2018-09-10 14:52:38 -05:00
Kamil Chmielewski
383de74f88 patch-shebangs: filename on unsupported shebang
Show the filename on unsupported shebang error.
Simplifies debugging packages with large set of scripts.
2018-08-29 21:47:27 +02:00
Matthew Bauer
f06942327a patch-shebangs: respect cross compilation
This hopefully makes patchShebangs respect cross compilation. It
introduces the concept of the HOST_PATH. Nothing is ever executed on
it but instead used as a way to get the proper path using ‘command
-v’. Needs more testing.

/cc @ericson2314 @dtzwill

Fixes #33956
Fixes #21138
2018-07-31 15:38:35 -04:00
Yegor Timoshenko
4b1b6ee6d1
patchShebangs: preserve times, resolves #33084
Close #33281.  Edits by vcunat:
 - use Eelco's idea: empty file instead of full copy
 - use longer name suffix to decrease the likelihood of collision
2018-01-21 12:09:07 +01:00
Nikolay Amiantov
d43bde5f2b patchShebangs: quote variable assignment 2016-07-09 03:05:11 +03:00
Cireo
ff3e7d2b4b patchShebangs: strip trailing whitespace
Prior to this commit, trailing whitespace would be introduced when
modifying '#!' lines with no arguments.  For example (whitespace added):

    /nix/store/.../foo: interpreter directive changed
        from "/bin/bash"
          to "/nix/store/...-bash-4.3-p42/bin/bash  "

    /nix/store/.../bar: interpreter directive changed
        from "/bin/baz wef"
          to "/nix/store/...-baz wef "

We add a sed command to strip trailing whitespace, so the above commands
would drop the two spaces after "bash", or the one space after "baz wef".

abbradar: fixed commit title

Closes #16785.
2016-07-09 03:00:27 +03:00
Danny Wilson
f9134ca9df Fix patchShebangs on SmartOS
Tail is very picky about the space after -c
 when compiled by Nix on Illumos (no idea why).
2015-11-16 17:20:14 +01:00
Eelco Dolstra
276e38a1bb patchShebangs: Ignore outputs that don't exist yet 2015-09-17 16:54:07 +02:00
Vladimír Čunát
8f33b8cc93 mass rewrite of find parameters to cross-platform style
Fixes #9044, close #9667. Thanks to @taku0 for suggesting this solution.
Now we have no modes starting with `/` or `+`.

Rewrite the `-perm` parameters of find:
 - completely safe: rewrite `/0100` and `+100` to `-0100`,
 - slightly semantics-changing: rewrite `+111` to `-0100`.
I cross-verified the `find` manual pages for Linux, Darwin, FreeBSD.
2015-09-06 10:26:30 +02:00
Vladimír Čunát
862900e42e mass-replace deprecated usage of find -perm (Fixes #9044)
sed 's|-perm +0|-perm /0|g'
It's a mass-rebuild due to usage in stdenv's default setup hooks.
2015-07-30 10:30:17 +02:00
Domen Kožar
9b7b2ce8eb Revert "Introduce patchShebangsPhase ran in preConfigurePhases"
This reverts commit 512fbb280f.

See #5368
2015-01-08 10:26:49 +01:00
Domen Kožar
512fbb280f Introduce patchShebangsPhase ran in preConfigurePhases
(cherry picked from commit 91c7e8747af1fdc2a70cd98594ccbb12a5c6902d)
Signed-off-by: Domen Kožar <domen@dev.si>
2015-01-02 02:58:32 +01:00
Eelco Dolstra
2def8e7499 Remove addHook
Just use bash arrays directly. I.e.

  addHook preConfigure myPreConfigure

is now

  preConfigureHooks+=(myPreConfigure)
2014-08-09 12:45:53 +02:00
Eelco Dolstra
daa66b8b1c Factor out fixup phase stuff into separate setup hooks 2014-08-09 12:44:49 +02:00