Skip to content
Closed
Show file tree
Hide file tree
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
4 changes: 3 additions & 1 deletion doc/stdenv/stdenv.chapter.md
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,8 @@ The propagated equivalent of `depsTargetTarget`. This is prefixed for the same r

A number between 0 and 7 indicating how much information to log. If set to 1 or higher, `stdenv` will print moderate debugging information during the build. In particular, the `gcc` and `ld` wrapper scripts will print out the complete command line passed to the wrapped tools. If set to 6 or higher, the `stdenv` setup script will be run with `set -x` tracing. If set to 7 or higher, the `gcc` and `ld` wrapper scripts will also be run with `set -x` tracing.

In order to set the `NIX_DEBUG` environment variable, Nix itself must be re-compiled so that it's set in the local derivation build environment. This ensures that the derivation's hash doesn't change because `NIX_DEBUG` has been set. Most Nix derivation have a `withNixDebug` attribute which can be overridden. Set the `nix.package` option to use this `NIX_DEBUG`-enabled Nix.

### Attributes affecting build properties {#attributes-affecting-build-properties}

#### `enableParallelBuilding` {#var-stdenv-enableParallelBuilding}
Expand Down Expand Up @@ -1421,7 +1423,7 @@ Both parameters take a list of flags as strings. The special `"all"` flag can be

For more in-depth information on these hardening flags and hardening in general, refer to the [Debian Wiki](https://wiki.debian.org/Hardening), [Ubuntu Wiki](https://wiki.ubuntu.com/Security/Features), [Gentoo Wiki](https://wiki.gentoo.org/wiki/Project:Hardened), and the [Arch Wiki](https://wiki.archlinux.org/title/Security).

Note that support for some hardening flags varies by compiler, CPU architecture, target OS and libc. Combinations of these that don't support a particular hardening flag will silently ignore attempts to enable it. To see exactly which hardening flags are being employed in any invocation, the `NIX_DEBUG` environment variable can be used.
Note that support for some hardening flags varies by compiler, CPU architecture, target OS and libc. Combinations of these that don't support a particular hardening flag will silently ignore attempts to enable it. To see exactly which hardening flags are being employed in any invocation, the [`NIX_DEBUG` environment variable](#var-stdenv-NIX_DEBUG) can be used.

### Hardening flags enabled by default {#sec-hardening-flags-enabled-by-default}

Expand Down
20 changes: 13 additions & 7 deletions pkgs/stdenv/generic/setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,18 @@ nixLog() {
echo "$@" >&"$NIX_LOG_FD"
}

# Call `nixLog` if $NIX_DEBUG is set to 1 or more.
nixLogDebug() {
if (( "${NIX_DEBUG:-0}" == 0 )); then return; fi
nixLog "$@"
}

# Log a hook, to be run before the hook is actually called.
# logging for "implicit" hooks -- the ones specified directly
# in derivation's arguments -- is done in _callImplicitHook instead.
_logHook() {
# Fast path in case nixLog is no-op.
if [[ -z ${NIX_LOG_FD-} ]]; then
# Fast path in case nixLog is no-op or NIX_DEBUG is 0 or unset.
if [[ -z ${NIX_LOG_FD-} ]] || (( "${NIX_DEBUG:-0}" == 0 )); then
return
fi

Expand Down Expand Up @@ -153,13 +159,13 @@ _callImplicitHook() {
local def="$1"
local hookName="$2"
if declare -F "$hookName" > /dev/null; then
nixLog "calling implicit '$hookName' function hook"
nixLogDebug "calling implicit '$hookName' function hook"
"$hookName"
elif type -p "$hookName" > /dev/null; then
nixLog "sourcing implicit '$hookName' script hook"
nixLogDebug "sourcing implicit '$hookName' script hook"
source "$hookName"
elif [ -n "${!hookName:-}" ]; then
nixLog "evaling implicit '$hookName' string hook"
nixLogDebug "evaling implicit '$hookName' string hook"
eval "${!hookName}"
else
return "$def"
Expand Down Expand Up @@ -705,7 +711,7 @@ activatePackage() {
(( hostOffset <= targetOffset )) || exit 1

if [ -f "$pkg" ]; then
nixLog "sourcing setup hook '$pkg'"
nixLogDebug "sourcing setup hook '$pkg'"
source "$pkg"
fi

Expand All @@ -729,7 +735,7 @@ activatePackage() {
fi

if [[ -f "$pkg/nix-support/setup-hook" ]]; then
nixLog "sourcing setup hook '$pkg/nix-support/setup-hook'"
nixLogDebug "sourcing setup hook '$pkg/nix-support/setup-hook'"
source "$pkg/nix-support/setup-hook"
fi
}
Expand Down
6 changes: 6 additions & 0 deletions pkgs/tools/package-management/lix/common.nix
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ assert (hash == null) -> (src != null);
# RISC-V support in progress https://github.com/seccomp/libseccomp/pull/50
withLibseccomp ? lib.meta.availableOn stdenv.hostPlatform libseccomp,
libseccomp,
# If non-`null`, the string value is what the environment variable `NIX_DEBUG`
# is set to when building any derivation. See the Nixpkgs manual for more.
withNixDebug ? null,

confDir,
stateDir,
Expand Down Expand Up @@ -161,6 +164,9 @@ stdenv.mkDerivation {

postPatch = ''
patchShebangs --build tests
'' + lib.optionalString (withNixDebug != null) ''
grep -r -l -Z -e 'env."NIX_LOG_FD".*"2"' src | \
xargs -0 sed -i '/NIX_LOG_FD.*2/a env["NIX_DEBUG"] = "${toString withNixDebug}";'
Comment on lines +168 to +169
Copy link
Member

Choose a reason for hiding this comment

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

The idea gets an ACK from me but this snippet must be refactored; it is unreadable.

Ideally, we'd have this as a build-time (or even runtime) feature in Lix.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm going to collect more feedback before investing the work to do that, especially if the idea itself gets mondo pushback from others. Having it work with upstream support definitely is "the right way", for sure.

Copy link
Member

Choose a reason for hiding this comment

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

I would say that the complain of Atemu is nonsense, given that grep and xargs are usually unreadable.

However, to me it looks like you are editing C source code, and in those cases I prefer to use parametric patches.

'';

preConfigure =
Expand Down
6 changes: 6 additions & 0 deletions pkgs/tools/package-management/nix/common.nix
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ in
, enableStatic ? stdenv.hostPlatform.isStatic
, withAWS ? !enableStatic && (stdenv.isLinux || stdenv.isDarwin), aws-sdk-cpp
, withLibseccomp ? lib.meta.availableOn stdenv.hostPlatform libseccomp, libseccomp
# If non-`null`, the string value is what the environment variable `NIX_DEBUG` is set to
# when building any derivation. See the Nixpkgs manual for more.
, withNixDebug ? null

, confDir
, stateDir
Expand Down Expand Up @@ -169,6 +172,9 @@ self = stdenv.mkDerivation {

postPatch = ''
patchShebangs --build tests
'' + lib.optionalString (withNixDebug != null) ''
grep -r -l -Z -e 'env."NIX_LOG_FD".*"2"' src | \
xargs -0 sed -i '/NIX_LOG_FD.*2/a env["NIX_DEBUG"] = "${toString withNixDebug}";'
'';

preConfigure =
Expand Down