-
-
Notifications
You must be signed in to change notification settings - Fork 14.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add usrBinEnv setup hook for running scripts with #!/usr/bin/env
shebang during build without patching them
#319420
base: master
Are you sure you want to change the base?
Conversation
…ebang during build without patching them
Setup hooks would go in |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
mkdir -m 0755 -p /usr/bin
fails here:
mkdir: cannot create directory '/usr': Permission denied
If this actually works on your system, please add a test demonstrating that.
For what it's worth, I ran into a fair few cases of things which expect
However, it gets downright painful when the scripts are generated during the build, and even more error-prone: one must patch whatever is generating the scripts (or somehow run part of the build, then All in all, that seems to me like extra tedium for packagers, and a source of subtle cross-compilation bugs. |
Running nix-shell in a folder containing the following files seems to work fine for me
{ pkgs ? import (fetchTarball {
name = "nixpkgs-24.05";
url = "https://github.com/NixOS/nixpkgs/archive/refs/tags/24.05.tar.gz";
sha256 = "sha256:1lr1h35prqkd1mkmzriwlpvxcb34kmhc9dnr48gkm8hh089hifmx";
}) {}
}:
let
usrBinEnv = pkgs.makeSetupHook { name = "create-usr-bin-env"; } (pkgs.writeText "create-usr-bin-env.sh" ''
mkdir -m 0755 -p /usr/bin
ln -sfn "${pkgs.coreutils}/bin/env" /usr/bin/env
'');
sayhello = pkgs.runCommand "sayhello" { nativeBuildInputs = [ usrBinEnv ]; } ''
mkdir -p $out && cd $out
${./makesayhello.sh}
'';
in
pkgs.mkShell {
buildInputs = [ sayhello ];
shellHook = ''sayhello'';
}
#!/usr/bin/env bash
mkdir -p bin
echo 'echo "Hello world"' > bin/sayhello
chmod +x bin/sayhello |
That fails here, both with |
@@ -1448,6 +1448,12 @@ with pkgs; | |||
#package writers | |||
writers = callPackage ../build-support/writers { }; | |||
|
|||
# Useful for creating /usr/bin/env in a build environment to run build scripts with #!/usr/bin/env shebang | |||
usrBinEnv = makeSetupHook { name = "create-usr-bin-env"; } (pkgs.writeText "create-usr-bin-env.sh" '' | |||
mkdir -m 0755 -p /usr/bin |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm getting
mkdir: cannot create directory '/usr': Permission denied
and ls -la / && id
from that hook looks like so
total 0
drwxr-x--- 1 nobody nixbld 48 Dec 7 13:51 .
drwxr-x--- 1 nobody nixbld 48 Dec 7 13:51 ..
drwxr-xr-x 1 nobody nogroup 4 Dec 7 13:51 bin
drwx------ 1 nixbld nixbld 0 Dec 7 13:51 build
drwxr-xr-x 1 nobody nogroup 120 Dec 7 13:51 dev
dr-xr-xr-x 1 nobody nogroup 32 Dec 7 13:51 etc
drwxr-xr-x 1 nobody nogroup 10 Dec 7 13:51 nix
dr-xr-xr-x 418 nobody nogroup 0 Dec 7 13:51 proc
drwxrwxrwt 1 nobody nogroup 0 Dec 7 13:51 tmp
uid=1000(nixbld) gid=100(nixbld) groups=100(nixbld)
Description of changes
Adds
pkgs.usrBinEnv
(setup hook) for providing/usr/bin/env
in build environments.Related issues:
/usr/bin/env
? #6227Motivation
Consider that you want to build a third party project with build-scripts written in bash, and you see the following line at the top:
#!/usr/bin/env bash ...
Now, if you want to run this script in your build sandbox, you have a problem, since
/usr/bin/env
doesn't exist in your build sandbox. You will typically find advice telling you that you need to patch the shebang line of the scripts.There is an alternative approach that doesn't require patching the third party source code though. You can create /usr/bin/env in your build sandbox before running the script as part of your build. This can be done with a setup hook.
This feels like something "common enough" that it maybe should be part of nixpkgs. Then the above example could be written as
Things done
nix.conf
? (See Nix manual)sandbox = relaxed
sandbox = true
nix-shell -p nixpkgs-review --run "nixpkgs-review rev HEAD"
. Note: all changes have to be committed, also see nixpkgs-review usage./result/bin/
)Add a 👍 reaction to pull requests you find important.