Merge pull request #178717 from ShamrockLee/write-multiple-references

trivial-builders: replace writeReferencesToFile with writeClosure

authored by Someone and committed by GitHub 63709965 84102223

+85 -60
+7 -3
doc/build-helpers/trivial-build-helpers.chapter.md
··· 658 658 659 659 ## `writeReferencesToFile` {#trivial-builder-writeReferencesToFile} 660 660 661 - Writes the closure of transitive dependencies to a file. 661 + Deprecated. Use [`writeClosure`](#trivial-builder-writeClosure) instead. 662 662 663 - This produces the equivalent of `nix-store -q --requisites`. 663 + ## `writeClosure` {#trivial-builder-writeClosure} 664 + 665 + 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. 666 + 667 + The result is equivalent to the output of `nix-store -q --requisites`. 664 668 665 669 For example, 666 670 667 671 ```nix 668 - writeReferencesToFile (writeScriptBin "hi" ''${hello}/bin/hello'') 672 + writeClosure [ (writeScriptBin "hi" ''${hello}/bin/hello'') ] 669 673 ``` 670 674 671 675 produces an output path `/nix/store/<hash>-runtime-deps` containing
+2
nixos/doc/manual/release-notes/rl-2405.section.md
··· 171 171 172 172 - 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) 173 173 174 + - `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. 175 + 174 176 - `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`. 175 177 176 178 - `paperless`' `services.paperless.extraConfig` setting has been removed and converted to the freeform type and option named `services.paperless.settings`.
-18
nixos/tests/nixops/default.nix
··· 93 93 94 94 inherit (import ../ssh-keys.nix pkgs) snakeOilPrivateKey snakeOilPublicKey; 95 95 96 - /* 97 - Return a store path with a closure containing everything including 98 - derivations and all build dependency outputs, all the way down. 99 - */ 100 - allDrvOutputs = pkg: 101 - let name = "allDrvOutputs-${pkg.pname or pkg.name or "unknown"}"; 102 - in 103 - pkgs.runCommand name { refs = pkgs.writeReferencesToFile pkg.drvPath; } '' 104 - touch $out 105 - while read ref; do 106 - case $ref in 107 - *.drv) 108 - cat $ref >>$out 109 - ;; 110 - esac 111 - done <$refs 112 - ''; 113 - 114 96 in 115 97 tests
+2 -2
pkgs/build-support/docker/default.nix
··· 29 29 , tarsum 30 30 , util-linux 31 31 , vmTools 32 - , writeReferencesToFile 32 + , writeClosure 33 33 , writeScript 34 34 , writeShellScriptBin 35 35 , writeText ··· 630 630 imageName = lib.toLower name; 631 631 imageTag = lib.optionalString (tag != null) tag; 632 632 inherit fromImage baseJson; 633 - layerClosure = writeReferencesToFile layer; 633 + layerClosure = writeClosure [ layer ]; 634 634 passthru.buildArgs = args; 635 635 passthru.layer = layer; 636 636 passthru.imageTag =
+2 -2
pkgs/build-support/oci-tools/default.nix
··· 1 - { lib, writeText, runCommand, writeReferencesToFile }: 1 + { lib, writeText, runCommand, writeClosure }: 2 2 3 3 { 4 4 buildContainer = ··· 72 72 set -o pipefail 73 73 mkdir -p $out/rootfs/{dev,proc,sys} 74 74 cp ${config} $out/config.json 75 - xargs tar c < ${writeReferencesToFile args} | tar -xC $out/rootfs/ 75 + xargs tar c < ${writeClosure args} | tar -xC $out/rootfs/ 76 76 ''; 77 77 } 78 78
+2 -2
pkgs/build-support/references-by-popularity/closure-graph.py
··· 8 8 # and how deep in the tree they live. Equally-"popular" paths are then 9 9 # sorted by name. 10 10 # 11 - # The existing writeReferencesToFile prints the paths in a simple 12 - # ascii-based sorting of the paths. 11 + # The existing writeClosure prints the paths in a simple ascii-based 12 + # sorting of the paths. 13 13 # 14 14 # Sorting the paths by graph improves the chances that the difference 15 15 # between two builds appear near the end of the list, instead of near
+2 -7
pkgs/build-support/singularity-tools/default.nix
··· 4 4 , storeDir ? builtins.storeDir 5 5 , writeScript 6 6 , singularity 7 - , writeReferencesToFile 7 + , writeClosure 8 8 , bash 9 9 , vmTools 10 10 , gawk ··· 50 50 }: 51 51 let 52 52 projectName = singularity.projectName or "singularity"; 53 - layer = mkLayer { 54 - inherit name; 55 - contents = contents ++ [ bash runScriptFile ]; 56 - inherit projectName; 57 - }; 58 53 runAsRootFile = shellScript "run-as-root.sh" runAsRoot; 59 54 runScriptFile = shellScript "run-script.sh" runScript; 60 55 result = vmTools.runInLinuxVM ( 61 56 runCommand "${projectName}-image-${name}.img" 62 57 { 63 58 buildInputs = [ singularity e2fsprogs util-linux gawk ]; 64 - layerClosure = writeReferencesToFile layer; 59 + layerClosure = writeClosure contents; 65 60 preVM = vmTools.createEmptyImage { 66 61 size = diskSize; 67 62 fullName = "${projectName}-run-disk";
+14 -10
pkgs/build-support/trivial-builders/default.nix
··· 1 - { lib, stdenv, stdenvNoCC, lndir, runtimeShell, shellcheck-minimal }: 1 + { lib, config, stdenv, stdenvNoCC, jq, lndir, runtimeShell, shellcheck-minimal }: 2 2 3 3 let 4 4 inherit (lib) ··· 625 625 626 626 # Docs in doc/build-helpers/trivial-build-helpers.chapter.md 627 627 # See https://nixos.org/manual/nixpkgs/unstable/#trivial-builder-writeReferencesToFile 628 - writeReferencesToFile = path: runCommand "runtime-deps" 628 + # TODO: Convert to throw after Nixpkgs 24.05 branch-off. 629 + writeReferencesToFile = (if config.allowAliases then lib.warn else throw) 630 + "writeReferencesToFile is deprecated in favour of writeClosure" 631 + (path: writeClosure [ path ]); 632 + 633 + # Docs in doc/build-helpers/trivial-build-helpers.chapter.md 634 + # See https://nixos.org/manual/nixpkgs/unstable/#trivial-builder-writeClosure 635 + writeClosure = paths: runCommand "runtime-deps" 629 636 { 630 - exportReferencesGraph = [ "graph" path ]; 637 + # Get the cleaner exportReferencesGraph interface 638 + __structuredAttrs = true; 639 + exportReferencesGraph.graph = paths; 640 + nativeBuildInputs = [ jq ]; 631 641 } 632 642 '' 633 - touch $out 634 - while read path; do 635 - echo $path >> $out 636 - read dummy 637 - read nrRefs 638 - for ((i = 0; i < nrRefs; i++)); do read ref; done 639 - done < graph 643 + jq -r ".graph | map(.path) | sort | .[]" "$NIX_ATTRS_JSON_FILE" > "$out" 640 644 ''; 641 645 642 646 # Docs in doc/build-helpers/trivial-build-helpers.chapter.md
+3
pkgs/build-support/trivial-builders/test/default.nix
··· 26 26 then references 27 27 else {}; 28 28 writeCBin = callPackage ./writeCBin.nix {}; 29 + writeClosure-union = callPackage ./writeClosure-union.nix { 30 + inherit (references) samples; 31 + }; 29 32 writeShellApplication = callPackage ./writeShellApplication.nix {}; 30 33 writeScriptBin = callPackage ./writeScriptBin.nix {}; 31 34 writeShellScript = callPackage ./write-shell-script.nix {};
+10 -6
pkgs/build-support/trivial-builders/test/references/default.nix
··· 12 12 , cleanSamples ? lib.filterAttrs (n: lib.isStringLike) 13 13 # Test targets 14 14 , writeDirectReferencesToFile 15 - , writeReferencesToFile 15 + , writeClosure 16 16 }: 17 17 18 18 # -------------------------------------------------------------------------- # ··· 46 46 samplesToString = attrs: 47 47 lib.concatMapStringsSep " " (name: "[${name}]=${lib.escapeShellArg "${attrs.${name}}"}") (builtins.attrNames attrs); 48 48 49 - references = lib.mapAttrs (n: v: writeReferencesToFile v) samples; 49 + closures = lib.mapAttrs (n: v: writeClosure [ v ]) samples; 50 50 directReferences = lib.mapAttrs (n: v: writeDirectReferencesToFile v) samples; 51 + collectiveClosure = writeClosure (lib.attrValues samples); 51 52 52 53 testScriptBin = stdenvNoCC.mkDerivation (finalAttrs: { 53 54 name = "references-test"; ··· 61 62 mkdir -p "$out/bin" 62 63 substitute "$src" "$out/bin/${finalAttrs.meta.mainProgram}" \ 63 64 --replace "@SAMPLES@" ${lib.escapeShellArg (samplesToString samples)} \ 64 - --replace "@REFERENCES@" ${lib.escapeShellArg (samplesToString references)} \ 65 - --replace "@DIRECT_REFS@" ${lib.escapeShellArg (samplesToString directReferences)} 65 + --replace "@CLOSURES@" ${lib.escapeShellArg (samplesToString closures)} \ 66 + --replace "@DIRECT_REFS@" ${lib.escapeShellArg (samplesToString directReferences)} \ 67 + --replace "@COLLECTIVE_CLOSURE@" ${lib.escapeShellArg collectiveClosure} 66 68 runHook postInstall 67 69 chmod +x "$out/bin/${finalAttrs.meta.mainProgram}" 68 70 ''; ··· 79 81 80 82 passthru = { 81 83 inherit 84 + collectiveClosure 82 85 directReferences 83 - references 86 + closures 84 87 samples 85 88 ; 86 89 }; ··· 109 112 ''; 110 113 passthru = { 111 114 inherit 115 + collectiveClosure 112 116 directReferences 113 - references 117 + closures 114 118 samples 115 119 testScriptBin 116 120 ;
+16 -9
pkgs/build-support/trivial-builders/test/references/references-test.sh
··· 33 33 34 34 cd "$(dirname "${BASH_SOURCE[0]}")" # nixpkgs root 35 35 36 - # Injected by Nix (to avoid evaluating in a derivation) 37 - # turn them into arrays 38 - # shellcheck disable=SC2206 # deliberately unquoted 36 + # Inject the path to compare from the Nix expression 37 + 38 + # Associative Arrays 39 39 declare -A samples=( @SAMPLES@ ) 40 - # shellcheck disable=SC2206 # deliberately unquoted 41 40 declare -A directRefs=( @DIRECT_REFS@ ) 42 - # shellcheck disable=SC2206 # deliberately unquoted 43 - declare -A references=( @REFERENCES@ ) 41 + declare -A closures=( @CLOSURES@ ) 42 + 43 + # Path string 44 + collectiveClosure=@COLLECTIVE_CLOSURE@ 44 45 45 - echo >&2 Testing direct references... 46 + echo >&2 Testing direct closures... 46 47 for i in "${!samples[@]}"; do 47 48 echo >&2 Checking "$i" "${samples[$i]}" "${directRefs[$i]}" 48 49 diff -U3 \ ··· 52 53 53 54 echo >&2 Testing closure... 54 55 for i in "${!samples[@]}"; do 55 - echo >&2 Checking "$i" "${samples[$i]}" "${references[$i]}" 56 + echo >&2 Checking "$i" "${samples[$i]}" "${closures[$i]}" 56 57 diff -U3 \ 57 - <(sort <"${references[$i]}") \ 58 + <(sort <"${closures[$i]}") \ 58 59 <(nix-store -q --requisites "${samples[$i]}" | sort) 59 60 done 61 + 62 + echo >&2 Testing mixed closures... 63 + echo >&2 Checking all samples "(${samples[*]})" "$collectiveClosure" 64 + diff -U3 \ 65 + <(sort <"$collectiveClosure") \ 66 + <(nix-store -q --requisites "${samples[@]}" | sort) 60 67 61 68 echo 'OK!'
+23
pkgs/build-support/trivial-builders/test/writeClosure-union.nix
··· 1 + { lib 2 + , runCommandLocal 3 + # Test targets 4 + , writeClosure 5 + , samples 6 + }: 7 + runCommandLocal "test-trivial-builders-writeClosure-union" { 8 + __structuredAttrs = true; 9 + closures = lib.mapAttrs (n: v: writeClosure [ v ]) samples; 10 + collectiveClosure = writeClosure (lib.attrValues samples); 11 + inherit samples; 12 + meta.maintainers = with lib.maintainers; [ 13 + ShamrockLee 14 + ]; 15 + } '' 16 + set -eu -o pipefail 17 + echo >&2 Testing mixed closures... 18 + echo >&2 Checking all samples "(''${samples[*]})" "$collectiveClosure" 19 + diff -U3 \ 20 + <(sort <"$collectiveClosure") \ 21 + <(cat "''${closures[@]}" | sort | uniq) 22 + touch "$out" 23 + ''
+2 -1
pkgs/top-level/stage.nix
··· 110 110 trivialBuilders = self: super: 111 111 import ../build-support/trivial-builders { 112 112 inherit lib; 113 + inherit (self) config; 113 114 inherit (self) runtimeShell stdenv stdenvNoCC; 114 - inherit (self.pkgsBuildHost) shellcheck-minimal; 115 + inherit (self.pkgsBuildHost) jq shellcheck-minimal; 115 116 inherit (self.pkgsBuildHost.xorg) lndir; 116 117 }; 117 118