Skip to content
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

trivial-builders: replace writeReferencesToFile with writeClosure #178717

Merged
merged 7 commits into from
Mar 19, 2024
10 changes: 7 additions & 3 deletions doc/build-helpers/trivial-build-helpers.chapter.md
Original file line number Diff line number Diff line change
Expand Up @@ -557,14 +557,18 @@ This creates a derivation with a directory structure like the following:

## `writeReferencesToFile` {#trivial-builder-writeReferencesToFile}

Writes the closure of transitive dependencies to a file.
Deprecated. Use [`writeClosure`](#trivial-builder-writeClosure) instead.

This produces the equivalent of `nix-store -q --requisites`.
## `writeClosure` {#trivial-builder-writeClosure}

Given a list of [store paths](https://nixos.org/manual/nix/stable/glossary#gloss-store-path) (or string-like expressions coercible to store paths), write their collective [closure](https://nixos.org/manual/nix/stable/glossary#gloss-closure) to a text file.

The result is equivalent to the output of `nix-store -q --requisites`.

For example,

```nix
writeReferencesToFile (writeScriptBin "hi" ''${hello}/bin/hello'')
writeClosure [ (writeScriptBin "hi" ''${hello}/bin/hello'') ]
```

produces an output path `/nix/store/<hash>-runtime-deps` containing
Expand Down
2 changes: 2 additions & 0 deletions nixos/doc/manual/release-notes/rl-2405.section.md
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,8 @@ The pre-existing [services.ankisyncd](#opt-services.ankisyncd.enable) has been m

- Invidious has changed its default database username from `kemal` to `invidious`. Setups involving an externally provisioned database (i.e. `services.invidious.database.createLocally == false`) should adjust their configuration accordingly. The old `kemal` user will not be removed automatically even when the database is provisioned automatically.(https://github.com/NixOS/nixpkgs/pull/265857)

- `writeReferencesToFile` is deprecated in favour of the new trivial build helper `writeClosure`. The latter accepts a list of paths and has an unambiguous name and cleaner implementation.

- `inetutils` now has a lower priority to avoid shadowing the commonly used `util-linux`. If one wishes to restore the default priority, simply use `lib.setPrio 5 inetutils` or override with `meta.priority = 5`.

- `paperless`' `services.paperless.extraConfig` setting has been removed and converted to the freeform type and option named `services.paperless.settings`.
Expand Down
18 changes: 0 additions & 18 deletions nixos/tests/nixops/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -93,23 +93,5 @@ let

inherit (import ../ssh-keys.nix pkgs) snakeOilPrivateKey snakeOilPublicKey;

/*
Return a store path with a closure containing everything including
derivations and all build dependency outputs, all the way down.
*/
allDrvOutputs = pkg:
let name = "allDrvOutputs-${pkg.pname or pkg.name or "unknown"}";
in
pkgs.runCommand name { refs = pkgs.writeReferencesToFile pkg.drvPath; } ''
touch $out
while read ref; do
case $ref in
*.drv)
cat $ref >>$out
;;
esac
done <$refs
'';

in
tests
4 changes: 2 additions & 2 deletions pkgs/build-support/docker/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
, tarsum
, util-linux
, vmTools
, writeReferencesToFile
, writeClosure
, writeScript
, writeShellScriptBin
, writeText
Expand Down Expand Up @@ -630,7 +630,7 @@ rec {
imageName = lib.toLower name;
imageTag = lib.optionalString (tag != null) tag;
inherit fromImage baseJson;
layerClosure = writeReferencesToFile layer;
layerClosure = writeClosure [ layer ];
passthru.buildArgs = args;
passthru.layer = layer;
passthru.imageTag =
Expand Down
4 changes: 2 additions & 2 deletions pkgs/build-support/oci-tools/default.nix
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{ lib, writeText, runCommand, writeReferencesToFile }:
{ lib, writeText, runCommand, writeClosure }:

{
buildContainer =
Expand Down Expand Up @@ -72,7 +72,7 @@
set -o pipefail
mkdir -p $out/rootfs/{dev,proc,sys}
cp ${config} $out/config.json
xargs tar c < ${writeReferencesToFile args} | tar -xC $out/rootfs/
xargs tar c < ${writeClosure args} | tar -xC $out/rootfs/
'';
}

4 changes: 2 additions & 2 deletions pkgs/build-support/references-by-popularity/closure-graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
# and how deep in the tree they live. Equally-"popular" paths are then
# sorted by name.
#
# The existing writeReferencesToFile prints the paths in a simple
# ascii-based sorting of the paths.
# The existing writeClosure prints the paths in a simple ascii-based
# sorting of the paths.
#
# Sorting the paths by graph improves the chances that the difference
# between two builds appear near the end of the list, instead of near
Expand Down
9 changes: 2 additions & 7 deletions pkgs/build-support/singularity-tools/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
, storeDir ? builtins.storeDir
, writeScript
, singularity
, writeReferencesToFile
, writeClosure
, bash
, vmTools
, gawk
Expand Down Expand Up @@ -50,18 +50,13 @@ rec {
}:
let
projectName = singularity.projectName or "singularity";
layer = mkLayer {
inherit name;
contents = contents ++ [ bash runScriptFile ];
inherit projectName;
};
runAsRootFile = shellScript "run-as-root.sh" runAsRoot;
runScriptFile = shellScript "run-script.sh" runScript;
result = vmTools.runInLinuxVM (
runCommand "${projectName}-image-${name}.img"
{
buildInputs = [ singularity e2fsprogs util-linux gawk ];
layerClosure = writeReferencesToFile layer;
layerClosure = writeClosure contents;
preVM = vmTools.createEmptyImage {
size = diskSize;
fullName = "${projectName}-run-disk";
Expand Down
24 changes: 14 additions & 10 deletions pkgs/build-support/trivial-builders/default.nix
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{ lib, stdenv, stdenvNoCC, lndir, runtimeShell, shellcheck-minimal }:
{ lib, config, stdenv, stdenvNoCC, jq, lndir, runtimeShell, shellcheck-minimal }:

let
inherit (lib)
Expand Down Expand Up @@ -625,18 +625,22 @@ rec {

# Docs in doc/build-helpers/trivial-build-helpers.chapter.md
# See https://nixos.org/manual/nixpkgs/unstable/#trivial-builder-writeReferencesToFile
writeReferencesToFile = path: runCommand "runtime-deps"
# TODO: Convert to throw after Nixpkgs 24.05 branch-off.
writeReferencesToFile = (if config.allowAliases then lib.warn else throw)
"writeReferencesToFile is deprecated in favour of writeClosure"
(path: writeClosure [ path ]);

# Docs in doc/build-helpers/trivial-build-helpers.chapter.md
# See https://nixos.org/manual/nixpkgs/unstable/#trivial-builder-writeClosure
writeClosure = paths: runCommand "runtime-deps"
{
exportReferencesGraph = [ "graph" path ];
# Get the cleaner exportReferencesGraph interface
__structuredAttrs = true;
exportReferencesGraph.graph = paths;
nativeBuildInputs = [ jq ];
}
''
touch $out
while read path; do
echo $path >> $out
read dummy
read nrRefs
for ((i = 0; i < nrRefs; i++)); do read ref; done
done < graph
jq -r ".graph | map(.path) | sort | .[]" "$NIX_ATTRS_JSON_FILE" > "$out"
Copy link
Contributor

Choose a reason for hiding this comment

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

Reminder: you can now open a PR with the lib.sort feature you proposed earlier. I still don't understand the implications this would have

Copy link
Contributor Author

@ShamrockLee ShamrockLee Apr 1, 2024

Choose a reason for hiding this comment

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

#300722 it is! Please take a look.

'';

# Docs in doc/build-helpers/trivial-build-helpers.chapter.md
Expand Down
3 changes: 3 additions & 0 deletions pkgs/build-support/trivial-builders/test/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ recurseIntoAttrs {
then references
else {};
writeCBin = callPackage ./writeCBin.nix {};
writeClosure-union = callPackage ./writeClosure-union.nix {
inherit (references) samples;
};
writeShellApplication = callPackage ./writeShellApplication.nix {};
writeScriptBin = callPackage ./writeScriptBin.nix {};
writeShellScript = callPackage ./write-shell-script.nix {};
Expand Down
16 changes: 10 additions & 6 deletions pkgs/build-support/trivial-builders/test/references/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
, cleanSamples ? lib.filterAttrs (n: lib.isStringLike)
# Test targets
, writeDirectReferencesToFile
, writeReferencesToFile
, writeClosure
}:

# -------------------------------------------------------------------------- #
Expand Down Expand Up @@ -46,8 +46,9 @@ let
samplesToString = attrs:
lib.concatMapStringsSep " " (name: "[${name}]=${lib.escapeShellArg "${attrs.${name}}"}") (builtins.attrNames attrs);

references = lib.mapAttrs (n: v: writeReferencesToFile v) samples;
closures = lib.mapAttrs (n: v: writeClosure [ v ]) samples;
directReferences = lib.mapAttrs (n: v: writeDirectReferencesToFile v) samples;
collectiveClosure = writeClosure (lib.attrValues samples);

testScriptBin = stdenvNoCC.mkDerivation (finalAttrs: {
name = "references-test";
Expand All @@ -61,8 +62,9 @@ let
mkdir -p "$out/bin"
substitute "$src" "$out/bin/${finalAttrs.meta.mainProgram}" \
--replace "@SAMPLES@" ${lib.escapeShellArg (samplesToString samples)} \
--replace "@REFERENCES@" ${lib.escapeShellArg (samplesToString references)} \
--replace "@DIRECT_REFS@" ${lib.escapeShellArg (samplesToString directReferences)}
--replace "@CLOSURES@" ${lib.escapeShellArg (samplesToString closures)} \
--replace "@DIRECT_REFS@" ${lib.escapeShellArg (samplesToString directReferences)} \
--replace "@COLLECTIVE_CLOSURE@" ${lib.escapeShellArg collectiveClosure}
runHook postInstall
chmod +x "$out/bin/${finalAttrs.meta.mainProgram}"
'';
Expand All @@ -79,8 +81,9 @@ let

passthru = {
inherit
collectiveClosure
directReferences
references
closures
samples
;
};
Expand Down Expand Up @@ -109,8 +112,9 @@ testers.nixosTest {
'';
passthru = {
inherit
collectiveClosure
directReferences
references
closures
samples
testScriptBin
;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,17 @@ set -euo pipefail

cd "$(dirname "${BASH_SOURCE[0]}")" # nixpkgs root

# Injected by Nix (to avoid evaluating in a derivation)
# turn them into arrays
# shellcheck disable=SC2206 # deliberately unquoted
# Inject the path to compare from the Nix expression

# Associative Arrays
declare -A samples=( @SAMPLES@ )
# shellcheck disable=SC2206 # deliberately unquoted
declare -A directRefs=( @DIRECT_REFS@ )
# shellcheck disable=SC2206 # deliberately unquoted
declare -A references=( @REFERENCES@ )
declare -A closures=( @CLOSURES@ )

# Path string
collectiveClosure=@COLLECTIVE_CLOSURE@

echo >&2 Testing direct references...
echo >&2 Testing direct closures...
for i in "${!samples[@]}"; do
echo >&2 Checking "$i" "${samples[$i]}" "${directRefs[$i]}"
diff -U3 \
Expand All @@ -52,10 +53,16 @@ done

echo >&2 Testing closure...
for i in "${!samples[@]}"; do
echo >&2 Checking "$i" "${samples[$i]}" "${references[$i]}"
echo >&2 Checking "$i" "${samples[$i]}" "${closures[$i]}"
diff -U3 \
<(sort <"${references[$i]}") \
<(sort <"${closures[$i]}") \
<(nix-store -q --requisites "${samples[$i]}" | sort)
done

echo >&2 Testing mixed closures...
echo >&2 Checking all samples "(${samples[*]})" "$collectiveClosure"
diff -U3 \
<(sort <"$collectiveClosure") \
<(nix-store -q --requisites "${samples[@]}" | sort)

echo 'OK!'
23 changes: 23 additions & 0 deletions pkgs/build-support/trivial-builders/test/writeClosure-union.nix
Copy link
Contributor

@SomeoneSerge SomeoneSerge Mar 19, 2024

Choose a reason for hiding this comment

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

(I've been waiting for the darwin check and couldn't resist posting a) Nit: might as well use nixpkgs#nixfmt-rfc-style for the new files:) But let's maybe not reset Ofborg

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That RFC style guide is a long one. Since it doesn't affect evaluation,we could reformat in subsequent PRs.

Copy link
Contributor Author

@ShamrockLee ShamrockLee Mar 19, 2024

Choose a reason for hiding this comment

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

tests.trivial-builders.references currently evaluates to { } on non-Linux to make OfBorg build always green. We don't have to wait for it.

BTW, #273183 provides non-Linux developers with utilities to run and inspect the tests, while keeping the CI green.

Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{ lib
, runCommandLocal
# Test targets
, writeClosure
, samples
}:
runCommandLocal "test-trivial-builders-writeClosure-union" {
__structuredAttrs = true;
closures = lib.mapAttrs (n: v: writeClosure [ v ]) samples;
collectiveClosure = writeClosure (lib.attrValues samples);
inherit samples;
meta.maintainers = with lib.maintainers; [
ShamrockLee
];
} ''
set -eu -o pipefail
echo >&2 Testing mixed closures...
echo >&2 Checking all samples "(''${samples[*]})" "$collectiveClosure"
diff -U3 \
<(sort <"$collectiveClosure") \
<(cat "''${closures[@]}" | sort | uniq)
touch "$out"
''
3 changes: 2 additions & 1 deletion pkgs/top-level/stage.nix
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,9 @@ let
trivialBuilders = self: super:
import ../build-support/trivial-builders {
inherit lib;
inherit (self) config;
inherit (self) runtimeShell stdenv stdenvNoCC;
inherit (self.pkgsBuildHost) shellcheck-minimal;
inherit (self.pkgsBuildHost) jq shellcheck-minimal;
inherit (self.pkgsBuildHost.xorg) lndir;
};

Expand Down