Commit Graph

428 Commits

Author SHA1 Message Date
Stefan Frijters
fea908fde9 stdenv: do not leak outputName out of loop
This is an implementation detail leaking out into the bash environment
and it did manange to confuse me as potentially being a way to get the "default"
output name when structuredAttrs is enabled, because of the "outputName" derivation attribute.
2026-05-18 14:56:50 +02:00
Philip Taron
065d762675 stdenv: fix prefixKey for structuredAttrs (#470397) 2026-01-05 15:15:15 +00:00
Stefan Frijters
d7b24b856e stdenv: fix prefixKey for structuredAttrs 2025-12-13 10:30:09 +01:00
Martin Häcker
1257f7b727 genericBuild: extract phases definition
This is very helpfull if you want to trigger partial builds in `nix
develop .#$some_package` and just need the to see all the phases, so you
can manually trigger them.
2025-09-13 17:06:00 +02:00
Wolfgang Walther
899c40e6f8 stdenv: remove .attrs.sh fallback for structuredAttrs
This was eventually fixed in Nix 2.16+. More discussion in #357053.
2025-08-12 12:34:30 +02:00
Emily
e4a2a06f17 stdenv/setup.sh: fix isMachO detection after bash-5.3 update (#432097) 2025-08-08 22:09:01 +01:00
Sergei Trofimovich
ab4295ea0f stdenv/setup.sh: fix isMachO detection after bash-5.3 update
Without the change isMachO fails to work on UTF-8 locales as `read -u 4`
decoders 4 UTF-8 chars and not 4 bytes. `LANG=C` forces it to read bytes.

Closes: https://github.com/NixOS/nixpkgs/issues/431934
2025-08-08 21:52:43 +01:00
Philip Taron
690e42e26a stdenv: make dumpVars test if $NIX_BUILD_TOP is a directory
This resolves the spate of user reports about strange failures in various derivations.

See https://github.com/NixOS/nixpkgs/pull/417132#issuecomment-3136320269 and related.
2025-08-02 13:23:51 -07:00
Philip Taron
2d6065471a stdenv: Don't reappend patchFlags (#424103) 2025-07-10 14:50:50 -07:00
Elliot Berman
bfc73fd139 stdenv: Don't reappend patchFlags
Presently, each patch in the for loop for patchPhase re-appends the
patchFlags. This can be harmless "patch -p1 -p1" is effectively the same
as "patch -p1". Other flags to patch, such as "-d" perform action each
time it is present in the argument list. The flagsArray doesn't contain
any patch-specific arguments and can be declared outside the loop.
2025-07-08 18:44:13 -07:00
Philip Taron
26b1b38321 stdenv: reduce execve calls when sourcing (#417132) 2025-06-16 09:43:35 -07:00
DavHau
4a42314f72 generic-builder: optimize - reduce execve calls when sourcing
This improves the implementation of `dumpVars` by removing a call to `install`.

This improves performance when sourcing setup.sh by more than 10%
It should also improve the performance when transitioning between build phases significantly as no process executions are issued anymore,

A test is added, which ensures that no extra execve calls are issued while sourcing setup.sh.
2025-06-16 22:21:39 +07:00
John Soo
e23b1eba99 pkgs/stdenv: remove --preserve=mode from cp in defaultUnpack
`mode` includes acls and xattrs. Those xattrs can include unsupported
attrs on the destination file system. I.E. If copying from nfs4 to ext
`fsetxattr` can be called with `system.nfs4_acl` which is an
unsupported operation on ext4. This will result in
`cp: preserving permissions for ...: Operation not supported`.

See, for instance:

https://lists.gnu.org/archive/html/coreutils/2024-09/msg00009.html

In our case we saw the following when adding a strace to the cp:

`lgetxattr("/nix/store/ihas6mzn9h54ldgp90pics0yrlmi6ln7-source", "system.nfs4_acl", NULL, 0) = -1 EOPNOTSUPP (Operation not supported)`

Removing this flag should not effect the resulting umask/permission
bits, which I think is all we care about. From the docs:

> In the absence of this option, the permissions of existing
> destination files are unchanged. Each new file is created with the
> mode of the corresponding source file minus the set-user-ID,
> set-group-ID, and sticky bits as the create mode; the operating system
> then applies either the umask or a default ACL, possibly resulting in
> a more restrictive file mode.

https://www.gnu.org/software/coreutils/manual/html_node/cp-invocation.html

So the only real problem I can foresee is if the setuid/setgid or
other sticky bits matter to a build. In which case I think an effected
build might have bigger issues.
2025-06-04 10:35:11 -06:00
Lin Jian
75281ae80a stdenv: fix propagatedUserEnvPkgs when __structuredAttrs is true
This is a follow-up of #388908.

Previously, $out/nix-support/propagated-user-env-packages was not
created when __structuredAttrs is true, the first element of
propagatedUserEnvPkgs is null and the length of propagatedUserEnvPkgs
is at least 2.

Fixes #388829
2025-03-25 08:37:18 +08:00
Lin Jian
adad17e2d0 stdenv: fix propagatedUserEnvPkgs when __structuredAttrs is true
Previously, when __structuredAttrs is true, only the first element of
propagatedUserEnvPkgs was written to
$out/nix-support/propagated-user-env-packages.
2025-03-11 15:43:10 +08:00
Wolfgang Walther
2e43b87c62 replaceVars: fail when exemption can't be found
This also changes stdenv's substitute --replace-fail to error out when
the replacement is the same as the search pattern, but can't be found.
This should not cause any problems in existing code, from what I can
tell from grepping nixpkgs.

The exception for pattern==replacement was previously introduced all the
way back in 5ff872aa24, but this was
apparently only used to make the check for the warning "simpler".
2025-02-10 19:19:59 +01:00
Connor Baker
8aa11323bb stdenv: add note in nix logging functions about not cluttering nix-shell 2025-01-23 04:14:49 +00:00
Connor Baker
60749f9cdc stdenv: rename nixLogWithLevel to _nixLogWithLevel to indicate it is meant to be private 2025-01-23 04:11:44 +00:00
Connor Baker
a2b293e3c9 stdenv: introduce nixLog and nixLogWithLevel 2025-01-03 22:43:56 +00:00
Wolfgang Walther
2332759d7a stdenv: fix nix develop
This was introduced in #360466.
2024-12-09 19:17:26 +01:00
Philip Taron
fdee9525f3 various: fix "list contains" check (#363191) 2024-12-09 09:17:28 -08:00
Wolfgang Walther
80344cb8d5 various: fix "list contains" check
Testing for the presence of an item in a space separated list with this
pattern needs to pad the list with spaces at the beginning and end.
Otherwise the first and last element will never be matched.

Resolves #354220
2024-12-08 13:11:59 +01:00
Martin Weinelt
41d4a66bba Revert "stdenv: runPhase returns status" 2024-12-04 02:18:53 +01:00
Philip Taron
6f40e94649 stdenv: runPhase returns status (#330751) 2024-12-02 08:03:36 -08:00
Wolfgang Walther
56b09628e3 stdenv: support multi-char separators in concatStringsSep
One prominent use-case for this is pytestCheckHook. This will help
making it work with structuredAttrs in the future.
2024-11-30 16:38:02 +01:00
Romanos Skiadas
bdac0fa35d pkgs/stdenv: fix defaultUnpack on nfs
-p preserves permission,mode,timestamps.

When trying to copy with permission on NFS, it can fail;

The closest documentation I found on this was:

https://access.redhat.com/solutions/725513

One suggestion is to change the netapp configuration, but another that
should work for everyone is to use the cp command presented in this
patch.

With this, builds on nfs work for me. I however cannot say that I'm
certain that removing the permission preservation won't cause any other
troubles elsewhere, e.g. by perhaps introducing some sort of
non-determinism.
2024-11-28 17:26:43 +02:00
Philip Taron
f2b00cb5ef pkgs/stdenv/generic/setup.sh: lint with ShellCheck (#351849) 2024-11-01 08:52:44 -07:00
Yueh-Shun Li
34ebbd650c pkgs/stdenv/generic/setup.sh: lint with ShellCheck
The behaviour of [[ -n/-z "${FOO[@]}" ]] is unspecified.
Use [[ -n/-z "${FOO[*]-}" ]] instead
2024-10-28 22:25:30 +08:00
Linus Heckemann
9767bb9bf1 stdenv/setup: make substituteInPlace require actual files
I've often run into substituteInPlace doing nothing when I invoked it
wrong, like:

substituteInPlace --replace-fail from to file.txt

The substitute function would pass file.txt on to substituteStream,
which complain about the extraneous argument. However, if no files are
passed, substitute is never called and substituteInPlace silently does
nothing.

It would be more annoying to make substituteInPlace accept file
arguments anywhere (since it would need to understand the arguments to
substituteStream), so let's error on this incorrect usage instead.

Change-Id: Ib2daad6f49bbe516a273a35d7f2a31fc42fd053c
2024-10-21 13:13:42 +02:00
Philip Taron
05e5a56f04 auto-patchelf: refactor structuredAttrs support (#340858) 2024-09-12 15:13:20 -07:00
Wolfgang Walther
bc0395ee6f auto-patchelf: refactor structuredAttrs support
stdenv now provides better tooling to support structuredAttrs without
depending on $__structuredAttrs itself.
2024-09-11 19:29:39 +02:00
Philip Taron
f4cd623ed3 stdenv: bump required Bash version from 4 to 5 (#340765) 2024-09-11 07:32:57 -07:00
Ivan Trubach
799d0c8a7d stdenv: bump required Bash version from 4 to 5
Currently stdenv requires Bash 4.x that was released in 2009. This
change bumps the required version to Bash 5.x (2019, 5 years ago).

See https://mywiki.wooledge.org/BashFAQ/061 for more details.

Using a relatively modern Bash version allows us to rely on newer
features (e.g. ${var@a}) and remove workarounds for older quirks (e.g.
https://stackoverflow.com/a/7577209, “old bash empty array problem”).
Note that many setup hooks are using features added after 4.0 version,
e.g. makeWrapper uses ${var@Q} from 4.4, but some even require >5.0,
e.g. cargoBuildHook uses ${var@U} from 5.1.
2024-09-09 15:22:11 +03:00
Wolfgang Walther
1efcffa700 stdenv: support default values in concatTo
The previously used pattern was introduced in #318614, but technically
leaked the default flags into the global scope. While this would
probably not make much of a practical difference, making concatTo
support default values is a much cleaner approach.
2024-08-24 12:23:35 +02:00
Thomas Gerbet
d00775c1d9 stdenv: create env-vars file before writing data to it
This fixes the regression introduced by c47a1e701d
on Darwin. The creation of the file using `install` and process
substitution does not work on Darwin, you get the following complain:
```
install: skipping file '/dev/fd/63', as it was replaced while being copied
```

Fixes #335016
2024-08-20 19:02:41 +02:00
Someone Serge
ebf6790c39 stdenv: concatTo: fall back to old behaviour for "*Array" variables
Fixes e.g. the `sudo` build failure
2024-08-16 12:51:51 +00:00
Someone
ccaaa9ca53 Merge pull request #318614 from wolfgangwalther/structured-attrs-setup-hooks
treewide: support structuredAttrs in setup hooks
2024-08-13 19:29:36 +00:00
Someone Serge
64eaa63181 stdenv: concatStringsSep: quote ${sep} 2024-08-10 23:38:10 +00:00
Philip Taron
d8fbb16219 stdenv: change the logging in _allFlags to talkative 2024-08-05 18:35:57 -07:00
Philip Taron
e844424e4f stdenv: replace other $NIX_DEBUG log statements 2024-08-05 18:35:57 -07:00
Philip Taron
465dbd2ddf stdenv: log hooks at nixTalkativeLog level
Nix filters out messages with level ≥ 4 by default as of this commit.
2024-08-05 18:35:57 -07:00
Philip Taron
624463391d stdenv: introduce specific logging functions 2024-08-05 18:35:57 -07:00
Philip Taron
bbb9f2f1c9 stdenv: set the phase in showPhaseHeader 2024-08-05 18:35:57 -07:00
Wolfgang Walther
471cbdd062 stdenv: add concatStringsSep helper
This can be used to separate lists for example with commas, when
creating argument strings. This works with both structuredAttrs disabled
and enabled.
2024-08-02 22:11:41 +02:00
Wolfgang Walther
8cb51ec38e stdenv: refactor default flags without __structuredAttrs use
Instead of checking for __structuredAttrs everywhere, it's easier to
just set the default value via parameter expansion and then hand the
array construction off to "concatTo".

Once more setup-hooks will be made structuredAttrs-aware, this pattern
will reduce the use of this implementation detail even more.
2024-08-02 22:11:38 +02:00
Wolfgang Walther
6bdfef9d2d stdenv: generalize _accumFlagsArray to concatTo
Passing "flagsArray" as the first argument allows using this function
in a few more places.
2024-08-02 22:11:38 +02:00
Wolfgang Walther
bfd97a691f stdenv: make _accumFlagsArray independent of structuredAttrs
structuredAttrs was used here to make an assumption about the type of
the named variables passed as arguments.  This can be done better by
looking at the actual types of those variables.

This gives a bit more backwards compatibility as well: Once you turn to
structuredAttrs, you should still be able to pass a bare string instead
of a list and have it behave as a whitespace-separated string like
before.
2024-08-02 21:54:25 +02:00
Thomas Gerbet
c47a1e701d stdenv: make sure the env-vars file created is not world readable
Under some circumstances this file might contain private information
that should not be accessible to everybody.
2024-08-01 19:26:05 +02:00
Wolfgang Walther
cdb2f2971c stdenv: refactor appendToVar and prependToVar
No need to call declare -p twice. The case statement is easier to read
than the multi-if.
2024-07-30 21:38:14 +02:00
Ivan Trubach
bd872b4a77 stdenv: fix unbound NIX_LOG_FD in nix develop
When running `nix develop` for a package, Nix records the stdenv
environment with NIX_LOG_FD set. That is, when the actual development
shell runs, it uses the functions that attempt to log to NIX_LOG_FD, but
this variable is not actually set.

As a workaround, check whether NIX_LOG_FD is set at runtime.

Example (before this change):
```console
$ nix develop --file . bash
$ echo "${NIX_LOG_FD-unset}"
unset
$ runPhase unpackPhase
bash: "$NIX_LOG_FD": Bad file descriptor
Running phase: unpackPhase
unpacking source archive /nix/store/v28dv6l0qk3j382kp40bksa1v6h7dx9p-bash-5.2.tar.gz
bash: "$NIX_LOG_FD": Bad file descriptor
source root is bash-5.2
bash: "$NIX_LOG_FD": Bad file descriptor
setting SOURCE_DATE_EPOCH to timestamp 1663942708 of file bash-5.2/y.tab.h
```

After this change:
```console
$ nix develop --file . bash
$ runPhase unpackPhase
Running phase: unpackPhase
unpacking source archive /nix/store/v28dv6l0qk3j382kp40bksa1v6h7dx9p-bash-5.2.tar.gz
source root is bash-5.2
setting SOURCE_DATE_EPOCH to timestamp 1663942708 of file bash-5.2/y.tab.h
```
2024-07-29 13:08:17 +03:00